I have my drawing template set up to receive title block entries from my part file's custom properties. I'd like to go a step farther and extract my part number and revision from the file name without explicitly entering the info in custom properties / title blocks. I'm seeing similar queries in other threads but I don't know enough about macros to successfully patch one of my own together for my scenario. Here's some specifics of my file structures:
- part file & drawing file are always named identical to each other
- Part number can be any length of alphanumeric characters and often includes a "-" (dash)
- Revision may be one, two, or more characters, may include be a "-" (dash) or " " (space)
- file name format is "(part number) Rev (revision)
- example
- part number is 101010-201
- revision is C SUB B
- file name is 101010-201 Rev C SUB B.sldprt
Is it possible to get there from here?
SolidworksApi macros
This is possible. The macro you make can simply add or modify the revision and part number properties of your part in order for them to auto populate to your drawing. You can get the file name and then use standard vba to sort through the title and pick the information you want. It should be easy to determine what is what due to the spaces existing between each of the categories of information.
See if this works for you. It automatically converts the rev text to all caps, I'm not sure if you want that or not.
Got it - mission accomplished. And for those of you that find this thread useful, see Deepak's macro. It's enough of a start that I was able to successfully adapt to my needs.
https://forum.solidworks.com/message/434651?sr=search&searchId=4fed8b4a-6668-4e7c-ae74-d1b2ce4f61bd&searchIndex=0#434651
Mike glad you find that macro helpful. I would advise you to move this thread to API section since it is related to macros. Please use the move discussion option on right side.
Darn, I lost it. Some time late last week or so my macro disappeared, no idea why. So I thought I'd take the opportunity to practice what I've learned. I'm now failing to rewrite the macro that worked great before. I believe my error is somewhere between how or what I'm declaring / or how to use the swCustProp tools - I'm starting to drown in lack of understanding of the mechanics of what I'm doing and I'm just stuck.
I'm running SW Premium 2014 x64 SP 4.0 on Windows 7 Professional SP 1
Here's my code, who can help me?
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim vConfs As Variant
Dim i As Integer
Dim retval As Variant
Dim FileName As String
Dim FileNameSize As Integer
Dim PartNumber As String
Dim Revision As String
Dim Description As String
Dim Rev As Integer
Dim Material As String
Dim Thickness As String
Dim KFactor As String
'*********************************** ABSTRACT ***********************************
'This macro was started by extracting code samples from the SolidWorks Forum API section
' Writing credits to Deepak Gupta & Artem Taturevich
'
'This macro is intended to restructure a part's custom properties.
'First it stores the existing descrtiption value (if any).
'Then delete all existing custom properties
'Then define and populate new custom properties
'Part file nomenclature follows the following file naming convention:
' (part number) Rev (part revision).sldprt
'This macro does not yet work properly, it's buggy and doesn't do anything.
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
swCustProp = swModelDocExt.get_CustomPropertyManager("")
'Read current custom properties and extract Description if there is one, else prompt for new Description
retval = swCustPropMgr.Get4("Description", False, Description, "")
If Len(Description) < 2 Then
Description = InputBox("Enter Description", "What is this thing?")
End If
'Delete current custom properties, call subroutine
ClearCustPrps ("")
vConfs = swModel.GetConfigurationNames
For i = 0 To UBound(vConfs)
ClearCustPrps (vConfs(i))
Next
'Define and insert new custom properties
'Import File Name & partition
FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
FileNameSize = Len(FileName)
Rev = InStr(FileName, " Rev")
'Define Part Number
PartNumber = Left(FileName, Rev)
retval = Part.AddCustomInfo3("", "Part Number", 30, PartNumber)
'Define Revision
Revision = Right(FileName, FileNameSize - Rev - 4)
retval = Part.AddCustomInfo3("", "Revision", 30, Revision)
retval = Part.AddCustomInfo3("", "Description", 30, Description)
Material = Chr(34) & "SW-Material@Part1.SLDPRT" & Chr(34)
retval = Part.AddCustomInfo3("", "Material", 30, Material)
Thickness = Chr(34) & "Thickness@Part1.SLDPRT" & Chr(34)
retval = Part.AddCustomInfo3("", "Thickness", 30, Thickness)
KFactor = Chr(34) & "D2@Sheet-Metal2" & Chr(34)
retval = Part.AddCustomInfo3("", "K Factor", 30, KFactor)
End Sub
'Subroutine called to delete existing custom properties
Sub ClearCustPrps(conf As String)
Dim j As Integer
Set swCustPropMgr = swModel.Extension.CustomPropertyManager(conf)
If Not swCustPropMgr Is Nothing Then
swCustPropMgr.GetAll vPropNames, Empty, Empty
If Not IsEmpty(vPropNames) Then
For j = 0 To UBound(vPropNames)
swCustPropMgr.Delete vPropNames(j)
Next
End If
End If
End Sub
I'm having issues with my code, posted earlier today.
Mike what is REV in your codes and from where it gets the value.
I've fixed some issue in the macro (highlighted in bold)
<p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swCustPropMgr As SldWorks.CustomPropertyManager</p><p>Dim vConfs As Variant</p><p>Dim i As Integer</p><p>Dim retval As Variant</p><p>Dim FileName As String</p><p>Dim FileNameSize As Integer</p><p>Dim PartNumber As String</p><p>Dim Revision As String</p><p>Dim Description As String</p><p>Dim Rev As Integer</p><p>Dim Material As String</p><p>Dim Thickness As String</p><p>Dim KFactor As String</p><p><strong>Dim valout As String</strong></p><p>'*********************************** ABSTRACT ***********************************</p><p>'This macro was started by extracting code samples from the SolidWorks Forum API section</p><p>' Writing credits to Deepak Gupta & Artem Taturevich</p><p>'</p><p>'This macro is intended to restructure a part's custom properties.</p><p>'First it stores the existing descrtiption value (if any).</p><p>'Then delete all existing custom properties</p><p>'Then define and populate new custom properties</p><p>'Part file nomenclature follows the following file naming convention:</p><p>' (part number) Rev (part revision).sldprt</p><p>'This macro does not yet work properly, it's buggy and doesn't do anything.</p><p></p><p>Sub main()</p><p></p><p>Set swApp = Application.SldWorks</p><p>Set swModel = swApp.ActiveDoc</p><p><strong>Set swCustProp = swModel.Extension.CustomPropertyManager("")</strong></p><p></p><p>'Read current custom properties and extract Description if there is one, else prompt for new Description</p><p></p><p><strong>retval = swCustProp.Get4("Description", False, Description, valout)</strong></p><p>If Len(Description) < 2 Then</p><p> Description = InputBox("Enter Description", "What is this thing?")</p><p> End If</p><p> </p><p>'Delete current custom properties, call subroutine</p><p> ClearCustPrps ("")</p><p> vConfs = swModel.GetConfigurationNames</p><p> </p><p> For i = 0 To UBound(vConfs)</p><p> ClearCustPrps (vConfs(i))</p><p> </p><p> Next</p><p> </p><p>'Define and insert new custom properties</p><p></p><p> 'Import File Name & partition</p><p> FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)</p><p> FileNameSize = Len(FileName)</p><p> Rev = InStr(FileName, " Rev")</p><p> </p><p> 'Define Part Number</p><p> PartNumber = Left(FileName, Rev)</p><p> retval = Part.AddCustomInfo3("", "Part Number", 30, PartNumber)</p><p></p><p> 'Define Revision</p><p> Revision = Right(FileName, FileNameSize - Rev - 4)</p><p> retval = Part.AddCustomInfo3("", "Revision", 30, Revision)</p><p></p><p> retval = Part.AddCustomInfo3("", "Description", 30, Description)</p><p></p><p> Material = Chr(34) & "SW-Material@Part1.SLDPRT" & Chr(34)</p><p> retval = Part.AddCustomInfo3("", "Material", 30, Material)</p><p></p><p> Thickness = Chr(34) & "Thickness@Part1.SLDPRT" & Chr(34)</p><p> retval = Part.AddCustomInfo3("", "Thickness", 30, Thickness)</p><p></p><p> KFactor = Chr(34) & "D2@Sheet-Metal2" & Chr(34)</p><p> retval = Part.AddCustomInfo3("", "K Factor", 30, KFactor)</p><p></p><p>End Sub</p><p></p><p>'Subroutine called to delete existing custom properties</p><p></p><p>Sub ClearCustPrps(conf As String)</p><p> Dim j As Integer</p><p> </p><p> Set swCustPropMgr = swModel.Extension.CustomPropertyManager(conf)</p><p> </p><p> If Not swCustPropMgr Is Nothing Then</p><p> swCustPropMgr.GetAll vPropNames, Empty, Empty</p><p> </p><p> If Not IsEmpty(vPropNames) Then</p><p> For j = 0 To UBound(vPropNames)</p><p> swCustPropMgr.Delete vPropNames(j)</p><p> Next</p><p> End If</p><p> </p><p> End If</p><p> </p><p>End Sub</p>Deepak, thank you for taking your time to look at my code. I applied your edits but I am still getting a Run-time error '91': Object variable or With block variable not set on your line:
Set swCustProp = swModel.Extension.CustomPropertyManager("")
I'll continue to research this occurrence but I'm still happy to receive assistance.
The REV variable is a place counter. My file name structure is (part number) Rev (revision) where the part number can be any number of characters as determined by our customers. My goal here is to partition the file name into it's two pieces and use them to define the custom properties, so the REV variable locates where in the file name string the text " Rev" appears, space included. I suppose I could give the variable a more descriptive variable name.
Sorry for overlooking; replace
with
Well I still get error code 91 on line
Set swCustProp = swModel.Extension.CustomPropertyManager("")
Could it be that I need to install or apply a plug-in? I read through the API help and the online SolidWorks Customer Portal and I'm unclear on if I have the necessary software updates. Although I did have a successful code running previously, perhaps that is not my problem..?
Can you attach one sample to check and fix the macro.
Also change the Subroutine to delete existing custom properties with following
Certainly
Hi Mike, attached is the fixed macro.
Here are the fixes I've done:
You might be getting error on this line if you've not any open/active document (added codes in the macro to check if there is an open/active document).
Set swCustProp = swModel.Extension.CustomPropertyManager("")
Instead of Part, swModel should be used in these lines (fixed):
<p> retval = <strong>Part</strong>.AddCustomInfo3("", "Part Number", 30, PartNumber)</p><p> retval = <strong>Part</strong>.AddCustomInfo3("", "Revision", 30, Revision)</p><p> retval = <strong>Part</strong>.AddCustomInfo3("", "Description", 30, Description)</p><p> retval = <strong>Part</strong>.AddCustomInfo3("", "Material", 30, Material)</p><p> retval = <strong>Part</strong>.AddCustomInfo3("", "Thickness", 30, Thickness)</p><p> retval = <strong>Part</strong>.AddCustomInfo3("", "K Factor", 30, KFactor)</p>vPropNames in the subroutine was not defined (fixed)
This line was generating file name with extension. So revision was getting the file extension also (fixed).
FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
This line was generating an extra gap at the end. (fixed)
PartNumber = Left(FileName, Rev)
Please check and let me know as how macro works OR in case you need any further tweaking.
Incredible! Everything works. Thank you Deepak!