[VBA] Selecting from the Design Tree

Hi everyone!

 

After research, and macro recording function, I managed to replace a component via macro! =)

 

It's cool, it works, and you can change anything by anything else (you have to know what you do on the assembly basically)

 

That's all well and good, but... I was only able to do a "GetSelectedObjectsComponent" =(

So the back and forth between solidworks and excel to continue (if there are ever several replacements to be made) is a shame.

 

So my question would be: "Would there be a way in vba to select an object in the design tree?"

 

As we do to select a file in a folder for example, a variable "filename" that we put in the code like "open(filename.sldprt)^^

 

There you go, once again I'm coming to bother you, forgive me

 

Friendly

 

Yoann

Option Explicit

 

' ******************************************************************************

' C:\Users\jfaradon\AppData\Local\Temp\swx8716\Macro1.swb - macro recorded on 02/19/14 by jfaradon

' ******************************************************************************

Dim swApp As SldWorks.SldWorks

 

Here is an example of a macro that replaces one component with another by selection (you have to give the name)

 

 

Dim swDoc As SldWorks.ModelDoc2

Dim swAss As SldWorks.AssemblyDoc

Dim stOldFileName As String, stNewFileName As String

Dim bStatus As Boolean

 

Sub main()

 

    Set swApp = Application.SldWorks

    Set swDoc = swApp.ActiveDoc

    

    If swDoc Is Nothing Then Exit Sub

    If swDoc.GetType <> swDocumentTypes_e.swDocASSEMBLY Then Exit Sub

    

    Set swAss = swDoc

    stOldFileName = swApp.GetCurrentMacroPathFolder & "\370-TreeManager\370-Burner.SLDPRT"

    stNewFileName = swApp.GetCurrentMacroPathFolder & "\370-TreeManager\370-Burner - Copy.SLDPRT"

    

    swDoc.Extension.SelectByID2 "370-Burner-1@370-Torch", "COMPONENT", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault

    bStatus = swAss.ReplaceComponents(stNewFileName, "", True, True)

 

    swDoc.Extension.SelectByID2 "370-Burner - Copy-1@370-Torch", "COMPONENT", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault

    bStatus = swAss.ReplaceComponents(stOldFileName, "", True, True)

 

 

End Sub

8 Likes

Hello to you jfaradon =)

 

If I understood the macro correctly, it would give something like this for the user to choose the part in the tree and the one that will replace it:

 

 

Dim swDoc As SldWorks.ModelDoc2

Dim swAss As SldWorks.AssemblyDoc

Dim stNewFileName As String

Dim bStatus As Boolean

Dim piece as string

Dim assem as string

 

 

 

Sub main()

 

    Set swApp = Application.SldWorks

    Set swDoc = swApp.ActiveDoc

   piece= InPutBox("Name of the part to be changed")

    assem =InPutBox("Open Assembly Name")

    Path= InPutBox("Replacement Part Path")

    Piecebis=InPutBox("Replacement Part Name")

    

    If swDoc Is Nothing Then Exit Sub

    If swDoc.GetType <> swDocumentTypes_e.swDocASSEMBLY Then Exit Sub

    

    Set swAss = swDoc

    stNewFileName = Path & Piecebis & ". SLDPRT"

 

    

    swDoc.Extension.SelectByID2 Piece & "@" & assem, "COMPONENT", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault

    bStatus = swAss.ReplaceComponents(stNewFileName, "", True, True)

 

End Sub

 

PS: yes, I must admit that the second part I didn't understand everything^^'

Yes, that's it...

and indeed I haven't cleaned up the end of the code, the last lines are useless ...

in fact, the ReplaceComponents API replaces the selected component with the specified file

 

 

4 Likes

It's strange, it doesn't matter, I just tried the selection also with a getpathname at the end to see if it selected the part, and it takes me out the assembly in the msgbox...

 

As if the swDoc variable had remained on the swApp.activeDoc value

Hi all!

 

still in my research!

 

I tried the .getbyname function, which worked but the replacecomponents did not take effect

 

I tried to modify the example of the solidworks help as well but in vain =(

 

Macro Operation:

 

"Select part in the building tree

  Give path + name of spare part
  Replace the shaft part with the spare part"

 

The macro I have right now doesn't show me any errors. but.... doesn't do anything either :crazy:

 

 

Sub main()

 
path = InputBox("Replacement part path")

piecebis = InputBox("Replacement Part Name")
stnewfilename = path & piecebis &". SLDPRT"

   

Set swApp = CreateObject("SldWorks.Application")
    Set swComp = swApp.ActiveDoc
    Set swModel = swApp.ActiveDoc

    Set swAssy = swModel
bstatus = swComp.Extension.SelectByID2("00-XXXXX-0-Came-1@swAssy", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

   

MsgBox (swComp.GetPathName)
   

bstatus = swAssy.ReplaceComponents("C:\PDM\11 SIMULATION\01 Continuous Cartoner\Product Introduction\00-XXXXX-0-Cam Return Plan. SLDPRT", "02", False, True)

 

MsgBox (swComp.GetPathName)
 
 swAssy.ForceRebuild

 

 

End Sub