VBA macro to create flat pattern drawing view form the multi-body sheet metal part

Edit ArticleEdit Article

This VBA example demonstrates how to create flat pattern view of a selected body from the multi-body sheet metal part.

When performing this operation manually from SOLIDWORKS, it is required to insert a drawing view of the full part, then select the single sheet metal body and set the view to Flat Pattern. In order to produce similar result from the API, different steps need to be performed. It is required to select the body from the visible source document before calling the IDrawingDoc::CreateFlatPatternViewFromModelView3 API method.

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As SldWorks.ModelDoc2
    
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
    
        Dim swSelMgr As SldWorks.SelectionMgr
        
        Set swSelMgr = swModel.SelectionManager
        
        Dim swBody As SldWorks.Body2
        
        Set swBody = swSelMgr.GetSelectedObject6(1, -1)
        
        If Not swBody Is Nothing Then
                        
            swBody.Select2 False, Nothing
            
            Dim templatePath As String
            templatePath = swApp.GetDocumentTemplate(swDocumentTypes_e.swDocDRAWING, "", swDwgPaperSizes_e.swDwgPaperA4size, 0, 0)
            
            Dim swDraw As SldWorks.DrawingDoc
            Set swDraw = swApp.NewDocument(templatePath, swDwgPaperSizes_e.swDwgPaperA4size, 0, 0)
            
            Dim swView As SldWorks.View
            
            Set swView = swDraw.CreateFlatPatternViewFromModelView3(swModel.GetPathName(), "", 0, 0, 0, False, False)
            
        Else
            Err.Raise vbError, "", "Body is not selected"
        End If
    
    Else
        Err.Raise vbError, "", "Open part document"
    End If
    
End Sub


Product of Xarial Product of Xarial