7 Replies Latest reply on Mar 14, 2017 1:21 PM by Jason Beeksma

    Automate: Make & Save New Drawing From Part/Assembly

    Jason Beeksma

      I am in the midst of creating a macro to automate all the "clicks" that are done in the process of making a "New Drawing From Part/Assembly"

       

      The macro process:

       

      1) The process begins with a part or assembly that is open in Solidworks.

      2) The user decides to make a drawing from that part or assembly by clicking the custom button I created "New Drawing From Part/Assembly".

      3) The user picks the drawing template.

      4) The automation then takes over to insert the part into the drawing, saves the drawing as current file name and file path (of the part/assembly), adds to vault, 'ok' of file data card.

      5) Finnish's by showing the drawing as the active doc to now be edited.

       

      The result is there would be only 2 clicks from the user, thus providing a drawing ready to be edited. I can actually get it down to 1 click by simply making a macro for the 5 sheet sizes we use. However, I am focused on just 2 clicks now.

       

      The macro is not working:

       

      'Description: Make a drawing from model. Automates the file name, path, adding to vault, and save.

       

      'Pre-condition(s): Model is open and the active window.

       

      'Post-status(s): Drawing is open and ready to be edited.

       

      'Variable

      Dim instance As swCommands_e

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      'Dim swPart As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.DrawingDoc

      Dim CompPathName As String

      'Dim DrawView As SldWorks.View

      Dim boolstatus As Boolean

      Dim FilePath As String

      Dim FileName As String

      Dim PathSize As String

      Dim PathNoExtension As String

      'Dim NewFilePath As String

      Dim lErrors As Long

      Dim lWarnings As Long

      'Dim instance As IModelDoc2

      'Dim value As System.String

       

       

      Sub main()

       

      'value = instance.GetPathName()

      'MsgBox (value)

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      'swApp.Visible = True

      'Set swPart = swModel

       

      'Set FilePath = swApp.GetPathName

      'Set FileName = swModel\

      'MsgBox (FileName)

       

      'run the actual SolidWorks commands

      swApp.RunCommand swCommands_MakeDrawingFromPartAssembly, "NewDrawingFromPartAssembly"

      swApp.RunCommand swCommands_Save

       

      'Get path name of model

       

      Set swDraw = swApp.ActiveDoc

       

      'MyFolder = CurDir$

      'MyPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") - 1)

      'MyPath = Right(MyPath, Len(MyPath) - InStrRev(MyPath, "\"))

       

      'Show File path in message box

      'MsgBox (MyPath)

       

      Debug.Print "File = " & swModel.GetPathName

      Debug.Print "Folder = " & MyPath

      Debug.Print "Current Folder = " & MyFolder

       

      'Save drawing into same file path and filename as model

      boolstatus = swDraw.Save(swSaveAsOptions_Silent, lErrors, lWarnings)

       

      ' Errors

      Debug.Print ("Errors as defined in swFileSaveError_e: " & lErrors)

       

      ' Warnings

      Debug.Print ("Warnings as defined in swFileSaveWarning_e: " & lWarnings)

       

      End Sub

          • Re: Automate: Make & Save New Drawing From Part/Assembly
            Jason Beeksma

            Hi Deepak,

             

            Thanks for the quick response. I've seen alot of your posts and appreciate the work you've done.

             

            I have reduced the code down to just a few lines, but the macro fails at the Save step.

            The code for the current macro is found below:

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

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            'Dim swPart As SldWorks.ModelDoc2

            'Dim swDraw As SldWorks.DrawingDoc

            Dim boolstatus          As Boolean

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

            Sub main()

             

            Set swApp = Application.SldWorks

            Set swModel = swApp.ActiveDoc

             

            'run the actual SolidWorks command MakeDrawingFromPartAssembly

            swApp.RunCommand swCommands_MakeDrawingFromPartAssembly, "NewDrawingFromPartAssembly"

             

            Dim swDraw As SldWorks.ModelDoc2

            Set swDraw = swApp.ActiveDoc

            Set swApp = Application.SldWorks

             

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

             

            'swDraw.Extension.SaveAs swModel.GetPathName + swModel.GetTitle(), 0, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

             

            End Sub

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

             

            Any feedback is welcome, thanks!

              • Re: Automate: Make & Save New Drawing From Part/Assembly
                Nilesh Patel

                Hi Jason,

                 

                'boolstatus = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)' will only save your part or assembly document from which you are creating the drawing document. To save the drawing document un-comment the following line: 'swDraw.Extension.SaveAs swModel.GetPathName + swModel.GetTitle(), 0, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0' and replace with following:

                 

                Dim sDrawPath As String

                sDrawPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))

                swDraw.Extension.SaveAs sDrawPath + swModel.GetTitle + ".slddrw", 0, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

                 

                I have tried it and it works.

                 

                Regards,

                  • Re: Automate: Make & Save New Drawing From Part/Assembly
                    Jason Beeksma

                    Hi Nilesh,

                     

                    Thanks for the response. Works.

                     

                    However, the custom properties do not get pushed to the drawing file. The properties do get pushed when I manually created a drawing the conventional way.

                     

                    Keep in mind that I have Solidworks PDM (EPDM), so I think something is going on here.

                     

                    What I am trying to automate is the sequence of events that a user would go through in creating a drawing file. I believe this to be the most stable. The file data card also should be shown, as it does when I manually create a drawing from a part/assembly. However, I want to hit 'ok' automatically as well.

                     

                    Below is the code that was refined with your suggestions:

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

                    Dim swApp As SldWorks.SldWorks

                    Dim swModel As SldWorks.ModelDoc2

                    Dim swDraw As SldWorks.ModelDoc2

                    Dim swDrawPath As String

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

                     

                    Sub main()

                     

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                     

                    'runs the SolidWorks command MakeDrawingFromPartAssembly

                    'user chooses drawing sheet template size

                    swApp.RunCommand swCommands_MakeDrawingFromPartAssembly, "NewDrawingFromPartAssembly"

                     

                    'Declares the newly made drawing as the active document

                    Set swDraw = swApp.ActiveDoc

                     

                    'Obtains file path and name of CAD model

                    swDrawPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))

                     

                    'Shows title and path of CAD model (this isn't required and can be turned off)

                    MsgBox swModel.GetTitle

                    MsgBox swModel.GetPathName

                     

                    'Saves the newly made drawing of the CAD models with the same title and folder path

                    swDraw.Extension.SaveAs swDrawPath + swModel.GetTitle + ".slddrw", 0, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

                     

                    End Sub