Hide Table of Contents

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

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.
// 3. Ensure the namespace matches the name of your C# project.
//
// 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.
//---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace InsertDXFDrawing_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
public void Main()
        {
            
const string sDwgFileName = "DXF_file_path";

            
ModelDoc2 swModel = default(ModelDoc2);
            
ModelView swModelView = default(ModelView);
            
DrawingDoc swDraw = default(DrawingDoc);
            
FeatureManager swFeatMgr = default(FeatureManager);
            
Feature swFeat = default(Feature);
            
Sketch swSketch = default(Sketch);
            
View swView = default(View);
            
double[] vPos = null;
            
bool bRet = false;
            
ImportDxfDwgData importData = default(ImportDxfDwgData);

            swModel = (
ModelDoc2)swApp.ActiveDoc;
            swModelView = (
ModelView)swModel.ActiveView;

            bRet = swModel.Extension.SelectByID2(
"Sheet1", "SHEET", 0.0, 0.0, 0, false, 0, null, 0);

            swDraw = (
DrawingDoc)swModel;
            swFeatMgr = swModel.FeatureManager;
            importData = (
ImportDxfDwgData)swApp.GetImportFileData(sDwgFileName);

            
//Unit
            importData.set_LengthUnit("", (int)swLengthUnit_e.swINCHES);

            
//Position
            bRet = importData.SetPosition("", (int)swDwgImportEntitiesPositioning_e.swDwgEntitiesCentered, 0, 0);

            
//Sheet scale
            bRet = importData.SetSheetScale("", 1.0, 2.0);

            
//Paper size
            bRet = importData.SetPaperSize("", (int)swDwgPaperSizes_e.swDwgPaperAsize, 0.0, 0.0);

            
//Import method
            importData.set_ImportMethod("", (int)swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingDrawing);

            
//Import file with importData
            swFeat = swFeatMgr.InsertDwgOrDxfFile2(sDwgFileName, importData);
            swSketch = (
Sketch)swFeat.GetSpecificFeature2();

            swView = (
View)swDraw.GetFirstView();

            
while ((swView != null))
            {
                
if (object.ReferenceEquals(swSketch, swView.GetSketch()))
                {
                    
break;
                }
                swView = (
View)swView.GetNextView();
            }

            vPos = (
double[])swView.Position;

            
Debug.Print("File = " + swModel.GetPathName());
            
Debug.Print("  Sketch       = " + swFeat.Name);
            
Debug.Print("  View         = " + swView.Name);
            
Debug.Print("    Old Pos    = (" + vPos[0] * 1000.0 + ", " + vPos[1] * 1000.0 + ") mm");

            
// Move to right
            vPos[0] = vPos[0] + 0.01;
            swView.Position = vPos;

            vPos = (
double[])swView.Position;
            
Debug.Print("    New Pos    = (" + vPos[0] * 1000.0 + ", " + vPos[1] * 1000.0 + ") mm");

            
// Redraw
            double[] rect = null;
            rect =
null;
            swModelView.GraphicsRedraw(rect);

        }

        
public SldWorks swApp;

    }
}



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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Insert and Position DXF/DWG File in Drawing Example (C#)
*Comment:  
*   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:




x

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
x

Web Help Content Version: API Help (English only) 2014 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.