CAD二次开发之“添加对象到模型空间”

来源:互联网 发布:淘宝企业店铺扣点吗 编辑:程序博客网 时间:2024/05/07 15:13

模型空间是一条名为BlockTableRecord.ModelSpace的特殊块表记录(添加到其中的对象会在模型空间图纸中绘制出其形状),
将对象添加到模型空间的方法与将对象添加到块表记录的方法一样。

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;
        }
    }
}
 加载程序后运行"ToModelSpace"模型空间显示如下:

end

 

原创粉丝点击