AnsweredAssumed Answered

update in SubAssembly when Transform a part

Question asked by Lenny Yang on Jan 15, 2019
Latest reply on Jan 17, 2019 by Artem Taturevych

Hi every one ,

I have some question about transform a part in subassambly.Iwant to update the location when I trandform a partdoc in subassambly for make a animation.

can you help me ?

thank you very much .


here is the code======================================


'Just Open the "TopAssem.SLDASM" and run this macro.

'you can see "Part1-1" have been update to correct location,but "Part1-2" is incorrect.

Dim swApp As Object

Dim swModel As SldWorks.ModelDoc2

Sub main()



Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Dim swComp1 As SldWorks.Component2

Dim swComp2 As SldWorks.Component2



'The "Part1-1" is Fix in SubAssem,it can update location when  "MoveComp(swApp, 0.3, swComp1)" finished immediately.

'PS:If a part or assembly if belong the "TopAssem" Directly,it can update location immediately whether it is floating or not.

Set swComp1 = GetCompByName(swModel, "Part1-1")

Call MoveComp(swApp, 0.3, swComp1)



'The "Part1-2" is Float in SubAssem(Assem1) of TopAssem.and when the"MoveComp(swApp, 0.2, swComp2)" finished it can not Update location.

'In order to display the correct location I have to rebuild the AssemblyDoc.

'PS:I don't want to Rebuild the model,because I want to make a Animation by this macro,It may move the part several times a second,if I rebuild the model the Animation will be stuck.

Set swComp2 = GetCompByName(swModel, "Part1-2")

Call MoveComp(swApp, 0.2, swComp2)



End Sub



Function GetCompByName(swModel As SldWorks.ModelDoc2, CompName As String) As SldWorks.Component2



    Dim swAssy As SldWorks.AssemblyDoc

    Dim swComp As SldWorks.Component2

    Set swAssy = swModel

    Set swComp = swAssy.GetComponentByName(CompName)

    Set GetCompByName = swComp


End Function



Sub MoveComp(swApp As SldWorks.SldWorks, MoveDistance As Double, swComp As SldWorks.Component2)

    Dim swMathUtil As SldWorks.MathUtility

    Dim swModel As SldWorks.ModelDoc2

    Dim swSelMgr As SldWorks.SelectionMgr

    Dim swXform As SldWorks.MathTransform

    Dim i As Long

    Dim bRet As Boolean

    Set swMathUtil = swApp.GetMathUtility

    Set swModel = swApp.ActiveDoc

    Dim MathVxform As SldWorks.IMathTransform

    Dim Xform(15) As Double

    Dim vXform As Variant


    Xform(0) = 1#

    Xform(1) = 0#

    Xform(2) = 0#

    Xform(3) = 0#

    Xform(4) = 1#

    Xform(5) = 0#

    Xform(6) = 0#

    Xform(7) = 0#

    Xform(8) = 1#

    Xform(9) = MoveDistance

    Xform(10) = 0#

    Xform(11) = 0#

    Xform(12) = 1#

    Xform(13) = 0#

    Xform(14) = 0#

    Xform(15) = 0#

    vXform = Xform

    Set swXform = swMathUtil.CreateTransform(vXform)

    swComp.Transform2 = swXform




End Sub


here is the code======================================