Need HELP for Macro to add new custom property with ConfigName!

Library for macros
VictorPleg
Posts: 3
Joined: Tue Feb 21, 2023 6:15 am
Answers: 0
x 1

Need HELP for Macro to add new custom property with ConfigName!

Unread post by VictorPleg »

Hello!
I am new to the world of macros, as well as to the programming language.
I need a macro to add a new custom property (which will always be called the same) and put in its 'Value/Text Expression' the evaluated value that binds to the configuration name property ($PRP:"SW-Configuration Name").

This is the value I need to write to the custom property:
image.png
And this is how the result would look after executing the desired macro:
image.png

I don't know how I can integrate this into the code...
It's possible?

I found a macro to be able to add a new custom property. But this only allows you to add the property name and value manually.
This is the macro:

Code: Select all

 Dim swApp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2

Dim swCustPropMgr As SldWorks.CustomPropertyManager

Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Code As String

 Sub setcode()

Code = "SIF0030010503M5"

End Sub

Sub main()

setcode

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

swCustPropMgr.Add3 "DENOM. SIF", swCustomInfoText, Code, swCustomPropertyReplaceValue


End Sub
The purpose of the macro is:
- Add (in the active document) a new property called 'DENOM. SIF', whose value or text expression is the result of the formula $PRP:"SW-Configuration Name" (In the macro that I show I have added the value manually --> Code = "SIF0030010503M5")
- Save the Part.
- Close the document.


I would greatly appreciate a little help.

Thanks in advance.
User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2225
x 1876
Contact:

Re: Need HELP for Macro to add new custom property with ConfigName!

Unread post by SPerman »

-
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
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 268
x 445

Re: Need HELP for Macro to add new custom property with ConfigName!

Unread post by AlexB »

I'm not sure why you need it to work this way, but is this what you're looking for?

- Add property to link to configuration name
- Read property to get property name that it's evaluated to
- Rewrite property value to hard-coded configuration name

Code: Select all

Option Explicit

Sub main()
    Const NAME_OF_CFG As String = ""
    Const NAME_OF_PROPERTY_TO_ADD As String = "NEWPROP"
    Const VALUE_OF_PROPERTY_TO_ADD As String = "$PRP:""SW-Configuration Name"""


    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swExt As ModelDocExtension
    Dim swCustPropMgr As CustomPropertyManager
    
    Dim swResolvedValue As String
    Dim swValue As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swExt = swModel.Extension
    Set swCustPropMgr = swExt.CustomPropertyManager(NAME_OF_CFG)
    
    swCustPropMgr.Add3 NAME_OF_PROPERTY_TO_ADD, swCustomInfoType_e.swCustomInfoText, VALUE_OF_PROPERTY_TO_ADD, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd
    swCustPropMgr.Get3 NAME_OF_PROPERTY_TO_ADD, False, swValue, swResolvedValue
    swCustPropMgr.Add3 NAME_OF_PROPERTY_TO_ADD, swCustomInfoType_e.swCustomInfoText, swResolvedValue, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
    
End Sub
VictorPleg
Posts: 3
Joined: Tue Feb 21, 2023 6:15 am
Answers: 0
x 1

Re: Need HELP for Macro to add new custom property with ConfigName!

Unread post by VictorPleg »

Thank you very much Alex B!
It's very similar to what I need!
The only thing different is the property name has to always be 'DENOM. SIF' instead of 'NEWPROP'.
I have tried to replace it in the code, but then it creates 2 properties, one called 'NEWPROP' and the other called 'DENOM. SIF'
Do you know how I could modify the code so that this doesn't happen?
Excuse my ignorance, but the reality is that I don't know how to read this programming language very well...

This is how it looks if I do what I mentioned instead:
image.png
thanks a lot!
It's almost ready!
VictorPleg
Posts: 3
Joined: Tue Feb 21, 2023 6:15 am
Answers: 0
x 1

Re: Need HELP for Macro to add new custom property with ConfigName!

Unread post by VictorPleg »

Actually, I notice that every time I modify that parameter again ('NEWPROP') and assign it another value, like for example 'Victor'
And if I then manually delete the properties written by the macro and run it again, then it creates 3 new properties. 'NEWPROP', 'DENOM. SIF' AND 'VICTOR'.

As you can see in the screenshot:
image.png
That's why what I need is to always write the named property 'DENOM. SIF' and if it already exists, rewrite it.
And that in such a case, the name of the property can be configured in the same macro (which in this case must always be 'DENOM. SIF')

I don't know if I'm explaining myself well...

Thanks again.
User avatar
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 268
x 445

Re: Need HELP for Macro to add new custom property with ConfigName!

Unread post by AlexB »

This should work now. I shouldn't have used "Const" for the settings it seems.

Code: Select all

Option Explicit

Sub main()
    Dim NAME_OF_CFG As String
    Dim NAME_OF_PROPERTY_TO_ADD As String
    Dim VALUE_OF_PROPERTY_TO_ADD As String
    
    NAME_OF_CFG = ""
    NAME_OF_PROPERTY_TO_ADD = "NEWPROP3"
    VALUE_OF_PROPERTY_TO_ADD = "$PRP:""SW-Configuration Name"""


    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swExt As ModelDocExtension
    Dim swCustPropMgr As CustomPropertyManager
    
    Dim swResolvedValue As String
    Dim swValue As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swExt = swModel.Extension
    Set swCustPropMgr = swExt.CustomPropertyManager(NAME_OF_CFG)
    
    swCustPropMgr.Add3 NAME_OF_PROPERTY_TO_ADD, swCustomInfoType_e.swCustomInfoText, VALUE_OF_PROPERTY_TO_ADD, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd
    swCustPropMgr.Get3 NAME_OF_PROPERTY_TO_ADD, False, swValue, swResolvedValue
    swCustPropMgr.Add3 NAME_OF_PROPERTY_TO_ADD, swCustomInfoType_e.swCustomInfoText, swResolvedValue, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
    
End Sub
Post Reply