Move a solid body using User Coordinate System(UCS)

<?xml encoding=”UTF-8″>By Sajith Subramanian

We had a query on how to move a solid body to a new position using the values obtained from the  User coordinate System or UCS .

Below is a code sample that does something similar. In the Inventor UI, there are three options viz. “Free Drag”, “Move along a Ray” and “Rotate about a line”, all of which are exposed through the API as well.

Public Sub MoveFeatureCreationSample()
' Get the active part document.
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
If oDoc Is Nothing Then
MsgBox "No part document!" & vbCrLf & "Please open a part with solids in it for this sample to run.", vbCritical, "Autodesk Inventor"
Exit Sub
End If
Dim oCompDef As PartComponentDefinition
Set oCompDef = oDoc.ComponentDefinition
If oCompDef.SurfaceBodies.Count = 0 Then
MsgBox "No solids to move!" & vbCrLf & "Please open a part with solids in it for this sample to run.", vbCritical, "Autodesk Inventor"
Exit Sub
End If
Dim oBodies As ObjectCollection
Set oBodies = ThisApplication.TransientObjects.CreateObjectCollection
' Specify a body to move.
oBodies.Add oCompDef.SurfaceBodies(1)
' Create a MoveFeatureDefinition.
Dim oMoveDef As MoveDefinition
Set oMoveDef = oCompDef.Features.MoveFeatures.CreateMoveDefinition(oBodies)
'----- Now Get the UCS---
Dim UCS As UserCoordinateSystem
Set UCS = oCompDef.UserCoordinateSystems.Item(1)
' Set the move operations onto the bodies.
Dim oFreeDrag As FreeDragMoveOperation
Set oFreeDrag = oMoveDef.AddFreeDrag(UCS.XOffset, UCS.YOffset, UCS.ZOffset)
' Dim oMoveAlongRay As MoveAlongRayMoveOperation
' Set oMoveAlongRay = oMoveDef.AddMoveAlongRay(oCompDef.WorkAxes(2), True, 2)
' Dim oRotateAboutAxis As RotateAboutLineMoveOperation
' Set oRotateAboutAxis = oMoveDef.AddRotateAboutAxis(oCompDef.WorkAxes(3), True, 0.5)
' Create the move feature.
Dim oMoveFeature As MoveFeature
Set oMoveFeature = oCompDef.Features.MoveFeatures.Add(oMoveDef)
End Sub

Below is an image that shows the change in the model before and after running the above code.

image

Discover more from Autodesk Developer Blog

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

Continue reading