0% found this document useful (0 votes)
35 views4 pages

Creating An AutoCAD To Excel Drafting Application Using C

The document outlines the steps to create an AutoCAD to Excel drafting application using C#, including installation of required references, setting up a C# project, and implementing the code to extract drawing data. The provided C# code retrieves lines, circles, and text from an AutoCAD drawing and exports this data into an Excel spreadsheet. It also details how to run the code within AutoCAD and specifies the necessary requirements for compatibility.

Uploaded by

dagimnega208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Creating An AutoCAD To Excel Drafting Application Using C

The document outlines the steps to create an AutoCAD to Excel drafting application using C#, including installation of required references, setting up a C# project, and implementing the code to extract drawing data. The provided C# code retrieves lines, circles, and text from an AutoCAD drawing and exports this data into an Excel spreadsheet. It also details how to run the code within AutoCAD and specifies the necessary requirements for compatibility.

Uploaded by

dagimnega208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Creating an AutoCAD to Excel drafting application using C# involves automating AutoCAD to

extract drawing data and exporting it to an Excel spreadsheet. Below are the full steps and C#
code required for this process.

Steps to Create AutoCAD to Excel Drafting Using C#

1. Install Required References

• You need AutoCAD .NET API (acmgd.dll, acdbmgd.dll, accoremgd.dll).


• Install Excel Interop Library (Microsoft.Office.Interop.Excel).

2. Set Up a C# Project in Visual Studio

• Open Visual Studio.


• Create a C# Console or Windows Forms Application.
• Add references to:
o acmgd.dll
o acdbmgd.dll
o accoremgd.dll
o Microsoft.Office.Interop.Excel

3. Implement C# Code

The following C# code extracts data (lines, circles, and text) from an AutoCAD drawing and
exports it to an Excel file.

C# Code for AutoCAD to Excel Export


csharp
CopyEdit
using System;
using System.IO;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Microsoft.Office.Interop.Excel;

[assembly: CommandClass(typeof(AutoCADToExcel.ExportAutoCADData))]

namespace AutoCADToExcel
{
public class ExportAutoCADData
{
[CommandMethod("ExportToExcel")]
public void ExportToExcel()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;

try
{
// Start an Excel application
Application excelApp = new Application();
Workbook workbook = excelApp.Workbooks.Add();
Worksheet worksheet = (Worksheet)workbook.Sheets[1];

worksheet.Cells[1, 1] = "Entity Type";


worksheet.Cells[1, 2] = "Start X";
worksheet.Cells[1, 3] = "Start Y";
worksheet.Cells[1, 4] = "End X";
worksheet.Cells[1, 5] = "End Y";
worksheet.Cells[1, 6] = "Radius";
worksheet.Cells[1, 7] = "Text";

int row = 2; // Start from the second row

using (Transaction tr =
db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,
OpenMode.ForRead);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead);

foreach (ObjectId objId in btr)


{
Entity ent = tr.GetObject(objId, OpenMode.ForRead) as
Entity;

if (ent is Line line)


{
worksheet.Cells[row, 1] = "Line";
worksheet.Cells[row, 2] = line.StartPoint.X;
worksheet.Cells[row, 3] = line.StartPoint.Y;
worksheet.Cells[row, 4] = line.EndPoint.X;
worksheet.Cells[row, 5] = line.EndPoint.Y;
}
else if (ent is Circle circle)
{
worksheet.Cells[row, 1] = "Circle";
worksheet.Cells[row, 2] = circle.Center.X;
worksheet.Cells[row, 3] = circle.Center.Y;
worksheet.Cells[row, 6] = circle.Radius;
}
else if (ent is DBText text)
{
worksheet.Cells[row, 1] = "Text";
worksheet.Cells[row, 2] = text.Position.X;
worksheet.Cells[row, 3] = text.Position.Y;
worksheet.Cells[row, 7] = text.TextString;
}
row++;
}
tr.Commit();
}

string filePath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"AutoCADData.xlsx");
workbook.SaveAs(filePath);
workbook.Close();
excelApp.Quit();

ed.WriteMessage("\nExport completed successfully. File saved


at: " + filePath);
}
catch (COMException ex)
{
ed.WriteMessage("\nError: " + ex.Message);
}
}
}
}

How It Works

1. Retrieve Entities from AutoCAD


o The code retrieves Lines, Circles, and Texts from the AutoCAD drawing.
2. Export to Excel
o It writes entity type, coordinates, radius (for circles), and text content into an
Excel file.
3. Save the Excel File
o The file is saved to the Desktop as AutoCADData.xlsx.

How to Run the Code

1. Open AutoCAD.
2. Load the DLL into AutoCAD using:

nginx
CopyEdit
NETLOAD

3. Run the command in AutoCAD:

nginx
CopyEdit
ExportToExcel

4. The Excel file will be generated on your Desktop.

Requirements

• AutoCAD 2016 or later (for compatibility with .NET API).


• Excel installed (for Microsoft.Office.Interop.Excel to work).
• .NET Framework 4.7 or later.

You might also like