Hello
I have a quick question about VBA Sldw macros.
In an assembly I want to look for an "X" component in the tree and if it is present then I modify my assembly.
The component can be on the first level or in a subassembly.
Hello
I have a quick question about VBA Sldw macros.
In an assembly I want to look for an "X" component in the tree and if it is present then I modify my assembly.
The component can be on the first level or in a subassembly.
I would start with this code the http://help.solidworks.com/2016/english/api/sldworksapi/get_dependencies_for_open_and_unopened_documents_example_vb.htm that allows you to find the external references and you adapt it so that they put an alert if the said part is present in the list
So if I understood correctly it will give Explicit Option
Sub main()
Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim vDepend As Variant Dim bRet As Boolean Dim i As Long
Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc
vDepend = swApp. GetDocumentDependencies2("PartSearch-1", True, True, False) If IsEmpty(vDepend) Then Debug.Print " No dependencies." Exit Sub else boolstatus = "what I want to do with my Assembly End If
For i = 0 TB (UBound(vDepend) - 1) / 2 Debug.Print " " + vDepend(2 * i) + " --> " + vDepend(2 * i + 1) Next i
End Sub
What is the purpose of the bRet? I imagine that the last three lines (For i ...) are to go down into the different levels of the tree.
In any case thank you for your answer
For example, yes, the important thing is at the level of the area
For i = 0 TB (UBound(vDepend) - 1) / 2
If vDepend(2 * i) = "blade shaft" Then MsgBox "blade shaft present in assembly"
Next i
or you can slide like there a control on the name of the room
Option Explicit
Sub main()
' Name of unopened document
Const sDefaultName As String = "c:\program files\solidworks corp\solidworks\samples\tutorial\advdrawings\98food processor.sldasm"
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sDocName As String
Dim vDepend as Variant
Dim bRet As Boolean
Sun i As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
sDocName = swModel.GetPathName
Else
sDocName = sDefaultName
End If
vDepend = swApp.GetDocumentDependencies2(sDocName, True, True, False)
'Debug.Print sDocName
If IsEmpty(vDepend) Then
Exit Sub
End If
For i = 0 TB (UBound(vDepend) - 1) / 2
If vDepend(2 * i) = "blade shaft" Then MsgBox "blade shaft present in assembly"
Next i
End Sub
In simplified version and with support for several parts sought
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vDepend as Variant
Dim bRet As Boolean
Sun i As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub
vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False)
If IsEmpty(vDepend) then Exit Sub
Dim Ref As Variant
For i = 0 TB (UBound(vDepend) - 1) / 2
For Each Ref In Array("drive shaft", "drive shaft pin")
If vDepend(2 * i) = Ref Then MsgBox Ref & " is present in the assembly."
Next Ref
Next i
End Sub
The line to be modified is as follows
For Each Ref In Array("drive shaft", "drive shaft pin")
or drive shaft and drive shaft pin sound the names of the parts searched you can put as many as you want as long as you repeit the exriture ("X", "Y", "Z", "etc")
Perfect, I managed to do what I wanted, I generalized the function according to what I want to do:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
If GetCompInAss("Part2") = True Then
If the document then presents:
boolstatus = Part.Extension.SelectByID2("Part2-1@Assembly", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.EditDelete 'Delete
End If
End Sub
Function GetCompInAss(DocCible As Variant)
Dim vDepend as Variant
Dim bRet As Boolean
Sun i As Long
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False)
If IsEmpty(vDepend) then Exit Function
For i = 0 TB (UBound(vDepend) - 1) / 2
If vDepend(2 * i) = DocTarget Then
GetCompInAss = True
End If
Debug.Print " " + vDepend(2 * i) + " --> " + vDepend(2 * i + 1)
Next i
End Function
Last questions, what is the bRet for? I am forced to repoint the document in the function cannot be avoided this kind of redundancy ?
If you have any other subjection to simplify the code I'm interested.
In any case, thank you for your help.
bRet is useless in the macro
Which can be simplified as follows
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
If GetCompInAss("Part2") = True Then
If the document then presents:
boolstatus = Part.Extension.SelectByID2("Part2-1@Assemblage", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.EditDelete 'Delete
End If
End Sub
Function GetCompInAss(DocCible As Variant) As Boolean
Dim vDepend as Variant
Sun i As Long
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False)
If IsEmpty(vDepend) then Exit Function
For i = 0 TB (UBound(vDepend) - 1) / 2
If vDepend(2 * i) = DocCible Then GetCompInAss = True
Next i
End Function
hello MaD,
I got your macro back which works fine.
I would like to hide the found component => how do I do it?
Thank you
Try this:
Part.EditDelete 'Delete - To replace with the one below Part.HideComponent2() ' Hide the component