ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
DLDave Laban08/12/2020

I'm having some trouble renaming configurations in the macro I'm working on that I was hoping to get some help on.

Macro objective:

One of our product lines will be being provided in multiple colour options.  This is being handled by multiple configurations of each part to account for the 5 options.  All our SW files are simply called the item number, e.g. 1234567.sldprt, the desired names for the 5 configurations are:

1234567

1234567-1

1234567-2

1234567-3

1234567-9

To account for black, green, blue, tan and unpainted options.  The workflow I'm trying to achieve is to model the unpainted configuration, then have a single button press to generate the four new configurations, rename the existing "Default" configuration to 1234567-9 and apply the colours to the configurations.

Where I've got to:

I've managed to create a macro that produces the extra configurations and applies my required colours:

' ******************************************************************************
' C:\Users\Dlaban\AppData\Local\Temp\swx16180\Macro1.swb - macro recorded on 12/07/20 by DLaban
' ******************************************************************************
Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swAppearance As SldWorks.RenderMaterial
Dim strName As String
Dim nDecalID As Long
Dim swConfig As SldWorks.Configuration
 

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Part2.SLDPRT", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True



' CREATE BLACK CONFIGURATION
boolstatus = Part.AddConfiguration2("BLACK", "", "", True, False, False, True, 257)
Set swConfig.Name = swModel.GetTitle
Part.ClearSelection2 True
' Appearance change subscript
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
swModel.ClearSelection2 True
' Apply the specified appearance to add to the model
strName = "C:\SW_DATA\Solidworks System Files\Appearances\064-064-064 flat black.p2m"
Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)
boolstatus = swAppearance.AddEntity(swModel)
boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
' Rebuild the model to see the newly applied appearance
Call swModel.Rebuild(swRebuildAll)



' CREATE GREEN CONFIGURATION
boolstatus = Part.AddConfiguration2("GREEN", "", "", True, False, False, True, 257)
Part.ClearSelection2 True
' Appearance change subscript
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
swModel.ClearSelection2 True
' Apply the specified appearance to add to the model
strName = "C:\SW_DATA\Solidworks System Files\Appearances\088-086-074 nato green bs381c 285.p2m"
Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)
boolstatus = swAppearance.AddEntity(swModel)
boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
' Rebuild the model to see the newly applied appearance
Call swModel.Rebuild(swRebuildAll)



' CREATE BLUE CONFIGURATION
boolstatus = Part.AddConfiguration2("BLUE", "", "", True, False, False, True, 257)
Part.ClearSelection2 True
' Appearance change subscript
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
swModel.ClearSelection2 True
' Apply the specified appearance to add to the model
strName = "C:\SW_DATA\Solidworks System Files\Appearances\050-067-090 insignia blue.p2m"
Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)
boolstatus = swAppearance.AddEntity(swModel)
boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
' Rebuild the model to see the newly applied appearance
Call swModel.Rebuild(swRebuildAll)



' CREATE TAN CONFIGURATION
boolstatus = Part.AddConfiguration2("TAN", "", "", True, False, False, True, 257)
Part.ClearSelection2 True
' Appearance change subscript
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
swModel.ClearSelection2 True
' Apply the specified appearance to add to the model
strName = "C:\SW_DATA\Solidworks System Files\Appearances\252-188-132 desert tan.p2m"
Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)
boolstatus = swAppearance.AddEntity(swModel)
boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)
' Rebuild the model to see the newly applied appearance
Call swModel.Rebuild(swRebuildAll)


'Rename Default configuration to "FILENAME-9"

End Sub

I assume there are many ways I can improve the efficiency of this code!

Where I'm stuck

I've tried referencing these two threads on how to rename configurations using the file name, however I can't get it to run in the context of my macro:  and    I've tried creating the configurations with a colour descriptive name and then renaming to use the file name, as well as an approach where the configurations are made with the correct name to begin with, but neither approach has stuck.  I'm guessing I've not created the callout at the top of the macro correctly but I'm starting to reach the limit with my understanding of programming.  Can any of your provide insight on what I might need to change to get the rename working?

Bonus round

If a Configuration-Specific Custom Property can be created for each config too, that takes the global "Description" custom property and appends it with " - BLACK", " - GREEN" etc. that would be cool too.

I suspect I'll be back at some point asking about how to set up the assembly easily too, but we'll leave that for now.

Thanks gang!