SolidWorks VBA API: Read-only

Hello

 

I'm developing a VBA macro that allows you to get read write on library files that are by default read-only.

 

I manage to remove the read-only in Windows (with [SetAttr FileName, vbReadOnly] or [SetAttr FileName, vbNormal]) but then I have to reload the component in SolidWorks in read-write.

 

With a macro by training, by doing "File > Reload" and unchecking the read-only box, I only get:

Set Part = swApp.ActiveDoc
Part.FileReload

 

which has no effect on reading and writing.

 

I found the "ReloadOrReplace" API, but I can't seem to use it correctly:

ret = Part.ReloadOrReplace(0, 0, 1)

doesn't have the desired effect: I think it corresponds more to "replacing a component in an assembly".

 

 

Anyone have an idea? Thanks in advance

It is always dangerous to unlock read files

there must be a reason for them to be locked!!

Hello

ReloadorReplace is the method to use (it replaces FileReload)

You have to indicate the name of the file to be replaced/reloaded, in the help I found this:

 

This example shows how to reload the active model with the last saved version of the model.

[...]

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim nRetVal As Long

 

Set swApp = CreateObject("SldWorks.Application")

Set swModel = swApp.ActiveDoc

' Fails for drawings because functionality is not supported in user interface

nRetVal = swModel.ReloadOrReplace(False, swModel.GetPathName, True)

I haven't tested, so to be confirmed

2 Likes

Thank you y.pacquelet I'll test this tomorrow, but I think I've already used it this way!

 

FYI , our library is read-only to prevent everyone from inadvertently modifying it.

For example, we have a single rolling file with many configurations (part family) which avoids having 200 different files.

If you want to add a bearing, you have to remove the read-only feature... but we put it back afterwards!

Hi to the forum.

Hi Lucas.

Be careful, it's not too advisable to have a fihier room with lots of configs. Especially for a turnover.

Your file may explode in size, after displaying all the configs.

Kind regards

 

Julian

Thank you for the advice, the company has been operating like this since 2006 before I arrived...

 

Here is a macro that reloads the active document if it is a part, or the selected component in the case of an assembly

 

 

Option Explicit

 

Dim swApp As SldWorks.SldWorks

Sub main()

Dim swModel                 As SldWorks.ModelDoc2   

Dim swPart                  As SldWorks.PartDoc   

Dim swAss                   As SldWorks.AssemblyDoc   

Dim nRetVal                 As swComponentReloadError_e   

    

    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

 

    Select Case swModel.GetType

    Box swDocumentTypes_e.swDocPART

        Set swPart = swModel

        swModel.ForceReleaseLocks

        nRetVal = swModel.ReloadOrReplace(False, swModel.GetPathName, True)

        

    Box swDocumentTypes_e.swDocASSEMBLY

        ' We test if a component is selected

        Dim blUnSelected As Boolean

        Set swAss = swModel

        Dim swSelectionMgr As SldWorks.SelectionMgr

        Set swSelectionMgr = swModel.SelectionManager

        If swSelectionMgr.GetSelectedObjectCount2(0) = 0 Then blUnSelected = True

        If swSelectionMgr.GetSelectedObjectType3(1, 0) <> swSelectType_e.swSelCOMPONENTS Then blUnSelected = True

        

        If blUnSelected Then

            MsgBox "Please select a component", vbInformation

            Exit Sub

        End If

        

        ' We retrieve the selected component

        Dim swComponent As SldWorks.Component2

        Set swComponent = swSelectionMgr.GetSelectedObject6(1, 0)

        

        ' We recharge it

        Dim swDoc As SldWorks.ModelDoc2

        Set swDoc = swComponent.GetModelDoc2

        

        swDoc.ForceReleaseLocks

        nRetVal = swDoc.ReloadOrReplace(False, UCase(swDoc.GetPathName), True)

     End Select

       

     ' Message of confirmation

     Dim stErr As Variant

     stErr = Array("Okay", "Access error", "Version error", "Modified not reloaded error", "Invalid option", _

                  "File not saved error", "Invalid component error", "Unexpected error", "Component light weight error", _

                  "File doesn't exist error", "File invalid or same name error", "Document has no view", "Document already opened error", _

                  "Document event error", "Document not changed", "Reload cancel")

     

     MsgBox CStr(stErr(nRetVal))

       

        

        

       ' ' Or we replace him

       ' Dim stPath As String: stPath = ...

       ' Dim stConfigName As String: stConfigName = ...

       ' Dim blRet As Boolean

       ' blRet = swAss.ReplaceComponents(stPath, stConfigName, True, True)

'

       ' If blRet Then

           ' MsgBox "Replacement Successful", vbInformation

       ' Else

           ' MsgBox ' Replacement failed, vbExclamation

       ' End If

    

 

   

End Sub


replace.zip
8 Likes