Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert primitives (cone, sphere, cylinder,...) macro 2

Status
Not open for further replies.

zaluski

Mechanical
Jul 4, 2012
3
PL
Hi,
I recorded set of macros for inserting primitives into part.
primitives_in_part_j2yemz.png


I prepared Toolbar, even icon for cone... It works... but only when i'm inserting primitives to part. When i'm trying insert it to part which is inside Product i have error in line :

Set part1 = partDocument1.Part
(Object doesn't support this property or method).

I can record second set of macros especialy for Product, but I would like to learn how to make this macro more universal.

Language="VBSCRIPT"

Sub CATMain()

Set partDocument1 = CATIA.ActiveDocument

Set part1 = partDocument1.Part

Set bodies1 = part1.Bodies

Set body1 = bodies1.Item(1)

Set sketches1 = body1.Sketches

Set originElements1 = part1.OriginElements

Set reference1 = originElements1.PlaneYZ

Set sketch1 = sketches1.Add(reference1)

Dim arrayOfVariantOfDouble1(8)
arrayOfVariantOfDouble1(0) = 0.000000
arrayOfVariantOfDouble1(1) = 0.000000
arrayOfVariantOfDouble1(2) = 0.000000
arrayOfVariantOfDouble1(3) = 0.000000
arrayOfVariantOfDouble1(4) = 1.000000
arrayOfVariantOfDouble1(5) = 0.000000
arrayOfVariantOfDouble1(6) = 0.000000
arrayOfVariantOfDouble1(7) = 0.000000
arrayOfVariantOfDouble1(8) = 1.000000
sketch1.SetAbsoluteAxisData arrayOfVariantOfDouble1

part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(0.000000, 140.000000)

point2D1.ReportName = 3

Set line2D3 = factory2D1.CreateLine(0.000000, 0.000000, 0.000000, 140.000000)

line2D3.ReportName = 4

Set point2D2 = axis2D1.GetItem("Origin")

line2D3.StartPoint = point2D2

line2D3.EndPoint = point2D1

Set constraints1 = sketch1.Constraints

Set reference2 = part1.CreateReferenceFromObject(line2D3)

Set reference3 = part1.CreateReferenceFromObject(line2D2)

Set point2D3 = factory2D1.CreatePoint(70.000000, -0.000000)

point2D3.ReportName = 5

Set line2D4 = factory2D1.CreateLine(-0.000000, 140.000000, 70.000000, -0.000000)

line2D4.ReportName = 6

line2D4.StartPoint = point2D1

line2D4.EndPoint = point2D3

Set line2D5 = factory2D1.CreateLine(70.000000, -0.000000, 0.000000, 0.000000)

line2D5.ReportName = 7

line2D5.StartPoint = point2D3

line2D5.EndPoint = point2D2

Set reference4 = part1.CreateReferenceFromObject(line2D5)

Set reference5 = part1.CreateReferenceFromObject(line2D1)

sketch1.CenterLine = line2D3

sketch1.CloseEdition

part1.InWorkObject = sketch1

part1.Update

Set shapeFactory1 = part1.ShapeFactory

Set shaft1 = shapeFactory1.AddNewShaft(sketch1)

part1.Update

End Sub
 
Replies continue below

Recommended for you

I see the difference:
cone_in_product_pp2fpj.png


----------------------
Language="VBSCRIPT"

Sub CATMain()

Set documents1 = CATIA.Documents

Set productDocument1 = documents1.Add("Product")

Set product1 = productDocument1.Product

Set products1 = product1.Products

Set product2 = products1.AddNewComponent("Part", "")

Set partDocument1 = documents1.Item("Part1.CATPart")

Set part1 = partDocument1.Part

Set bodies1 = part1.Bodies

Set body1 = bodies1.Item("PartBody")

Set sketches1 = body1.Sketches

Set originElements1 = part1.OriginElements

Set reference1 = originElements1.PlaneXY

Set sketch1 = sketches1.Add(reference1)

Dim arrayOfVariantOfDouble1(8)
arrayOfVariantOfDouble1(0) = 0.000000
arrayOfVariantOfDouble1(1) = 0.000000
arrayOfVariantOfDouble1(2) = 0.000000
arrayOfVariantOfDouble1(3) = 1.000000
arrayOfVariantOfDouble1(4) = 0.000000
arrayOfVariantOfDouble1(5) = 0.000000
arrayOfVariantOfDouble1(6) = 0.000000
arrayOfVariantOfDouble1(7) = 1.000000
arrayOfVariantOfDouble1(8) = 0.000000
sketch1.SetAbsoluteAxisData arrayOfVariantOfDouble1

part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(0.000000, 98.973969)

point2D1.ReportName = 3

Set line2D3 = factory2D1.CreateLine(0.000000, 0.000000, 0.000000, 98.973969)

line2D3.ReportName = 4

Set point2D2 = axis2D1.GetItem("Origin")

line2D3.StartPoint = point2D2

line2D3.EndPoint = point2D1

Set constraints1 = sketch1.Constraints

Set reference2 = part1.CreateReferenceFromObject(line2D3)

Set reference3 = part1.CreateReferenceFromObject(line2D2)

Set constraint1 = constraints1.AddBiEltCst(catCstTypeVerticality, reference2, reference3)

constraint1.Mode = catCstModeDrivingDimension

Set point2D3 = factory2D1.CreatePoint(54.502567, 0.000000)

point2D3.ReportName = 5

Set line2D4 = factory2D1.CreateLine(0.000000, 98.973969, 54.502567, 0.000000)

line2D4.ReportName = 6

line2D4.StartPoint = point2D1

line2D4.EndPoint = point2D3

Set reference4 = part1.CreateReferenceFromObject(point2D3)

Set reference5 = part1.CreateReferenceFromObject(line2D1)

Set constraint2 = constraints1.AddBiEltCst(catCstTypeOn, reference4, reference5)

constraint2.Mode = catCstModeDrivingDimension

Set line2D5 = factory2D1.CreateLine(54.502567, 0.000000, 0.000000, 0.000000)

line2D5.ReportName = 7

line2D5.StartPoint = point2D3

line2D5.EndPoint = point2D2

Set reference6 = part1.CreateReferenceFromObject(line2D5)

Set reference7 = part1.CreateReferenceFromObject(line2D1)

Set constraint3 = constraints1.AddBiEltCst(catCstTypeHorizontality, reference6, reference7)

constraint3.Mode = catCstModeDrivingDimension

sketch1.CenterLine = line2D3

sketch1.CloseEdition

part1.InWorkObject = sketch1

part1.Update

Set shapeFactory1 = part1.ShapeFactory

Set shaft1 = shapeFactory1.AddNewShaft(sketch1)

Set parameters1 = part1.Parameters

Set length1 = parameters1.Item("Part1\PartBody\Shaft.1\ThickThin1")

length1.Value = 1.000000

Set parameters2 = part1.Parameters

Set length2 = parameters2.Item("Part1\PartBody\Shaft.1\ThickThin2")

length2.Value = 0.000000

part1.Update

End Sub

---------------------------

But I don't know how to combine both macros into one. Macro abowe doesn't work on document with one part and vice versa, previous one doesn't work for this kind of product structure (specification tree). On forum I found advice about V5Automation.chm but i'm not programer (yet ;) ) and this is useless on my level. I have tried sign up here three days ago but I have no response. I'm starting adventure with macros, VB, Catscript, VBScript...I this moment I try to understand the difference ;) . Should I write a code with any condition?

I asked my friend for help with it, but he is C++ programer. We tried run this code without success:


Language="VBSCRIPT"

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

On Error Resume Next

Set product1 = productDocument1.Product

Set products1 = product1.Products

Set product2 = products1.AddNewComponent("Part", "")

Set documents1 = CATIA.Documents

Set partDocument1 = documents1.Item("Part1.CATPart")

Set part1 = partDocument1.Part

If part1 Is Nothing then
part1 = productDocument1.Part
End if

Set bodies1 = part1.Bodies

Set body1 = bodies1.Item("PartBody")

Set sketches1 = body1.Sketches

Set originElements1 = part1.OriginElements

Set reference1 = originElements1.PlaneYZ

Set sketch1 = sketches1.Add(reference1)

Dim arrayOfVariantOfDouble1(8)
arrayOfVariantOfDouble1(0) = 0.000000
arrayOfVariantOfDouble1(1) = 0.000000
arrayOfVariantOfDouble1(2) = 0.000000
arrayOfVariantOfDouble1(3) = 0.000000
arrayOfVariantOfDouble1(4) = 1.000000
arrayOfVariantOfDouble1(5) = 0.000000
arrayOfVariantOfDouble1(6) = 0.000000
arrayOfVariantOfDouble1(7) = 0.000000
arrayOfVariantOfDouble1(8) = 1.000000
sketch1.SetAbsoluteAxisData arrayOfVariantOfDouble1

part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set circle2D1 = factory2D1.CreateClosedCircle(0.000000, 0.000000, 81.611998)

Set point2D1 = axis2D1.GetItem("Origin")

circle2D1.CenterPoint = point2D1

circle2D1.ReportName = 3

sketch1.CloseEdition

part1.InWorkObject = sketch1

part1.Update

Set shapeFactory1 = part1.ShapeFactory

Set pad1 = shapeFactory1.AddNewPad(sketch1, 20.000000)

part1.Update

End Sub

-------------------
I found CATIA_Portable_Script_Center.zip ... it looks more friendly :)
 
Take a look at these captures. I used Notepad++ with Compare plugin to see the differences in the code.

Capture_compare_an9zfb.jpg


Capture_bxwhfw.jpg


And now take a look here

tttt_yqgsnz.jpg


Can you tell what you see? There are some lines with comments in front, when you record something CATIA will put something which it seems to be useless (in other words clean a little bit the code). Please modify the code first and test it. Then think a little bit of conditions: are you running the code with nothing opened in CATIA (like in CATProduct), or there is a specific part or product opened? Think of subroutines who can help you in different cases...you can play also with recording for those cases. Think also at input boxes for you dimensions...and come back with the code, I'm sure there will be peoples which will help you (but read first more in documentation about what you want to do).



Regards
Fernando

- Romania
- EU
 
Hi Fernando,

Thanks for tip with Compare plugin.
In CATIA_Portable_Script_Center.zip I found Part_2_Product.CATScript . I think it is close to what I need. I have to play it a bit few days.

Best Regards
 
you said:
Should I write a code with any condition?

Yes, this is what you need.

but before you put some code together I suggest you put the idea on paper first:

[ul]
[li]find activedocument[/li]


[li]check if activedocument is a product, a part or nothing[/li]


[li]do whatever is required based on check[/li]

[li]create geometry[/li]
[/ul]

Eric N.
indocti discant et ament meminisse periti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top