12 Replies Latest reply on May 29, 2018 4:47 PM by Tim Webb

    PDM, How to check out a Drawing file already open using API (VBA)

    Cad Admin

      I looked at using ForceReleaseLocks Method, but it doesn't work on drawings.


      I started looking into See ISldWorks::CloseAndReopen for details about closing and reopening a drawing document without unloading.  But I'm confused to its usage, the sample API, is limited in descriptive help.

        • Re: PDM, How to check out a Drawing file already open using API (VBA)
          Jim Sculley

          Have you downloaded the API Example as suggested in the API Help?  Basically, you register as a listener for the FileCloseNotify event.  In your event handler function, check that the reason for the event is swFileCloseNotifyReason_CloseForReload, and if so, do whatever you need to do to the file.  If you want to open a different file than the one you closed, you can call the ISldWorks::SetPromptFileName2 method and pass in the appropriate file path.  When your event handler function returns 1, the file will then be re-opened.


          Having said that, I must warn you that I have no luck getting CloseAndReopen to work when dealing with files in an EPDM vault in my C# add-in.  See  SOLIDWORKS Crash Using CloseAndReopen in Conjunction with EPDM.


          If in the end this bug affects you, be sure to vote for SPR 967430.

          • Re: PDM, How to check out a Drawing file already open using API (VBA)
            Tim Webb

            This won't work on drawings but works on models


            Hope this helps

            Tim CEPA

            Believe in The Q!


            Option Explicit
            Sub main()
                Dim swApp                               As SldWorks.SldWorks
                Dim swDoc                               As SldWorks.ModelDoc2
                Dim strOldDocPath                       As String
                Set swApp = Application.SldWorks
                Set swDoc = swApp.ActiveDoc
                strOldDocPath = swDoc.GetPathName
                Dim vault As EdmVault5
                Set vault = New EdmVault5
                Debug.Print "Vault object was set to new EdmVault5"
                Dim vaultname As String
                vaultname = vault.GetVaultNameFromPath(strOldDocPath)
                Debug.Print "Vault name = " & vaultname
                vault.LoginAuto vaultname, 0
                Debug.Print "Vault was logged in"
                Dim file As IEdmFile5
                Dim folder As IEdmFolder5
                Set file = vault.GetFileFromPath(strOldDocPath, folder)
                Debug.Print "File object was set: " & file.Name
                Debug.Print "Folder object was set: " & folder.LocalPath
                If file.IsLocked = False Then
                    Debug.Print "File was not locked"
                    Dim iRelease As Integer
                    iRelease = swDoc.ForceReleaseLocks
                    Debug.Print "swDoc.ForceReleaseLocks was issued"
                    If iRelease = 1 Then
                        Debug.Print "swDoc.ForceReleaseLocks = 1"
                        file.LockFile folder.ID, 0
                        Debug.Print "File was checked out"
                        Dim nRetVal As Long
                        nRetVal = swDoc.ReloadOrReplace(False, swDoc.GetPathName, True)
                        Debug.Print "File = " & swDoc.GetPathName
                        Debug.Print "  ReloadOrReplace (0 = swComponentReloadError_e.swReloadOkay) = " & nRetVal
                    ElseIf iRelease = 0 Then
                        Debug.Print "swDoc.ForceReleaseLocks = 0"
                        MsgBox "Unable to check out the file " & file.Name, vbOKOnly, "Error!"
                    End If
                End If
            End Sub
              • Re: PDM, How to check out a Drawing file already open using API (VBA)
                Jim Sculley

                Incidentally, there is a regression (SPR 1009156) in 2017 that causes ReloadOrReplace to fail if the file is renamed between the calls to ForceReleaseLocks and ReloadOrReplace.  It appears to only affect .NET.  The VBA version doesn't have a problem with the rename.

                • Re: PDM, How to check out a Drawing file already open using API (VBA)
                  Cad Admin


                  Thanks, this may help me a lot...


                  Working with my VAR, we came up with the snippet below,  Checks out all types, but is a little unstable.  Also not the cleanest, due to back and forth email changes


                  ' Option Explicit

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim swDrawModel As SldWorks.ModelDoc2

                  Dim swDraw As SldWorks.DrawingDoc

                  Dim swView As SldWorks.View

                  Dim swCustProp As CustomPropertyManager

                  Dim Rbuild As Boolean



                  Sub RevMod()


                  Set swApp = Application.SldWorks

                  Set swDraw = swApp.ActiveDoc

                  Set swDrawModel = swApp.ActiveDoc


                  Set swView = swDraw.GetFirstView

                  Set swView = swView.GetNextView

                  Set swModel = swView.ReferencedDocument


                  Dim vault As IEdmVault5

                  Set vault = New EdmVault5


                  Dim vaultName As String

                  vaultName = "ProdVault"

                  Call vault.LoginAuto(vaultName, 0)


                  Dim file As IEdmFile5

                  Dim Draw_file As IEdmFile5

                  Dim ParentFolder As IEdmFolder5

                  Dim Draw_ParentFolder As IEdmFolder5


                  path_to_file = swModel.GetPathName()

                  path_to_drawing = swDrawModel.GetPathName()

                  Open_model = swDraw.GetPathName()


                  Set file = vault.GetFileFromPath(path_to_file, ParentFolder)

                  Set Draw_file = vault.GetFileFromPath(path_to_drawing, Draw_ParentFolder)


                  If (file.IsLocked = False) Then ' False = not checked out


                  swModel.ForceReleaseLocks 'Release the locks on the file

                  file.LockFile ParentFolder.ID, 0, 0


                  If (Draw_file.IsLocked = False) Then

                  Draw_file.LockFile Draw_ParentFolder.ID, 0, 0

                  swDrawModel.SetReadOnlyState False

                  End If


                  swModel.ReloadOrReplace False, swModel.GetPathName, True 'Set the locks back in place

                  swDrawModel.ReloadOrReplace False, swDrawModel.GetPathName, True

                  End If


                  Set swCustProp = swDrawModel.Extension.CustomPropertyManager("")

                  1. swCustProp.Set "Revision", "test"

                  Rbuild = swDraw.ForceRebuild3(False)


                  End Sub


                  It sometimes (on assembly drawings) it will check out the drawing the referenced assembly and all its components. It also has a issues if after running the macro I also can’t RMB on any file in the PDM right Pane, it brings up the bar below…only get the PDM back if I use the check in icon at the top.  Most likely residual memory hold…


                  • Re: PDM, How to check out a Drawing file already open using API (VBA)
                    Chris Manger

                    Hello Tim.   I tried using your code but I am receiving a message stating : "An attempt was made to access a file that is exclusively opened by another application."    


                    The file isn't locked (checked out) but it is open in SOLIDWORKS.