Macro to add Custom Properties

Hi, I would like to write a macro that would add the property "Espessura" to all the parts in the assembly i run it. The property "Espessura" would be the value of the Equation "Espessura" which solidworks provides by default

SolidworksApi macros
Answers 5
DG
DG 2017-11-28 Best Answer

Try the macro by here   and then modify as needed.

In case you need option to input the custom property name or it's value then check here

MN
MN 2017-11-29

What i need to do is add a custom property with the "Espessura@*Partname*.SLDPRT". How can i write that in the macro? Which things should i replace? Sorry, i'm a beginner in macro writing

DG
DG 2017-12-01

Make these two changes:

Change this line

to

wo_num = "Espessura"

And change this line

to

cpm.Add3 mValue, swCustomInfoText, Chr(34) & mValue & "@" & swModel.GetTitle & ".SLDPRT" & Chr(34), 1

comment imagecomment imagecomment imagecomment image
JH
JH 2020-08-24

im trying to edit this to add sw-mass as the value and weight as the property name.

and additional set the units of the part to kilograms.

on new part i have this set up, but need to add this to lots add this to lots legacy parts.

code i have so far but not quite working.

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim vComps As Variant
Dim swComp As SldWorks.Component2
Dim swAssy As SldWorks.AssemblyDoc
Dim i As Integer
Dim wo_num As String
Dim strfilename As String


Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
wo_num = "SW-Mass" & "@" & swModel.GetTitle


updateProperty swModel, wo_num

If swModel.GetType = swDocASSEMBLY Then
Set swAssy = swModel
vComps = swAssy.GetComponents(False)
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
If swComp.GetSuppression = swComponentFullyResolved Then
Set swModel = swComp.GetModelDoc2
updateProperty swModel, wo_num
Else
MsgBox "warn user lightweight or suppressed"
End If

Next i
End If
End Sub

Function updateProperty(swModel As SldWorks.ModelDoc2, mValue As String) As Boolean
Dim cpm As CustomPropertyManager
Set cpm = swModel.Extension.CustomPropertyManager("")
cpm.Add3 "Weight", swCustomInfoText, mValue, 1
End Function

DG
DG 2020-08-24

Try this line  wo_num = Chr(34) + "SW-Mass" + Chr(34)