Move occurence along axis or Edge

Move occurence along axis or Edge

GeorgK
Advisor Advisor
567 Views
4 Replies
Message 1 of 5

Move occurence along axis or Edge

GeorgK
Advisor
Advisor

Hello together,

 

how could I move a selected part along an edge or axis? The sample works along the z-axis.

 

Public Sub MovePart()
    Dim occ As ComponentOccurrence
    Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter,"Select an occurrence.")

    Dim i As Integer
    For i = 1 To 10
        Dim trans As Matrix
        Set trans = occ.Transformation
        trans.Cell(2, 4) = trans.Cell(2, 4) + .5
        occ.Transformation = trans
    Next
   
End Sub

 

Thanks Georg

0 Likes
Accepted solutions (1)
568 Views
4 Replies
Replies (4)
Message 2 of 5

ekinsb
Alumni
Alumni

Moving an occurrence is done by changing the transformation matrix associated with it.  There are various ways of defining that matrix.  The example code you've posted it doing it at the lowest level possible by setting specific values within the matrix that control the translation portion of the matrix.  Below is a picture that illustrates how a matrix defines the position of an occurrence. 

 

Matrix.png

 

The easiest way to think about it is that it defines a coordinate system.  The first, second, and third columns are vectors that defines the direction of the X, Y, and Z axes of the coordinate system.  The fourth column defines the position.  The matrix shown above is a special matrix called an "identity" matrix because it specifies a coordinate system that is the same as the world coordinate system.  The X Axis is (1,0,0), or along the work X axis, the Y Axis is (0,1,0), or along the world Y axis, and the Z axis is along the world Z axis.  The origin is at (0,0,0).

 

Your code is modifying the origin position.  Cell(2,4) will edit the second row in the fourth column, which is actually the Y direction.  Cell(1,4) will edit the X component and Cell(3,4) will edit the Z.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 5

GeorgK
Advisor
Advisor

Hello Brian,

 

thanks for the explanation. The problem is to calculate the transformation from the selected axis or edge.

 

Georg

0 Likes
Message 4 of 5

GeorgK
Advisor
Advisor

Does nobody know a solution for the problem?

 

Georg

0 Likes
Message 5 of 5

ekinsb
Alumni
Alumni
Accepted solution

Here's some rough VBA code that demonstrates moving a selected occurrence along a selected edge or work axis a specified distance.

 

Public Sub MoveOccAlongEdge()
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
    Dim occ As ComponentOccurrence
    Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select occurrence to move")
    
    Dim vec As Vector
    If False Then
        Dim edg As Edge
        Set edg = ThisApplication.CommandManager.Pick(kPartEdgeLinearFilter, "Select edge.")
        Dim lin As LineSegment
        Set lin = edg.Geometry
        Set vec = lin.Direction.AsVector
    Else
        Dim axis As WorkAxis
        Set axis = ThisApplication.CommandManager.Pick(kWorkAxisFilter, "Select work axis.")
        Set vec = axis.Line.Direction.AsVector
    End If
    
    Dim strDistance As String
    strDistance = InputBox("Enter the distance to move.", "Distance", "1 in")
    Dim distance As Double
    distance = asmDoc.UnitsOfMeasure.GetValueFromExpression(strDistance, kDefaultDisplayLengthUnits)
    
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    
    vec.Normalize
    Call vec.ScaleBy(distance)
    
    Dim trans As Matrix
    Set trans = occ.Transformation
    
    trans.Cell(1, 4) = trans.Cell(1, 4) + vec.X
    trans.Cell(2, 4) = trans.Cell(2, 4) + vec.y
    trans.Cell(3, 4) = trans.Cell(3, 4) + vec.Z
    
    occ.Transformation = trans
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog