public static void DwgToBlock() //把图纸当作图块插入 { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor;  romptResult res = ed.GetString("Give me a file to insert"); if (res.Status != PromptStatus.OK) return; string fname = res.StringResult; if (!File.Exists(fname)) fname = HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default); using (Database db = new Database(false, false)) { //得到想插入的文档 db.ReadDwgFile(fname, FileShare.Read, true, null); BlockTableRecord dwgBTR=null; using (Transaction t = doc.TransactionManager.StartTransaction()) { [U][U]try { BlockTable dwgBT=(BlockTable)t.GetObject(db.BlockTableId,OpenMode.ForWrite,true); dwgBTR=(BlockTableRecord)t.GetObject(dwgBT["Title"],OpenMode.ForWrite,true); } catch(System.Exception ess) { ed.WriteMessage(ess.Message); } foreach(ObjectId dwgID in dwgBTR) { Entity ent = (Entity)t.GetObject(dwgID, OpenMode.ForRead, false); //打开当前的对象! if (ent is AttributeDefinition) { //设置属性为属性索引中的属性定义 AttributeDefinition attDef = ((AttributeDefinition)(ent)); ed.WriteMessage(attDef.TextString); } }[/U][/U]
//创建一个块 ObjectId idBTR = doc.Database.Insert("test", db, false); BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); double x,y,z=0;  romptPointResult resPoint = ed.GetPoint("输入插入点坐标(x,y)"); if (resPoint.Status != PromptStatus.OK) return; BlockReference bref = new BlockReference(resPoint.Value, dwgBTR.Id); btr.AppendEntity(bref); t.AddNewlyCreatedDBObject(bref, true); t.Commit(); } } }
加下划线的部分是想处理属性来着 |