Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify existing custom prop

Status
Not open for further replies.

CycloneWade

Automotive
Apr 1, 2005
76
0
0
US
Hi All,

I have a macro that reads a custom property from a part and then runs a routine on it to acquire another value. This value then needs to get set as another custom property. I have what I think works the first time if the custom property does not exits. It fails to rewrite the custom property if they change depending on the input. What kind of code can I use to basically force a rewrite of an existing property?

Below is the line where I write the new custom property.

Part.AddCustomInfo3 "", "UnitPrice", 30, Stlprice

It is possible that Stlprice value could change thus the custom prop called UnitPrice needs to change each time the macro runs.

Thanks.
 
Replies continue below

Recommended for you

Hi, I'm not sure if this is the correct way of doing it but works for me.

Add the following line of code before the addprops line that you have.


SwModel.DeleteCustomInfo2 "", "unitprice"

This deletes it and your line of code then recreates it with your new value.

Hope this helps

 
Sorry... had to run before I finished...

I usually double up on AddCustomInfo3 and CustomInfo2. This way the property gets added if it is not there and then changed if it already existed.

Note that these API calls are now obsolete. SW took a simple thing and made it "better" by way of the "Custom Property Manager". At least it's more complicated and makes their programmers feel a sense of validation.
 
One could also check to see if the property exists, and then create it or set it as appropriate. The following code is for a property of type swCustomInfoText.

Code:
Sub setStringProperty(document As SldWorks.ModelDoc2, config As String, propertyName As String, value As String)
    Dim propertyManager As SldWorks.CustomPropertyManager
    Set propertyManager = document.Extension.CustomPropertyManager(config)
    
    If (propertyExists(propertyManager, propertyName)) Then
        propertyManager.Set propertyName, value
    Else
        propertyManager.Add2 propertyName, swCustomInfoText, value
    End If
End Sub

Function propertyExists(propertyManager As SldWorks.CustomPropertyManager, propertyName As String) As Boolean
    Dim found As Boolean
    found = False
    
    If (propertyManager.Count > 0) Then
        Dim propertyNames() As String
        propertyNames = propertyManager.GetNames
        
        Dim i As Integer
        For i = LBound(propertyNames) To UBound(propertyNames)
            If StrComp(propertyNames(i), propertyName) = 0 Then
                found = True
                Exit For
            End If
        Next i
    End If

    propertyExists = found
End Function

Eric
 
Did you get that from me (Esox Republic)? I usually use hard numbers instead of named constants. SW has named constants for different values, but I try not to get tied into using their reference module.

30 = text property type

If your StlPrice variable is not a string, you may want to force it to be a string using CSTR
Code:
Part.AddCustomInfo3 "", "UnitPrice", 30, CStr(Stlprice)

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
I'm trying to do something similar but am running into problems. First I add an equation called "Blank" then I want a property to be that variable, so the value would be in SW "Blank@Filename.SLDPRT" but the file name cannot be a generic like an asterisk or it won't automatically calculate the numerical value. So for part 54321, the value of the property should be "Blank@54321.SLDPRT" but I don't know what to use to call out the file name.
 
Status
Not open for further replies.
Back
Top