×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro for Symmetry

Macro for Symmetry

Macro for Symmetry

(OP)
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

RE: Macro for Symmetry

(OP)
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.

RE: Macro for Symmetry

Hi Jopal,
I think if user have to pick the plane each time, significance of a macro will reduce.
Think about it.

Regards,
Vikt

RE: Macro for Symmetry

(OP)
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

RE: Macro for Symmetry

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 --> CATScript

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

https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...

RE: Macro for Symmetry

(OP)
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

RE: Macro for Symmetry

(OP)
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

RE: Macro for Symmetry

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))) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources