ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
RGRyan Gaudreau11/09/2017

I've been working on a macro to automate drawings from an assembly.  The overall goal is for the macro to store the filepaths and names of all components in the assembly, and then start a new drawing, creating and populating the sheets with components in the three standard views.  I have it working where it will store all the filepaths and names, open a new drawing, and drop components into the first two sheets (the first two sheets are in the template), but fails at creating any new sheets.  I'm not too familiar with the drawing commands, and things seem to work differently for me than what is described in the API help. For example, "create3rdangleviews" command won't work with my drawingdoc instance but will with my modeldoc instance.  If anyone can help me with this it would be greatly appreciated.  I have my code posted below.  I really just need help with the new sheet creation and view insertion, I think once I get that working I will be able to figure out my other problems and goals that I have listed in the notes.  Sorry in advance for my code, it is probably sloppy and inefficient.

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swAssy As SldWorks.AssemblyDoc

Dim swComp As SldWorks.Component2

Dim swCompModel As SldWorks.ModelDoc2

Dim swDoc As SldWorks.ModelDoc2

Dim swDraw As SldWorks.DrawingDoc

Dim swSheet As SldWorks.sheet

Dim boolstatus As Boolean

Dim bRet As Boolean

Dim i As Integer

Dim h As Integer

Dim j As Integer

Dim k As Integer

Dim mypaths(50) As String

Dim mynames(50) As String

Dim mysheet As String

Dim myprops As Object

Dim vComps As Variant

   

Sub main()

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swAssy = swModel

   

    h = 0

   

    vComps = swAssy.GetComponents(False)

   

    If IsEmpty(vComps) Then Exit Sub

   

    'TRAVERSES ASSEMBLY FOR PARTS/SUB ASSEMBLIES

    For i = 0 To UBound(vComps)

        Set swComp = vComps(i)

        Set swCompModel = swComp.GetModelDoc2

                 

            'gets the full pathway for every component in an assembly (not the assembly itself)

            Dim vConfigNames As String

               vConfigNames = swComp.GetPathName

           

            'returns last 3 places in component title (ex 001, 200, ect) --- will be used to name sheets in drawing

            Dim vTitleNames As String

                vTitleNames = swCompModel.GetTitle

                    vTitleNames = Strings.Right(vTitleNames, 3)

                               

            'this stores new names only and skips over duplicate parts

                For j = 0 To UBound(mypaths)

                If vConfigNames = mypaths(j) Then

                GoTo Skip

                                         

                Exit For

                End If

                Next j

                           

            'STORES NEW PARTS, DUPLICATE PARTS SKIP OVER THIS SECTION FROM CODE ABOVE

            mypaths(h) = vConfigNames

            mynames(h) = vTitleNames

            'MsgBox (mypaths(h))

            'MsgBox (mynames(h))

            h = h + 1

Skip:

     

    Next i

   

    'closes assembly document

        'boolstatus = swApp.CloseAllDocuments(False)

'CREATE NEW DRAWING DOCUMENT

    Set swDoc = swApp.NewDocument("C:\SOLIDWORKS Data\PSF-GN.DRWDOT", 1, 0.2794, 0.2159)

    Set swDoc = swApp.ActiveDoc

    'INSERT CURRENT OPEN ASSEMBLY

    'FIRST PAGE NAME IN TEMPLATE = "GN" SECOND PAGE NAME = "2"

   

    'INSERTS VIEW INTO FIRST TWO PAGES, THIS WILL BE LARGE ASSEMBLY

    boolstatus = swDoc.Create3rdAngleViews(mypaths(1))

   

    'ACTIVATE SHEET "2", INSERT MODEL VIEWS

    boolstatus = swDoc.ActivateSheet("2")

    boolstatus = swDoc.Create3rdAngleViews(mypaths(1))

      

    'GET TEMPLATE OF SHEET "2" TO BE USED IN NEWLY CREATED SHEET

    'mysheet = swSheet.GetTemplateName()

   

    'CREATE NEW Sheet, INSERT MODELS FROM MYPATHS(I)

        'trying to go through list of components, create a new sheet for each, and insert model views

        'For k = 0 To UBound(mynames)

  

-----fails here       

            boolstatus = swDraw.NewSheet4(mynames(k), "", 12, "", "", "", "c:\programdata\solidworks\solidworks 2016\lang\english\sheetformat\a0 - landscape.slddrt", "", "", "", "", "", "", "", "", "")

            'boolstatus = swDoc.ActivateSheet(mynames(k))

           

            'boolstatus = swDoc.Create3rdAngleViews(mypaths(k))

            'Next k

  

    'SORT SHEETS BASED ON NUMBERS, THIS WILL PUT ASSEMBLIES FIRST --> METAL PARTS --> WOOD PARTS ECT

      

    'POSSIBLY ADD A CHOICE FUNCTION, ENTER YES IF YOU WANT TO FIT ANOTHER MODEL ONTO SHEET

        ' ENTER NO IF YOU WANT TO CREATE NEW SHEET AND INSERT MODEL

            ' THIS WOULD ALLOW PAGE BY PAGE CONTROL ON THE DRAWING, WOULD NEED TO SET UP ZONES

End Sub