Custom Properties
Custom Properties
(OP)
At the request of another user I am writing a macro to help automate the exporting of SW drawings to AutoCAD DWG and DXF. Our office standard is to put the rev level at the end of the filename (ie: 63570018_Rev001.dwg). The current Rev level is stored in the actual model file (sldprt or sldasm) as "Rev". When exporting the SW drawing I need to get the current Rev level of the part or assembly. How can I do this?
Any help is greatly aprreciated.
SolidWorks 2007 SP1
Any help is greatly aprreciated.
SolidWorks 2007 SP1






RE: Custom Properties
RE: Custom Properties
RE: Custom Properties
Use [code]post code here[/code]
RE: Custom Properties
Actually in SW2007 you should now look under the CustomPropertyManager; CustomInfo is now obsolete.
To get the Rev level, you must get the modeldoc2 object of the the part or assembly. One way to do this is to call ModelDoc2.GetDependencies2 (after you have the modeldoc2 for the drawing). After you have the the model name used in the drawing, you then call SldWorks::GetOpenDocumentByName to get the modeldoc2 of the part or assembly. Next you get the CustomPropertyManager by calling ModelDoc2.Extension.CustomPropertyManger. Finally call CustomPropertyManger.Get2 to get the rev value.
SA
RE: Custom Properties
Can you possible give some example code for that? I am not that well versed with SolidWorks API yet.
RE: Custom Properties
Go to SolidWorks API Help menu, and type "api custom property". You will find sample codes for "Get and Set Custom Property Values Example". ".GetCustomPropertyValues" is method you need to get revision info.
According to SolidWorks, also consistent with ASME Y14, only drawing documents have revision. Models (parts or assemblies) do not have revision although you can force them to have revision.
Also, you may want to take another look at your current file system. Putting revision info into file names is a very bad practice. Not every revision of a drawing affect model documents. To put revision info in file names is just like to put tatoo of a person's age onto his/her face. You do not want to do that because a person age is changing.
Good Luck!
Alex
RE: Custom Properties
We put the rev level on the end of the filename when we export to DWG for archiving. this way we can keep every rev level of a part. the reason we give parts and assemblies rev levels so we can keep track of how many changes, all of our parts and assemblies are used on many other assemblies for each product line, some are used across other product lines.
I was looking at the help but I can't seem to get it to work.
RE: Custom Properties
Did you try the following codes from SW API help menu? This is a VB program to set and get custom properties. You will need to twist it abit in order to run in SW VBA.
Or, maybe you can record one using SW macro command.
Good Luck!
Alex
*****************************************************
Get and Set Custom Property Values Example (VB)
This example shows how to get and set custom property values using the SolidWorks Document Manager API.
'-----------------------------------------------------------------
'
' Preconditions: E:\2005\linkedProp_ST\LinkedPropTest.SLDPRT exists
'
' Postconditions: MyMass and MyDimension custom property values are changed
'
'------------------------------------------------------------------
Option Explicit
Dim swDocMgr As SwDocumentMgr.SwDMApplication
Dim swDoc As SwDocumentMgr.SwDMDocument
Dim vExtRefArr As Variant
Dim vExtRef As Variant
Dim nretval As Long
Dim tapp As SwDMApplication
Sub Main()
'On Error GoTo errorHandler
Dim classfac As SwDocumentMgr.SwDMClassFactory
Set classfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
Set tapp = classfac.GetApplication("")
Dim d_2005 As SwDMDocument3
Dim s1 As String
Dim e As Long
Dim proptype As Long
Dim path As String
path = "E:\2005\linkedProp_ST\"
Set d_2005 = tapp.GetDocument("E:\2005\linkedProp_ST\LinkedPropTest.SLDPRT", swDmDocumentPart, True, e)
Dim v As Variant
v = d_2005.GetCustomPropertyNames
Debug.Print 'param string: ' & d_2005.GetCustomProperty('MyMass', proptype)
Debug.Print 'value: ' & d_2005.GetCustomPropertyValues('MyMass', proptype, s1)
''Note, you can also get the param string from
''SwDMDocument3::GetCustomPropertyValues or
''SwDMConfiguration4::GetCustomPropertyValues linkedTo parameter (s1)
Debug.Print 'param string: ' & s1
Debug.print ' '
Debug.Print 'param string: ' & d_2005.GetCustomProperty('DocLevelNL', proptype)
Debug.Print 'value: ' & d_2005.GetCustomPropertyValues('DocLevelNL', proptype, s1)
Dim c As SwDMConfiguration4
Set c = d_2005.ConfigurationManager.GetConfigurationByName("config_h1")
Dim v2 As Variant
v2 = c.GetCustomPropertyNames
Debug.Print "param string: " & c.GetCustomProperty("MyDimension", proptype)
Debug.Print "value: " & c.GetCustomPropertyValues("MyDimension", proptype, s1)
Debug.Print "param string: " & c.GetCustomProperty("ConfigLevelNL", proptype)
Debug.Print "value: " & c.GetCustomPropertyValues("CONFIGLEVELNL", proptype, s1)
Set c = d_2005.ConfigurationManager.GetConfigurationByName("config_h3")
Debug.Print "param string: " & c.GetCustomProperty("MyDimension", proptype)
Debug.Print "value: " & c.GetCustomPropertyValues("MyDimension", proptype, s1)
d_2005.SetCustomProperty "MyMass", "new non-linked value"
Debug.Print "param string: " & d_2005.GetCustomProperty("MyMass", proptype)
Debug.Print "value: " & d_2005.GetCustomPropertyValues("MyMass", proptype, s1)
c.SetCustomProperty "MyDimension", "new non-linked value, CS"
Debug.Print "param string: " & c.GetCustomProperty("MyDimension", proptype)
Debug.Print "value: " & c.GetCustomPropertyValues("MyDimension", proptype, s1)
d_2005.CloseDoc
Set d_2005 = Nothing
Set tapp = Nothing
Set classfac = Nothing
End Sub
RE: Custom Properties
Could you post your code so that I could post an example in the code style you are using?
SA
RE: Custom Properties
CODE
'==========================================================================
'added by Dan Landrum version 1.10 === begin ===
'==========================================================================
Dim iSelectCount As Integer
'==========================================================================
'added by Dan Landrum version 1.10 === end ===
'==========================================================================
'==========================================================================
'added by Dan Landrum version 1.11 === begin ===
'==========================================================================
Dim strRev As String
'''
Dim vDepend As Variant
Dim swModel1 As SldWorks.ModelDoc2
Dim CPM As CustomPropertyManager
Dim vPropNames As Variant
Dim nNbrProps As Long
Dim custPropType As Long
Dim valOut As String
Dim resolvedValOut As String
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swConfig As SldWorks.Configuration
'==========================================================================
'added by Dan Landrum version 1.11 === end ===
'==========================================================================
CheckBoxFilter.Enabled = False
TextBoxFilter.Enabled = False
TextBoxFilter.BackColor = vbButtonFace
CommandSelect.Enabled = False
CommandClear.Enabled = False
CommandSelectAll.Enabled = False
CommandClearAll.Enabled = False
LabelType.Enabled = False
ListBoxUserSelect.Locked = True
ComboFileType.Enabled = False
ComboFileType.BackColor = vbButtonFace
CommandExport.Enabled = False
CommandClose.Enabled = False
TimeStart = Now
CT = 0
For x = 0 To ListBoxUserSelect.ListCount - 1 ' each item
If ListBoxUserSelect.Selected(x) = True Then ' item select
CT = CT + 1
End If
Next ' Get next cfg
LabelStatus.Caption = "" ' Clear status
DocName = ModelDoc2.GetTitle ' Doc title
'==========================================================================
'added by Dan Landrum version 1.10 === begin ===
'==========================================================================
'if there is only one sheet then we don't want the sheet name
'tagged onto the end of the filename. So we check the quantity of items in
'in the list and that the user selected only one item for export.
'If only one item then we remove the sheet name from the docname.
blnNameset = False
If DocMode = 1 Then
iSelectCount = 0
For i = 0 To ListBoxUserSelect.ListCount - 1
If ListBoxUserSelect.Selected(i) = True Then
iSelectCount = iSelectCount + 1
End If
Next
i = 0
z = 0
'==========================================================================
'added by Dan Landrum version 1.11 === begin ===
'==========================================================================
Set swConfigMgr = ModelDoc2.ConfigurationManager
Set swConfig = swConfigMgr.ActiveConfiguration
Set CPM = swConfig.CustomPropertyManager
nNbrProps = CPM.Count
vPropNames = CPM.GetNames
'Set CPM = swModel1.Extension.CustomPropertyManager("")
For j = 0 To nNbrProps - 1
CPM.Get2 vPropNames(j), valOut, resolvedValOut
custPropType = CPM.GetType2(vPropNames(j))
Debug.Print " Name, type, and resolved value of custom property: " & vPropNames(j) & " - "; custPropType & " - " & resolvedValOut
Next j
'strRev = CPM.Get2("Rev", "strRev", "strRevResolved")
MsgBox ("Revision: " & strRev)
'==========================================================================
'added by Dan Landrum version 1.11 === end ===
'==========================================================================
If iSelectCount = 1 Then
For i = 1 To Len(DocName)
If Mid$(DocName, i, 1) = "-" Then z = i
Next i
DocName = Left$(DocName, (z - 2))
blnNameset = True
End If
End If
'==========================================================================
'added by Dan Landrum version 1.10 === end ===
'==========================================================================
DocPathname = ModelDoc2.GetPathName ' Doc path
For i = 1 To Len(DocPathname)
If Mid$(DocPathname, i, 1) = "\" Then z = i ' last slash
Next i
DocPath = Left$(DocPathname, z) ' remove doc nam
Count = 0
For x = 0 To ListBoxUserSelect.ListCount - 1 ' each item
If ListBoxUserSelect.Selected(x) = True Then ' item select
If DocMode = 0 Then ' -- SolidWorks Models --
ThisConfigName = ListBoxUserSelect.List(x, 0)
ExportName = DocName & "_" & ThisConfigName & _
ComboFileType.List(ComboFileType.ListIndex, 1) ' Export name
LabelStatus.Caption = " (" & Count + 1 & "/" & CT & ")" & _
" Exporting: " & ExportName ' Show name
FormDocExport.Repaint ' Repaint form
ModelDoc2.ShowConfiguration2 ThisConfigName ' Show config
ModelDoc2.GraphicsRedraw2 ' Redraw screen
ModelDoc2.SaveAs4 DocPath & ExportName, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent, Errors, Warnings ' Create file
Count = Count + 1
CheckBoxFilter.Enabled = True
EXType = "configurations"
Else ' -- SolidWorks Drawings --
FormDocExport.Repaint ' Repaint form
ThisSheetName = ListBoxUserSelect.List(x, 0)
ModelDoc2.ActivateSheet ThisSheetName ' Show sheet
'==========================================================================
'added by Dan Landrum version 1.10 === begin ===
'==========================================================================
'if we already set the name above then don't do it here
If blnNameset = False Then
DocName = ModelDoc2.GetTitle
End If
'REM out next line, part of original code.
'DocName = ModelDoc2.GetTitle
'==========================================================================
'added by Dan Landrum version 1.10 === end ===
'==========================================================================
ExportName = DocName & _
ComboFileType.List(ComboFileType.ListIndex, 1) ' Export name
LabelStatus.Caption = " Exporting: " & ExportName ' Show name
ModelDoc2.GraphicsRedraw2 ' Redraw screen
ModelDoc2.SaveAs4 DocPath & ExportName, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent, Errors, Warnings ' Create file
Count = Count + 1
EXType = "sheets"
End If
End If
Next ' Get next cfg
TimeEnd = Now
LabelStatus.Caption = " Done: " & Count & " " & EXType & " exported in " & _
Format(DateDiff("s", TimeStart, TimeEnd) / 60, "###.00") & " minutes."
CheckBoxFilter.Enabled = True
CheckBoxFilter_Click
CommandSelectAll.Enabled = True
CommandClearAll.Enabled = True
ListBoxUserSelect.Locked = False
LabelType.Enabled = True
ComboFileType.Enabled = True
ComboFileType.BackColor = vbWindowBackground
CommandExport.Enabled = True
CommandClose.Enabled = True
End Sub
RE: Custom Properties
The example you posted is for documentmanager. Generally you do not use this on open documents (at least that is how I would use it).
SA
RE: Custom Properties
RE: Custom Properties
Try this code.
CODE
Dim swApp As SldWorks.SldWorks
Dim swDwgModelDoc As SldWorks.ModelDoc2
Dim swModelDoc As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim vDwgDependent As Variant
Dim strRev As String
Dim strRevResolved As String
Sub main()
'get solidworks
Set swApp = Application.SldWorks
'get drawing modeldoc
Set swDwgModelDoc = swApp.ActiveDoc
'get path/name of model or assembly doc used to make drawing
vDwgDependent = swDwgModelDoc.GetDependencies2(False, False, False)
'assume only one model or assembly doc for drawing
'get dependent modeldoc
Set swModelDoc = swApp.GetOpenDocumentByName(vDwgDependent(1))
'get custom property manager for modeldoc
Set swCustPropMgr = swModelDoc.Extension.CustomPropertyManager("")
'get revison value
swCustPropMgr.Get2 "Rev", strRev, strRevResolved
'display revision value
MsgBox "Model Revision is " & strRev & " ", vbOKOnly, "Example Program"
End Sub
SA
RE: Custom Properties
To take your comment about document manager a bit further, can you use DocumentManager on an open document? I haven't messed with it much, but I'm not sure if you can get the current, open version of a file into DocumentManager. (I used that fact in a macro to compare the update stamp on disk with the update stamp in memory and determine how many changes were made since the last save.) If this is the case, then using DocumentManager would generally leave you wide open to missing changes made in the current SW session.
RE: Custom Properties
I do not understand. In your post you stated:
Does that mean you were able to have the model open and get the SwDMDocument object? From a test I just conducted, if I tried to access the file on the disk with the file open, the swDMDocument object came back equal to Nothing and the result parameter set to swDmDocumentOpenErrorFail which according to SW API help means
However, if I saved the file to a different disk and left it open and ran the code again so it tried to access the file (with the same filename) on the first disk, then an object was returned and the result parameter came back equal to swDmDocumentOpenErrorNone.
So the to answer your question is No when you try to access the same file on a disk that is also open in SolidWorks. And Yes if you try to access a file on a disk with the same name as a file open in SolidWorks that was opened from a different disk. In all cases disk includes a path which means the the files could reside on the same disk drive but in different folders.
SA
RE: Custom Properties
Thanks a million... works like a charm...
RE: Custom Properties
I noticed that the Rev level will not be retrieved if that property is stored as configuration specific property. There is only one configuration shown per sheet. Is there a way to get the Rev level then?
RE: Custom Properties
Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
http://sw.fcsuper.com/index.php
RE: Custom Properties
each configuration is the same basic part but has different hole patterns punched in it.
each individual configuration may get updated which requires the rev to roll forward for that configuration.
RE: Custom Properties
You should be able to do this. The SldWorks.ModelDoc2.CustomInfo2 method can take a string specifying which configuration to retrieve the property from. You can use SldWorks.view.ReferencedDocument and SldWorks.view.ReferencedConfiguration to get the model and configuration named used in a view. Hopefully that will be enough to point you in the right direction.
Eric
RE: Custom Properties
CODE
Dim swApp As SldWorks.SldWorks
Dim swDwgModelDoc As SldWorks.ModelDoc2
Dim swModelDoc As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swView As SldWorks.View
Dim swDraw As SldWorks.DrawingDoc
Dim vDwgDependent As Variant
Dim strRev As String
Dim strRevResolved As String
Dim strConfig As String
Sub main()
'get solidworks
Set swApp = Application.SldWorks
'get drawing modeldoc
Set swDwgModelDoc = swApp.ActiveDoc
'get path/name of model or assembly doc used to make drawing
vDwgDependent = swDwgModelDoc.GetDependencies2(False, False, False)
'assume only one model or assembly doc for drawing
'get dependent modeldoc
Set swModelDoc = swApp.GetOpenDocumentByName(vDwgDependent(1))
Set swDraw = swDwgModelDoc
Set swView = swDraw.GetFirstView
Do While Not swView Is Nothing
Debug.Print " View = " + swView.Name
Debug.Print " Model = " + swView.GetReferencedModelName
Debug.Print " Config = " + swView.ReferencedConfiguration
strConfig = swView.ReferencedConfiguration
Set swView = swView.GetNextView
Loop
'get custom property manager for modeldoc
Set swCustPropMgr = swModelDoc.Extension.CustomPropertyManager(strConfig)
'get revison value
swCustPropMgr.Get2 "Rev", strRev, strRevResolved
'display revision value
MsgBox "Model Revision is " & strRev & " " & "Config: " & strConfig, vbOKOnly, "Example Program"
End Sub