C# 使用.NETAPI以正确的方式向模型空间添加元素 方法一

C# 使用.NETAPI以正确的方式向模型空间添加元素 方法一,c#,autocad,bricscad,zwcad,C#,Autocad,Bricscad,Zwcad,其中AddToModelSpace是: public static void AddToModelSpace(string strLayer, _AcDb.Entity oEntity) { _AcAp.Document acDoc = _AcAp.Application.DocumentManager.MdiActiveDocument; _AcDb.Database acCurDb = acDoc.Database; _AcEd.Editor ed = acDoc.E

其中
AddToModelSpace
是:

public static void AddToModelSpace(string strLayer, _AcDb.Entity oEntity)
{
    _AcAp.Document acDoc = _AcAp.Application.DocumentManager.MdiActiveDocument;
    _AcDb.Database acCurDb = acDoc.Database;
    _AcEd.Editor ed = acDoc.Editor;

    using (_AcDb.BlockTable bt = acCurDb.BlockTableId.GetObject(_AcDb.OpenMode.ForRead) as _AcDb.BlockTable)
    using (_AcDb.BlockTableRecord ms = bt[_AcDb.BlockTableRecord.ModelSpace].GetObject(_AcDb.OpenMode.ForWrite) as _AcDb.BlockTableRecord)
        ms.AppendEntity(oEntity);
    oEntity.Layer = strLayer;
    oEntity.Dispose();
}
方法二
我在我的项目中使用了
AddToModelSpace
,所以我希望它很好

方法二是Autodesk在开发人员文档中推荐的方法(您可以阅读)

在方法一中,使用
ObjectId.GetObject()
方法打开
BlockTable
和模型空间“BlockTableRecord”。此方法使用top transaction to open对象,这意味着您应该使用一个活动事务来添加新创建的实体。您可以通过
数据库.TransactionManager.TopTransaction
获得它。如果您根本不想使用事务,则必须使用“仅供高级使用”
ObjectId.Open()
方法

方法三应该使用一些扩展方法从事务中调用。下面是我使用的这些方法的简化(非错误检查)摘录

static class ExtensionMethods
{
    public static T GetObject<T>(
        this ObjectId id, 
        OpenMode mode = OpenMode.ForRead,
        bool openErased = false, 
        bool forceOpenOnLockedLayer = false)
        where T : DBObject
    {
        return (T)id.GetObject(mode, openErased, forceOpenOnLockedLayer);
    }

    public static BlockTableRecord GetModelSpace(this Database db, OpenMode mode = OpenMode.ForRead)
    {
        return SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject<BlockTableRecord>(mode);
    }

    public static ObjectId Add (this BlockTableRecord owner, Entity entity)
    {
        var tr = owner.Database.TransactionManager.TopTransaction;
        var id = owner.AppendEntity(entity);
        tr.AddNewlyCreatedDBObject(entity, true);
        return id;
    }
}

谢谢你会把oEntity.Layer=strLayer放在哪里;调用?层属性在行实例化中设置:
var Line=newline(startPt,endPt){Layer=layerName}
// Get the current document and database
_AcAp.Document docActive = _AcAp.Application.DocumentManager.MdiActiveDocument;
_AcDb.Database docDB = docActive.Database;

// Start a transaction
using (_AcDb.Transaction acTrans = docDB.TransactionManager.StartTransaction())
{
    // Open the Block table for read
    _AcDb.BlockTable acBlkTbl;
    acBlkTbl = acTrans.GetObject(docDB.BlockTableId,
                                    _AcDb.OpenMode.ForRead) as _AcDb.BlockTable;

    // Open the Block table record Model space for write
    _AcDb.BlockTableRecord acBlkTblRec;
    acBlkTblRec = acTrans.GetObject(acBlkTbl[_AcDb.BlockTableRecord.ModelSpace],
                                    _AcDb.OpenMode.ForWrite) as _AcDb.BlockTableRecord;

    // Create line
    using (_AcDb.Line acLine = new _AcDb.Line(ptStart, ptEnd))
    {

        // Add the new object to the block table record and the transaction
        acBlkTblRec.AppendEntity(acLine);
        acTrans.AddNewlyCreatedDBObject(acLine, true);
    }

    // Save the new object to the database
    acTrans.Commit();
}
static class ExtensionMethods
{
    public static T GetObject<T>(
        this ObjectId id, 
        OpenMode mode = OpenMode.ForRead,
        bool openErased = false, 
        bool forceOpenOnLockedLayer = false)
        where T : DBObject
    {
        return (T)id.GetObject(mode, openErased, forceOpenOnLockedLayer);
    }

    public static BlockTableRecord GetModelSpace(this Database db, OpenMode mode = OpenMode.ForRead)
    {
        return SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject<BlockTableRecord>(mode);
    }

    public static ObjectId Add (this BlockTableRecord owner, Entity entity)
    {
        var tr = owner.Database.TransactionManager.TopTransaction;
        var id = owner.AppendEntity(entity);
        tr.AddNewlyCreatedDBObject(entity, true);
        return id;
    }
}
using (var tr = db.TransactionManager.StartTransaction())
{
    var line = new Line(startPt, endPt) { Layer = layerName };
    db.GetModelSpace(OpenMode.ForWrite).Add(line);
    tr.Commit();
}