AnsweredAssumed Answered

Inserting model views into a drawing (VBA)

Question asked by Ian Farquharson on Aug 9, 2018
Latest reply on Aug 13, 2018 by Ian Farquharson

I have been trying to use this bit of code to automate my drawing process:

 

'-----------------------------------------------------------------------

' Preconditions:

' 1. Open public_documents\samples\tutorial\advdrawings\blade shaft.sldasm.

' 2. Click File > Make Drawing from Assembly > OK.

' 3. Click a blank area of the FeatureManager design tree.

'

' Postconditions:

' 1. Drops the Current, Isometric, and Front views onto

' the drawing sheet.

' 2. Examine the drawing and Immediate window.

'-----------------------------------------------------------------------

Option Explicit

Dim swApp As SldWorks.SldWorks
Sub main()
    Dim swModel As SldWorks.ModelDoc2
    Dim swDrawing As SldWorks.DrawingDoc
    Dim swFeature As SldWorks.Feature
    Dim swSubFeature As SldWorks.Feature
    Dim swSheet As SldWorks.Sheet
    Dim swView As SldWorks.View
    Dim bFound As Boolean
    Dim vViewNames As Variant
    Dim vViewName As Variant
    Dim strViewName As String
    Dim lNumViews As Long
    Dim dSheetScale As Double
    Dim vSheetProperties As Variant
    Dim nPaperSize As swDwgPaperSizes_e
    Dim dWidth As Double
    Dim dHeight As Double   
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDrawing = swModel
    ' Get current sheet
    Set swSheet = swDrawing.GetCurrentSheet
    vSheetProperties = swSheet.GetProperties
    dSheetScale = vSheetProperties(2) / vSheetProperties(3)
    nPaperSize = swSheet.GetSize(dWidth, dHeight)
    Debug.Print "Sheet scale  = " & dSheetScale
    Debug.Print "Sheet width  = " & dWidth
    Debug.Print "Sheet height = " & dHeight
    lNumViews = 0
    vViewNames = swDrawing.GetDrawingPaletteViewNames
    If (Not (IsEmpty(vViewNames))) Then
        lNumViews = (UBound(vViewNames) - LBound(vViewNames) + 1)
        Debug.Print "Number of views on palette = " & lNumViews
        For Each vViewName In vViewNames
            strViewName = vViewName
            Debug.Print strViewName
            If (strViewName = "*Current") Then
                Set swView = swDrawing.DropDrawingViewFromPalette2(strViewName, dWidth / 5#, dHeight / 5#, 0#)
                Debug.Print "  Dropped => " & swView.Name
            End If
            If (strViewName = "*Isometric") Then
                Set swView = swDrawing.DropDrawingViewFromPalette2(strViewName, dWidth / 3#, dHeight / 2#, 0#)
                Debug.Print "  Dropped => " & swView.Name
            End If
            If (strViewName = "*Front") Then
                Set swView = swDrawing.DropDrawingViewFromPalette2(strViewName, dWidth / 4#, dHeight / 3#, 0#)
                Debug.Print "  Dropped => " & swView.Name
            End If
        Next vViewName
    End If
End Sub

 

 

This bit of code works great, if you make drawing from part/assembly.

 

My workplace's process is to have a single drawing file with multiple sheets with each assembly and child component that has any extra operations on it other than a straight cut. This means that if I use the above code per each sheet on the drawing, I can only put that model on those sheets because the "DropDrawingViewFromPalette2" only chooses the model that the drawing was created from.

 

Another note is that the above code does not work if you open a new drawing, then open the model to place in the drawing, and then run the code.

 

Is this possible to do? I have already made a macro to create a drawing and add the correct amount of sheets/ names based on sub assemblies/ parts.

 

Thanks,

Ian

 

EDIT:

 

I found that using predefined views works: swDraw.InsertModelInPredefinedView swCurModel.GetPathName(). I just don't know if there is a way to add predefined views to a new sheet, or add them to the sheet format. My current plan is to copy a "Source Sheet" and paste it over and over until I have all the sheets are there that I need. At the end of the macro I will delete it.

Outcomes