7 Replies Latest reply on Jul 13, 2018 10:00 PM by Francisco Martínez

    Help with modifying Macro, open part/assembly, set diametric, zoom to fit, set wireframe, rotate part, save as png

    Francisco Martínez

      Hello,

       

      I am trying to modify a macro to complete a lengthy task on 3000 files. What I am trying to accomplish is to open a part or assembly( might use #task to batch)

      set to diametric view, zoom to fit, set wire frame view, rotate part, force rebuild, then last is save as png.

      I am getting a compile error right now with the codes I have put together.

       

      I would appreciate any help getting this to work, I am definitely lacking training on API  SW2017 sp5

       

      Here are my codes

       

      Option Explicit

          Dim swApp                   As SldWorks.SldWorks

          Dim swModel                 As SldWorks.ModelDoc2

          Dim vConfNameArr            As Variant

          Dim sConfigName             As String

          Dim sPath                   As String

          Dim i                       As Long

          Dim bRebuild                As Boolean

          Dim bRet                    As Boolean

      Sub Main()

       

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

         

          If swModel Is Nothing Then

          swApp.SendMsgToUser2 "Please open a PART file!", swMbWarning, swMbOk

          Exit Sub

          End If

        

        

         Set swModelView = swModel.ActiveView

         Part.ShowNamedView2 "*Dimetric", 9

         Part.ViewZoomtofit2

         swModel.ViewDisplayWireframe

         swModelView.RollBy (Degrees2Radians(-90))                                 ' Roll the model view by the specified angle

       

       

             

             

          bRebuild = swModel.ForceRebuild3(False)                                 'Rebuild the current part

             

             

          sPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))   'Get the path

       

       

          swModel.SaveAs (sPath & ".png")                           'Save as .png

             

       

       

          Next

       

       

      End Sub

       

       

      ---------------------------------------------------------------------------

       

      this is the module for rotating parts

       

      Function Degrees2Radians(ByVal DegVal As Double) As Double

       

       

           Dim Pi As Double

       

       

           Pi = 3.14159265359

       

       

           Degrees2Radians = DegVal * 180 / Pi

        • Re: Help with modifying Macro, open part/assembly, set diametric, zoom to fit, set wireframe, rotate part, save as png
          Fifi Riri

          Hello, I fixed:

          swModelView definition

          swModel instead of Part

          save path was missing a filename

          end function missing

           

          Option Explicit

              Dim swApp                  As SldWorks.SldWorks

              Dim swModel                As SldWorks.ModelDoc2

              Dim vConfNameArr            As Variant

              Dim sConfigName            As String

              Dim sPath                  As String

              Dim i                      As Long

              Dim bRebuild                As Boolean

              Dim bRet                    As Boolean

           

          Sub Main()

              Set swApp = CreateObject("SldWorks.Application")

              Set swModel = swApp.ActiveDoc

              If swModel Is Nothing Then

                  swApp.SendMsgToUser2 "Please open a PART file!", swMbWarning, swMbOk

                  Exit Sub

              End If

              Dim swModelView As SldWorks.ModelView

              Set swModelView = swModel.ActiveView

              swModel.ShowNamedView2 "*Dimetric", 9

              swModel.ViewZoomtofit2

              swModel.ViewDisplayWireframe

              swModelView.RollBy (Degrees2Radians(-90))                                ' Roll the model view by the specified angle

              bRebuild = swModel.ForceRebuild3(False)                                'Rebuild the current part

              sPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "."))  'Get the path

              swModel.SaveAs (sPath & "png")                          'Save as .png

          End Sub

           

          Function Degrees2Radians(ByVal DegVal As Double) As Double

              Dim Pi As Double

              Pi = 3.14159265359

              Degrees2Radians = DegVal * 180 / Pi

          End Function

          • Re: Help with modifying Macro, open part/assembly, set diametric, zoom to fit, set wireframe, rotate part, save as png
            Alex Burnett

            You were missing a few declarations as well as the title of the file to save. I think I got it working the way you wanted with the code you provided.

             

            Option Explicit
            
            
                Dim swApp                   As SldWorks.SldWorks
                Dim swModel                 As SldWorks.ModelDoc2
                Dim vConfNameArr            As Variant
                Dim sConfigName             As String
                Dim sPath                   As String
                Dim i                       As Long
                Dim bRebuild                As Boolean
                Dim bRet                    As Boolean
                
                Dim swModelView             As ModelView         ' I added this
                Dim Part                    As PartDoc      ' I added this
            
            
            Sub Main()
            
            
                Set swApp = CreateObject("SldWorks.Application")
                Set swModel = swApp.ActiveDoc
            
            
                If swModel Is Nothing Then
                    swApp.SendMsgToUser2 "Please open a PART file!", swMbWarning, swMbOk
                    Exit Sub
                End If
            
            
                Set swModelView = swModel.ActiveView
                Set Part = swModel                          ' I added this
                Part.ShowNamedView2 "", swStandardViews_e.swDimetricView  ' I changed this
                Part.ViewZoomtofit2
                swModel.ViewDisplayWireframe
                swModelView.RollBy (Degrees2Radians(-90))                                 ' Roll the model view by the specified angle
            
            
                bRebuild = swModel.ForceRebuild3(False)                                 'Rebuild the current part
            
            
                sPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))   'Get the path
                Debug.Print sPath
                swModel.SaveAs (sPath & swModel.GetTitle() & ".png")              'I changed this             'Save as .png
            
            
            
            
            End Sub
            
            
            '---------------------------------------------------------------------------
            'this is the module for rotating parts
            
            
            Function Degrees2Radians(ByVal DegVal As Double) As Double
            
            
            
            
                 Dim Pi As Double
                 Pi = 3.14159265359
            
            
                 Degrees2Radians = DegVal * 180 / Pi
                 
            End Function ' I added this
            
            • Re: Help with modifying Macro, open part/assembly, set diametric, zoom to fit, set wireframe, rotate part, save as png
              Francisco Martínez

              It worked pretty good, I am going to try to add these for #task but I am not sure if I can run this in #task with a function module.

               

              Edit** I got a little ahead of myself, I forgot to set background to white and I also need to rotate the part about the y-Axis

               

               

              task.png

                • Re: Help with modifying Macro, open part/assembly, set diametric, zoom to fit, set wireframe, rotate part, save as png
                  Francisco Martínez

                  I had to pull off of this to make some cnc programs and renderings, I think I found the right code to rotate by selected axis

                  I am going to try to see if I can read up and implement this into the current macro you guys have helped me with.

                   

                  this example shows how to rotate a model in the graphics area.

                  '---------------------------------------------------------------------------- ' Preconditions: ' 1. Verify that the specified part template exists. ' 2. Watch the graphics while the macro runs. ' ' Postconditions: ' 1. Creates a new part document. ' 2. Inserts and extrudes a rectangular sketch. ' 3. Rotates the sketch multiple times. '--------------------------------------------------------------------------- Dim swApp As SldWorks.SldWorks
                  Dim Part As SldWorks.ModelDoc2
                  Dim myFeature As SldWorks.Feature
                  Dim vSkLines As Variant
                  Dim boolstatus As Boolean
                  Option Explicit
                  Sub main()

                      Set swApp = Application.SldWorks
                    

                      Set Part = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2012\templates\Part.prtdot", 0, 0, 0)
                   

                      boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", -5.67254111166863E-02, 7.53958008310182E-03, 2.48109468921342E-02, False, 0, Nothing, 0)
                      Part.SketchManager.InsertSketch True
                    

                      vSkLines = Part.SketchManager.CreateCornerRectangle(-4.93169981371904E-02, 1.73783707721528E-02, 0, 5.58925978888158E-02, -4.55595125648331E-02, 0)
                      Part.ShowNamedView2 "*Trimetric", 8

                      boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
                      boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
                      boolstatus = Part.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
                      boolstatus = Part.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
                    

                      Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.016256, 0.00254, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
                      Part.SelectionManager.EnableContourSelection = False
                    

                      Part.ViewRotate
                      Part.ViewRotateminusx
                      Part.ViewRotateminusy
                      Part.ViewRotateminusz
                      Part.ViewRotateplusx
                      Part.ViewRotateplusy
                      Part.ViewRotateplusz
                      Part.ViewRotXMinusNinety
                      Part.ViewRotXPlusNinety
                      Part.ViewRotYMinusNinety
                      Part.ViewRotYPlusNinety
                    

                  End Sub

                    • Re: Help with modifying Macro, open part/assembly, set diametric, zoom to fit, set wireframe, rotate part, save as png
                      Francisco Martínez

                      Ok back at it and I got it to run exactly how I need this to run but just not in #task, i really need to run this in #task so I can batch the files

                       

                      Can someone please help me with adding the arguments into this macro for #task?

                       

                      This is the video I was trying to follow for adding the program into #task,

                      Creating custom #TASK Script - YouTube.

                      I have created the sts file and imported the current code into it, I just need to add the arguments. 1 for file name( save as file name) and one for file location(specify), I think I do not need a argument for opening the fileS since I will drag folders into #task , sts file attached

                       

                      Here is the current code, Again I want to thank you guys for helping me get this far

                       

                      I remember what the engineer I was apprenticing under always yelled at me " KEEP IT SIMPLE" so I looked for the simplest way

                      to accomplish this and I found a simple way to rotate, arrow keys.

                       

                       

                      -----------------------------------------------------------------------------------------------------------------------------------

                       

                      Option Explicit

                       

                       

                          Dim swApp                   As SldWorks.SldWorks

                          Dim swModel                 As SldWorks.ModelDoc2

                          Dim vConfNameArr            As Variant

                          Dim sConfigName             As String

                          Dim sPath                   As String

                          Dim i                       As Long

                          Dim bRebuild                As Boolean

                          Dim bRet                    As Boolean

                           

                          Dim swModelView             As ModelView         ' I added this

                          Dim Part                    As PartDoc      ' I added this

                       

                       

                      Sub Main()

                       

                       

                          Set swApp = CreateObject("SldWorks.Application")

                          Set swModel = swApp.ActiveDoc

                       

                       

                          If swModel Is Nothing Then

                              swApp.SendMsgToUser2 "Please open a PART file!", swMbWarning, swMbOk

                              Exit Sub

                          End If

                       

                       

                          Set swModelView = swModel.ActiveView

                          Set Part = swModel                          ' I added this

                          Part.ShowNamedView2 "", swStandardViews_e.swFrontView   ' I changed this, CHANGED TO FRONT

                          Part.ViewRotateplusy

                          Part.ViewRotateplusy

                          Part.ViewRotateminusx                                    ' Roll the model view by the specified angle, REMOVED, ADDED ROTATE KEY INPUT

                          Part.ViewZoomtofit2                                          ' AFTER ROTATE SO PART IS CENTERED                                                                            

                          swModel.ViewDisplayHiddenremoved              'CHANGED TO HIDDEN REMOVED    

                                                                

                       

                       

                       

                          bRebuild = swModel.ForceRebuild3(False)                                 'Rebuild the current part

                       

                       

                          sPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))   'Get the path

                          Debug.Print sPath

                          swModel.SaveAs (sPath & swModel.GetTitle() & ".png")              'I changed this             'Save as .png

                       

                       

                       

                       

                      End Sub