12 Replies Latest reply on Dec 19, 2016 12:41 PM by Mathew Challinor

    Drawing Macro

    Mathew Challinor

      Hi,

       

      This is my first post so apologies if this has been covered.

       

      I want to create a macro that will save my current assembly as an STL, load the assembly into my drawing template and create a view from the current assembly view and then create a bill of materials from my bill of material template and then auto balloon the view.

       

      we will have approximately 1000 drawings to-do so need to get this running.

       

      I have got so far and now I am having a few problems. The problems I have are as follows;

       

      • When I save the part as an stl this needs to be in the current assembly location and have the same name as the assembly.stl
      • When I load the "Current View" I want to bring in "import annotation" and "design annotations" as well.
      • When I try to load a bill of materials I get an error on Set swBOMTable

       

      any help would be much appreciated.

       

      Dim swApp As Object

      Dim Part As Object
      Dim boolstatus As Boolean
      Dim longstatus As Long, longwarnings As Long

      Sub main()

      Set swApp = _
      Application.SldWorks

       

      ' Save Part file as .stp and load drawing template

       

      Set Part = swApp.ActiveDoc
      longstatus = Part.SaveAs3("V:\Engineering\AMC Tool Library\Tool Assemblies\TA-1001\TA-1001.STEP", 0, 0)
      Set Part = swApp.NewDocument("V:\Engineering\Software\Solidworks\TATA_Templates\Tooling_Drawing.drwdot", 12, 0.21, 0.297)
      Dim myModelView As Object
      Set myModelView = Part.ActiveView
      myModelView.FrameState = swWindowState_e.swWindowMaximized
      Set Part = swApp.ActiveDoc
      Set myModelView = Part.ActiveView
      myModelView.FrameLeft = 0
      myModelView.FrameTop = 22

       

       

      'Load Drawing View

       

      Set Part = swApp.ActiveDoc
      Set myModelView = Part.ActiveView
      myModelView.FrameState = swWindowState_e.swWindowMaximized
      Dim myView As Object
      Set myView = Part.CreateDrawViewFromModelView3("V:\Engineering\AMC Tool Library\Tool Assemblies\TA-1001\TA-1001.SLDASM", "Current Model View", 0.146409898581714, 0.11700997510004, 0)
      boolstatus = Part.Extension.SelectByID2("Drawing View11", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
      boolstatus = Part.ActivateView("Drawing View11")
      Part.ClearSelection2 True

       

      ' Bill of materials

       

      Set Part = swApp.ActiveDoc
      Part.ClearSelection2 True
      Dim swActiveView As Object
      Set swActiveView = Part.ActiveDrawingView
      Dim swBOMTable As Object
      Set swBOMTable = swActiveView.InsertBomTable2(False, 0.110111505717365, 0.199996468697135, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, swBomType_e.swBomType_Indented, "Default", "V:\Engineering\Software\Solidworks\TATA_Templates\bomtooling.sldbomtbt")
      boolstatus = Part.EditRebuild3()

       

      ' Auto Baloon

       

      Set Part = swApp.ActiveDoc
      boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.13842553485699, 0.137862387566601, 0, False, 0, Nothing, 0)
      boolstatus = Part.ActivateView("Drawing View1")
      Dim vNotes As Variant
      Dim autoballoonParams As Object
      Set autoballoonParams = Part.CreateAutoBalloonOptions()
      autoballoonParams.Layout = 1
      autoballoonParams.ReverseDirection = False
      autoballoonParams.IgnoreMultiple = True
      autoballoonParams.InsertMagneticLine = True
      autoballoonParams.LeaderAttachmentToFaces = False
      autoballoonParams.Style = 1
      autoballoonParams.Size = 2
      autoballoonParams.EditBalloonOption = 1
      autoballoonParams.EditBalloons = 1
      autoballoonParams.UpperTextContent = 1
      autoballoonParams.UpperText = """"
      autoballoonParams.Layername = "-None-"
      autoballoonParams.ItemNumberStart = 1
      autoballoonParams.ItemNumberIncrement = 1
      autoballoonParams.ItemOrder = 0
      vNotes = Part.AutoBalloon5(autoballoonParams)
      Part.ClearSelection2 True

      End Sub

       

      Regards

      Mat

        • Re: Drawing Macro
          John Stoltzfus

          If you have that many drawings and a short time to do it contact Keith Rice and let him write you a good macro.....   

          • Re: Drawing Macro
            Mathew Challinor

            Time is not an issue for us, we could do 30 in a week or zero in 2 months. Its just a process we want to streamline

            • Re: Drawing Macro
              Deepak Gupta

              Some of the things could be done using #TASK

              • Re: Drawing Macro
                Peter Brinkhuis

                I did some cleaning up of the macro code that you linked together. I've added some checks so you can only use it on assemblies. The bill of materials and the balloons work for an assembly of mine, so I assume it works with your files as well. It doesn't yet address some of your issues, but I hope it helps you a little further anyway If you have more questions, all of us are here to help.

                 

                Option Explicit

                 

                Sub main()

                    Dim swApp               As SldWorks.SldWorks

                    Dim swModel             As ModelDoc2

                    Dim swAssy              As AssemblyDoc

                    Dim swDraw              As DrawingDoc

                   

                    Dim swView              As View

                    Dim boolstatus          As Boolean

                    Dim longstatus          As Long, longwarnings As Long

                    Dim swBOMTable          As BomTableAnnotation

                    Dim vNotes()            As Note

                    Dim autoballoonParams   As AutoBalloonOptions

                   

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                   

                    'Check if a file is open

                    If swModel Is Nothing Then

                        MsgBox ("Please open an assembly and run the macro again.")

                        Exit Sub

                    End If

                    'Check if a drawing is open

                    If Not swModel.GetType = swDocASSEMBLY Then

                        MsgBox ("Please open an assembly and run the macro again.")

                        Exit Sub

                    End If

                    

                    ' Save assembly file as .stp

                    longstatus = swModel.SaveAs3("V:\Engineering\AMC Tool Library\Tool Assemblies\TA-1001\TA-1001.STEP", 0, 0)

                   

                    ' Create drawing

                    Set swDraw = swApp.NewDocument("V:\Engineering\Software\Solidworks\TATA_Templates\Tooling_Drawing.drwdot", 12, 0.21, 0.297)

                    Set swModel = swApp.ActiveDoc

                    

                    'Load Drawing View

                    Set swView = swDraw.CreateDrawViewFromModelView3("V:\Engineering\AMC Tool Library\Tool Assemblies\TA-1001\TA-1001.SLDASM", "Current Model View", 0#, 0#, 0)

                 

                    ' Bill of materials

                    Set swBOMTable = swView.InsertBomTable4(False, 0, 0, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, swBomType_e.swBomType_Indented, "Default", "V:\Engineering\Software\Solidworks\TATA_Templates\bomtooling.sldbomtbt", False, swNumberingType_e.swNumberingType_Detailed, False)

                    boolstatus = swModel.EditRebuild3()

                    

                    ' Auto Baloon

                    boolstatus = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.13842553485699, 0.137862387566601, 0, False, 0, Nothing, 0)

                    Set autoballoonParams = swDraw.CreateAutoBalloonOptions()

                   

                    autoballoonParams.Layout = 1

                    autoballoonParams.ReverseDirection = False

                    autoballoonParams.IgnoreMultiple = True

                    autoballoonParams.InsertMagneticLine = True

                    autoballoonParams.LeaderAttachmentToFaces = False

                    autoballoonParams.Style = 1

                    autoballoonParams.Size = 2

                    autoballoonParams.EditBalloonOption = 1

                    autoballoonParams.EditBalloons = 1

                    autoballoonParams.UpperTextContent = 1

                    autoballoonParams.UpperText = """"

                    autoballoonParams.Layername = "-None-"

                    autoballoonParams.ItemNumberStart = 1

                    autoballoonParams.ItemNumberIncrement = 1

                    autoballoonParams.ItemOrder = 0

                   

                    vNotes = swDraw.AutoBalloon5(autoballoonParams)

                End Sub

                  • Re: Drawing Macro
                    Mathew Challinor

                    Peter,

                    Thank you for tiding it up.

                     

                    my last few issues are that it does not insert a BOM table, however I don't get any errors this time. Also when I bring the drawing view in, it’s now positioned off the page and I still need it to come in with "import annotation" and "design annotations".

                     

                    Thanks for the help

                    Mat

                      • Re: Drawing Macro
                        Peter Brinkhuis

                        It did work here, but I picked either the standard BOM template or I supplied an empty string to the function. Could you try that?

                          • Re: Drawing Macro
                            Mathew Challinor

                            Peter,

                             

                            I created a new BomTemp and ran it off the C drive and it worked fine, I need to explore if its an issue pulling the template from a network drive. The Last niggles I have, can I get the table to attach to the anchor point in my template (sorted this issue if I set the use anchor type to True). when it creates a drawing from my template can I get it to set the drawing name, like you do when you "create drawing from assembly"? and finally I want to clear selection before End Sub I tried I few methods but it returned errors.

                             

                            Appreciate the help. this is the macro so far, working as I want with a few tweaks needed.

                             

                             

                            Sub main()
                                Dim swApp               As SldWorks.SldWorks
                                Dim swModel             As ModelDoc2
                                Dim swAssy              As AssemblyDoc
                                Dim swDraw              As DrawingDoc
                               
                                Dim swView              As View
                                Dim boolstatus          As Boolean
                                Dim longstatus          As Long, longwarnings As Long
                                Dim swBOMTable          As BomTableAnnotation
                                Dim vNotes()            As Note
                                Dim autoballoonParams   As AutoBalloonOptions
                               
                                Dim FilePath            As String
                                Dim PathSize            As Long
                                Dim PathNoExtention     As String
                               
                               
                                Set swApp = Application.SldWorks
                                Set swModel = swApp.ActiveDoc
                               
                               
                                'Check if a file is open
                                If swModel Is Nothing Then
                                    MsgBox ("Please open an assembly and run the macro again.")
                                    Exit Sub
                                End If
                               
                                'Check if a drawing is open
                                If Not swModel.GetType = swDocASSEMBLY Then
                                    MsgBox ("Please open an assembly and run the macro again.")
                                    Exit Sub
                                End If
                                
                                
                                 'Get file path with no extention
                                 FilePath = swModel.GetPathName
                                 PathSize = Strings.Len(FilePath)
                                 PathNoExtention = Strings.Left(FilePath, PathSize - 7)
                                  
                                
                                'Save assembly file as .stp
                                Set swModel = swApp.ActiveDoc
                                longstatus = swModel.SaveAs3(PathNoExtention & ".STEP", 0, 0)
                               
                                ' Create drawing
                               
                                Set swDraw = swApp.NewDocument("V:\Engineering\Software\Solidworks\TATA_Templates\Tooling_Drawing.drwdot", 12, 0.21, 0.297)
                                Set swModel = swApp.ActiveDoc
                                
                                'Load Drawing View and annotations
                               
                                Set swView = swDraw.CreateDrawViewFromModelView3(FilePath, "Current Model View", 0.146409898581714, 0.11700997510004, 0)
                                swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 32776, True, True, False, False
                              
                               
                                ' Bill of materials
                               
                                Set swBOMTable = swView.InsertBomTable4(False, 0, 0, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight, swBomType_e.swBomType_Indented, "Default", "C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\TestBom.sldbomtbt", False, swNumberingType_e.swNumberingType_Detailed, False)
                                boolstatus = swModel.EditRebuild3()
                               
                                
                                ' Auto Baloon
                                boolstatus = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.13842553485699, 0.137862387566601, 0, False, 0, Nothing, 0)
                                Set autoballoonParams = swDraw.CreateAutoBalloonOptions()
                               
                                autoballoonParams.Layout = 1
                                autoballoonParams.ReverseDirection = False
                                autoballoonParams.IgnoreMultiple = True
                                autoballoonParams.InsertMagneticLine = True
                                autoballoonParams.LeaderAttachmentToFaces = False
                                autoballoonParams.Style = 1
                                autoballoonParams.Size = 2
                                autoballoonParams.EditBalloonOption = 1
                                autoballoonParams.EditBalloons = 1
                                autoballoonParams.UpperTextContent = 1
                                autoballoonParams.UpperText = """"
                                autoballoonParams.Layername = "-None-"
                                autoballoonParams.ItemNumberStart = 1
                                autoballoonParams.ItemNumberIncrement = 1
                                autoballoonParams.ItemOrder = 0
                               
                                vNotes = swDraw.AutoBalloon5(autoballoonParams)
                              

                               
                            End Sub

                             

                             

                            Regards Mat

                              • Re: Drawing Macro
                                Peter Brinkhuis

                                This should do the tricks that you requested. You can get the title from the assembly and then use it again for the drawing. For clearing the selection I've copied the line from you macro code, that should work. In the part where you create the STEP file, I've removed the line swmodel=swapp.activedoc because swmodel was already set.

                                 

                                Option Explicit

                                 

                                Sub main()

                                    Dim swApp               As SldWorks.SldWorks

                                    Dim swModel             As ModelDoc2

                                    Dim swAssy              As AssemblyDoc

                                    Dim swDraw              As DrawingDoc

                                   

                                    Dim swView              As View

                                    Dim boolstatus          As Boolean

                                    Dim longstatus          As Long, longwarnings As Long

                                    Dim swBOMTable          As BomTableAnnotation

                                    Dim vNotes()            As Note

                                    Dim autoballoonParams   As AutoBalloonOptions

                                   

                                    Dim FilePath            As String

                                    Dim PathSize            As Long

                                    Dim PathNoExtention     As String

                                    Dim sPartName           As String

                                   

                                    Set swApp = Application.SldWorks

                                    Set swModel = swApp.ActiveDoc

                                   

                                    'Check if a file is open

                                    If swModel Is Nothing Then

                                        MsgBox ("Please open an assembly and run the macro again.")

                                        Exit Sub

                                    End If

                                   

                                    'Check if a drawing is open

                                    If Not swModel.GetType = swDocASSEMBLY Then

                                        MsgBox ("Please open an assembly and run the macro again.")

                                        Exit Sub

                                    End If

                                    

                                     'Get file path with no extention

                                     FilePath = swModel.GetPathName

                                     PathSize = Strings.Len(FilePath)

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

                                      

                                    'Save assembly file as .stp

                                    longstatus = swModel.SaveAs3(PathNoExtention & ".STEP", 0, 0)

                                   

                                    sPartName = swModel.GetTitle

                                   

                                    ' Create drawing

                                    Set swDraw = swApp.NewDocument("V:\Engineering\Software\Solidworks\TATA_Templates\Tooling_Drawing.drwdot", 12, 0.21, 0.297)

                                    Set swModel = swApp.ActiveDoc

                                    swModel.SetTitle2 (sPartName)

                                    

                                    'Load Drawing View and annotations

                                    Set swView = swDraw.CreateDrawViewFromModelView3(FilePath, "Current Model View", 0.146409898581714, 0.11700997510004, 0)

                                    swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 32776, True, True, False, False

                                  

                                    ' Bill of materials

                                    Set swBOMTable = swView.InsertBomTable4(False, 0, 0, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight, swBomType_e.swBomType_Indented, "Default", "C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\TestBom.sldbomtbt", False, swNumberingType_e.swNumberingType_Detailed, False)

                                    boolstatus = swModel.EditRebuild3()

                                   

                                    ' Auto Baloon

                                    boolstatus = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.13842553485699, 0.137862387566601, 0, False, 0, Nothing, 0)

                                    Set autoballoonParams = swDraw.CreateAutoBalloonOptions()

                                   

                                    autoballoonParams.Layout = 1

                                    autoballoonParams.ReverseDirection = False

                                    autoballoonParams.IgnoreMultiple = True

                                    autoballoonParams.InsertMagneticLine = True

                                    autoballoonParams.LeaderAttachmentToFaces = False

                                    autoballoonParams.Style = 1

                                    autoballoonParams.Size = 2

                                    autoballoonParams.EditBalloonOption = 1

                                    autoballoonParams.EditBalloons = 1

                                    autoballoonParams.UpperTextContent = 1

                                    autoballoonParams.UpperText = """"

                                    autoballoonParams.Layername = "-None-"

                                    autoballoonParams.ItemNumberStart = 1

                                    autoballoonParams.ItemNumberIncrement = 1

                                    autoballoonParams.ItemOrder = 0

                                   

                                    vNotes = swDraw.AutoBalloon5(autoballoonParams)

                                     

                                    swModel.ClearSelection2 True

                                   

                                End Sub

                        • Re: Drawing Macro
                          Mathew Challinor

                          I have the macro working now how I want. The only issue is it wont insert the BOM table. Can anyone help?

                           

                          ' Bill of materials

                            

                              Set swBOMTable = swView.InsertBomTable4(False, 0, 0, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight, swBomType_e.swBomType_Indented, "Default", "V:\Engineering\Software\Solidworks\TATA_Templates\bomtooling.sldbomtbt", False, swNumberingType_e.swNumberingType_Detailed, False)

                              boolstatus = swModel.EditRebuild3()

                           

                          Regards

                          Mat