CAD二次开发之“添加对象到模型空间”
模型空间是一条名为BlockTableRecord.ModelSpace的特殊块表记录(添加到其中的对象会在模型空间图纸中绘制出其形状),
将对象添加到模型空间的方法与将对象添加到块表记录的方法一样。
加载程序后运行"ToModelSpace"模型空间显示如下:
end
将对象添加到模型空间的方法与将对象添加到块表记录的方法一样。
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
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。