Macro for addding custom properties

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
TTevolve
Posts: 252
Joined: Wed Jan 05, 2022 10:15 am
Answers: 3
x 84
x 159

Macro for addding custom properties

Unread post 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.
AddProperties.swp
(41.5 KiB) Downloaded 104 times
User avatar
SPerman
Posts: 2035
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2207
x 1860
Contact:

Re: Macro for addding custom properties

Unread post 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
image.png
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.)
image.png
In the debugger, hit the "run" button. You will see a yellow highlight on the line with the break point.
image.png
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.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
JSculley
Posts: 638
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 868

Re: Macro for addding custom properties

Unread post 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
TTevolve
Posts: 252
Joined: Wed Jan 05, 2022 10:15 am
Answers: 3
x 84
x 159

Re: Macro for addding custom properties

Unread post 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!
Uncle_Hairball
Posts: 194
Joined: Fri Mar 19, 2021 12:21 pm
Answers: 2
x 37
x 95

Re: Macro for addding custom properties

Unread post by Uncle_Hairball »

Thanks TTevolve, JScully, and SPerman. That macro is going to be very useful to me.
MattW
Posts: 58
Joined: Wed May 12, 2021 12:39 pm
Answers: 2
x 5
x 31

Re: Macro for addding custom properties

Unread post 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.
MattW
Posts: 58
Joined: Wed May 12, 2021 12:39 pm
Answers: 2
x 5
x 31

Re: Macro for addding custom properties

Unread post by MattW »

image.png
TTevolve
Posts: 252
Joined: Wed Jan 05, 2022 10:15 am
Answers: 3
x 84
x 159

Re: Macro for addding custom properties

Unread post 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.
MattW
Posts: 58
Joined: Wed May 12, 2021 12:39 pm
Answers: 2
x 5
x 31

Re: Macro for addding custom properties

Unread post by MattW »

Opening the custom properties tab does exactly that.
Post Reply