Automatic Part Code Using Custom Property Tab Builder

call
Samarsinh Khanvilkar, Application Engineer Jan 8th, 2024

The problem statement emphasizes the requirement for user-created parts to be saved using organization-generated predefined naming conventions. These naming conventions result from a combination of inputs, originating from either user-defined values or pre-established defaults. Now, let’s delve into how this can be achieved using SOLIDWORKS.

Setting up the predefined structure of code

To embark on our journey, the initial phase involves establishing a standardized structure for the code or formula that will underpin the entire process. The specifics of this structure may vary depending on the organization and the intricacy of the code. Within this procedural framework, we’ll employ a logic that comprehensively encompasses various scenarios, leaving no space unexplored. This strategic approach ensures thorough coverage throughout the process.

The part number schema will look something like this: XXX.XX.XX.X.XX

  • XXX: First 3 letters are for Assembly type (Alphabets)
  • XX: 4th and 5th letters are for Sub Type number (Numbers)
  • XX: 6th and 7th letters are for Department (Alphabets)
  • X: 8th number is for Capacity (Number)
  • XX: 9th and 10th are for Revision (Number)

The first 3 sections are dependent on each other. Like every assembly type has already assigned to a particular department and the depending on subtypes are also assigned.

Creating the Excel sheet for the dependable

As per the data gathered from the organizational structure, we can proceed with the making of the excel sheet. We have the data in the following format.


The data can be huge but let’s use this for the first 3 parts. Save the data in the excel file. And head towards the Custom Property Tab Builder.

Creating the tabs in property tab builder

In the Custom Property Tab Builder, Let’s build the tabs and the associated values.
First open the custom property tab builder.

Assigning the Excel Data in the list Format:

Make your SolidWorks design work easier by adding a “List Group” above the “Groupbox.” This helps make things look better and keeps your project organized. First, change the properties to fit your project. Then, add your Excel file data and make sure to start from the second row, with the headers in the first row (like A2:D9). Since you have four columns of data, drag, and drop new lists into the “List Group” to match. Set up the columns in order, from the first to the last. Keep everything consistent across all configurations, so your design works well. (Click on this Icon to apply it to all configurations)

Adding remaining file properties:

The capacity can be added by simply inserting the list and filling it up with the needed capacity values, such as 1, 2, 3, or 4. Next, include a Textbox in the same group. This allows users to manually input revisions, providing flexibility and customization options for your project. With this straightforward approach, you enhance the capacity of your project and empower users to easily manage, and update revisions as needed.

After making all the changes your tab will look like the following.

Now save it to the custom property template location and open a new part.

Assigning Custom Properties Through Tab Builder

After opening of the part, Now we can open the custom properties in the task pane by selecting this icon . Now press F5 in the window and select the template we just saved.

You will see a form we just created which can be filled as per the requirements of the part file. After clicking apply, the custom properties will be applied to the file which can be viewed in the file properties. If we do all the above steps correctly. Our file properties should look something like this:

Creating the code in custom property.

Add the new property by typing “Formula” as name in the latest row. And use the expression ($PRP:”property name”) to link the custom property to the formula.

Now use the same expression again and again to link all the properties in correct order. So that the evaluated value of the property will look like the code we want. If we take look at the formula which evaluates the code we are generating will look something like this: ($PRP:”Abbrevation”$PRP:”SubType”$PRP:”Department”$PRP:”Capacity”$PRP:”Revisions”).

And the Code will be evaluated to: SHT01LR101
This is how we can generate the code we required.

Assigning the code to the file name

After successfully creating the code, add the another property which will only reflect the same code by using the expression $PRP:”Formula” and rename the property to Part Number.

Save the current opened part file to rename the file. Then run the following macro file which will rename the part file with the custom property “Part Number”

Option Explicit

Sub RenamePartBasedOnCustomProperty()
‘ Declare variables
Dim swApp As Object
Dim swModel As Object
Dim swCustPropMgr As Object
Dim swNewModel As Object
Dim propName As String
Dim propValue As String
Dim propEvaluatedValue As String
Dim newPartName As String

‘ Initialize SolidWorks
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

‘ Check if a document is open
If swModel Is Nothing Then
MsgBox “Please open a SolidWorks part document first.”, vbExclamation
Exit Sub
End If

‘ Specify the custom property to use for renaming
propName = “Part Number”

‘ Get the custom property manager
Set swCustPropMgr = swModel.Extension.CustomPropertyManager(“”)

‘ Check if the custom property exists
If Not swCustPropMgr Is Nothing Then
‘ Get the custom property value
propValue = swCustPropMgr.Get(propName)

If Not IsEmpty(propValue) Then
‘ Validate and clean the custom property value for use in a file name
propValue = CleanFileName(propValue)

‘ Get the evaluated value of the custom property
propEvaluatedValue = swModel.GetCustomInfoValue(“”, propName)

‘ Get the full path of the part using a workaround
Dim modelPath As String
modelPath = swModel.GetPathName
If modelPath = “” Then
‘ If the part is not saved, prompt user to save
MsgBox “Please save the part before renaming.”, vbExclamation
Exit Sub
End If

‘ Generate the new part name using the evaluated custom property value
newPartName = Left(modelPath, InStrRev(modelPath, “\”)) & propEvaluatedValue & “.SLDPRT” ‘ You can adjust the file extension if needed

‘ Save the part with the new name using SaveAs3
swModel.SaveAs3 newPartName, 0, 0

‘ Close the original part without saving changes
swApp.CloseDoc modelPath

‘ Open the new part to refresh the SolidWorks model
Set swNewModel = swApp.OpenDoc(newPartName, swDocPART)

‘ Set the custom property in the new part
Dim swNewCustPropMgr As Object
Set swNewCustPropMgr = swNewModel.Extension.CustomPropertyManager(“”)
swNewCustPropMgr.Set propName, propEvaluatedValue

‘ Save the new part to retain the custom property
swNewModel.Save

MsgBox “Part successfully renamed to: ” & propEvaluatedValue, vbInformation
Else
MsgBox “Custom property ‘Description’ is empty.”, vbExclamation
End If

Else
MsgBox “Custom property manager not found.”, vbExclamation
End If
End Sub

Function CleanFileName(value As String) As String
‘ Replace invalid characters in a file name with underscores
Dim invalidChars As String
Dim i As Integer
invalidChars = “\/:*?””<>|”
For i = 1 To Len(invalidChars)
value = Replace(value, Mid(invalidChars, i, 1), “_”)
Next i
CleanFileName = value
End Function

And with this your File will be renamed with the part code successfully.

Tip: You can save the part as template to save the formula in the template. Just make sure you delete the values in the custom properties and then save it as a template.

We Urge You To Call Us For Any Doubts & Clarifications That You May Have. We Are Eager to Talk To You

Call Us: +91 7406663589

Karnataka & Goa

Chennai

Mumbai

Pune

Gujarat

Hyderabad

For SOLIDWORKS Product Enquiry Contact:

AUTHOR: Samarsinh Khanvilkar, Application Engineer

How useful was this?

Click on a star below to rate us out of 5 stars

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *

NEWSLETTER SIGN-UP