using System; using System.Collections.Generic; using System.IO; using System.Threading; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Windows; using ExcelLateBinding; using AcAp = Autodesk.AutoCAD.ApplicationServices.Application; namespace ExcelAttribute { public class Commands { private string _expMsg, _dispMsg, _impMsg; private bool _fr = Thread.CurrentThread.CurrentCulture.Name.StartsWith("fr"); private Database _db = HostApplicationServices.WorkingDatabase; private Editor _ed = AcAp.DocumentManager.MdiActiveDocument.Editor; public Commands() { _expMsg = _fr ? "Extraction d'attributs" : "Attributes Extract"; _dispMsg = _fr ? "Ouvrir le fichier Excel ?" : "Open Excel file ?"; _impMsg = _fr ? "Importation d'attributs" : "Attributes Import"; } [CommandMethod("EATT", CommandFlags.UsePickSet)] public void ExportAttributes() { TypedValue[] filter = new TypedValue[2] { new TypedValue(0, "INSERT"), new TypedValue(66, 1) }; PromptSelectionResult psr = _ed.GetSelection(new SelectionFilter(filter)); if (psr.Status != PromptStatus.OK) return; string path = (string)AcAp.GetSystemVariable("DWGNAME"); path = string.Format("{0}{1}.xls", AcAp.GetSystemVariable("DWGPREFIX"), path.Substring(0, path.LastIndexOf('.'))); SaveFileDialog dlg = new SaveFileDialog(_expMsg, path, "xls;xlsx", "", 0); if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; string filename = dlg.Filename; System.Data.DataTable table = new System.Data.DataTable(); table.Columns.Add("HANDLE", typeof(String)); table.Columns.Add("NOM", typeof(String)); using (Transaction tr = _db.TransactionManager.StartTransaction()) { foreach (SelectedObject so in psr.Value) { BlockReference br = tr.GetObject(so.ObjectId, OpenMode.ForRead) as BlockReference; BlockTableRecord btr = br.IsDynamicBlock ? (BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead) : (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead); table.Rows.Add(new string[2] { br.Handle.ToString(), btr.Name }); foreach (KeyValuePair pair in GetAttributes(br, tr)) { if (!table.Columns.Contains(pair.Key)) table.Columns.Add(pair.Key, typeof(String)); table.Rows[table.Rows.Count - 1][pair.Key] = pair.Value; } } } ExcelWriter xlw = new ExcelWriter(); try { xlw.Open(filename); if (File.Exists(filename)) xlw.Clear(); object columns = LateBindings.Get(xlw.Worksheet, "Columns"); for (int i = 0; i < table.Columns.Count; i++) { xlw.Write(table.Columns[i].ColumnName); } xlw.NewLine(); for (int i = 0; i < table.Rows.Count; i++) { xlw.WriteLine(table.Rows[i].ItemArray); } LateBindings.Invoke(columns, "AutoFit"); xlw.Save(); if (System.Windows.Forms.MessageBox.Show( _dispMsg, _expMsg, System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { LateBindings.Set(xlw.Application, "DisplayAlerts", true); LateBindings.Set(xlw.Application, "Visible", true); LateBindings.Set(xlw.Application, "UserControl", true); } else { xlw.Close(); } } catch (System.Exception e) { xlw.Close(); _ed.WriteMessage("\n{0}\n{1}", e.Message, e.StackTrace); } }