1 Reply Latest reply on May 27, 2016 5:45 PM by John Alexander

    Using the Solidworks API alongside the Document Manager API

    John Alexander

      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.

        • Re: Using the Solidworks API alongside the Document Manager API
          John Alexander

          I got it to work more consistently.

           

          I think the original problem was that I was trying to save the model, set it to read only, open and get write access for the DMDocument every time I wanted to set or get the invisible custom property.

           

          If anyone finds it useful - here is the rest of the code for that invisble custom property setting class. It requires implementing an additional class for getting the DMApplication object (which I cannot share because it contains my super secret license key).

          Option Explicit

           

           

          Private pDMApp      As SwDocumentMgr.SwDMApplication

          Private pDMDoc      As SwDocumentMgr.SwDMDocument3

          Private pModel      As SldWorks.ModelDoc2

           

           

          Private pPathName   As String

           

           

          Private Const pCustomPropertyName = "AT-CONFIG"

           

           

          Public Function init(swModel As SldWorks.ModelDoc2)

              Dim AT_DMLoader As ATDMLoader

              Dim open_error As SwDmDocumentOpenError

             

              Set AT_DMLoader = New ATDMLoader

              Set pDMApp = AT_DMLoader.DMApp

              Set pModel = swModel

              pPathName = pModel.GetPathName

             

              getDMDocWriteAccess

             

              'Set pDMDoc = pDMApp.GetDocument(pPathName, SwDmDocumentType.swDmDocumentPart, False, open_error)

              Debug.Assert Not pDMDoc Is Nothing

             

              If Not propertyExists Then

                  pDMDoc.AddInvisibleCustomProperty pCustomPropertyName, swDmCustomInfoText, ""

              End If

              Debug.Assert propertyExists

          End Function

           

           

          Public Function getValue() As String

              getValue = pDMDoc.GetInvisibleCustomProperty(pCustomPropertyName, swDmCustomInfoText)

          End Function

           

           

          Public Sub setValue(value As String)

              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)

          End Sub

           

           

          Private Function propertyExists() As Boolean

              Dim vCustomPropertyName     As Variant

             

              propertyExists = False

              For Each vCustomPropertyName In pDMDoc.GetInvisibleCustomPropertyNames

                  If StrComp(vCustomPropertyName, pCustomPropertyName) = 0 Then

                      propertyExists = True

                      Exit Function

                  End If

              Next vCustomPropertyName

          End Function

           

           

          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 = swReloadOkay)

          End Sub