.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 2 (366 Views)
Accepted Solution

Adding an entity on a new drawing (document) with a new layer

366 Views, 1 Replies
11-29-2012 10:44 PM

Hi, I am trying to add an entity on a new drawing (document) with a new layer.

Following is the code snippet I have right now.

 

        [CommandMethod("TEST")]
        public void Test()
        {
            Document newDoc = Application.DocumentManager.Add("acad");
            Database db = newDoc.Database;
            DocumentLock docLock = newDoc.LockDocument();

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                                    as BlockTable;
                    BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
                                    as BlockTableRecord;

                    DBPoint p = new DBPoint(new Point3d(0, 0, 0));
                    string newLayer = PTPUtils.CreateLayer(db, "NewLayer", 3);
                    p.Layer = newLayer;

                    btr.AppendEntity(p);
                    tr.AddNewlyCreatedDBObject(p, true);

                    tr.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception e)
            {
                Application.ShowAlertDialog(e.Message);
            }

            docLock.Dispose();
            Application.DocumentManager.MdiActiveDocument = newDoc;
        }

 

PTPUtils.CreateLayer() method creates a new layer in the given DB.

However, the code gives me an "eKeyNotFound" exception in the line p.Layer = newLayer.

If I remove the p.Layer line, the code works very well. But the entity only goes to the default layer "0".

How could I solve this?

 

Thank you in advance.

 

DynamicScope.

 

FYI,

 

ObjectARX 2010 (C#)

VS 2008

Hi,

 

There is no database attached to the new created DBPoint entity (derived from DBObject that has Database property), so it throws eKeyNotFound error when you try to set its layer (that requires database to store).

 

You just swap the code, append the entity to Model Space first (this will attach database to this entity), then change its layer later. You code can be rewritten as:

 

public void AddNewLayerTest()
{
Document newDoc = Application.DocumentManager.Add("acad");
Database db = newDoc.Database;
DocumentLock docLock = newDoc.LockDocument();
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
as BlockTableRecord;
DBPoint p = new DBPoint(new Point3d(0, 0, 0));
btr.AppendEntity(p);
tr.AddNewlyCreatedDBObject(p, true);
string layerName = "NewLayer";
PTPUtils.CreateLayer(db, layerName, 3);
p.Layer = layerName;
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
Application.ShowAlertDialog(e.Message);
}
docLock.Dispose();
Application.DocumentManager.MdiActiveDocument = newDoc;
}

 

-Khoa

 

Mentor
khoa.ho
Posts: 213
Registered: ‎09-15-2011
Message 2 of 2 (363 Views)

Re: Adding an entity on a new drawing (document) with a new layer

11-29-2012 11:21 PM in reply to: dynamicscope

Hi,

 

There is no database attached to the new created DBPoint entity (derived from DBObject that has Database property), so it throws eKeyNotFound error when you try to set its layer (that requires database to store).

 

You just swap the code, append the entity to Model Space first (this will attach database to this entity), then change its layer later. You code can be rewritten as:

 

public void AddNewLayerTest()
{
    Document newDoc = Application.DocumentManager.Add("acad");
    Database db = newDoc.Database;
    DocumentLock docLock = newDoc.LockDocument();

    try
    {
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                            as BlockTable;
            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
                            as BlockTableRecord;

            DBPoint p = new DBPoint(new Point3d(0, 0, 0));

            btr.AppendEntity(p);
            tr.AddNewlyCreatedDBObject(p, true);

            string layerName = "NewLayer";
            PTPUtils.CreateLayer(db, layerName, 3);
            p.Layer = layerName;

            tr.Commit();
        }
    }
    catch (Autodesk.AutoCAD.Runtime.Exception e)
    {
        Application.ShowAlertDialog(e.Message);
    }

    docLock.Dispose();
    Application.DocumentManager.MdiActiveDocument = newDoc;
}

 

-Khoa

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.