using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly:CommandClass(typeof(_01_环境测试.AddToModelSpace))]
namespace _01_环境测试
{
    class AddToModelSpace
    {
        [CommandMethod("ToModelSpace")]
        public void ModelSpace()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            DBText txt=new DBText();
            txt.Position=new Point3d(100,200,0);
            txt.TextString = "Bim中心";
            ToModelSpace(txt, db);
        }

        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;
        }
    }
}