×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Modify existing custom prop

Modify existing custom prop

Modify existing custom prop

(OP)
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.

RE: Modify existing custom prop

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

  

RE: Modify existing custom prop

correction to previous post, in your case use

part.DeleteCustomInfo2 "", "unitprice"

(not SwModel)

RE: Modify existing custom prop

AddCustomInfo3 will not overwrite an existing property.

RE: Modify existing custom prop

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.

RE: Modify existing custom prop

(OP)
I tried Custom Property Manager but didn't know how to make it work, so I punted and used code I stole from the web.

Thanks.

RE: Modify existing custom prop

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

RE: Modify existing custom prop

I don't like Custom Property Manager, either.  One more object to manage.  The old API's will still work.  I still use them.

RE: Modify existing custom prop

(OP)
What does the 30 do in the following statement.  Yes, I'm a hacker.


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

Thanks.

RE: Modify existing custom prop

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)

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

RE: Modify existing custom prop

(OP)
Perhaps, I've looked at a lot of macros on the web the past couple days.  If I did, thank you.

Wade

RE: Modify existing custom prop

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources