Page 1 of 1
Macro for addding custom properties
Posted: Wed Aug 16, 2023 8:03 am
by TTevolve
I have a macro that I use to add custom properties. We just upgraded to SW 2023 and it does not work on files created from the 2023 templates (I made new part/assembly templates from the standard 2023 ones since there was an issue in my old templates relating to unfolding sheet metal parts). Is there some way to see what is wrong with the macro when it runs? It doesn't show any errors, just finishes but the custom properties are not created.
Re: Macro for addding custom properties
Posted: Wed Aug 16, 2023 8:28 am
by SPerman
You can set a breakpoint at the beginning of the file and step through it line by line to see where it is failing.
open the editor
Browse to your file and open.
Next to where it says "Sub main()" click in the little column to the left. A red dot should appear (This is a breakpoint.) The code will stop when it hits this line. (You can have as many setpoints as you like.)
In the debugger, hit the "run" button. You will see a yellow highlight on the line with the break point.
From here, you can hit F-8 and it will step through the code line by line, to try and figure out what the problem is.
Re: Macro for addding custom properties
Posted: Wed Aug 16, 2023 8:49 am
by JSculley
The AddCustomInfo function has been marked as obsolete for a LONG time. It's possible they have finally disabled it entirely. You should be using ICustomPropertyManager::Add3. You'll have to use ModelDocExtension to get the CustomerPropertyManager object. Something like this:
Code: Select all
' ******************************************************************************
' C:\Users\timt.MALISH\AppData\Local\Temp\swx10492\Macro1.swb - macro recorded on 05/11/17 by timt
' ******************************************************************************
Dim swApp As Object
Dim Part As ModelDoc2
'Dim cusPropMgr As SldWorks.CustomPropertyManager
'Dim boolstatus As Boolean
'Dim longstatus As Long, longwarnings As Long
Dim mExt As ModelDocExtension
Dim propMgr As CustomPropertyManager
'Dim ActiveConfig As Configuration
Sub main()
Set swApp = Application.SldWorks
'Set swdoc = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set mExt = Part.Extension
'Set swConfigMgr = swdoc.ConfigurationManager
'Set ActiveConfig = swConfigMgr.ActiveConfiguration
Set propMgr = mExt.CustomPropertyManager("")
'Dim Configuration As String
'Dim FieldName As String
'Dim FieldValue As String
Dim value As Boolean
'ActConfigName = ActiveConfig.Name
'Replace <PropertyName with what you want your custom property to be called.
'Replace <PropertyType> with the type of property, the drop down in the property manager window. Most often Text will work for anything, but you can set it to match data type if needed/wanted.
'Replace <PropertyValue> with what you want the custom property to be. I usually have it place a - in the field, as older versions of SW would not allow a blank property.
'Copy the value = ... line as many times as needed and update.
value = propMgr.Add3("Revision", swCustomInfoType_e.swCustomInfoText, "--", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Material", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("MaterialNum", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Description", swCustomInfoType_e.swCustomInfoText, " ", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Description2", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("AltDescription", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Vendor", swCustomInfoType_e.swCustomInfoText, "None", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("VendorNumber", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Date", swCustomInfoType_e.swCustomInfoText, "XX/XX/XX", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Finish", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Color", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Concentricity", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Parallelism", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Flatness", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Author", swCustomInfoType_e.swCustomInfoText, "TJT", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Number", swCustomInfoType_e.swCustomInfoText, "--------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
'Set ActiveConfig = Nothing
'Set swConfigMgr = Nothing
'Set swdoc = Nothing
'Set swApp = Nothing
End Sub
Re: Macro for addding custom properties
Posted: Wed Aug 16, 2023 10:28 am
by TTevolve
JSculley wrote: ↑Wed Aug 16, 2023 8:49 am
The AddCustomInfo function has been marked as obsolete for a LONG time. It's possible they have finally disabled it entirely. You should be using ICustomPropertyManager::Add3. You'll have to use ModelDocExtension to get the CustomerPropertyManager object. Something like this:
Code: Select all
' ******************************************************************************
' C:\Users\timt.MALISH\AppData\Local\Temp\swx10492\Macro1.swb - macro recorded on 05/11/17 by timt
' ******************************************************************************
Dim swApp As Object
Dim Part As ModelDoc2
'Dim cusPropMgr As SldWorks.CustomPropertyManager
'Dim boolstatus As Boolean
'Dim longstatus As Long, longwarnings As Long
Dim mExt As ModelDocExtension
Dim propMgr As CustomPropertyManager
'Dim ActiveConfig As Configuration
Sub main()
Set swApp = Application.SldWorks
'Set swdoc = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set mExt = Part.Extension
'Set swConfigMgr = swdoc.ConfigurationManager
'Set ActiveConfig = swConfigMgr.ActiveConfiguration
Set propMgr = mExt.CustomPropertyManager("")
'Dim Configuration As String
'Dim FieldName As String
'Dim FieldValue As String
Dim value As Boolean
'ActConfigName = ActiveConfig.Name
'Replace <PropertyName with what you want your custom property to be called.
'Replace <PropertyType> with the type of property, the drop down in the property manager window. Most often Text will work for anything, but you can set it to match data type if needed/wanted.
'Replace <PropertyValue> with what you want the custom property to be. I usually have it place a - in the field, as older versions of SW would not allow a blank property.
'Copy the value = ... line as many times as needed and update.
value = propMgr.Add3("Revision", swCustomInfoType_e.swCustomInfoText, "--", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Material", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("MaterialNum", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Description", swCustomInfoType_e.swCustomInfoText, " ", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Description2", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("AltDescription", swCustomInfoType_e.swCustomInfoText, "------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Vendor", swCustomInfoType_e.swCustomInfoText, "None", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("VendorNumber", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Date", swCustomInfoType_e.swCustomInfoText, "XX/XX/XX", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Finish", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Color", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Concentricity", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Parallelism", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Flatness", swCustomInfoType_e.swCustomInfoText, "N/A", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Author", swCustomInfoType_e.swCustomInfoText, "TJT", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
value = propMgr.Add3("Number", swCustomInfoType_e.swCustomInfoText, "--------", swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew)
'Set ActiveConfig = Nothing
'Set swConfigMgr = Nothing
'Set swdoc = Nothing
'Set swApp = Nothing
End Sub
Thanks, I will give that a try. I figured something had changed. I have used that Macro for years now and my templates were old as well, so it always worked until I just tried it on the new templates I created yesterday. I think the templates I was using were from SW2009!
Re: Macro for addding custom properties
Posted: Thu Aug 17, 2023 1:35 pm
by Uncle_Hairball
Thanks TTevolve, JScully, and SPerman. That macro is going to be very useful to me.
Re: Macro for addding custom properties
Posted: Thu Aug 17, 2023 4:30 pm
by MattW
I don't know what you want the macro to do, but our templates have no custom properties and we use the custom properties tab and the property tab builder to control the addition of custom properties. These are customizable and will probably do what you want.
Re: Macro for addding custom properties
Posted: Thu Aug 17, 2023 4:37 pm
by MattW
Re: Macro for addding custom properties
Posted: Fri Aug 18, 2023 11:24 am
by TTevolve
Pretty much the same thing without the buttons and/or drop down boxes.
The macro comes in handy on imported parts or old parts that were created without the custom properties. Run the macro and you have all your custom properties in the part or assembly.
Re: Macro for addding custom properties
Posted: Fri Aug 18, 2023 12:17 pm
by MattW
Opening the custom properties tab does exactly that.