|
从官方例题改的,不知道哪里错了,插入块后,我想得到块名:myblock1,但我只得到:*Model_Space,为什么?请高手帮我看下,谢谢
[CommandMethod("createblock1")] //插入块
public void createb()
{
Database db = HostApplicationServices.WorkingDatabase; //数据库
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Transaction trans = db.TransactionManager.StartTransaction();//使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
try
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(CreateBlock("myblock1"), OpenMode.ForWrite);
Ellipse ellipse = new Ellipse(new Point3d(200, 200, 0), Vector3d.ZAxis, new Vector3d(300, 0, 0), 0.5, 0, 0);
ellipse.ColorIndex = 1;
btr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(ellipse, true);
trans.Commit();
}
catch
{
ed.WriteMessage("Error");
}
finally
{
trans.Dispose();
}
}
public ObjectId CreateBlock(string blockname)
{
ObjectId blockId = new ObjectId(); //它返回函数的值
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
//首先取得块表……
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
//检查blockname块是否存在……
if (bt.Has(blockname))
{
blockId = bt[blockname];
}
else
{
//如果blockname块不存在,就创建它
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blockname; //设置块的名字
blockId = bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
}
trans.Commit();
}
catch
{
ed.WriteMessage("Error");
}
finally
{
trans.Dispose();
}
return blockId;
}
[CommandMethod("ShowData")]
public void PrintoutData() //CreateDivision
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
//首先,获取块表和模型空间块表记录
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId id in btr)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打开当前对象!
if (ent.GetType() == typeof(BlockReference))
{
ed.WriteMessage(ent.BlockName);
}
}
trans.Commit();
}
catch
{
ed.WriteMessage("Error");
}
finally
{
trans.Dispose();
}
}
|
|