.NET

Reply
New Member
tmvCAD1717
Posts: 2
Registered: ‎03-04-2013
Message 1 of 5 (541 Views)

How do I programmatically add labels to objects on a CAD drawing?

541 Views, 4 Replies
03-06-2013 10:47 AM

My requirement is to programmatically add labels to the objects on a CAD 2013 drawing in one operation (as opposed to typing them in).  The labels exist in an external excel spreadsheet.  I am new to CAD development, Is there a preferred way to do this?  

xrq
Valued Mentor
gasty1001
Posts: 480
Registered: ‎04-11-2010
Message 2 of 5 (508 Views)

Re: How do I programmatically add labels to objects on a CAD drawing?

03-06-2013 02:44 PM in reply to: tmvCAD1717

Hi,

 

There is no such thing as a label entity in AutoCAD (though they exists en Map and maybe other vertical), there are text objects (single and multiline), block attributes, and other objects related to text (dimensions, tables, etc). Posting an example dwg of what you meant for a label,  will help us to understand your problem. It also helps to know OS, AutoCAD version, prefered language (C#,VB)  etc.

 

Gaston Nunez

New Member
tmvCAD1717
Posts: 2
Registered: ‎03-04-2013
Message 3 of 5 (498 Views)

Re: How do I programmatically add labels to objects on a CAD drawing?

03-06-2013 04:49 PM in reply to: gasty1001

Hi Gaston, thanks for the reply!  Here is my environment:  Windows 7 x64, CAD 2013, C#  (installed VS 2010 Template Wizards and ObjectARX 2013 - have the basic tutorial plugin going). My goal is to take cost data from a .NET dataset and pepper it into my drawing (maybe eventually a text or attribute fields that you mentioned). Each object  has a unique attribute that can be used to map the costs from the .NET dataset.   I am looking for high level approaches to the solution - general approaches that others have used to solve similar problem. 

xrq
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 5 (473 Views)

Re: How do I programmatically add labels to objects on a CAD drawing?

03-07-2013 07:08 AM in reply to: tmvCAD1717

You can get datatable from Excel using OpenXML SDK, this is fastest way, then you have to get data from table and pull it in your entities (attributes) Found it on 'stackoverflow: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; // borrowed from this page: // http://stackoverflow.com/questions/3321082/from-excel-to-datatable-in-c-sharp-with-open-xml namespace OpenXMLConsoleCS { class Program { // use OpenXML SDK 2.0 or higher // Requires references: // DocumentFormat.OpenXml // WindowsBase //__________________________________________________________// /// /// Return System.Data.DataTable /// ///full path of Excel file ///sheet number zero based /// public static System.Data.DataTable GetDataTableFromSheet(string xlFilePath, int sheetnum) { System.Data.DataTable dt = new System.Data.DataTable(); using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(xlFilePath, false)) { WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart; IEnumerable sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild().Elements(); string relationshipId = sheets.ElementAt(sheetnum).Id.Value; WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId); Worksheet workSheet = worksheetPart.Worksheet; SheetData sheetData = workSheet.GetFirstChild(); IEnumerable rows = sheetData.Descendants(); foreach (Cell cell in rows.ElementAt(0)) { dt.Columns.Add(GetCellValue(spreadSheetDocument, cell)); } foreach (Row row in rows) { System.Data.DataRow tempRow = dt.NewRow(); for (int i = 0; i < row.Descendants().Count(); i++) { tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants().ElementAt(i)); } dt.Rows.Add(tempRow); } } return dt; } public static string GetCellValue(SpreadsheetDocument document, Cell cell) { SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart; string value = cell.CellValue.InnerXml; if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) { return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText; } else { return value; } } //______________________________________________________________________________________// static void Main(string[] args) { string filename = @"C:\Test\XlPoints.xlsx";// filename int num = 0;// Sheet1 if (!File.Exists(filename)) { Console.WriteLine("\nFile:\t{0} does not exists, Exit.", filename); Console.Read(); return; } System.Data.DataTable dt = GetDataTableFromSheet(filename, num); int c = 0; foreach (System.Data.DataRow dr in dt.Rows) { object[] line = dr.ItemArray; c++; for (int r = 0; r < line.Length; r++) Console.WriteLine(line[r].ToString()); } Console.WriteLine("\tLast Row:\t{0}", c.ToString()); Console.Read(); } } }

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 5 of 5 (457 Views)

Re: How do I programmatically add labels to objects on a CAD drawing?

03-07-2013 12:01 PM in reply to: Hallex

I can't really visualize how you would like to label the elements. Yesterday I was at this text. that might be of interest there.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.