菜鸟请教
<p>从官方例题改的,不知道哪里错了,插入块后,我想得到块名:myblock1,但我只得到:*Model_Space,为什么?请高手帮我看下,谢谢</p><p> //插入块<br/> public void createb()<br/> {<br/> Database db = HostApplicationServices.WorkingDatabase; //AutoCAD数据库<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> Transaction trans = db.TransactionManager.StartTransaction();//使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来<br/> try<br/> {<br/> BlockTableRecord btr = (BlockTableRecord)trans.GetObject(CreateBlock("myblock1"), OpenMode.ForWrite);<br/> Ellipse ellipse = new Ellipse(new Point3d(200, 200, 0), Vector3d.ZAxis, new Vector3d(300, 0, 0), 0.5, 0, 0);<br/> ellipse.ColorIndex = 1;<br/> btr.AppendEntity(ellipse);<br/> trans.AddNewlyCreatedDBObject(ellipse, true);</p><p> trans.Commit();<br/> }<br/> catch<br/> {<br/> ed.WriteMessage("Error");<br/> }<br/> finally<br/> {<br/> trans.Dispose();<br/> }<br/> }</p><p> public ObjectId CreateBlock(string blockname)<br/> {<br/> ObjectId blockId = new ObjectId(); //它返回函数的值<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> Transaction trans = db.TransactionManager.StartTransaction();<br/> try<br/> {<br/> //首先取得块表……<br/> BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);<br/> //检查blockname块是否存在……<br/> if (bt.Has(blockname))<br/> {<br/> blockId = bt;<br/> }<br/> else<br/> {<br/> //如果blockname块不存在,就创建它<br/> BlockTableRecord btr = new BlockTableRecord();<br/> btr.Name = blockname; //设置块的名字<br/> blockId = bt.Add(btr);<br/> trans.AddNewlyCreatedDBObject(btr, true);<br/> }<br/> trans.Commit();<br/> }<br/> catch<br/> {<br/> ed.WriteMessage("Error");<br/> }<br/> finally<br/> {<br/> trans.Dispose();<br/> }<br/> return blockId;<br/> }<br/></p><p> <br/> public void PrintoutData() //CreateDivision<br/> {<br/> Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> Transaction trans = db.TransactionManager.StartTransaction();<br/> try<br/> {<br/> //首先,获取块表和模型空间块表记录<br/> BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);<br/> foreach (ObjectId id in btr)<br/> {<br/> Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打开当前对象!<br/> if (ent.GetType() == typeof(BlockReference))<br/> {<br/> ed.WriteMessage(ent.BlockName);<br/> }<br/> }<br/> trans.Commit();<br/> }<br/> catch<br/> {<br/> ed.WriteMessage("Error");<br/> }<br/> finally<br/> {<br/> trans.Dispose();<br/> }<br/> }</p> <p>块操作的概念要搞清楚</p><p>首先在块表中创建块定义(BlockTableRecord)</p><p>然后在模型空间中按块定义生成块引用(BlockReference)</p> <p>我在ShowData()中用了BlockReference呀</p><p><strong><font face="Verdana" color="#da2549">lzh741206能帮我看看错在哪里吗,不胜感激</font></strong></p> <p>BlockName:</p><p>Accesses the name of the owner block.</p><p>块引用是放在模型空间中的,返回的当然是:*Model_Space</p><p>你应该引用Name属性</p> <p>Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);</p><p>ent没有Name属性</p> <p> BlockReference ent = trans.GetObject(id, OpenMode.ForRead, false) as BlockReference; //打开当前对象!<br/> if (ent != null)<br/> {<br/> ed.WriteMessage(ent.Name);<br/> }</p><p></p><p>另外,如果是在模型空间中选择特定实体,用选择集更方便</p> 谢谢非常感谢
页:
[1]