16 Replies Latest reply on Jun 7, 2018 8:58 PM by Nick Gvk

    Multisave Different views

    Nick Gvk

      im trying to make an macro that is going to save me some time

       

      first of all im trying to find the name of the view that is blue in the picture

      i have made a custom view of that point that i called "bottom left "

      but i want the original name of that view point

      so i can use this macro in every pc that we run solidwork on

      so my first question is what is the name of that point ?

       

      my second problem that im having is that

      i want to save the jpg`s with their respected custom names (isometric1 , bottomleft1)

      and it wil only save 1 view at this moment as it override`s the isometric view

      Can someone point out what i did wrong here

       

      its been a long time since i done macro`s so im a bit rusty

       

       

       

      Dim swApp As Object

      Dim Part As Object

       

       

      Sub main()

      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc

       

       

      Dim FilePath As String

      Dim PathSize As Long

      Dim PathNoExtention As String

      Dim NewFilePath As String

       

       

      Dim FilePath2 As String

      Dim PathSize2 As Long

      Dim PathNoExtention2 As String

      Dim NewFilePath2 As String

       

       

      Dim FilePath3 As String

      Dim PathSize3 As Long

      Dim PathNoExtention3 As String

      Dim NewFilePath3 As String

      Dim myModelView As Object

      Set myModelView = Part.ActiveView

      myModelView.AddPerspective

       

       

      Call swApp.SetUserPreferenceToggle(swDisplayShadowsInShadedMode, True)

      Call swApp.SetUserPreferenceToggle(swDraftQualityAmbientOcclusion, True)

       

       

       

       

       

       

      FilePath = Part.GetPathName

      PathSize = Strings.Len(FilePath)

      PathNoExtention = Strings.Left(FilePath, PathSize - 6)

      NewFilePath = PathNoExtention & "jpg"

       

       

      FilePath2 = Part.GetPathName

      PathSize2 = Strings.Len(FilePath2)

      PathNoExtention2 = Strings.Left(FilePath2, PathSize2 - 6)

      NewFilePath2 = PathNoExtention2 & "png"

       

       

      FilePath3 = Part.GetPathName

      PathSize3 = Strings.Len(FilePath3)

      PathNoExtention3 = Strings.Left(FilePath3, PathSize3 - 6)

      NewFilePath3 = PathNoExtention3 & "tif"

       

       

       

       

       

       

      '1th save file

      Part.ShowNamedView2 "*Isometric", 7

      Part.SaveAs2 NewFilePath, 0, True, False

       

       

      '2th save file

      Part.ShowNamedView2 "bottom left", -1

      Part.SaveAs2 NewFilePath2, 0, True, False

       

       

      '3th save file

      Call swApp.SetUserPreferenceToggle(swDisplayShadowsInShadedMode, False)

      Part.ShowNamedView2 "*Isometric", 7

      Part.SaveAs2 NewFilePath3, 0, True, False

       

       

      MsgBox "Saved As : Multiple.JPG!"

       

       

      End Sub

          • Re: Multisave Different views
            Nick Gvk

            both just rename the  view`s just like i did

            only then with a macro

             

            i dont want to rename them

            i want to know the original view name that solidworks uses

            then take a screenshot/save as jpg from that angle and go to the next angle and the next ......

              • Re: Multisave Different views
                Deepak Gupta

                I do think there is any names for those view but you can simply call them ISO views from different angles.

                  • Re: Multisave Different views
                    Nick Gvk

                    wel the macro is running  like this

                     

                    but i want it to output different names for the different vieuw that im calling

                    at this moment i have them being outputted as:  jpg, png, tif

                    but i like it to have a custom name with it  and i want it to be only .jpg`s

                      • Re: Multisave Different views
                        Deepak Gupta

                        Nick Gvk wrote:

                         

                        but i want it to output different names for the different vieuw that im calling

                        at this moment i have them being outputted as: jpg, png, tif

                        but i like it to have a custom name with it and i want it to be only .jpg`s

                        You can add the desired view name in the file name. For e.g.

                         

                        NewFilePath = PathNoExtention & "-Isometric" & "jpg"

                         

                         

                        Check corrected reply in post below.

                          • Re: Multisave Different views
                            Nick Gvk

                            that does not work

                              • Re: Multisave Different views
                                Deepak Gupta

                                Sorry it should be

                                 

                                FilePath = Part.GetPathName

                                PathSize = Strings.Len(FilePath)

                                PathNoExtention = Strings.Left(FilePath, PathSize - 7)

                                NewFilePath = PathNoExtention & "-Isometric" & ".jpg"

                                  • Re: Multisave Different views
                                    Nick Gvk

                                    this works !

                                    why do we add the .

                                    and why do we increase that number to 7 ?

                                     

                                     

                                    now i still have to solve the problem for how the name of the view is called

                                      • Re: Multisave Different views
                                        Jonathan Westcott

                                        I think the -7 represents the .sldprt (7 characters) so you are just deleting the extension.

                                         

                                        Had a quick look at the views and not sure what they are called but found a macro to generate them as a named view after a google search.

                                         

                                        Check this out:

                                        https://groups.google.com/forum/#!topic/comp.cad.solidworks/P9j61CLCB1E

                                         

                                        By my calcs you are looking to generate a view something like Front-Bottom-Left so you will need to modify the code with these lines.

                                         

                                        'Add new values for X & Y to rotate the model in the opposite direction.

                                        X2 = Atn(-Z / Sqr(Z * Z + 1))

                                        Y2 = 45 * pi / 180

                                         

                                        'Add new code to generate a new view.

                                        Part.ShowNamedView2 "*Front", -1

                                        Part.ActiveView().RotateAboutCenter X2, Y2

                                        Part.ViewZoomtofit

                                        Part.NameView ("ISO Front-Bottom-Left")

                                         

                                        You can then save off from these named views.

                                         

                                        Hope this makes sense.

                                          • Re: Multisave Different views
                                            Nick Gvk

                                            i tried everything to make that code work

                                             

                                            unfortunatly  i cant make it work

                                            it seems a rather dificult angle to make

                                            i have the whole code working except for this part

                                             

                                             

                                             

                                             

                                            Dim swApp As Object

                                            Dim Part As Object

                                             

                                            Sub main()

                                            Set swApp = Application.SldWorks

                                            Set Part = swApp.ActiveDoc

                                             

                                            Dim FileName As String

                                             

                                            Dim FilePath As String

                                            Dim PathSize As Long

                                            Dim PathNoExtention As String

                                            Dim NewFilePath As String

                                             

                                             

                                            Dim FileName2 As String

                                            Dim FilePath2 As String

                                            Dim PathSize2 As Long

                                            Dim PathNoExtention2 As String

                                            Dim NewFilePath2 As String

                                             

                                             

                                            Dim FileName3 As String

                                            Dim FilePath3 As String

                                            Dim PathSize3 As Long

                                            Dim PathNoExtention3 As String

                                            Dim NewFilePath3 As String

                                             

                                             

                                            Dim myModelView As Object

                                            Set myModelView = Part.ActiveView

                                            myModelView.AddPerspective

                                             

                                             

                                             

                                             

                                             

                                             

                                            'delete existing views with these names if they exist

                                             

                                             

                                            Part.DeleteNamedView ("ISO Front-Bottom-Left")

                                            Part.ShowNamedView2 "*Isometric", 7

                                            X2 = Atn(-Z / Sqr(Z * Z + 1))

                                            Y2 = 45 * pi / 180

                                             

                                             

                                             

                                            Part.ShowNamedView2 "*Bottom", -1

                                            Part.ActiveView().RotateAboutCenter X2, Y2

                                            Part.ViewZoomtofit

                                            Part.NameView ("ISO Front-Bottom-Left")

                                             

                                             

                                             

                                             

                                             

                                             

                                            'Make shure the Ambient And Shaddows are Active

                                            Call swApp.SetUserPreferenceToggle(swDisplayShadowsInShadedMode, True)

                                            Call swApp.SetUserPreferenceToggle(swDraftQualityAmbientOcclusion, True)

                                             

                                             

                                             

                                             

                                            'A Check to Check if the original file has been saved in order to prevent the Script jumping in error

                                            If Part.GetPathName = "" Then

                                            MsgBox ("Model must be saved first")

                                            Exit Sub

                                            End If

                                             

                                             

                                             

                                            'set first file name

                                            FileName = "IsoMetric1"

                                            FilePath = Part.GetPathName

                                            PathSize = Strings.Len(FilePath)

                                            PathNoExtention = Strings.Left(FilePath, PathSize - 7)

                                            NewFilePath = PathNoExtention & FileName & ".JPG"

                                             

                                             

                                            'set second file name

                                            FileName2 = "IsoMetric2"

                                            FilePath2 = Part.GetPathName

                                            PathSize2 = Strings.Len(FilePath2)

                                            PathNoExtention2 = Strings.Left(FilePath2, PathSize2 - 7)

                                            NewFilePath2 = PathNoExtention2 & FileName2 & ".JPG"

                                             

                                             

                                            'set third file name (u can keep incrementing this and also use it to save it as different file formats)

                                            FileName3 = "IsoMetric3"

                                            FilePath3 = Part.GetPathName

                                            PathSize3 = Strings.Len(FilePath3)

                                            PathNoExtention3 = Strings.Left(FilePath3, PathSize3 - 7)

                                            NewFilePath3 = PathNoExtention3 & FileName3 & ".JPG"

                                             

                                             

                                            '1th Set and Save file

                                            Part.ShowNamedView2 "*Isometric", 7

                                            Part.SaveAs2 NewFilePath, 0, True, False

                                             

                                            '2th Set and Save file

                                            Part.ShowNamedView2 "bottom left", -1

                                            Part.SaveAs2 NewFilePath2, 0, True, False

                                             

                                             

                                            '3th Set and Save file

                                            Call swApp.SetUserPreferenceToggle(swDisplayShadowsInShadedMode, False)

                                            Part.ShowNamedView2 "*Isometric", 7

                                            Part.SaveAs2 NewFilePath3, 0, True, False

                                             

                                             

                                             

                                            MsgBox "Saved As : Multiple.JPG!"

                                             

                                             

                                            End Sub

                                             

                                             

                                             

                                             

                                             

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

                                             

                                            i know i have to change these

                                            but the result wil be the same

                                            the projected view that is being created is not the one i needed

                                            i tried changing it to Front Back Right Left Top Bottom

                                            but for me it does not seem to change anything

                                             

                                            i had it working for a little bit

                                            but that was only because i was in the view i was trying to creat

                                             

                                            there MUST be  solidworks internal command for that view

                                            because solidworks uses itself as u can just "click on it "

                                             

                                             

                                            i also tried to use a var and increment the jpg

                                            but for that i need to reloop

                                            a funtion that i unfortunatly have not wrapped my brain around yet in this language

                                             

                                             

                                             

                                            any help would be apriciated

                                              • Re: Multisave Different views
                                                Nick Gvk

                                                the standart isometric  view

                                                 

                                                 

                                                the view im trying to create

                                                displayed by the blue box

                                                  • Re: Multisave Different views
                                                    Jonathan Westcott

                                                    Nick,

                                                     

                                                    Is this the sort of thing you are after?

                                                    You should now be able to adjust as required.

                                                     

                                                    Dim swApp                  As SldWorks.SldWorks

                                                    Dim swModel                As SldWorks.ModelDoc2

                                                    Dim swPart                  As SldWorks.PartDoc

                                                    Dim swView                  As SldWorks.View

                                                    Dim strCurrentView          As String

                                                    Dim vViews                  As Variant

                                                    Dim FileName                As String

                                                    Dim FilePath                As String

                                                    Dim PathSize                As Long

                                                    Dim PathNoExtention        As String

                                                    Dim NewFilePath            As String

                                                    Dim FileName2              As String

                                                    Dim FilePath2              As String

                                                    Dim PathSize2              As Long

                                                    Dim PathNoExtention2        As String

                                                    Dim NewFilePath2            As String

                                                    Dim FileName3              As String

                                                    Dim FilePath3              As String

                                                    Dim PathSize3              As Long

                                                    Dim PathNoExtention3        As String

                                                    Dim NewFilePath3            As String

                                                    Dim oModelView            As Object

                                                     

                                                    Sub main()

                                                     

                                                        Set swApp = Application.SldWorks

                                                        Set swModel = swApp.ActiveDoc

                                                        Set swPart = swModel

                                                        Set oModelView = swPart.ActiveView

                                                     

                                                     

                                                        'A Check to Check if the original file has been saved in order to prevent the Script jumping in error

                                                        If swPart.GetPathName = "" Then

                                                            MsgBox ("Model must be saved first")

                                                            Exit Sub

                                                        End If

                                                     

                                                     

                                                        'Set the view to be in perspective

                                                        oModelView.AddPerspective

                                                     

                                                     

                                                        'Delete existing views with these names if they exist

                                                        vViews = swModel.GetModelViewNames

                                                     

                                                     

                                                        For i = 0 To UBound(vViews)

                                                            swModel.ShowNamedView vViews(i)

                                                            strCurrentView = vViews(i)

                                                        

                                                            Select Case (strCurrentView)

                                                        

                                                                Case "ISO Front-Bottom-Left"

                                                                    swPart.DeleteNamedView ("ISO Front-Bottom-Left")

                                                                

                                                                Case "ISO Front-Bottom-Right"

                                                                    swPart.DeleteNamedView ("ISO Front-Bottom-Right")

                                                                Case Else

                                                            End Select

                                                        Next i

                                                     

                                                     

                                                        'Prepare the rotation components

                                                        pi = 4 * Atn(1)

                                                        Z = Tan(30 * pi / 180)

                                                        X = Atn(Z / Sqr(-Z * Z + 1))

                                                        Y = -45 * pi / 180

                                                        X2 = Atn(-Z / Sqr(Z * Z + 1))

                                                        Y2 = 45 * pi / 180

                                                     

                                                     

                                                        'Make sure the Ambient And Shaddows are Active

                                                        swApp.SetUserPreferenceToggle swDisplayShadowsInShadedMode, True

                                                        swApp.SetUserPreferenceToggle swDraftQualityAmbientOcclusion, True

                                                        swPart.ShowNamedView2 "*Isometric", 7

                                                     

                                                     

                                                        'Rotate the model and generate the named view

                                                        swPart.ShowNamedView2 "*Front", -1

                                                        swPart.ActiveView().RotateAboutCenter X2, Y2

                                                        swPart.ViewZoomtofit

                                                        swPart.NameView ("ISO Front-Bottom-Left")

                                                     

                                                        swPart.ShowNamedView2 "*Front", -1

                                                        swPart.ActiveView().RotateAboutCenter X2, Y

                                                        swPart.ViewZoomtofit

                                                        swPart.NameView ("ISO Front-Bottom-Right")

                                                     

                                                        'Build and save the first file name

                                                        FileName = "IsoMetric1"

                                                        FilePath = swPart.GetPathName

                                                        PathSize = Strings.Len(FilePath)

                                                        PathNoExtention = Strings.Left(FilePath, PathSize - 7)

                                                        NewFilePath = PathNoExtention & FileName & ".JPG"

                                                        swPart.ShowNamedView2 "*Isometric", 7

                                                        swPart.ViewZoomtofit

                                                        swPart.SaveAs2 NewFilePath, 0, True, False

                                                     

                                                        'Build and save the second file name

                                                        FileName2 = "IsoMetric2"

                                                        FilePath2 = swPart.GetPathName

                                                        PathSize2 = Strings.Len(FilePath2)

                                                        PathNoExtention2 = Strings.Left(FilePath2, PathSize2 - 7)

                                                        NewFilePath2 = PathNoExtention2 & FileName2 & ".JPG"

                                                        swPart.ShowNamedView2 "ISO Front-Bottom-Left", -1

                                                        swPart.ViewZoomtofit

                                                        swPart.SaveAs2 NewFilePath2, 0, True, False

                                                     

                                                        'Build and savethe third file name

                                                        FileName3 = "IsoMetric3"

                                                        FilePath3 = swPart.GetPathName

                                                        PathSize3 = Strings.Len(FilePath3)

                                                        PathNoExtention3 = Strings.Left(FilePath3, PathSize3 - 7)

                                                        NewFilePath3 = PathNoExtention3 & FileName3 & ".JPG"

                                                        swApp.SetUserPreferenceToggle swDisplayShadowsInShadedMode, False

                                                        swPart.ShowNamedView2 "ISO Front-Bottom-Right", -1

                                                        swPart.ViewZoomtofit

                                                        swPart.SaveAs2 NewFilePath3, 0, True, False

                                                     

                                                     

                                                    'MsgBox "Saved As : Multiple.JPG!"

                                                    oModelView.RemovePerspective

                                                    swPart.ShowNamedView2 "*Isometric", 7

                                                    swPart.ViewZoomtofit

                                                    swModel.ClearSelection2 False

                                                     

                                                     

                                                    End Sub

                                                      • Re: Multisave Different views
                                                        Nick Gvk

                                                        yes that works
                                                        the problem with it is the header throws errors

                                                         

                                                         

                                                         

                                                         

                                                        im swApp                  As SldWorks.SldWorks

                                                        Dim swModel                As SldWorks.ModelDoc2

                                                        Dim swPart                  As SldWorks.PartDoc

                                                         

                                                        Dim oModelView            As Object

                                                         

                                                        Sub main()

                                                         

                                                            Set swApp = Application.SldWorks

                                                            Set swModel = swApp.ActiveDoc

                                                            Set swPart = swModel

                                                            Set oModelView = swPart.ActiveView

                                                         

                                                         

                                                         

                                                        swPart is causing the whole thing not to run

                                                        i changed them all to "Part" and then the code runs

                                                        wich leads me to believe we set up swPart wrong

                                                • Re: Multisave Different views
                                                  Deepak Gupta

                                                  Nick Gvk wrote:

                                                   

                                                  why do we add the .

                                                  and why do we increase that number to 7 ?

                                                  SW files extension is of 6 characters and with period (.) it becomes 7. Now to add anything in the file name, you generally need to add it before the period (.). And since period (.) was removed, it was required to be added again make the complete file name with file extension like .jpg