Hide Table of Contents

Insert and Position DXF/DWG File in Drawing Example (VBA)

This example shows how to insert and position a DXF/DWG file in a drawing.

' Preconditions:
' 1. Open a drawing.
' 2. Replace DXF_file_path with the pathname of an existing DXF/DWG file.
' Postconditions:
' 1. Inspect the Immediate window.
' 2. The DXF/DWG file is inserted as per the specified import data.
' 3. The drawing is moved to the right.


Option Explicit

Sub main()

    Const sDwgFileName                  As String = "DXF_file_path"

    Dim swApp                           As SldWorks.SldWorks
    Dim swModel                         As SldWorks.ModelDoc2
    Dim swModelView                     As SldWorks.ModelView
    Dim swDraw                          As SldWorks.DrawingDoc
    Dim swFeatMgr                       As SldWorks.FeatureManager
    Dim swFeat                          As SldWorks.Feature
    Dim swSketch                        As SldWorks.Sketch
    Dim swView                          As SldWorks.View
    Dim vPos                            As Variant
    Dim bRet                            As Boolean
    Dim importData                      As SldWorks.ImportDxfDwgData

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelView = swModel.ActiveView

    bRet = swModel.Extension.SelectByID2("Sheet1", "SHEET", 0#, 0#, 0, False, 0, Nothing, 0)

    Set swDraw = swModel
    Set swFeatMgr = swModel.FeatureManager
    Set importData = swApp.GetImportFileData(sDwgFileName)

    importData.LengthUnit("") = SwConst.swLengthUnit_e.swINCHES

    bRet = importData.SetPosition("", swDwgEntitiesCentered, 0, 0)


    'Sheet scale
    bRet = importData.SetSheetScale("", 1#, 2#)

    'Paper size
    bRet = importData.SetPaperSize("", SwConst.swDwgPaperSizes_e.swDwgPaperAsize, 0#, 0#)

    'Import method
    importData.ImportMethod("") = swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingDrawing


    'Import file with importData
    Set swFeat = swFeatMgr.InsertDwgOrDxfFile2(sDwgFileName, importData)

    Set swSketch = swFeat.GetSpecificFeature2

    Set swView = swDraw.GetFirstView

    Do While Not swView Is Nothing
        If swSketch Is swView.GetSketch Then
            Exit Do
        End If
        Set swView = swView.GetNextView

    vPos = swView.Position

    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  Sketch       = " & swFeat.Name
    Debug.Print "  View         = " & swView.Name
    Debug.Print "    Old Pos    = (" & vPos(0) * 1000# & ", " & vPos(1) * 1000# & ") mm"

    ' Move to right
    vPos(0) = vPos(0) + 0.01
    swView.Position = vPos

    vPos = swView.Position
    Debug.Print "    New Pos    = (" & vPos(0) * 1000# & ", " & vPos(1) * 1000# & ") mm"

    ' Redraw
    Dim rect() As Double
    swModelView.GraphicsRedraw rect

End Sub

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Insert and Position DXF/DWG File in Drawing Example VB
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.