vb.net recursively search reference files

All,

I am wondering if anyone can direct me to where I might find somesample code to recursively search the active assembly and displayall the fasteners in it, in a testbox.

Sorry, very new to programming and do not even have a start for youto display where I am at. I am starting from scratch.

Any help will be greatly appreciated.

Thanks!!SolidworksApi macros
Answers 3
SF
SF 2009-07-01 Best Answer
Hi Rodney.

I think this would be what you might be looking for. I wouldstrongly suggest getting the API manual

Steps: Create a userForm and add a button called cmdDo and a buttoncalled cmdExit and a listbox called lstComp
(This should be the buttons' names not the caption)

You have to have an assy open.

Regards



Public Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swAssy As SldWorks.AssemblyDoc
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim nStart As Single
Dim bRet As Boolean

Set swApp = Application.SldWorks ' connect to SW
Set swModel = swApp.ActiveDoc 'get the active assy
Set swConfigMgr = swModel.ConfigurationManager
Set swConf = swConfigMgr.ActiveConfiguration
Set swRootComp = swConf.GetRootComponent

nStart = Timer
UserForm1.lstComp.AddItem ("File = " & swModel.GetPathName)

'traverse all of the assy features
traverseModelFeatures swModel, 1

'Now traverse all of the components and sub assemblies
traverseComponent swRootComp, 1
UserForm1.lstComp.AddItem ("")
UserForm1.lstComp.AddItem (" Time = " & Timer - nStart & "s")

End Sub


Sub traverseModelFeatures(swModel As SldWorks.ModelDoc2, nLevel AsLong)
'this code recursively traverses all of the features in a model
Dim swFeat As SldWorks.Feature
Set swFeat = swModel.FirstFeature
traverseFeatureFeatures swFeat, nLevel
End Sub


Sub traverseFeatureFeatures(swFeat As SldWorks.Feature, nLevel AsLong)
'recursively traverses the feature's features
Dim swSubFeat As SldWorks.Feature
Dim swSubSubFeat As SldWorks.Feature
Dim swSubSubSubFeat As SldWorks.Feature
Dim sPadStr As String
Dim i As Long

For i = 0 To nLevel
sPadStr = sPadStr + " "
Next i

While Not swFeat Is Nothing
UserForm1.lstComp.AddItem (sPadStr + swFeat.Name + " [" +swFeat.GetTypeName2 + "]")
Set swSubFeat = swFeat.GetFirstSubFeature

While Not swSubFeat Is Nothing
UserForm1.lstComp.AddItem (sPadStr + swSubFeat.Name + " [" +swSubFeat.GetTypeName2 + "]")
Set swSubSubFeat = swSubFeat.GetFirstSubFeature

While Not swSubSubFeat Is Nothing
UserForm1.lstComp.AddItem (sPadStr + swSubSubFeat.Name + " [" +swSubSubFeat.GetTypeName2 + "]")
Set swSubSubSubFeat = swSubSubFeat.GetFirstSubFeature

While Not swSubSubSubFeat Is Nothing
UserForm1.lstComp.AddItem (sPadStr + swSubSubSubFeat.Name + " [" +swSubSubSubFeat.GetTypeName2 + "]")
Set swSubSubSubFeat = swSubSubSubFeat.GetNextSubFeature
Wend

Set swSubSubFeat = swSubSubFeat.GetNextSubFeature
Wend

Set swSubFeat = swSubFeat.GetNextSubFeature
Wend
Set swFeat = swFeat.GetNextFeature
Wend
End Sub

Sub traverseComponent(swComp As SldWorks.Component2, nLevel AsLong)

'recursively traverses all the components in the assembly
Dim vchildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim swCompConfig As SldWorks.Configuration
Dim sPadStr As String
Dim i As Long


For i = 0 To nLevel - 1
sPadStr = sPadStr + " "
Next i

vchildComp = swComp.GetChildren

For i = 0 To UBound(vchildComp)

Set swChildComp = vchildComp(i)

UserForm1.lstComp.AddItem (sPadStr & "+ " &swChildComp.Name2 & " <" &swChildComp.ReferencedConfiguration & ">")
'shows other components like sensors / design binder / etc frombeing listed
'traverseComponentFeatures swChildComp, nLevel
traverseComponent swChildComp, nLevel + 1
continueFor:
Next i



End Sub

Sub traverseComponentFeatures(swComp As SldWorks.Component2, nLevelAs Long)
'this recursively traverses all of the components features
Dim swFeat As SldWorks.Feature
Set swFeat = swComp.FirstFeature
traverseFeatureFeatures swFeat, nLevel
End Sub

Sub customWhereUsed(swComp As SldWorks.Component2)

On Error GoTo ErrorHandler
' Trap all errors that would cause the program to halt.
Dim Classfac As SwDMClassFactory
Dim swDocMgr As SwDMApplication
Dim swDoc As SwDMDocument
Dim nretval As Long


' Instantiate a SolidWorks Document Manager session.
Set Classfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
Set swDocMgr = Classfac.GetApplication("90000053761842439PQDC5CC")'Specify your license key

Set swDoc = swDocMgr.GetDocument("C:\parts\derived_part.sldprt",swDmDocumentPart, False, nretval) ' Open the file as read-write.
Debug.Print nretval ' Examine this variable to ensure there wereno errors opening the file.


' Assume this part is derived from a base part.
Debug.Print swDoc.Author
swDoc.Author = "New Author"


Dim vnames As Variant
vnames = swDoc.GetCustomPropertyNames
' Examine vnames in the debugger.
Debug.Print swDoc.AddCustomProperty("New Sample Date Property",swDmCustomInfoDate, "3/2/1943") ' Should print out True, unless theproperty already exists.
Debug.Print swDoc.GetCustomProperty("New Sample Date Property",swDmCustomInfoDate) ' Should print out 3/2/1943.

Dim ActiveConfig As SwDMConfiguration
Set ActiveConfig =swDoc.ConfigurationManager.GetConfigurationByName(swDoc.ConfigurationManager.GetActiveConfigurationName)
' Get the active configuration name and then gets the associatedconfiguration.

Debug.Print ActiveConfig.Name2 ' Prints out the activeconfiguration name.
Dim bodycount As Long
bodycount = ActiveConfig.GetBodiesCount

If bodycount > 0 Then ' If solid bodies exist, then save thefirst
' body as a Parasolid binary file. Should print out 0 for noerror.
Debug.Print ActiveConfig.GetBody(0, "C:\parts\derived_part.x_b")
End If

Dim SearchOpt As SwDMSearchOption
Set SearchOpt = swDocMgr.GetSearchOptionObject
SearchOpt.AddSearchPath ("C:\parts")

Dim vWhereUsed As Variant
vWhereUsed = swDoc.WhereUsed(SearchOpt)
' Examine vWhereUsed in the debugger. If the base part of
' derived_part.sldprt is in C:\parts, then the filename isreturned.

Dim vReferences As Variant
vReferences = swDoc.GetAllExternalReferences(SearchOpt)
' Examine vReferences in the debugger. If the part has no
' references, the array should be empty.

swDoc.Save ' Save the changes made to the custom properties.

swDoc.CloseDoc ' Close the document before exiting.
' Examine the part file's new custom properties in SolidWorks orWindows Explorer.

ErrorHandler:
Debug.Print Err.Description
If Not swDoc Is Nothing Then
swDoc.CloseDoc
End If

End Sub


SF
SF 2009-07-01
This should set you in the right direction for now
RM
RM 2009-07-01
Thanks Andries!