Rotate assembly component around axis

<?xml encoding=”UTF-8″>By Adam Nagy

There is already a blog post on rotating inside a drawing, now we’ll do the same with an assembly component / component occurrence.

Once you have the matrix of the rotation and the current transformation of the component, you just have to combine them and assign that to the component.

Just select the axis you want to use and the component you want to rotate by 90 degrees, then run the VBA code:

Sub RotateAroundAxis()
Const PI = 3.14159265358979
Dim d As AssemblyDocument
Set d = ThisApplication.ActiveDocument
Dim a As WorkAxis
Set a = d.SelectSet(1)
Dim o As ComponentOccurrence
Set o = d.SelectSet(2)
Dim t As TransientGeometry
Set t = ThisApplication.TransientGeometry
Dim l As Line
Set l = a.Line
Dim mRot As Matrix
Set mRot = t.CreateMatrix()
' PI / 2 => 90 deg
Call mRot.SetToRotation(PI / 2, l.Direction.AsVector(), l.RootPoint)
Dim m As Matrix
Set m = o.Transformation
Call m.PreMultiplyBy(mRot)
o.Transformation = m
End Sub

Result:

Rotate_component

 


Comments

10 responses to “Rotate assembly component around axis”

  1. Hi, how rotate without select part and axis manualy?
    thank you
    Marcos

  2. You can simply create a Vector and Point using the CreateVector() and CreatePoint() functions of the Application.TransientGeometry object, and pass those to the SetToRotation() function.

  3. Maarten Maertens Avatar
    Maarten Maertens

    Cool! Usefull code, thanks.
    How can I rotate standard along the X-axis – without selecting the axis?
    Step 1: Select the part in the assembly
    Step 2: flip 180°. I would link a keyboard button to the macro.
    We “drag and drop” our parts from our internal library. It would help if I don’t need to rotate manually every single part again. With a shortcut ‘flip 180°’, it would go much faster.
    Thanks!!

  4. In the above code, instead of
    “Set a = d.SelectSet(1)”
    you could say
    “Set a = d.ComponentDefinition.WorkAxes(1)”
    And here’s how to create a Ribbon button for a VBA macro:
    https://modthemachine.typepad.com/my_weblog/2016/02/creating-a-button-for-a-vba-macro.html

  5. Tobias Avatar
    Tobias

    Awesome code!
    Is there any way to rotate an inserted assembly around its own X-axis?
    I use the added code you wrote to Maarten, but that only solves the active assemblies axis and not the occurrence axis.
    Many thanks and keep it up!

  6. The Transformation property of the occurrence provides the coordinate system of the inserted assembly in context of the main assembly. And you can get the X axis of that using Matrix.GetCoordinateSystem()

  7. can you show a code to make the same think but with out selecting the part and the axis. how can i get x, y or z axis of a part with code .

  8. You can get the Axes from the ComponentDefinition (PartComponentDefinition or AssemblyComponentDefinition) of the Document: https://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-0A56D0BB-5768-4591-A3E8-84A8952644C0

  9. Maarten Maertens Avatar
    Maarten Maertens

    Hi Adam, could you write the code please of how you rotate the part around it’s own occurrence axis?
    Now I have this code:
    Sub RotateAroundAxis()
    Const PI = 3.14159265358979
    Dim d As AssemblyDocument
    Set d = ThisApplication.ActiveDocument
    Dim o As ComponentOccurrence
    Set o = d.SelectSet(1)
    Dim a As WorkAxis
    Set a = d.ComponentDefinition.WorkAxes(1)
    Dim t As TransientGeometry
    Set t = ThisApplication.TransientGeometry
    Dim l As Line
    Set l = a.Line
    Dim mRot As Matrix
    Set mRot = t.CreateMatrix()
    ‘ PI / 2 => 90 deg
    Call mRot.SetToRotation(PI, l.Direction.AsVector(), l.RootPoint)
    Dim m As Matrix
    Set m = o.Transformation
    Call m.PreMultiplyBy(mRot)
    o.Transformation = m
    End Sub

  10. The only thing missing there is that you have to bring the WorkAxis (a variable) in to the context of the assembly – see https://adndevblog.typepad.com/manufacturing/2013/07/occurrences-contexts-definitions-proxies.html
    Something like Call o.CreateGeometryProxy(a, aProxy) then work with aProxy from then on.

Leave a Reply to Maarten MaertensCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading