Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

SWMD Custom Property API

Status
Not open for further replies.

Bwaggoner

Mechanical
Apr 9, 2007
23
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
 
Replies continue below

Recommended for you

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor