Hide Table of Contents

Save Drawing as DXF Example (C#)

This example shows how to save the current drawing file as a DXF file in the same folder.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Open a drawing.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Gets and sets DXF-related system settings.
// 2. Saves the drawing as a DXF file in the same folder as the drawing,
//    overwriting any existing file of the same name.
// 3. Examine the Immediate window and the folder to which the DXF file
//    was saved.
//----------------------------------------------------------------------------


using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
namespace SaveDrawingAsDXF_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
ModelDoc2 swModel;
        
string sPathName;
        
int nErrors;
        
int nWarnings;
        
int nRetval;
        
bool bShowMap;

        
bool bRet;

        
public void Main()
        {
            swModel = (
ModelDoc2)swApp.ActiveDoc;

            
// Strip off SOLIDWORKS drawing file extension (.slddrw)
            // and add DXF file extension (.dxf)
            sPathName = swModel.GetPathName();
            sPathName = sPathName.Substring(0, sPathName.Length - 6);
            sPathName = sPathName +
"dxf";
            
Debug.Print(sPathName);

            
// Show current settings
            Debug.Print("DxfMapping             = " + swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swDxfMapping));
            
Debug.Print("DXFDontShowMap         = " + swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swDXFDontShowMap));
            
Debug.Print("DxfVersion             = " + swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfVersion));
            
Debug.Print("DxfOutputFonts         = " + swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfOutputFonts));
            
Debug.Print("DxfMappingFileIndex    = " + swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfMappingFileIndex));
            
Debug.Print("DxfOutputLineStyles    = " + swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfOutputLineStyles));
            
Debug.Print("DxfOutputNoScale       = " + swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfOutputNoScale));
            
Debug.Print("DxfMappingFiles        = " + swApp.GetUserPreferenceStringListValue((int)swUserPreferenceStringListValue_e.swDxfMappingFiles));
            
Debug.Print("DxfOutputScaleFactor   = " + swApp.GetUserPreferenceDoubleValue((int)swUserPreferenceDoubleValue_e.swDxfOutputScaleFactor));
            
Debug.Print("");

            
// Turn off showing of map
            bShowMap = swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swDXFDontShowMap);
            
Debug.Print("bShowMap = " + bShowMap);

            swApp.SetUserPreferenceToggle((
int)swUserPreferenceToggle_e.swDXFDontShowMap, false);

            bRet = swModel.SaveAs4(sPathName, (
int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, ref nErrors, ref nWarnings);

            
if (bRet == false)
            {
                nRetval = swApp.SendMsgToUser2(
"Problems saving file.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
            }

            
// Restore showing of map
            swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swDXFDontShowMap, bShowMap);

        }


        
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:   Save Drawing as DXF 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) 2018 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.