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:
' ******************************************************************************<br>' C:\\Users\\Dlaban\\AppData\\Local\\Temp\\swx16180\\Macro1.swb - macro recorded on 12/07/20 by DLaban<br>' ******************************************************************************<br>Dim swApp As Object<br><br>Dim Part As Object<br>Dim boolstatus As Boolean<br>Dim longstatus As Long, longwarnings As Long<br><br>Dim swModel As SldWorks.ModelDoc2<br>Dim swModelDocExt As SldWorks.ModelDocExtension<br>Dim swAppearance As SldWorks.RenderMaterial<br>Dim strName As String<br>Dim nDecalID As Long<br>Dim swConfig As SldWorks.Configuration<br> <br><br>Sub main()<br><br>Set swApp = Application.SldWorks<br><br>Set Part = swApp.ActiveDoc<br>boolstatus = Part.Extension.SelectByID2("Part2.SLDPRT", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)<br>Part.ClearSelection2 True<br><br><br><br>' CREATE BLACK CONFIGURATION<br>boolstatus = Part.AddConfiguration2("BLACK", "", "", True, False, False, True, 257)<br>Set swConfig.Name = swModel.GetTitle<br>Part.ClearSelection2 True<br>' Appearance change subscript<br>Set swApp = Application.SldWorks<br>Set swModel = swApp.ActiveDoc<br>Set swModelDocExt = swModel.Extension<br>swModel.ClearSelection2 True<br>' Apply the specified appearance to add to the model<br>strName = "C:\\SW_DATA\\Solidworks System Files\\Appearances\\064-064-064 flat black.p2m"<br>Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)<br>boolstatus = swAppearance.AddEntity(swModel)<br>boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)<br>' Rebuild the model to see the newly applied appearance<br>Call swModel.Rebuild(swRebuildAll)<br><br><br><br>' CREATE GREEN CONFIGURATION<br>boolstatus = Part.AddConfiguration2("GREEN", "", "", True, False, False, True, 257)<br>Part.ClearSelection2 True<br>' Appearance change subscript<br>Set swApp = Application.SldWorks<br>Set swModel = swApp.ActiveDoc<br>Set swModelDocExt = swModel.Extension<br>swModel.ClearSelection2 True<br>' Apply the specified appearance to add to the model<br>strName = "C:\\SW_DATA\\Solidworks System Files\\Appearances\\088-086-074 nato green bs381c 285.p2m"<br>Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)<br>boolstatus = swAppearance.AddEntity(swModel)<br>boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)<br>' Rebuild the model to see the newly applied appearance<br>Call swModel.Rebuild(swRebuildAll)<br><br><br><br>' CREATE BLUE CONFIGURATION<br>boolstatus = Part.AddConfiguration2("BLUE", "", "", True, False, False, True, 257)<br>Part.ClearSelection2 True<br>' Appearance change subscript<br>Set swApp = Application.SldWorks<br>Set swModel = swApp.ActiveDoc<br>Set swModelDocExt = swModel.Extension<br>swModel.ClearSelection2 True<br>' Apply the specified appearance to add to the model<br>strName = "C:\\SW_DATA\\Solidworks System Files\\Appearances\\050-067-090 insignia blue.p2m"<br>Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)<br>boolstatus = swAppearance.AddEntity(swModel)<br>boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)<br>' Rebuild the model to see the newly applied appearance<br>Call swModel.Rebuild(swRebuildAll)<br><br><br><br>' CREATE TAN CONFIGURATION<br>boolstatus = Part.AddConfiguration2("TAN", "", "", True, False, False, True, 257)<br>Part.ClearSelection2 True<br>' Appearance change subscript<br>Set swApp = Application.SldWorks<br>Set swModel = swApp.ActiveDoc<br>Set swModelDocExt = swModel.Extension<br>swModel.ClearSelection2 True<br>' Apply the specified appearance to add to the model<br>strName = "C:\\SW_DATA\\Solidworks System Files\\Appearances\\252-188-132 desert tan.p2m"<br>Set swAppearance = swModelDocExt.CreateRenderMaterial(strName)<br>boolstatus = swAppearance.AddEntity(swModel)<br>boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)<br>' Rebuild the model to see the newly applied appearance<br>Call swModel.Rebuild(swRebuildAll)<br><br><br>'Rename Default configuration to "FILENAME-9"<br><br>End SubI 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!
SolidworksApi/macros