ShaggyPE
Mechanical
- Sep 8, 2003
- 1,127
So I am trying to hack a macro together based on a traverse assembly macro (and a find prop macro that handleman provided some time ago). I would like to have an assembly open and traverse it and its children and their configs to look for a custom property. If the property does not exist or has no value, I want it to put it into a txt file. The biggest problem so far seems to be putting it into a text file. I have not used the print operation very much and it seems to operate in a way that I didn't anticipate.
This code basically works so far, but I am not able to add the info from the traverseConfigurations sub to the txt file.
In general I think my traverseConfigurations sub is crap because that is basically the one I am trying to add based on the help file.
Any help is much appreciated. Mind you this code is to be used on SW2006.
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
This code basically works so far, but I am not able to add the info from the traverseConfigurations sub to the txt file.
In general I think my traverseConfigurations sub is crap because that is basically the one I am trying to add based on the help file.
Any help is much appreciated. Mind you this code is to be used on SW2006.
Code:
Const swDocASSEMBLY = 2
Dim swApp As SldWorks.SldWorks
Dim ModelDoc As SldWorks.ModelDoc2
Dim strText As Variant
Dim FindThisProp As String
Dim value As String
Sub Main()
Dim swRootComp As SldWorks.Component2
Dim swConf As SldWorks.configuration
Dim FileNum As Integer
Dim FS As Scripting.FileSystemObject
Dim FileName As String
Set swApp = Application.SldWorks
Set ModelDoc = swApp.ActiveDoc
FindThisProp = CStr(InputBox("Enter custom property name"))
If Not ModelDoc Is Nothing Then
If ModelDoc.GetType = swDocASSEMBLY Then
Debug.Print ModelDoc.GetTitle
strText = "Files Missing Custom Property: " & FindThisProp & vbCrLf & vbCrLf
value = ModelDoc.GetCustomInfoValue("", FindThisProp)
If value = "" Then
Debug.Print ModelDoc.GetTitle & "" & vbCrLf & vbCrLf
strText = strText & ModelDoc.GetTitle
End If
'Get the active configuration
Set swConf = ModelDoc.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
'Traverse the assembly
TraverseComponent swRootComp, 1
End If
End If
Set FS = New Scripting.FileSystemObject
FileNum = FreeFile
'Get a temporary filename
FileName = FS.GetSpecialFolder(TemporaryFolder) & "\" & FS.GetTempName
'Open the file for writing
Open FileName For Output As FileNum
Print #FileNum, strText
Close #FileNum
'Open the output in notepad
Shell "notepad.exe " & FileName, vbNormalFocus
Set FS = Nothing
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
'this recursively traverses all of the components in an assembly and prints their name to the immediate window
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
'Dim swCompConfig As SldWorks.configuration
Dim i As Long
Dim childvalue As String
Dim ModDoc As Object ' ModelDoc2 of child component
Dim ModName As String
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
ModName = swChildComp.Name
Set ModDoc = swChildComp.GetModelDoc()
childvalue = ModDoc.GetCustomInfoValue("", FindThisProp)
If childvalue = "" Then
Debug.Print swChildComp.Name2
strText = strText & vbCrLf & ModDoc.GetTitle
End If
TraverseConfigurations ModDoc
'TraverseComponentFeatures swChildComp, nLevel
TraverseComponent swChildComp, nLevel + 1
Next i
End Sub
[b]Sub TraverseConfigurations(swModel As SldWorks.ModelDoc2)
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim vConfName As Variant
Dim swConfComp As SldWorks.Component2
Dim ConfValue As String
Dim k As Long
vConfName = swModel.GetConfigurationNames
For k = 0 To UBound(vConfName)
ConfValue = swModel.GetCustomInfoValue("", FindThisProp)
'MsgBox (ConfValue)
If ConfValue = "" Then
Debug.Print swChildComp.Name2
strText = strText & vbCrLf & ModDoc2.GetTitle
End If
Next k
End Sub[/b]
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist