Macro multiplication

Hello
I would need your VBA skills.

I already have a macro that creates a custom property called WEIGHT and uses the solidworks value "SW-Mass".

I would like to see a custom SURFACE PIECE property created in this macro and its value to be the result of multiplying "SW-Mass" by a specific numeric RATIO.

But I'm a bit stuck with my lousy VBA level... Can someone help me please?

Hello

Just retrieve the value, convert it to a number and then multiply. Convert the result to text and put it in the custom property

1 Like

Hello
If the version is recent than 21, it is possible to use the "equation" type directly in the "manager property", otherwise declare it in "equation manager" and then create a link to a property, it will update automatically

1 Like

Thank you very much @Cyril.f and @Lynkoa15, but with my shabby level, I don't know how to go about it at all.
Do you have a small example of code so that I can adapt this to my macro? :sweat_smile:

Hello

Basically, it would look like this:

Dim swApp               As SldWorks.SldWorks
Dim swModel             As SldWorks.ModelDoc2
Dim swModelDocExt       As ModelDocExtension
Dim swCustProp          As CustomPropertyManager
Dim sMasse              As String
Dim sValout             As String
Dim sVal                As String

Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swCustProp = swModelDocExt.CustomPropertyManager("Défaut") 'Si la propriété est dans l'onglet personnaliser, laisser vide en remplaçant "Défaut" par ""
bRet = swCustProp.Get6("Masse", False, sValout, sMasse, False, False) 'Récupération de la propriété de masse, changer le nom de la propriété en fonction de votre modèle
sVal = CStr(CDec(sMasse) * 3) 'Conversion du texte en valeur décimale, multiplication x 3 et conversion en donné de type texte
bRet = swCustProp.Set2("Ra", sVal) 'Renseignement de la propriété Ra avec la valeur convertie en texte
End Sub


3 Likes

Thanks @Cyril.f , I'll start from that :+1:

@Cyril.f , I copied/pasted the lines and I modified what should be modified but the macro blocks at the sVal line:


I looked in the syntax but I don't see anything abnormal. I had a ratio with decimal, so I tested with a ratio of 2 but the macro still crashes.
Do you have an idea of the problem?
Thank you

Hello
What is the error message and which version of SW are you on?

2 Likes

Hello @Cyril.f
Here is the error message:
image
And I'm on Solidworks 2022 Sp05

I don't have this problem, possibility to send me a test file with the properties to check?

1 Like

Ok, here's the part I tested as well as the macro:
HEXA POLE TEST. SLDPRT (567.1 KB)
00-MASS-SURFACE.swp (36 KB)

No problem on my side whatever the value of the coefficient (you have to put a period as a comma).
Maybe check in the macro project references:
image
Otherwise, there may be a subtlety on the side of the locale of the station and the decimal separator.

2 Likes

@Cyril.f
Here are my Macro references:

And here are my regional parameters^tres:

Try replacing the comma with a period.

2 Likes

Hello @Cyril.f
The macro only works if I create the SURFACE PIECE property in F8.
If the field is not created, nothing happens.
I've been looking for how to create the field automatically, but I'm stuck.

Here's what I tried to tinker with without success:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Sun sMass As String
Dim sValout As String
Dim sVal As String
Dim bRet As String

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

Set swCustProp = swModelDocExt.CustomPropertyManager("PC") 'If the property is in the customize tab, leave blank by replacing 'PC' with ''

bRet = swCustProp.Get6', False, sValout, sMasse, False, False) 'Retrieving the mass property, changing the name of the property to suit your model

sVal = CStr(CDec(sMass) * 2) 'Convert text to decimal value, multiply x RATIO and convert to text data

'bRet = swCustProp.Set2("SURFACE PIECE", sVal) 'Populate the SURFACE PIECE property with the value converted to text

bRet = swModel.AddCustomInfo3("PC", "SURFACE PIECE", val)

End Sub

I tried with this command, but I think something is wrong:
bRet = swModel.AddCustomInfo3("PC", "SURFACE PIECE", val)

Do you have an idea to correct this please?

Rather than:
'bRet = swCustProp.Set2("SURFACE PIECE", sVal)

Put something like:
bRet = swCustPropMgr.Add3(" SURFACE PIECE ", swCustomInfoType_e.swCustomInfoDate, sVal, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)

Set is when the property is already existing, it seems to me.

See this link:
https://help.solidworks.com/2020/English/api/sldworksapi/Add_and_Get_Custom_Properties_Example_VB.htm?verRedirect=1

Or more complete:

2 Likes

That's right.
If the property doesn't exist you have to create it, if it does exist it also happens that SW can't write (it happened to me on old files) and in this case you have to delete and recreate the property.

2 Likes

I answer myself, it's fixed with Add3 which allows you to overwrite if the property already exists.
So basically:

swCustProp.Add3("SURFACE PIECE", 30, sVal, 1)
2 Likes

Hello, thank you for your answers.
The macro 00-MASS-SURFACE works well. However, do you know how to have only 3 decimal places for the area value?
00-MASS-SURFACE.swp (45.5 KB)

And I'd like to integrate this macro into the 00-MACRO HEXA macro below:
00-MACRO HEXA.swp (63.5 KB)

I tested, but I get this error when I launch the macro:
image

The debug gives me this:

Do you know how to correct this?

Thank you

Hello

So for the error, it's probably the swCustProp.Get6 variable that's empty, so either the property doesn't exist or there's a variable declaration issue.
To limit to 3 decimal places, you must use the "Format" function:

    sVal = CStr(Format(CDec(sMasse) * 0.0556, "0.000"))

2 Likes