1 Reply Latest reply on Sep 18, 2014 6:40 AM by Jorn Bjarning

    How to get and change Read Only property of referenced component (Part or Assembly) of Assembly in VB

    Pawel Krzeminski

      I need a macro wich will work similar to Reload command in component tree on Assembly document (switch Read Only).

      It should change Read Only mark on particular (selected) part / assembly.


      Below code I have done:


      Sub SwitchReadOnlyProperty()

          Dim swApp                   As SldWorks.SldWorks
          Dim swModel                 As SldWorks.ModelDoc2
          Dim swSelMgr                As SldWorks.SelectionMgr
          Dim swSelData               As SldWorks.SelectData
          Dim swComp                  As SldWorks.Component2
          Dim Name                    As String
          Dim longstatus              As Long
          Dim longwarnings            As Long
          Dim Komp1                   As String
          Dim boolstatus              As Boolean
          Dim Part                    As Object
          Dim selection               As Integer
          Dim File
          Dim nRetVal                 As Long

          Set swApp = CreateObject("SldWorks.Application")
          Set Part = swApp.ActiveDoc
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc

         ' checking is there any document is opened
          If swModel Is Nothing Then
              MsgBox "You don't have opened (or activate) any document"
              Exit Sub
          End If
           ' checking if Assembly is opened
          If swModel.GetType <> 2 Then MsgBox "You must have opened Assembly and one Component selected": Exit Sub ' Not Assembly is opened
              'Else - Assembly is opened
              Set swSelMgr = swModel.SelectionManager
              Set swSelData = swSelMgr.CreateSelectData
              Set swComp = swSelMgr.GetSelectedObjectsComponent(1)
              'is there any component selected
              selection = swModel.SelectionManager.GetSelectedObjectCount
              If selection <> 1 Then MsgBox "Nothing selected, or selected more then one component": Exit Sub
                  'Else - Assembly is opened and one component selected, getting data to selected component
                  Name = swComp.GetPathName
                  MsgBox "File = " & Name 'this line just for info (to be removed)
                  Komp1 = "Opened Assembly:          " & swModel.GetPathName & Chr(10) & Chr(13) & Chr(13) & _
                          "Selected Component:     " & swComp.Name2 & Chr(10) & Chr(13) & Chr(13) & _
                          "File:                                 " & swComp.GetPathName & " is ReadOnly = " & nRetVal & Chr(10) & Chr(13) & _
                          "Do you want to switch Read Only status of this component?" & Chr(13)
                          If MsgBox(Komp1, vbYesNo, "Change Read Only Status") = vbNo Then Exit Sub
          'reloading component
          'instance.ReloadOrReplace (ReadOnly, ReplaceFileName, DiscardChanges)
          'nRetVal = swModel.ReloadOrReplace(True, "", True)
          'swModel.ReloadOrReplace True, "", True
          MsgBox "File = " & swComp.GetPathName & "  Is ReadOnly = " & nRetVal
      End Sub


      Code above identify if there is assembly opened and only one component selected, but I have got problem with the most important part of the macro - how to get and change Read Only property of referenced component (Part or Assembly) of Assembly.

      Any Halp appreciated