.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

4 REPLIES 4
Reply
Message 1 of 5
tmvCAD1717
1342 Views, 4 Replies

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

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
4 REPLIES 4
Message 2 of 5
hgasty1001
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

Message 3 of 5
tmvCAD1717
in reply to: hgasty1001

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
Message 4 of 5
Hallex
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
Message 5 of 5
WWFreeman
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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost