.NET

Reply
Active Member
baw6718
Posts: 6
Registered: ‎09-25-2012
Message 1 of 2 (245 Views)
Accepted Solution

Outputting a dxf file from an active drawing.

245 Views, 1 Replies
10-18-2012 02:52 PM

I have several closed ployline shapes  in a particular layer, I need to know a way to:

 

a) pick all the entites on that layer

b) export to a dxf file each individual entity on the layer (yes, each entity saved to a separate dxf)

 

Any help would be great. I am almost positive I can answer 'a' on my own using an object collection and a conditional statement? I am really more interested in how to export an entity from current open drawing to a dxf file in .net

Try this code:

 

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(DxfOut.MyCommands))]
namespace DxfOut
{
public class MyCommands
{
[CommandMethod("MyDxfOut")]
public static void RunMyCommand()
{
Document dwg = Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
PromptSelectionResult res = ed.GetSelection();
if (res.Status != PromptStatus.OK)
{
ed.WriteMessage("\n*Cancel*");
return;
}
string folderPath = @"C:\Temp\ExportedFiles";
try
{
ExportEntityToDxf(dwg.Database, res.Value.GetObjectIds(), folderPath);
ed.WriteMessage("\nSelected entities exported");
}
catch (System.Exception ex)
{
ed.WriteMessage("\nError: {0}", ex.ToString());
ed.WriteMessage("\n*Cancel*");
}
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
private static void ExportEntityToDxf(
Database sourceDB, ObjectId[] entIds, string folderName)
{
int i=0;
foreach (ObjectId id in entIds)
{
i++;
string fileName =
folderName + "\\Test" + i + "_" + id.ObjectClass.DxfName + ".dxf";
if (File.Exists(fileName)) File.Delete(fileName);
using (Database db = new Database(true, true))
{
ObjectIdCollection ids = new ObjectIdCollection(new ObjectId[]{id});
sourceDB.Wblock(db, ids, Point3d.Origin, DuplicateRecordCloning.Ignore);
db.DxfOut(fileName, 16, DwgVersion.Current);
}
}
}
}
}

 I use AutoCAD2012. I started a drawing, drew a few entities (lines, circles...), placed them in different layers, colors. Then run the command "MyDxfOut". I ended up with a bunch of *.dxf files in C:\temp folder, which I opened in AutoCAD, and saw each Dxf file has a single entity with its layer/color being the same as the source entity.

 

HTH.

*Expert Elite*
norman.yuan
Posts: 1,049
Registered: ‎04-27-2009
Message 2 of 2 (221 Views)

Re: Outputting a dxf file from an active drawing.

10-19-2012 03:22 PM in reply to: baw6718

Try this code:

 

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(DxfOut.MyCommands))]

namespace DxfOut
{
    public class MyCommands 
    {
        [CommandMethod("MyDxfOut")]
        public static void RunMyCommand()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            PromptSelectionResult res = ed.GetSelection();
            if (res.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\n*Cancel*");
                return;
            }

            string folderPath = @"C:\Temp\ExportedFiles";

            try
            {
                ExportEntityToDxf(dwg.Database, res.Value.GetObjectIds(), folderPath);
                ed.WriteMessage("\nSelected entities exported");
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.ToString());
                ed.WriteMessage("\n*Cancel*");
            }

            
            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

        private static void ExportEntityToDxf(
            Database sourceDB, ObjectId[] entIds, string folderName)
        {
            int i=0;

            foreach (ObjectId id in entIds)
            { 
                i++;
                string fileName = 
                    folderName + "\\Test" + i + "_" + id.ObjectClass.DxfName + ".dxf";

                if (File.Exists(fileName)) File.Delete(fileName);

                using (Database db = new Database(true, true))
                {
                    ObjectIdCollection ids = new ObjectIdCollection(new ObjectId[]{id});
                    sourceDB.Wblock(db, ids, Point3d.Origin, DuplicateRecordCloning.Ignore);
                    db.DxfOut(fileName, 16, DwgVersion.Current);
                }
            }
        }
    }
}

 I use AutoCAD2012. I started a drawing, drew a few entities (lines, circles...), placed them in different layers, colors. Then run the command "MyDxfOut". I ended up with a bunch of *.dxf files in C:\temp folder, which I opened in AutoCAD, and saw each Dxf file has a single entity with its layer/color being the same as the source entity.

 

HTH.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!