将对象添加到模型空间的方法与将对象添加到块表记录的方法一样。
using
System;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass( typeof (Sample.AddToModelSpace))]
namespace Sample
{
class AddToModelSpace
{
[CommandMethod( " ToModelSpace " )]
public void ModelSpace()
{
Database db = HostApplicationServices.WorkingDatabase;
DBText txt = new DBText(); txt.Position = new Point3d();
txt.TextString = " BimCAD.org " ; ToModelSpace(txt, db);
}
/// <summary>
/// 将一个图形对象加入到指定的Database的模型空间
/// </summary>
/// <param name="ent"> 实体对象 </param>
/// <param name="db"> 数据库 </param>
/// <returns></returns>
public static ObjectId ToModelSpace(Entity ent, Database db)
{
ObjectId entId; using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent); trans.AddNewlyCreatedDBObject(ent, true ); trans.Commit();
}
return entId;
}
}
}
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass( typeof (Sample.AddToModelSpace))]
namespace Sample
{
class AddToModelSpace
{
[CommandMethod( " ToModelSpace " )]
public void ModelSpace()
{
Database db = HostApplicationServices.WorkingDatabase;
DBText txt = new DBText(); txt.Position = new Point3d();
txt.TextString = " BimCAD.org " ; ToModelSpace(txt, db);
}
/// <summary>
/// 将一个图形对象加入到指定的Database的模型空间
/// </summary>
/// <param name="ent"> 实体对象 </param>
/// <param name="db"> 数据库 </param>
/// <returns></returns>
public static ObjectId ToModelSpace(Entity ent, Database db)
{
ObjectId entId; using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent); trans.AddNewlyCreatedDBObject(ent, true ); trans.Commit();
}
return entId;
}
}
}
end