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?
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
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.
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(); } } }
Can't find what you're looking for? Ask the community or share your knowledge.