I'm using the Document Manager API alongside the regular Solidworks API to access the same model document. The reason for this dubious practice is that I need access to the InvisibleCustomProperty methods that the DM API provides and features of the model document. I'm not aware of a way of setting InvisibleCustomProperties through the normal Solidworks API.
My problem is that, when I set the property, save the document, and then re-open it. The invisible custom property doesn't exist anymore. This behavior isn't consistent - sometimes it does manage to save. If I rebuild the open Solidworks Model and then save it, the InvisibleCustomProperty is gone.
To combat this problem, every time I attempt to read from, write to, or create the InvisibleCustomProperty, I wrap the execution in this pair of functions:
Private Sub getDMDocWriteAccess()
Dim open_error As SwDmDocumentOpenError
Dim bRet As Boolean
'make the model document read only before opening
If pModel.IsOpenedReadOnly = False Then
pModel.Save
bRet = pModel.SetReadOnlyState(True): Debug.Assert bRet
Debug.Print "Set the model document to read only"
End If
Debug.Assert Not (pModel.IsOpenedReadOnly = False)
Set pDMDoc = pDMApp.GetDocument(pPathName, SwDmDocumentType.swDmDocumentPart, False, open_error)
If Not open_error = swDmDocumentOpenErrorNone Then
Set pDMDoc = pDMApp.GetDocument(pPathName, SwDmDocumentType.swDmDocumentPart, False, open_error): Debug.Assert open_error = swDmDocumentOpenErrorNone
End If
End Sub
Public Sub saveAndCloseDmDoc()
Dim save_err As SwDmDocumentSaveError
Dim reload_err As swComponentReloadError_e
Dim swDMDocument As SwDocumentMgr.swDMDocument
Dim bRet As Boolean
Set swDMDocument = pDMDoc
save_err = swDMDocument.Save: Debug.Assert save_err = swDmDocumentSaveErrorNone
swDMDocument.CloseDoc
bRet = pModel.SetReadOnlyState(False): Debug.Assert bRet
reload_err = pModel.ReloadOrReplace(False, pModel.GetPathName, True): Debug.Assert ((reload_err = swDocumentNotChanged) Or (reload_err = swReloadOkay))
End Sub
For instance, setting the custom property is implemented in this way:
Public Sub setValue(value As String)
getDMDocWriteAccess
createCustomProperty
pDMDoc.SetInvisibleCustomProperty pCustomPropertyName, value
If Not (StrComp(pDMDoc.GetInvisibleCustomProperty(pCustomPropertyName, swDmCustomInfoText), value) = 0) Then
pDMDoc.SetInvisibleCustomProperty pCustomPropertyName, value
End If
Debug.Assert (StrComp(pDMDoc.GetInvisibleCustomProperty(pCustomPropertyName, swDmCustomInfoText), value) = 0)
Debug.Print "SET VALUE:" & pDMDoc.GetInvisibleCustomProperty(pCustomPropertyName, swDmCustomInfoText); ""
saveAndCloseDmDoc
End Sub
These are all in a class with private variable names starting with "p" in those examples.
The macro runs without any of those debug.asserts throwing. However, the problem persists - the InvisibleCustomProperty does not persist.
The trouble I'm having with debugging this is I feel like I can't trust the return values for the DMDocument's save method and there isn't a way of observing the change when it is successful.
I would really prefer to use this functionality because it would prevent user-error and allow me to write future macros that use this value purely through the Document Manager API.
SolidworksApi macros