13 Replies Latest reply on Dec 18, 2017 9:55 AM by Kishore K.

    Need a macro for Assembly drawing

    Kishore K.

      Hi,

       

      Does any one have macro for creating drawing from assembly

       

      For example : Assembly contains 5+ components, It has to create Isometric view in  Sheet1 and rest of the each components should be in Sheet 2 , Sheet 3...With standard 3 views and save in same location  where assembly file located.

        • Re: Need a macro for Assembly drawing
          Deepak Gupta

          Kishore, I do not think there is any such macro available. You can use #TASK tool to create individual drawings and then combine into one drawing.

          • Re: Need a macro for Assembly drawing
            Fatih Mehmet Ozcan

            Hello

            This is not perfect at all but it's something. You need to configure drawing template and new sheets.

             

            Option Explicit
            Dim swApp As SldWorks.SldWorks

             

            Sub main()

             

            Set swApp = Application.SldWorks
            Dim swmodel As SldWorks.ModelDoc2
            Set swmodel = swApp.ActiveDoc
            If swmodel Is Nothing Then Exit Sub

             


            Dim swcomponent As SldWorks.Component2
            Dim swdraw As DrawingDoc
            Dim swassy As AssemblyDoc
            Dim com
            Dim templatepath As String
            Dim assypathlist() As Variant
            Dim k As Integer
            Dim i As Integer
            Dim composcount As Integer

             

            Set swassy = swmodel
            composcount = swassy.GetComponentCount(True)

             

            'get all component full paths and put them in a list
            For Each com In swassy.GetComponents(True)
                k = k + 1
                'MsgBox com.Name
                ReDim Preserve assypathlist(1 To k)
                assypathlist(k) = com.GetPathName
                'MsgBox assypathlist(k)
            Next com

             


            Dim shcount As Variant
            Dim sh As SldWorks.Sheet
            Dim j As Integer
            Dim boolstatus As Boolean

             

            templatepath = swApp.GetUserPreferenceStringValue(swDefaultTemplateDrawing)
            Set swdraw = swApp.NewDocument(templatepath, 0, 0, 0)
            'new drawing is created then views are inserted
            swdraw.Create3rdAngleViews2 assypathlist(LBound(assypathlist))

             

            shcount = swdraw.GetSheetCount
            For j = shcount To composcount - 1
                'creates a new sheet and inserts views
                boolstatus = swdraw.NewSheet4("Sheet" & j + 1, swDwgPaperAsize, swDwgTemplateAsize, 1, 1, True, "", 0, 0, "", 0.5, 0.5, 0.5, 0.5, 4, 4)
                swdraw.Create3rdAngleViews2 assypathlist(j + 1)
            Next j

             

              

             

            End Sub

            • Re: Need a macro for Assembly drawing
              Kishore K.

              What is code for inserting  Isometric view for Individual component in each sheet?

              Fatih Mehmet OzcanDeepak Gupta

               

              For j = shcount To composcount - 1

                  'creates a new sheet and inserts views

                  boolstatus = swdraw.NewSheet4("Sheet" & j + 1, swDwgPaperAsize, swDwgTemplateAsize, 1, 1, True, "", 0, 0, "", 0.5, 0.5, 0.5, 0.5, 4, 4)

              swdraw.CreateDrawViewFromModelView3(Filename, "*Isometric", 0, 0, 0)

                  swdraw.Create3rdAngleViews2 assypathlist(j + 1)

              Next j

               

              By using this swdraw.CreateDrawViewFromModelView3(Filename, "*Isometric", 0, 0, 0)

              I'm getting an isometric view of whole assembly

               

              Then I need to align isometric view in the right top corner of the template

              Is that possible to make with this macro?

               

               

              Dim view As view

              Dim vOutline As Variant, vPosition As Variant

              Dim viewWidth As Double, viewHeight As Double

               

               

              Set view = swdraw.CreateDrawViewFromModelView3(Filename, "*Isometric", 0, 0, 0)

               

               

              ' Now get view size and move left and down to bring into sheet

              vOutline = view.GetOutline

              vPosition = view.Position

              viewWidth = vOutline(2) - vOutline(0)

              viewHeight = vOutline(3) - vOutline(1)

               

               

              vPosition(0) = vPosition(0) - viewWidth

              vPosition(1) = vPosition(1) - viewHeight

              view.Position = vPosition

               

              Thanks

                • Re: Need a macro for Assembly drawing
                  Fatih Mehmet Ozcan

                  When using CreateDrawViewFromModelView3 you can set view locations as required

                   

                  I did some changes

                  I used A3 paper size

                  Copied a template and sheet format files to desktop, if you copy yours too it will work without any change or input

                  It sets your isometric views' center to 50mm from top and right edge to right top corner

                  I hope I didn't miss anything you asked for

                   

                  Option Explicit
                  Dim swApp As SldWorks.SldWorks
                  Sub main()

                   

                  Set swApp = Application.SldWorks
                  Dim swmodel As SldWorks.ModelDoc2
                  Set swmodel = swApp.ActiveDoc
                  Dim swext As SldWorks.ModelDocExtension
                  Set swext = swmodel.Extension

                   

                  If swmodel Is Nothing Then
                  ' If no model is currently loaded, then exit
                  Exit Sub
                  ElseIf (swmodel.GetType <> swDocASSEMBLY) Then
                  swApp.SendMsgToUser ("Open an assembly please")
                  Exit Sub
                  End If

                   


                  Dim swcomponent As SldWorks.Component2
                  Dim swdraw As DrawingDoc
                  Dim swassy As AssemblyDoc
                  Dim com As Variant
                  Dim templatepath As String
                  Dim assypathlist() As Variant

                   

                  Dim k As Integer
                  Dim i As Integer
                  Dim composcount As Integer

                   

                  Set swassy = swmodel
                  composcount = swassy.GetComponentCount(True)

                   

                  For Each com In swassy.GetComponents(True)
                      k = k + 1
                      'MsgBox com.Name
                      ReDim Preserve assypathlist(1 To k)
                      assypathlist(k) = com.GetPathName
                      'MsgBox assypathlist(k)
                  Next com

                   


                  Dim shcount As Variant
                  Dim sh As SldWorks.Sheet
                  Dim j As Integer
                  Dim boolstatus As Boolean

                   

                  templatepath = Environ("userprofile") + "\Desktop\Draw-template.slddrw"
                  Set swdraw = swApp.NewDocument(templatepath, 0, 0, 0)
                  Set swdraw = swApp.ActiveDoc
                  Set sh = swdraw.GetCurrentSheet
                  swext.ViewZoomToSheet

                   

                  Dim sheetheight As Double
                  Dim sheetwidth As Double
                  Dim longwar As Long
                  sh.GetSize sheetwidth, sheetheight

                   

                  'checking automatic scale setting in solidworks system options, disabling if it's on
                  Dim checksetting As Boolean
                  If swApp.GetUserPreferenceToggle(swAutomaticScaling3ViewDrawings) = True Then
                  checksetting = True
                  swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swAutomaticScaling3ViewDrawings, False
                  End If

                   

                  'swdraw.Create3rdAngleViews2 assypathlist(LBound(assypathlist))

                   

                  'creates isometric view to specified location
                  swdraw.CreateDrawViewFromModelView3 assypathlist(LBound(assypathlist)), "*Isometric", sheetwidth - 0.05, sheetheight - 0.05, 0

                   

                  shcount = swdraw.GetSheetCount
                  For j = shcount To composcount - 1
                      'creates a new sheet and inserts views
                      boolstatus = swdraw.NewSheet4("Sheet" & j + 1, swDwgPaperA3size, swDwgTemplateCustom, 1, 1, False, Environ("userprofile") + "\Desktop\a3 - landscape.slddrt", 0#, 0#, 0#, 0#, 0#, 0#, 0#, 0#, 0#)
                  swext.ViewZoomToSheet
                      'swdraw.Create3rdAngleViews2 assypathlist(j + 1)
                      swdraw.CreateDrawViewFromModelView3 assypathlist(j + 1), "*Isometric", sheetwidth - 0.05, sheetheight - 0.05, 0
                  Next j

                   

                  'set automatic scale option true if it was on at the beginning
                  If checksetting = True Then
                  swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swAutomaticScaling3ViewDrawings, True
                  End If

                   

                  End Sub