Hi Wayne,
I'm not sure of understanding your requirements but the code included in the attached file may help you with your final solution...
I also publish the code here:
Sub Main
AddWP(100, 10, 20, "Test")
AddComponent(100, 100, 0, ThisDoc.Path & "\Parte2.ipt") 'change the partname as you like
MsgBox("Now I move the Component")
MoveComponent(200, 300, 500, ComponentByName("Sfera"))
End Sub
Private Sub AddWP(x As Double, y As Double, z As Double, oName As String) ' this routine work on the activedocument
' Create a TransientGeometry
oTG = ThisApplication.TransientGeometry
' Create 3 workpoints to define the origin, x-direction and y-direction points
oWorkPoint = ThisApplication.ActiveDocument.ComponentDefinition.WorkPoints.AddFixed(oTG.CreatePoint(x,y,z))
oWorkPoint.Name = oName
End Sub
Private Sub AddComponent(X As Double, Y As Double, Z As Double, oPath As String)
If System.IO.File.Exists(oPath) Then
' Create a TransientGeometry
oTG = ThisApplication.TransientGeometry
oMatrix = oTG.CreateMatrix
oMatrix.SetTranslation(oTG.CreateVector(X, Y, Z))
' Add the occurrence
oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.Add(oPath, oMatrix)
oOccurrence.Grounded = False ' True to ground the component
oOccurrence.Name = "Sfera" ' if you like to rename the occurence you can add the new name as parameter of this Sub
End If
End Sub
Private Sub MoveComponent(X As Double, Y As Double, Z As Double, oOccurrence As ComponentOccurrence)
If oOccurrence IsNot Nothing Then
' Get the current transformation matrix from the occurrence.
Dim oTransform As Matrix
oTransform = oOccurrence.Transformation
' Move the occurrence honoring any existing constraints.
'oTransform.SetTranslation ThisApplication.TransientGeometry.CreateVector(oNOri.Point.X, oNOri.Point.Y, oNOri.Point.Z)
'oOccurrence.Transformation = oTransform
' Move the occurrence ignoring any constraints.
' Anything that causes the assembly to recompute will cause the
' occurrence to reposition itself to honor the constraints.
oTransform.SetTranslation(ThisApplication.TransientGeometry.CreateVector(X, Y, Z))
Call oOccurrence.SetTransformWithoutConstraints(oTransform)
End If
End Sub
Private Function ComponentByName(oCname As String) As ComponentOccurrence
ComponentByName = Nothing
For Each oOcc In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
If oOcc.Name = oCname Then
ComponentByName = oOcc
Exit Function
End If
Next
End Function Bregs
Rossano Praderi
--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------