PLACE, POSITION, MOVE, TURN, COPY IN PATTERN, components in an assembly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi folks, I create this post to share the code I've found here.
Thanks to the authors !
And special thanks to @MechMachineMan ! Most of this codes come from him.
I use this codes to quickly create an assembly of some special configurations of a scaffolding.
With these codes, I use an iLogic form, so you will need to create some user parameters, or modify some code.
I've put some code in bold to reflect this.
To place a component :
'''PLACE components Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition Dim oTG As TransientGeometry = ThisApplication.TransientGeometry Dim oMatrix As Matrix = oTG.CreateMatrix Dim oOccurrence As ComponentOccurrence 'Create matrix oMatrix.SetTranslation(oTG.CreateVector( _ Parameter("X_Pos")/10, _ Parameter("Y_Pos")/10, _ Parameter("Z_Pos")/10)) 'PLACE component oOccurrence = oAsmCompDef.Occurrences.Add(Parameter("Path and file name"), oMatrix) oOccurrence.Grounded = True 'Reset parameters Parameter("X_Pos") = 0 Parameter("Y_Pos") = 0 Parameter("Z_Pos") = 0 '''End of rule
To position some components :
Relocate selected components to coordinates from the assembly origin.
'''RELOCATE components Dim oDoc As Document oDoc = ThisApplication.ActiveDocument 'check for selection Dim oSelSet As SelectSet oSelSet = oDoc.SelectSet If oSelSet.Count = 0 Then Return End If 'Save current selection Dim oItems As ObjectsEnumerator oItems = ThisApplication.TransientObjects.CreateObjectCollection Dim Item As Object For Each Item In oSelSet oItems.Add(Item) Next 'RELOCATE each occurrence Dim CompOcc As ComponentOccurrence Dim oConstraint As AssemblyConstraint Dim oMatrix As Matrix Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry For Each CompOcc In oSelSet 'Create matrix oMatrix = CompOcc.Transformation oMatrix.SetTranslation(oTG.CreateVector( _ Parameter("X_Pos")/10, _ Parameter("Y_Pos")/10, _ Parameter("Z_Pos")/10)) 'RELOCATE ignoring any constraints CompOcc.SetTransformWithoutConstraints(oMatrix) 'RELOCATE according to constraints 'CompOcc.Transformation = oMatrix CompOcc.Grounded = True Next 'Reset parameters Parameter("X_Pos") = 0 Parameter("Y_Pos") = 0 Parameter("Z_Pos") = 0 'Update components constraints oDoc.Rebuild 'Call saved Selection oDoc.SelectSet.SelectMultiple(oItems) '''End of rule
To move some components :
Move selected components from their current places to input distances.
'''MOVE components Dim oDoc As Document oDoc = ThisApplication.ActiveDocument 'check for selection Dim oSelSet As SelectSet oSelSet = oDoc.SelectSet If oSelSet.Count = 0 Then Return End If 'Save current selection Dim oItems As ObjectsEnumerator oItems = ThisApplication.TransientObjects.CreateObjectCollection Dim Item As Object For Each Item In oSelSet oItems.Add(Item) Next 'MOVE each occurrence Dim CompOcc As ComponentOccurrence Dim oMatrix As Matrix Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry Dim Coords(2) As Double For Each CompOcc In oSelSet 'Create matrix oMatrix = CompOcc.Transformation oMatrix.Translation.GetVectorData(Coords)'in cm oMatrix.SetTranslation(oTG.CreateVector( _ Coords(0)+Parameter("X_Pos")/10, _ Coords(1)+Parameter("Y_Pos")/10, _ Coords(2)+Parameter("Z_Pos")/10))'in cm 'MOVE ignoring any constraints CompOcc.SetTransformWithoutConstraints(oMatrix) 'MOVE according to constraints 'CompOcc.Transformation = oMatrix CompOcc.Grounded = True Next 'Reset parameters Parameter("X_Pos") = 0 Parameter("Y_Pos") = 0 Parameter("Z_Pos") = 0 'Update components constraints oDoc.Rebuild 'Call saved Selection oDoc.SelectSet.SelectMultiple(oItems) '''End of rule
To turn some components :
'''ROTATE components Dim oDoc As Document oDoc = ThisApplication.ActiveDocument 'check for selection Dim oSelSet As SelectSet oSelSet = oDoc.SelectSet If oSelSet.Count = 0 Then Return End If 'Save current selection Dim oItems As ObjectsEnumerator oItems = ThisApplication.TransientObjects.CreateObjectCollection For Each Item As Object In oSelSet oItems.Add(Item) Next 'Create matrix Dim oTG As TransientGeometry = ThisApplication.TransientGeometry Dim oRx As Matrix oRx = oTG.CreateMatrix() oRx.Cell(2,2) = Math.Cos(Parameter("X_Rot")*Math.PI/180) oRx.Cell(2,3) = -Math.Sin(Parameter("X_Rot")*Math.PI/180) oRx.Cell(3,2) = Math.Sin(Parameter("X_Rot")*Math.PI/180) oRx.Cell(3,3) = Math.Cos(Parameter("X_Rot")*Math.PI/180) Dim oRy As Matrix oRy = oTG.CreateMatrix() oRy.Cell(1,1) = Math.Cos(Parameter("Y_Rot")*Math.PI/180) oRy.Cell(1,3) = Math.Sin(Parameter("Y_Rot")*Math.PI/180) oRy.Cell(3,1) = -Math.Sin(Parameter("Y_Rot")*Math.PI/180) oRy.Cell(3,3) = Math.Cos(Parameter("Y_Rot")*Math.PI/180) Dim oRz As Matrix oRz = oTG.CreateMatrix() oRz.Cell(1,1) = Math.Cos(Parameter("Z_Rot")*Math.PI/180) oRz.Cell(1,2) = -Math.Sin(Parameter("Z_Rot")*Math.PI/180) oRz.Cell(2,1) = Math.Sin(Parameter("Z_Rot")*Math.PI/180) oRz.Cell(2,2) = Math.Cos(Parameter("Z_Rot")*Math.PI/180) 'ROTATE each component Dim oOcc As ComponentOccurrence For Each oOcc In oSelSet Dim oOccTransform As Matrix = oOcc.Transformation Dim oTransVec As Vector = oOccTransform.Translation If Parameter("X_Rot") > 0 Then oOccTransform.PreMultiplyBy(oRx) oOccTransform.SetTranslation(oTransVec, False) oOcc.SetTransformWithoutConstraints(oOccTransform) End If If Parameter("Y_Rot") > 0 Then oOccTransform.PreMultiplyBy(oRy) oOccTransform.SetTranslation(oTransVec, False) oOcc.SetTransformWithoutConstraints(oOccTransform) End If If Parameter("Z_Rot") > 0 Then oOccTransform.PreMultiplyBy(oRz) oOccTransform.SetTranslation(oTransVec, False) oOcc.SetTransformWithoutConstraints(oOccTransform) End If oOcc.Grounded = True Next 'Update components constraints oDoc.Rebuild 'Call saved Selection oSelSet.SelectMultiple(oItems) '''End of rule
To copy in pattern some components :
Like a rectangular pattern...but in 3 direction.
'''COPY components in pattern Dim oDoc As Document oDoc = ThisApplication.ActiveDocument 'check for selection Dim oSelSet As SelectSet oSelSet = oDoc.SelectSet If oSelSet.Count = 0 Then Return End If Dim CompOcc As ComponentOccurrence Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oDoc.ComponentDefinition Dim oTG As TransientGeometry oTG = ThisApplication.TransientGeometry Dim oMatrix As Matrix oMatrix = oTG.CreateMatrix Dim oOccurrence As ComponentOccurrence Dim Coords(2) As Double For Each CompOcc In oSelSet 'Get the full file name of selected occurence FullFileName = CompOcc.Definition.Document.FullFileName 'Get occurence coordinates oMatrix = CompOcc.Transformation oMatrix.Translation.GetVectorData(Coords)'in cm 'Copy occurence according to user parameters If Parameter("Nb_X") - 1 > 0 Then For i = 1 To Parameter("Nb_X") - 1 'Set new matrix oMatrix.SetTranslation(oTG.CreateVector( _ Coords(0) + i * Parameter("Esp_X")/10, _ Coords(1), _ Coords(2))) oOccurrence = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix) oOccurrence.Grounded = True Next End If If Parameter("Nb_Y") - 1 > 0 Then For i = 1 To Parameter("Nb_Y") - 1 'Set new matrix oMatrix.SetTranslation(oTG.CreateVector( _ Coords(0), _ Coords(1) + i * Parameter("Esp_Y")/10, _ Coords(2))) oOccurrence = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix) oOccurrence.Grounded = True Next End If If Parameter("Nb_Z") - 1 > 0 Then For i = 1 To Parameter("Nb_Z") - 1 'Set new matrix oMatrix.SetTranslation(oTG.CreateVector( _ Coords(0), _ Coords(1), _ Coords(2) + i * Parameter("Esp_Z")/10)) oOccurrence = oAsmCompDef.Occurrences.Add(FullFileName, oMatrix) oOccurrence.Grounded = True Next End If Next 'Reset parameters Parameter("Nb_X") = 1 Parameter("Esp_X") = 0 Parameter("Nb_Y") = 1 Parameter("Esp_Y") = 0 Parameter("Nb_Z") = 1 Parameter("Esp_Z") = 0 '''End of rule
Hope you will enjoy !
Give Kudos if you like it
Thomas
Mechanical Designer / Inventor Professional 2025