×
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

SWMD Custom Property API

SWMD Custom Property API

SWMD Custom Property API

(OP)
This is an example from the Doc. Mgr API help file.  We have several files with the custom property of by_date filled out as a text type.  I need to change this to a date type.  This code works but I am having an issue.  If I run the code and check the results by RMB clicking on the file in Win Explorer and selecting properties then custom.  I see the new value.  If I try to change that back to a text type I get an error "changes could not be saved.  The file is marked read-only or may be in use by another app".  Then it blows all of the properties away.  They do remain in the part file though.
I can't imagine why our users would change the Property this way but I'm guessing someone will.

Any advice would be appreciated.

CODE

'Option Explicit
Sub main()
On Error GoTo ErrorHandler
 ' Trap all errors that would cause the program to halt.
    Dim Classfac As SwDMClassFactory
    Dim swDocMgr As SwDMApplication
    Dim swDoc As SwDMDocument
    Dim nretval As Long
    ' Instantiate a SolidWorks Document Manager session.
    Set Classfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
    Set swDocMgr = Classfac.GetApplication("DM Key") 'Specify your license key
    Set swDoc = swDocMgr.GetDocument("Path to part file", swDmDocumentPart, False, nretval)  ' Open the file as read-write.
    Debug.Print nretval ' Examine this variable to ensure there were no errors opening the file.
    Dim vnames As Variant
    vnames = swDoc.GetCustomPropertyNames
    ' Examine vnames in the debugger.
    myval = swDoc.GetCustomProperty("by_date", swDmCustomInfoText)
    swDoc.DeleteCustomProperty ("by_date")
    Debug.Print swDoc.AddCustomProperty("by_date", swDmCustomInfoDate, myval)  ' Should print out True, unless the property already exists.
    Debug.Print swDoc.GetCustomProperty("by_date", swDmCustomInfoDate) ' Should print out 3/2/1943.
    swDoc.Save ' Save the changes made to the custom properties.
    swDoc.CloseDoc  ' Close the document before exiting.
ErrorHandler:
    Debug.Print Err.Description
    If Not swDoc Is Nothing Then
        swDoc.CloseDoc
    End If
    swDoc.CloseDoc
    Set swDoc = Nothing
    Set swDocMgr = Nothing
    Set Classfac = Nothing
End Sub

RE: SWMD Custom Property API

Something I've noticed with solidworks properties is once they're created they're very finicky about wanting to be changed.

IE you can't change the case sensitivity of a property without deleting it, and re-entering it how you want it.  This may be an extension of that.

One thing you may try is to dump the property name & value into a VB variable, delete the property and re-enter it with the new type using the saved variables.

James Spisich
Design Engineer, CSWP

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