Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify missing custom property or config-specific property macro 1

Status
Not open for further replies.

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.

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
 
Replies continue below

Recommended for you

Probably the easiest way to write to a text file is to use the Scripting library. Go to Tools->References and find the Microsoft Scripting Runtime library.

Then look online for samples of writing to a text file using VBScript.

-handleman, CSWP (The new, easy test)
 
I was hoping not to totally overhaul the donor macro. I did however notice the Microsoft Scripting Runtime library was already selected. The printing to the text file basically works except for the TraverseConfigurations sub. I get an error in the Debug.Print swChildComp.Name2 line. It is error 424 "object required". I think there is a minor problem there pertaining to what is being transferred into the sub. I know it is gathering the data because when I enable the MsgBox, it lists all of the properties. It just won't print the filename when the property doesn't exist.

-Dustin
Professional Engineer
Certified SolidWorks Professional
Certified COSMOSWorks Designer Specialist
Certified SolidWorks Advanced Sheet Metal Specialist
 
Sorry, didn't read as thoroughly as I should have.

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)
 
handleman,
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:
Document.EditConfiguration configurationname, configurationname, "", "", 0, 0, 0, 0, 1
where the 1 or 0 drives that option.

But how do get it, not set it.

Here is the current portion of the code:
Code:
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)
    
****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
 
Yes, that is what you need to use.

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)
 
Here is my code currently: I looked at the help for the GetConfigurationByName function... but it isn't working. I get error 91 "Object variable or With variable not set".

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:
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)
    
            [b]swConfig = swModel.GetConfigurationByName(vConfName(k))
            BOMsource = swConfig.swBOMPartNumberSource
            If BOMsource = swBOMPartNumber_ConfigurationName[/b] 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
 
For any type of variable that is an object, you need to use the "Set ____ = ____" type syntax.

swConfig is a Configuration object, so you need

Set swConfig = swModel.GetConfigurationByName(vConfName(k))

-handleman, CSWP (The new, easy test)
 
Awesome... that moved me one step closer.

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
 
It's BOMPartNoSource not swBOMPartNumberSource

-handleman, CSWP (The new, easy test)
 
handleman,
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:
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 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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor