AnsweredAssumed Answered

some question about get context between subassembly and topassembly

Question asked by Lenny Yang on May 1, 2019
Latest reply on May 2, 2019 by Lenny Yang

hi everybody,

I have some question about get context between subassembly and topassembly use "GetCorresponding" or "GetCorrespondingEntity".

See the code and attachments。

 

'here is my vba code=====================================================

Option Explicit

Sub main()

 

    Dim swApp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2

    Dim swModelDocExt As SldWorks.ModelDocExtension

    Dim swSelMgr As SldWorks.SelectionMgr

    Dim swComp As SldWorks.Component2

    Dim swAssComp As SldWorks.Component2

    Dim PreSelectType As Integer

    Dim swCompModel As SldWorks.ModelDoc2

    Dim nRetval As Long

    Dim vPIDarr As Variant

 

 

     

'Step1-Open "SubAssembly"

'Step2-Select component "A" or an Edge of "A"

'Step3-Run this macro

 

 

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Dim SelType As swSelectType_e

    SelType = swSelMgr.GetSelectedObjectType2(1)

    'mark the selection type

    If SelType = swSelectType_e.swSelCOMPONENTS Then

        PreSelectType = 0

        Dim swSelComp As SldWorks.Component2

        Set swSelComp = swSelMgr.GetSelectedObject6(1, 0)

        Set swModelDocExt = swModel.Extension

        vPIDarr = swModelDocExt.GetPersistReference3(swSelComp)

    ElseIf SelType = swSelectType_e.swSelEDGES Then

        PreSelectType = 1

        Dim swSelEnt As SldWorks.Entity

        Set swSelEnt = swSelMgr.GetSelectedObject6(1, 0)

        Set swModelDocExt = swModel.Extension

        vPIDarr = swModelDocExt.GetPersistReference3(swSelEnt)

    End If

     

 

Stop

'Step4-Close "SubAssembly"

'Step5-Open the "TopAssembly"

'Step6-Select the component "SubAssembly" in SubAssembly

'Step7-Press F5 to Continue this macro

 

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swAssComp = swSelMgr.GetSelectedObject6(1, 0)

    Set swCompModel = swAssComp.GetModelDoc

    Set swModelDocExt = swCompModel.Extension

    swModel.ClearSelection

    If PreSelectType = 0 Then 'if we select a component

        Dim compAInSubAss As SldWorks.Component2

        'Here we can Reacquire the component or edge which save in "vPIDarr".

        Set compAInSubAss = swModelDocExt.GetObjectByPersistReference3((vPIDarr), nRetval)

        Dim compAInTopAss As SldWorks.Component2

        Set compAInTopAss = swAssComp.GetCorresponding(compAInSubAss)

        Debug.Print compAInTopAss.Name2

        compAInTopAss.Select (False) '--------------------->can not select the compInTopAss successful.why????????

    ElseIf PreSelectType = 1 Then'if we select an edge

        Dim entityInSubAss As SldWorks.Entity

        'Here we can Reacquire the component or edge which save in "vPIDarr".

        Set entityInSubAss = swModelDocExt.GetObjectByPersistReference3((vPIDarr), nRetval)

        Dim entityInTopAss As SldWorks.Entity

        Set entityInTopAss = swAssComp.GetCorrespondingEntity(entityInSubAss) '-------------------------->here return nothing,why????????????????

        entityInTopAss.Select (False)

    End If

     

End Sub

'here is my vba code=====================================================

Attachments

Outcomes