Identify missing custom property or config-specific property macro
Identify missing custom property or config-specific property macro
(OP)
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.
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
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
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
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
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist






RE: Identify missing custom property or config-specific property macro
Then look online for samples of writing to a text file using VBScript.
-handleman, CSWP (The new, easy test)
RE: Identify missing custom property or config-specific property macro
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
RE: Identify missing custom property or config-specific property macro
Try just commenting out that debug.print line. It really has no function in your macro. If you really want to send some output to the immediate window, change it to debug.print swModel.gettitle.
Also, the line immediately following that, change ModDoc2 to swModel.
-handleman, CSWP (The new, easy test)
RE: Identify missing custom property or config-specific property macro
That did the trick. Now, new question, but regarding this macro.
I need to toss an if statement prior to the configuration operation. Basically if the "part number to be displayed in the bill of materials" is set to "configuration" I want to look to see if that config has the property. I the "part number to be displayed in the bill of materials" is set to "document name" I want to skip the config altogether.
The true issue I am having is how do I find out what that is set to? In a previous macro, I was able to set that using:
CODE
But how do get it, not set it.
Here is the current portion of the code:
CODE
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)
****SOMETHING IN HERE*****
GoTo skipconfig
MsgBox (vConfName & Flag)
End If
ConfValue = swModel.GetCustomInfoValue("", FindThisProp)
'MsgBox (ConfValue)
If ConfValue = "" Then
Debug.Print swModel.GetTitle
strText = strText & vbCrLf & swModel.GetTitle
GoTo MissingProp2
End If
skipconfig:
Next k
MissingProp2:
End Sub
I think I either need to use Configuration.BOMPartNoSource or Configuration.AlternateName but I can't seem to get either of those to work.
Thanks again.
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
RE: Identify missing custom property or config-specific property macro
First, you need to get a pointer to the configuration you want using GetConfigurationByName. You know the name of the configuration, you just need to get the actual configuration.
-handleman, CSWP (The new, easy test)
RE: Identify missing custom property or config-specific property macro
Once that works, I am not sure if the BOMSource line will work. There wasn't an example on using .swBOMPartNumberSource in the help. Also, the if statement after that... not sure what that will do.
CODE
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim vConfName As Variant
Dim swConfComp As SldWorks.Component2
Dim ConfValue As String
Dim swConfig As SldWorks.Configuration
Dim k As Long
Dim BOMsource As String
vConfName = swModel.GetConfigurationNames
For k = 0 To UBound(vConfName)
swConfig = swModel.GetConfigurationByName(vConfName(k))
BOMsource = swConfig.swBOMPartNumberSource
If BOMsource = swBOMPartNumber_ConfigurationName Then
'MsgBox (BOMsource)
ConfValue = swModel.GetCustomInfoValue("", FindThisProp)
'MsgBox (ConfValue)
If ConfValue = "" Then
Debug.Print swModel.GetTitle
strText = strText & vbCrLf & swModel.GetTitle
GoTo MissingProp2
End If
End If
Next k
MissingProp2:
End Sub
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
RE: Identify missing custom property or config-specific property macro
swConfig is a Configuration object, so you need
Set swConfig = swModel.GetConfigurationByName(vConfName(k))
-handleman, CSWP (The new, easy test)
RE: Identify missing custom property or config-specific property macro
Now I am getting error 438 "Object doesn't support this property or method" on the BOMsource = swConfig.swBOMPartNumberSource line.
Any recommedations? I wish help had an example for this type of operation. Sorry for all of the questions.
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
RE: Identify missing custom property or config-specific property macro
-handleman, CSWP (The new, easy test)
RE: Identify missing custom property or config-specific property macro
Thanks again for the all the help. Below is the completed code.
If anyone is interested. This macro will traverse the open assembly, its children and all of their configs. It will look for a custom property and/or config specific custom property in all of the files. If it finds that a file is missing the property first in custom then in config specific custom, it will list that filename in a text file.
There is also a little if statement that when it gets to the config, it will determine if the "part number displayed in BOM" setting is set to "configuration name". If that is the case, it will look for the config specific custom property. This option is modifiable in the code and there are comments in the code to explain it.
This was created in SW2006, so it should work for most anyone.
CODE
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 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
GoTo MissingProp1 'jump out because we found a missing property in custom
End If
'MsgBox (ModDoc.GetTitle)
TraverseConfigurations ModDoc
MissingProp1:
'TraverseComponentFeatures swChildComp, nLevel
TraverseComponent swChildComp, nLevel + 1
Next i
End Sub
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 swConfig As SldWorks.Configuration
Dim k As Long
Dim BOMsource As String
vConfName = swModel.GetConfigurationNames
For k = 0 To UBound(vConfName)
Set swConfig = swModel.GetConfigurationByName(vConfName(k))
BOMsource = swConfig.BOMPartNoSource
If BOMsource = 2 Then
'> 1 = swBOMPartNumber_DocumentName
'> 2 = swBOMPartNumber_ConfigurationName
'> 3 = swBOMPartNumber_ParentName
'> 4 = swBOMPartNumber_UserSpecified
ConfValue = swModel.GetCustomInfoValue(vConfName(k), FindThisProp)
If ConfValue = "" Then
Debug.Print swModel.GetTitle
strText = strText & vbCrLf & swModel.GetTitle '& " -> " & vConfName(k)
GoTo MissingProp2 'jump out because we found a missing property in config specific props
End If
End If
Next k
MissingProp2:
End Sub
-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist