// заполнить таблицу свойств по объектам public void Fill() { try { doc.ImpliedSelectionChanged += new EventHandler(doc_EntitySelectionChanged); db.ObjectAppended += new ObjectEventHandler(db_AddEntityRow); this.Clear(); // начинаем транзакцию using (Transaction trn = db.TransactionManager.StartTransaction()) { // Открываем Block table record для чтения BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; foreach (ObjectId btrId in bt) { BlockTableRecord btr = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord; BlockTableRecordEnumerator enumer = btr.GetEnumerator(); while (enumer.MoveNext()) { Entity ent = trn.GetObject(enumer.Current, OpenMode.ForRead) as Entity; // событие на изменение объекта this.Add(new PropertyData(ref ent, ent_ModifEntityRow)); } } trn.Commit(); } } catch (System.Exception ex) { doc.Editor.WriteMessage("Exception: " + ex); } }
public void InsertBlockRefWithAtt(string blockName, Point3d point, Scale3d scale, double rotateAngle, string roomnumber) { Database db = HostApplicationServices.WorkingDatabase; using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; if (!bt.Has(blockName)) { return; } BlockTableRecord blockwithatt = (BlockTableRecord)trans.GetObject(bt[blockName], OpenMode.ForRead); BlockReference blockRef = new BlockReference(point, bt[blockName]); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); blockRef.ScaleFactors = scale; blockRef.Rotation = rotateAngle; btr.AppendEntity(blockRef); trans.AddNewlyCreatedDBObject(blockRef, true); //获取blockName块的遍历器,以实现对块中对象的访问 BlockTableRecordEnumerator iterator = blockwithatt.GetEnumerator(); //如果blockName块包含属性 if (blockwithatt.HasAttributeDefinitions) { //利用块遍历器对块中的对象进行遍历 while (iterator.MoveNext()) { //获取块遍历器当前指向的块中的对象 AttributeDefinition attdef = trans.GetObject(iterator.Current, OpenMode.ForRead) as AttributeDefinition; //定义一个新的属性参照对象 AttributeReference att = new AttributeReference(); //判断块遍历器当前指向的块中的对象是否为属性定义 if (attdef != null) { //从属性定义对象中继承相关的属性到属性参照对象中 att.SetAttributeFromBlock(attdef, blockRef.BlockTransform); //设置属性参照对象的位置为属性定义的位置+块参照的位置 att.Position = attdef.Position + blockRef.Position.GetAsVector(); //判断属性定义的名称 switch (attdef.Tag) { //如果为"NUMBER",则设置块参照的属性值 case "NUMBER": att.TextString = roomnumber; break; } //判断块参照是否可写,如不可写,则切换为可写状态 if (!blockRef.IsWriteEnabled) { blockRef.UpgradeOpen(); } //添加新创建的属性参照 blockRef.AttributeCollection.AppendAttribute(att); //通知事务处理添加新创建的属性参照 trans.AddNewlyCreatedDBObject(att, true); } } } trans.Commit();//提交事务处理 } }
public static ObjectId[] GetAllPos() { List <ObjectId> list = new List <ObjectId>(); Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { try { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); using (BlockTableRecordEnumerator it = btr.GetEnumerator()) { while (it.MoveNext()) { if (it.Current.ObjectClass == Autodesk.AutoCAD.Runtime.RXObject.GetClass(typeof(RebarPos))) { list.Add(it.Current); } } } } catch (System.Exception) { ; } } return(list.ToArray()); }
public static ObjectId[] GetPosWithShape(string shape) { List <ObjectId> list = new List <ObjectId>(); Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { try { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); using (BlockTableRecordEnumerator it = btr.GetEnumerator()) { while (it.MoveNext()) { RebarPos pos = tr.GetObject(it.Current, OpenMode.ForRead) as RebarPos; if (pos != null) { if (pos.Shape == shape) { list.Add(it.Current); } } } } } catch (System.Exception) { ; } } return(list.ToArray()); }
/// <summary> /// Get the blocks in a layout /// </summary> /// <param name="tr"></param> /// <param name="mode"></param> /// <returns></returns> private IEnumerable <BlockReference> GetLayoutBlocks(Transaction tr, OpenMode mode) { var blocks = new List <BlockReference>(); //Get the drawings layout dictionary ObjectId curLayoutID = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout); var layout = curLayoutID.GetObject(OpenMode.ForRead) as Layout; if (layout != null) { if (tr != null) { var layoutRecord = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord; //Get the enumerator if (layoutRecord != null) { BlockTableRecordEnumerator recordEnumerator = layoutRecord.GetEnumerator(); //Loop through all blocks in the block table while (recordEnumerator.MoveNext()) { var blcokEnt = tr.GetObject(recordEnumerator.Current, mode) as Entity; BlockReference oBr; if ((oBr = blcokEnt as BlockReference) != null) { blocks.Add(oBr); } } } } } return(blocks.Where(b => ValidateBlock(b, tr)).ToList()); }
private void RuttonReplace_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 (DWG item in DWGs) { Database acDb = new Database(false, true); using (acDb) { acDb.ReadDwgFile(item.Path, 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 (recEnum.MoveNext()) { Entity ent = (Entity)trans.GetObject(recEnum.Current, OpenMode.ForRead); if (ent.GetType() == typeof(BlockReference)) { BlockReference blockRef = ent as BlockReference; if (blockRef.Handle == hBlock) { AttributeCollection attCol = blockRef.AttributeCollection; foreach (ObjectId att in attCol) { using (AttributeReference attRef = trans.GetObject(att, OpenMode.ForWrite) as AttributeReference) { if (attRef.Tag == ComboAttribute.SelectedValue.ToString()) { attRef.TextString = TBReplacement.Text; break; } } } } } } trans.Commit(); acDb.SaveAs(item.Path, DwgVersion.Current); item.Check = true; } } } }
private void ReplaceBlocks() { 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 (DWG item in DWGs) { Database acDb = new Database(false, true); using (acDb) { acDb.ReadDwgFile(item.Path, 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 (recEnum.MoveNext()) { Entity ent = (Entity)trans.GetObject(recEnum.Current, OpenMode.ForWrite); if (ent.GetType() == typeof(BlockReference)) { BlockReference blockRef = ent as BlockReference; if (blockRef.Handle.Value.CompareTo(hBlockReplace.Value) == 0) { using (BlockReference acBlkRef = new BlockReference(blockRef.Position, bt[BlockReplacement])) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = trans.GetObject(acDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec.AppendEntity(acBlkRef); trans.AddNewlyCreatedDBObject(acBlkRef, true); AddAttributes(acBlkRef); } item.Check = true; blockRef.Erase(); break; } } } trans.Commit(); acDb.SaveAs(item.Path, DwgVersion.Current); item.Check = true; } } } }
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; }
public static void EraseBolckTableRecord(this BlockTableRecord btr) { Tools.StartTransaction(() => { if (btr.Id != ObjectId.Null) { btr = btr.Id.GetObjectForRead <BlockTableRecord>(false); if (!btr.IsErased) { btr.UpgradeOpen(); using (BlockTableRecordEnumerator enumerator = btr.GetEnumerator()) { while (enumerator.MoveNext()) { enumerator.Current.GetObject((OpenMode)OpenMode.ForWrite).Erase(); } } btr.DowngradeOpen(); } } }); }
/// <summary> /// Gets all the blocks in a drawing /// </summary> /// <param name="tr"></param> /// <param name="mode"></param> /// <returns></returns> private IEnumerable <BlockReference> GetDrawingBlocks(Transaction tr, OpenMode mode) { var blocks = new List <BlockReference>(); //Get the drawings layout dictionary var layoutDict = tr.GetObject(_db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary; if (layoutDict != null) { foreach (DictionaryEntry id in layoutDict) { //Get the layout object var layout = tr.GetObject((ObjectId)id.Value, OpenMode.ForRead) as Layout; if (layout == null) { continue; } var layoutRecord = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord; //Get the enumerator if (layoutRecord != null) { BlockTableRecordEnumerator recordEnumerator = layoutRecord.GetEnumerator(); //Loop through all blocks in the block table while (recordEnumerator.MoveNext()) { var blcokEnt = tr.GetObject(recordEnumerator.Current, mode) as Entity; BlockReference oBr; if ((oBr = blcokEnt as BlockReference) != null) { blocks.Add(oBr); } } } } } return(blocks.Where(b => ValidateBlock(b, tr)).ToList()); }
public static List <PosCopy> ReadAll(PosGrouping grouping, bool skipEmpty) { List <ObjectId> items = new List <ObjectId>(); Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); using (BlockTableRecordEnumerator it = btr.GetEnumerator()) { while (it.MoveNext()) { if (it.Current.ObjectClass == RXObject.GetClass(typeof(RebarPos))) { items.Add(it.Current); } } } } return(ReadAllInSelection(items, skipEmpty, grouping)); }
private void _eraseBolckTableRecord() { using (Transaction trans = Tools.StartTransaction()) { if (this.BlockTableRecordId != ObjectId.Null) { BlockTableRecord btr = (BlockTableRecord)this.BlockTableRecordId.GetObject(OpenMode.ForRead, true, true); if (!btr.IsErased) { btr.UpgradeOpen(); using (BlockTableRecordEnumerator enumerator = btr.GetEnumerator()) { while (enumerator.MoveNext()) { enumerator.Current.GetObject((OpenMode)OpenMode.ForWrite).Erase(); } } trans.Commit(); _entities = new List <Entity>(); } } } }
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); } } } }
public void SPlineToLine() { int num; int num4; object obj; try { IL_01: ProjectData.ClearProjectError(); num = -2; IL_09: int num2 = 2; Document mdiActiveDocument = Application.DocumentManager.MdiActiveDocument; IL_16: num2 = 3; Database database = mdiActiveDocument.Database; IL_1F: num2 = 4; using (Transaction transaction = database.TransactionManager.StartTransaction()) { BlockTable blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, 1); SymbolTableEnumerator enumerator = blockTable.GetEnumerator(); while (enumerator.MoveNext()) { ObjectId objectId = enumerator.Current; BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(objectId, 1); BlockTableRecordEnumerator enumerator2 = blockTableRecord.GetEnumerator(); while (enumerator2.MoveNext()) { ObjectId objectId2 = enumerator2.Current; Entity entity = (Entity)transaction.GetObject(objectId2, 1); if (entity is Spline) { Spline spline = (Spline)entity; Line e = new Line(spline.StartPoint, spline.EndPoint); CAD.AddEnt(e); spline.Erase(); } } if (enumerator2 != null) { enumerator2.Dispose(); } } if (enumerator != null) { enumerator.Dispose(); } transaction.Commit(); } IL_11B: goto IL_187; IL_11D: int num3 = num4 + 1; num4 = 0; @switch(ICSharpCode.Decompiler.ILAst.ILLabel[], num3); IL_141: goto IL_17C; IL_143: num4 = num2; if (num <= -2) { goto IL_11D; } @switch(ICSharpCode.Decompiler.ILAst.ILLabel[], num); IL_159 :; } catch when(endfilter(obj is Exception & num != 0 & num4 == 0)) { Exception ex = (Exception)obj2; goto IL_143; } IL_17C: throw ProjectData.CreateProjectError(-2146828237); IL_187: if (num4 != 0) { ProjectData.ClearProjectError(); } }
public void FindAndReplace() { Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; //Valitaan avoinna oleva dokumentti Database acCurDb = acDoc.Database; PromptKeywordOptions promptKey = new PromptKeywordOptions("\nScope:"); promptKey.Keywords.Add("Drawing"); promptKey.Keywords.Add("Selection"); PromptResult promptRes = acDoc.Editor.GetKeywords(promptKey); PromptStringOptions promptString = new PromptStringOptions("\nReplace:"); PromptResult promptStringResult = acDoc.Editor.GetString(promptString); string replace = promptStringResult.StringResult; promptString = new PromptStringOptions("\nWith:"); promptStringResult = acDoc.Editor.GetString(promptString); string replaceWith = promptStringResult.StringResult; switch (promptRes.StringResult) { case "Drawing": using (Transaction trans = acCurDb.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)trans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead); BlockTableRecord ms = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead); BlockTableRecordEnumerator recEnum = ms.GetEnumerator(); while (recEnum.MoveNext()) { Entity ent = (Entity)trans.GetObject(recEnum.Current, OpenMode.ForRead); if (ent.GetType() == typeof(BlockReference)) { BlockReference blockRef = ent as BlockReference; AttributeCollection attCol = blockRef.AttributeCollection; foreach (ObjectId att in attCol) { using (AttributeReference attRef = trans.GetObject(att, OpenMode.ForWrite) as AttributeReference) { if (attRef.TextString.Contains(replace)) { attRef.TextString = attRef.TextString.Replace(replace, replaceWith); } } } } } trans.Commit(); } break; case "Selection": PromptSelectionResult selRes = acDoc.Editor.GetSelection(); if (selRes.Status == PromptStatus.OK) { using (Transaction trans = acCurDb.TransactionManager.StartTransaction()) { SelectionSet selSet = selRes.Value; if (selSet[0] != null) { for (int i = 0; i < selSet.Count; i++) { Entity acEnt = trans.GetObject(selSet[i].ObjectId, OpenMode.ForRead) as Entity; if (acEnt != null) { if (acEnt.GetType() != typeof(BlockReference)) { return; } BlockReference selectedBlock = acEnt as BlockReference; AttributeCollection attrCol = selectedBlock.AttributeCollection; BlockTable blkTable = trans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord blkRec; blkRec = trans.GetObject(blkTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord; if (attrCol.Count == 0) { return; } foreach (ObjectId attRefId in attrCol) { using (AttributeReference attRef = trans.GetObject(attRefId, OpenMode.ForWrite) as AttributeReference) { if (attRef.TextString.Contains(replace)) { attRef.TextString = attRef.TextString.Replace(replace, replaceWith); } } } trans.Commit(); } } } } } break; default: acDoc.Editor.WriteMessage("\nUnknown keyword\n"); break; } }