static public void BreakPolyLine(ref Polyline pline, ref Point3dCollection pointList) { DBObjectCollection collection = new DBObjectCollection(); pline.Explode(collection); System.Collections.IEnumerator Enumerator = collection.GetEnumerator(); while (Enumerator.MoveNext() == true) { DBObject dbObject = (DBObject)Enumerator.Current; if (Line.GetClass(typeof(Line)) == dbObject.GetRXClass()) { Line line = (Line)dbObject; BreakLine(ref line, ref pointList); line.Dispose(); } else if (Arc.GetClass(typeof(Arc)) == dbObject.GetRXClass()) { Arc arc = (Arc)dbObject; BreakArc(ref arc, ref pointList); arc.Dispose(); } } }
GetObjClassName(DBObject obj) { // if this is a proxy we can't use the proxy class // name because we would get duplicates on the list // for each object. if (obj.IsAProxy) { ProxyObject proxyObj = obj as ProxyObject; if (proxyObj != null) { return(proxyObj.OriginalClassName); } else { ProxyEntity proxyEnt = obj as ProxyEntity; if (proxyEnt != null) { return(proxyEnt.OriginalClassName); } else { Debug.Assert(false); return(obj.GetRXClass().Name); } } } else { return(obj.GetRXClass().Name); } }
/// <inheritdoc /> public override void Close(DBObject dbObject) { Debug.Print(dbObject?.GetRXClass().Name); if (IsApplicable(dbObject)) { EntityUtils.ObjectOverruleProcess( dbObject, () => EntityReaderService.Instance.GetFromEntity <BreakLine>(dbObject)); } base.Close(dbObject); }
/// <summary> /// 获取块参照数据 /// </summary> /// <param name="block2">块参照</param> /// <returns>提取的数据字符串</returns> public static List <string> AnalysisBlockReferenceFun(Autodesk.AutoCAD.DatabaseServices.BlockReference _br) { List <string> result = new List <string>(); Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { result.Add("#BlockReference#"); result.Add(ReflectionClass.GetAllPropertyInfoEx(_br, "BlockReference")); BlockTableRecord block = trans.GetObject(_br.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord; foreach (ObjectId id in block) { DBObject ODB = id.GetObject(OpenMode.ForRead); if (ODB.GetRXClass().Equals(RXClass.GetClass(typeof(BlockReference)))) { BlockReference br = trans.GetObject(id, OpenMode.ForRead) as BlockReference; BlockTableRecord block1 = trans.GetObject(br.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord; result.Add("#Sub_BlockReference#"); result.Add(ReflectionClass.GetAllPropertyInfoEx(br, "Sub_BlockReference")); foreach (var item in block1) { result.Add("#" + item.GetObject(OpenMode.ForRead).GetRXClass().Name + "#"); result.Add(ReflectionClass.GetAllPropertyInfoEx(trans.GetObject(item, OpenMode.ForRead), item.GetObject(OpenMode.ForRead).GetRXClass().Name)); result.Add("#" + item.GetObject(OpenMode.ForRead).GetRXClass().Name + "#"); } result.Add("#Sub_BlockReference#"); break; } else { result.Add("#" + id.GetObject(OpenMode.ForRead).GetRXClass().Name + "#"); result.Add(ReflectionClass.GetAllPropertyInfoEx(trans.GetObject(id, OpenMode.ForRead), id.GetObject(OpenMode.ForRead).GetRXClass().Name)); result.Add("#" + id.GetObject(OpenMode.ForRead).GetRXClass().Name + "#"); } } } //扩展数据 result.Add("#BlockReference#"); return(result); }
public void getMTextObj() //dumps all text contents to text file { //giving error for files not containing text --check string contents = ""; try { using (Database db = HostApplicationServices.WorkingDatabase) { Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor; TypedValue[] filterlist = new TypedValue[1]; Logger.WriteLog("COMMAND - EXTT : Attempting to extract text...", MessageType.Information); //select Mtext entities //filterlist[0] = new TypedValue((int)DxfCode.LayerName, "View Labels"); //Test filterlist[0] = new TypedValue((int)DxfCode.Start, "MTEXT"); SelectionFilter filter = new SelectionFilter(filterlist); //we can add custom data to be extracted here PromptSelectionResult selRes = ed.SelectAll(filter); if (selRes.Status != PromptStatus.OK) { Logger.WriteLog("COMMAND - EXTT : Error during text extraction...", MessageType.Error); //return; //test } //ObjectId[] MTextObjIds = selRes.Value.GetObjectIds(); //if (!(MTextObjIds == null)) //test { //if (MTextObjIds.Length > 0) //test { using (Transaction tr = acApp.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()) { #if !_TEST LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead); foreach (ObjectId id in lt) { LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead); //if (ltr.Name.Equals("0")) // iterate all layers { contents += ltr.Name + "\t\n"; ObjectId[] ids = GetTextFromLayerOrBlock(tr, ltr.Name, true); if (ids == null) { contents += "NULL\n"; } else { for (int i = 0; i < ids.Length; ++i) { DBObject obj = tr.GetObject(ids[i], OpenMode.ForRead); contents += "\n Layer Object Details : " + obj.GetType().ToString() + " " + obj.GetRXClass().DxfName + "\n"; switch (obj.GetRXClass().DxfName) { //Autodesk.AutoCAD.DatabaseServices.Table case "ACAD_TABLE": Table tb = obj as Table; for (int row = 0; row < tb.Rows.Count; ++row) { for (int col = 0; col < tb.Rows.Count; ++col) { var cell = tb.Cells[row, col]; contents += cell.Value + "\t"; //formatting lost //contents += cell.GetValue(FormatOption.FormatOptionNone) + "\t"; } } break; //Autodesk.AutoCAD.DatabaseServices.MText case "MTEXT": MText mtext = obj as MText; if (mtext != null) { //if(mtext.Text != String.Empty) contents += "MTEXT : " + mtext.Text + "\n"; } break; //Autodesk.AutoCAD.DatabaseServices.DBText case "TEXT": DBText dbtext = obj as DBText; if (dbtext != null) { //if(dbtext.TextString != string.Empty) contents += "DBTEXT : " + dbtext.TextString + "\n"; } break; //Autodesk.AutoCAD.DatabaseServices.BlockReference case "INSERT": BlockReference br = obj as BlockReference; foreach (ObjectId attrId in br.AttributeCollection) { AttributeReference attRef = (AttributeReference)tr.GetObject(attrId, OpenMode.ForRead); string str = ("\n Attribute Tag: " + attRef.Tag + "\n Attribute String: " + attRef.TextString + "\n"); contents += str; } break; } } } //break; } } //MText MTextObj; //foreach (ObjectId MTextObjId in MTextObjIds) //{ // MTextObj = tr.GetObject(MTextObjId, OpenMode.ForRead) as MText; // if (MTextObj != null) // { // contents += MTextObj.Text + "\n"; // } //} if (!String.IsNullOrEmpty(contents)) { File.WriteAllText(Path.ChangeExtension(db.Filename, "txt"), contents); } } //tr.Commit(); // no changes so no need to commit #else // Open the blocktable, get the modelspace //BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); //BlockTableRecord btrModel = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead); //BlockTableRecord btrPaper = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead); // Iterate through it, dumping objects // Iterate through it, dumping objects //foreach (ObjectId objId in btr) //{ // Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForRead); // string typeString = ent.GetType().ToString(); // //if(ent.GetType() == Type.GetType("MText")) // if(typeString.Equals("MTEXT")) // { // MText txt = ent as MText; // contents += txt.Contents; // } // else if (ent.GetType() == Type.GetType("DBText")) // { // DBText txt = ent as DBText; // contents += txt.TextString; // } //} //contents += "*****************MODEL SPACE TEXT***********************\n"; //foreach (ObjectId id in btrModel) //{ // DBObject obj = tr.GetObject(id, OpenMode.ForRead) as MText; // MText mtext = obj as MText; // if (mtext != null) // { // contents += "\nMTEXT-->" + mtext.Text + "\n"; // continue; // } // DBText dbtext = obj as DBText; // if (dbtext != null) // { // contents += "\nDBTEXT-->" + dbtext.TextString + "\n"; // } //} //contents += "*****************PAPER SPACE TEXT***********************\n"; //foreach (ObjectId id in btrPaper) //{ // DBObject obj = tr.GetObject(id, OpenMode.ForRead) as MText; // MText mtext = obj as MText; // if (mtext != null) // { // contents += "\nMTEXT-->" + mtext.Text + "\n"; // continue; // } // DBText dbtext = obj as DBText; // if (dbtext != null) // { // contents += "\nDBTEXT-->" + dbtext.TextString + "\n"; // } //} IterateBlockTable(db, tr, out contents); //IterateAutoCADSymbolTables(db, tr, out contents, TableType.BlockTable, RecordType.BlockTableRecord); if (!String.IsNullOrEmpty(contents)) { File.WriteAllText(Path.ChangeExtension(db.Filename, "txt"), contents); } } #endif } } Logger.WriteLog("COMMAND - EXTT : Command executed successfully...", MessageType.Information); } }
public static void test() { Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; Transaction t = db.TransactionManager.StartTransaction(); ObjectId dtoi = ObjectId.Null; try { DBDictionary dbd = (DBDictionary)t.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite, false); DBDictionary d = null; if (!dbd.Contains("测试")) { d = new DBDictionary(); ObjectId oi = dbd.SetAt("测试", d); t.AddNewlyCreatedDBObject(d, true); } else { ObjectId oi = dbd.GetAt("测试"); d = (DBDictionary)t.GetObject(oi, OpenMode.ForWrite, false); } DataTable dt = new DataTable(); dtoi = d.SetAt("1", dt); t.AddNewlyCreatedDBObject(dt, true); t.Commit(); } finally { t.Dispose(); } t = db.TransactionManager.StartTransaction(); try { BlockTable bt = (BlockTable)t.GetObject(db.BlockTableId, OpenMode.ForRead, false); BlockTableRecord btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false); ztAcadMgd.DatabaseServices.Line l = new ztAcadMgd.DatabaseServices.Line(new Point3d(), new Point3d(10, 10, 0)); btr.AppendEntity(l); t.AddNewlyCreatedDBObject(l, true); l.AddPersistentReactor(dtoi); for (int i = 0; i < l.GetPersistentReactorIds().Count; i++) { DBObject dbo = t.GetObject(l.GetPersistentReactorIds()[i], OpenMode.ForRead, false); Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n" + dbo.GetRXClass().Name); } l.SetValue("属性", "直线"); Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n" + l.GetValue("属性")); t.Commit(); } finally { t.Dispose(); } }
private void file_open_handler(object sender, EventArgs e) { if (DialogResult.OK == openFileDialog.ShowDialog(this)) { if (lm != null) { lm.LayoutSwitched -= new Teigha.DatabaseServices.LayoutEventHandler(reinitGraphDevice); HostApplicationServices.WorkingDatabase = null; lm = null; } bool bLoaded = true; database = new Database(false, false); if (openFileDialog.FilterIndex == 1) { try { database.ReadDwgFile(openFileDialog.FileName, FileOpenMode.OpenForReadAndAllShare, false, ""); //现在进入数据库并获得数据库的块表引用 Transaction trans = database.TransactionManager.StartTransaction(); BlockTable bt = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead, false, true); //从块表的模型空间特性中获得块表记录,块表记录对象包含DWG文件数据库实体 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false, true); foreach (ObjectId btrId in btr) { DBObject entBlock = (DBObject)trans.GetObject(btrId, OpenMode.ForRead, false, true); String dxfName = entBlock.GetRXClass().DxfName; if (entBlock.GetRXClass().DxfName.ToUpper() == "INSERT") { BlockReference bRef = (BlockReference)entBlock; if (bRef.AttributeCollection.Count != 0) { System.Collections.IEnumerator bRefEnum = bRef.AttributeCollection.GetEnumerator(); while (bRefEnum.MoveNext()) { ObjectId aId = (ObjectId)bRefEnum.Current;//这一句极其关键 AttributeReference aRef = (AttributeReference)trans.GetObject(aId, OpenMode.ForRead, false, true); string sss = aRef.TextString; //this.textBox1.Text = aRef.TextString;//此语句即获得属性单行文本,请自行在此语句前添加 属性单行文本 赋于的变量 } } } if (entBlock.GetRXClass().DxfName.ToUpper() == "MTEXT") { MText bRef = (MText)entBlock; string sd = bRef.Text; } if (entBlock.GetRXClass().DxfName.ToUpper() == "LINE") { Line bRef = (Line)entBlock; ObjectId oid = bRef.Id; } } trans.Commit(); //提交事务处理 btr.Dispose(); bt.Dispose(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); bLoaded = false; } } else if (openFileDialog.FilterIndex == 2) { try { database.DxfIn(openFileDialog.FileName, ""); } catch (System.Exception ex) { MessageBox.Show(ex.Message); bLoaded = false; } } if (bLoaded) { HostApplicationServices.WorkingDatabase = database; lm = LayoutManager.Current; lm.LayoutSwitched += new Teigha.DatabaseServices.LayoutEventHandler(reinitGraphDevice); String str = HostApplicationServices.Current.FontMapFileName; //menuStrip. exportToolStripMenuItem.Enabled = true; zoomToExtentsToolStripMenuItem.Enabled = true; zoomWindowToolStripMenuItem.Enabled = true; setAvtiveLayoutToolStripMenuItem.Enabled = true; fileDependencyToolStripMenuItem.Enabled = true; panel1.Enabled = true; pageSetupToolStripMenuItem.Enabled = true; printPreviewToolStripMenuItem.Enabled = true; printToolStripMenuItem.Enabled = true; this.Text = String.Format("OdViewExMgd - [{0}]", openFileDialog.SafeFileName); initializeGraphics(); Invalidate(); } } }