AnsweredAssumed Answered

Move component with mouse event from center of mass

Question asked by roberto gennari on Jan 31, 2019
Latest reply on Feb 1, 2019 by roberto gennari


I created an assembly macro that clones a component and adds it to the current assembly, the position is decided by the user at the click of the mouse, it works as "Add component" becouse the user decide where to place it at the click of the mouse.

As you can see from the attached video the transformation of the component is performed at the mouse move event , at each movement the position of the 3 coordinates in the Modelview is calculated and then passed to the transformation of the component.

I would like the component to move relative to the center of mass and not from the origin of the component, is it possibile?


This is the part of code that performs the translation:



    Private Sub TimerWizardEvent(ByVal sender As Object, ByVal e As EventArgs)

        If IsNothing(swcomponent) Then Exit Sub

        Dim ModViewX As Double = CDbl(XWizard)
        Dim ModViewY As Double = CDbl(YWizard)


        PreSelectionPointAssembly(swModel, ModViewX, ModViewY)
        Dim vTransformData As Object
        Dim swTransform As MathTransform

        swTransform = swcomponent.Transform2
        vTransformData = swTransform.ArrayData


    'Is here the problem, in this case the new location of the component is move about his the origin


        vTransformData(9) = ModViewLocation(0)
        vTransformData(10) = ModViewLocation(1)
        vTransformData(11) = ModViewLocation(2)




        ''Modify matrix 
        swTransform.ArrayData = vTransformData

        ''Apply new transform 
        swcomponent.Transform2 = swTransform


    End Sub


Private Function PreSelectionPointAssembly(ByVal ModDoc As ModelDoc2, ByVal ModViewX As Double, ByVal ModViewY As Double) As Double()
        Dim ModView As ModelView = ModDoc.IActiveView
        If IsNothing(ModView) = False Then
            Dim MathUtil As MathUtility = iSwApp.IGetMathUtility
            Dim ModViewTransform As MathTransform = ModView.Transform
            Dim ModelViewPoint As MathPoint = MathUtil.CreatePoint(New Double(2) {ModViewX, ModViewY, 0})
            Dim ModelSpacePoint As MathPoint = ModelViewPoint.IMultiplyTransform(ModViewTransform.Inverse)

            ModViewLocation(0) = ModelSpacePoint.ArrayData(0)
            ModViewLocation(1) = ModelSpacePoint.ArrayData(1)
            ModViewLocation(2) = ModelSpacePoint.ArrayData(2)
        End If
        Return ModViewLocation

    End Function

    Private Function TheMouseWz_MouseLBtnDownNotify(ByVal x As Integer, ByVal y As Integer, ByVal WParam As Integer) As Integer

        Debug.Print("Left-mouse button pressed.")
        'Interrompo il mouse Down
        RemoveHandler TimerWizard.Tick, AddressOf Me.TimerWizardEvent
        RemoveHandler TheMouseWz.MouseLBtnDownNotify, AddressOf Me.TheMouseWz_MouseLBtnDownNotify
        RemoveHandler TheMouseWz.MouseMoveNotify, AddressOf TheMouseWz_MouseMoveNotify

        iSwApp.CommandInProgress = False

        swcomponent = Nothing

    End Function