5 Replies Latest reply on Oct 19, 2016 11:36 AM by Sanya Shmidt

    Isometric + zoom to fit + rebuild + save

    Hrituc Alexandru


      I want to have a macro button which when i press it to make Isometric + zoom to fit + rebuild + save to model, to assembly and even to drawing if is possible...

      How can make this?... Now it works only with parts.




      ' Preconditions: Model document is open.


      ' Postconditions: Current view mode is changed to isometric and Zoom to Fit.




      Option Explicit

      Sub main()

          Dim swApp                   As SldWorks.SldWorks

          Dim swModel                 As SldWorks.ModelDoc2

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

           Dim boolstatus          As Boolean

          Dim lErrors             As Long

          Dim lWarnings           As Long

          Dim Part As Object


          ' Not quite the same as when done through the user interface;

          ' model is zoomed out a bit further


          swModel.ShowNamedView2 "*Isometric", -1


          ' Now view the same as done through the user interface



          boolstatus = swModel.EditRebuild3()

          boolstatus = swModel.ForceRebuild3(True)

          boolstatus = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)



      End Sub

        • Re: Isometric + zoom to fit + rebuild + save
          Deepak Gupta

          The same macro should work fine for assembly. You can add if condition to check if the active file is drawing or not and then remove the isometric from there.

          • Re: Isometric + zoom to fit + rebuild + save
            Leon Wurr

                 If I'm not mistaken, this line: boolstatus = swModel.Save3(swSaveAsOptions_SaveReferenced, lErrors, lWarnings); will already rebuild your doc before saving.


                 Also, it is possible to disable temporarily the window updating so the code run faster.


                 The folowing code will also close your part/assembly after the saving is done:


            Option Explicit
            Dim swApp               As SldWorks.SldWorks
            Dim swModel             As SldWorks.ModelDoc2
            Dim swModelView         As SldWorks.ModelView
            Dim lngErrors           As Long
            Dim lngWarnings         As Long
            Dim bolStatus           As Boolean
            Sub main()
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                If swModel Is Nothing Then End
                Set swModelView = swModel.GetFirstModelView
                swModelView.EnableGraphicsUpdate = False        'Disable window updating to run faster
                swModel.ForceRebuild3 True
                swModel.ShowNamedView2 "Isometric", 7           'Usualy the Isometric View ID is 7, if you're getting problems here, replace 7 with -1 and fidle with the view's name
                swModel.ViewZoomtofit2                          'Zoom to fit
                swModelView.EnableGraphicsUpdate = True
                bolStatus = swModel.Save3(1 + 4 + 8, lngErrors, lngWarnings)
                If bolStatus = 1 Then
                    swApp.CloseDoc Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
                    MsgBox "There was an error while saving this DOC."
                End If
            End Sub
              • Re: Isometric + zoom to fit + rebuild + save
                Sanya Shmidt

                > swModelView.EnableGraphicsUpdate = False        'Disable window updating to run faster 


                Didn`t know about this. Helps a little while running 3D modeling in the background.


                I also use the code below , helps a lot.


                ' Keep SolidWorks frame invisible when ISldWorks::ActivateDoc2 is called

                        Public pFrame As SldWorks.Frame

                        pFrame = swApp.Frame

                        pFrame.KeepInvisible = Not (AppVisibleYESNO) ' False Visible

                        swApp.Visible = AppVisibleYESNO ' True - visible