Continue to Site

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!

Macro for Symmetry

Status
Not open for further replies.

jopal

Automotive
Dec 5, 2007
121
CA
is it possible to create a macro or a script to do the following. I have no scripting experience

In a single catpart consisting of multiple part bodies, i would like to create a symmetry of each individual part body about a specified plane the user can pick. There could be anywhere from 10 to 50 or so part bodies in any particular instance, there would be no specific names to the part bodies, and the first main part body would always be empty. See picture. Any help would be appreciated
 
Replies continue below

Recommended for you

Yes i want it to happen in the same file. I was just showing what i want the end result to look like in the picture. I manually symmetried all of those part bodies.
 
Hi Jopal,
I think if user have to pick the plane each time, significance of a macro will reduce.
Think about it.

Regards,
Vikt
 
All bodies would be symmetried about the same plane. so if user pics yz plane, all bodies in catpart symmetry about yz. There would never be different planes for different bodies, but there may be a different plane for entire catpart
 
Let me explain how I create this macro (this is an advice for beginners). First I've done few symmetry operations manually and I noticed that to use symmetry command I have to put each Body in Work Object (Define in Work Object) , then use Symmetry command.

I started to record a macro by repeating all those operations just for few bodies. Then I looked into code and deleted all repetitive parts (let just first part of code, if you will be curios to record a macro I'm sure you will understand which part to keep).

Then you need to think a little bit how to do same part of code in a repetitive way...the simple way is to go by search all bodies, put them in a selection, then do same thing for each one of them starting with number 1 and ending with Count2.

Sometimes is very easy to combine recorded codes with something which you can find in v5automation file...of course it will be something else if you want to pick an arbitrary plane and not this specific YZ plane.

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument

''''''''search by name to create selection - this part can be also recorded
Dim selection1 As Selection
Set selection1 = partDocument1.Selection
selection1.Search "Name=PartBody' 'from*,all"

X = selection1.Count2    ''count all bodies according with search criteria

For i = 1 To X  ''''''''start cycle

NumePB = selection1.Item(i).Value.Name  ''we need to get the name of each item to use it later
''''MsgBox NumePB  '' comment, MsgBox was just for additional checks

''''''''this part of code was initially recorded 
Dim part1 As Part
Set part1 = partDocument1.Part

Dim bodies1 As Bodies
Set bodies1 = part1.Bodies

Dim body1 As Body
Set body1 = bodies1.Item(NumePB)  ''Item(NumePB) is the only part of code modified instead of what was recorded, need to have something general not only for a specific name 

part1.InWorkObject = body1

Dim shapeFactory1 As Factory
Set shapeFactory1 = part1.ShapeFactory

Dim originElements1 As OriginElements
Set originElements1 = part1.OriginElements

Dim hybridShapePlaneExplicit1 As AnyObject
Set hybridShapePlaneExplicit1 = originElements1.PlaneYZ

Dim reference1 As Reference
Set reference1 = part1.CreateReferenceFromObject(hybridShapePlaneExplicit1)

Dim symmetry1 As AnyObject
Set symmetry1 = shapeFactory1.AddNewSymmetry2(reference1)

Dim hybridShapeSymmetry1 As HybridShape
Set hybridShapeSymmetry1 = symmetry1.HybridShape

part1.InWorkObject = hybridShapeSymmetry1
part1.Update 

''''end of first "block" of the recorded code

Next  ''''to repeat operation for each item

End Sub



Regards
Fernando

 
Ok not a problem. One more thing,

I'm trying to do the exact same code, except instead of symmetry, i want a rotate instead about the default z axis of the file. I cant seem to get it to work
 
So i've tried to record a "rotate" command, instead of "symmetry", and update this code, but not sure why it doesnt work. I notice your code has declerations (Dim), where as when I record, i dont get this





Language="VBSCRIPT"

Sub CATMain()

Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument

''''''''search by name to create selection - this part can be also recorded
Dim selection1 As Selection
Set selection1 = partDocument1.Selection
selection1.Search "Name=PartBody' 'from*,all"

X = selection1.Count2 ''count all bodies according with search criteria

For i = 1 To X ''''''''start cycle

NumePB = selection1.Item(i).Value.Name ''we need to get the name of each item to use it later
''''MsgBox NumePB '' comment, MsgBox was just for additional checks

''''''''this part of code was initially recorded
Dim part1 As Part
Set part1 = partDocument1.Part

Dim bodies1 As Bodies
Set bodies1 = part1.Bodies

Dim body1 As Body
Set body1 = bodies1.Item(NumePB) ''Item(NumePB) is the only part of code modified instead of what was recorded, need to have something general not only for a specific name

part1.InWorkObject = body1

Dim shapeFactory1 As Factory
Set shapeFactory1 = part1.ShapeFactory


Dim hybridShapeLineExplicit1 as AnyObject
Set hybridShapeLineExplicit1 =hybridShapes1.Item("Z Axis")


Set reference1 = part1.CreateReferenceFromObject(hybridShapeLineExplicit1)

Set rotate1 = shapeFactory1.AddNewRotate2(reference1, 180.000000)

Set hybridShapeRotate1 = rotate1.HybridShape

hybridShapeRotate1.RotationType = 0

hybridShapeRotate1.Axis = reference1

part1.InWorkObject = hybridShapeRotate1



part1.Update



''''end of first "block" of the recorded code

Next ''''to repeat operation for each item

End Sub

 
this works for me...


comment this lines:

Code:
'Dim hybridShapeLineExplicit1 As AnyObject
'Set hybridShapeLineExplicit1 = hybridShapes1.Item("Z Axis")
'Set reference1 = part1.CreateReferenceFromObject(hybridShapeLineExplicit1)

and add this just after ...

Code:
Dim axisVect(2)
axisVect(0) = 0
axisVect(1) = 0
axisVect(2) = 1
Set hsf = part1.HybridShapeFactory
Set reference1 = part1.CreateReferenceFromObject(hsf.AddNewDirectionByCoord(axisVect(0), axisVect(1), axisVect(2)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top