private void ButtonRead_Click(object sender, RoutedEventArgs e) { foreach (DataRow DR in DT.Rows) { int index = 1; Database acDb = new Database(false, true); using (acDb) { acDb.ReadDwgFile(DR["Filename"].ToString(), FileShare.Read, false, ""); using (Transaction trans = acDb.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)trans.GetObject(acDb.BlockTableId, OpenMode.ForWrite); BlockTableRecord ms = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); BlockTableRecordEnumerator recEnum = ms.GetEnumerator(); while (index < DT.Columns.Count - 1) { recEnum.Reset(); Handle bHandle = new Handle(long.Parse(DT.Columns[index].ColumnName.Split('-')[1].ToString())); string blockName = DT.Columns[index].ColumnName.Split('-')[0].ToString(); while (recEnum.MoveNext()) { Entity ent = (Entity)trans.GetObject(recEnum.Current, OpenMode.ForRead); if (ent.GetType() == typeof(BlockReference)) { BlockReference blockRef = ent as BlockReference; if (blockRef.Handle.Value.CompareTo(bHandle.Value) == 0) { AttributeCollection attCol = blockRef.AttributeCollection; foreach (ObjectId att in attCol) { using (AttributeReference attRef = trans.GetObject(att, OpenMode.ForRead) as AttributeReference) { if (DT.Columns.Contains(blockName + "-" + bHandle.Value + "-" + attRef.Tag)) { DR[blockName + "-" + bHandle.Value + "-" + attRef.Tag] = attRef.TextString; index++; } } } } } } } } } } Spreadsheet.ItemsSource = null; Spreadsheet.ItemsSource = DT.DefaultView; }
private void ButtonWrite_Click(object sender, RoutedEventArgs e) { if (Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.Count > 0) { Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.CurrentDocument.Editor.WriteMessage("Close the open documents:\n"); foreach (Document doc in Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager) { Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.CurrentDocument.Editor.WriteMessage(doc.Name + "\n"); } return; } foreach (DataRow DR in DT.Rows) { int index = 1; Database acDb = new Database(false, true); using (acDb) { if (File.Exists(DR["Filename"].ToString())) { acDb.ReadDwgFile(DR["Filename"].ToString(), FileShare.ReadWrite, false, ""); pathExists = DR["Filename"].ToString(); } else { acDb.ReadDwgFile(pathExists, FileShare.ReadWrite, false, ""); } using (Transaction trans = acDb.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)trans.GetObject(acDb.BlockTableId, OpenMode.ForWrite); BlockTableRecord ms = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); BlockTableRecordEnumerator recEnum = ms.GetEnumerator(); while (index < DT.Columns.Count - 1) { recEnum.Reset(); Handle bHandle = new Handle(long.Parse(DT.Columns[index].ColumnName.Split('-')[1].ToString())); string blockName = DT.Columns[index].ColumnName.Split('-')[0].ToString(); while (recEnum.MoveNext()) { Entity ent = (Entity)trans.GetObject(recEnum.Current, OpenMode.ForRead); if (ent.GetType() == typeof(BlockReference)) { BlockReference blockRef = ent as BlockReference; if (blockRef.Handle.Value.CompareTo(bHandle.Value) == 0) { AttributeCollection attCol = blockRef.AttributeCollection; foreach (ObjectId att in attCol) { using (AttributeReference attRef = trans.GetObject(att, OpenMode.ForWrite) as AttributeReference) { if (DT.Columns.Contains(blockName + "-" + bHandle.Value + "-" + attRef.Tag)) { attRef.TextString = DR[blockName + "-" + bHandle.Value + "-" + attRef.Tag].ToString(); index++; } } } break; } } } } trans.Commit(); acDb.SaveAs(DR["Filename"].ToString(), DwgVersion.Current); } } } }