<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Adding an entity on a new drawing (document) with a new layer in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/3717308#M52214</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is no database attached to the new created DBPoint entity&amp;nbsp;(derived from&amp;nbsp;DBObject that has Database property), so it throws eKeyNotFound error when you try to set its layer (that&amp;nbsp;requires database to store).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You just swap the code, &lt;U&gt;append the entity to Model Space first&lt;/U&gt; (this will&amp;nbsp;attach database to this entity), &lt;U&gt;then change its layer later&lt;/U&gt;. You code can be rewritten as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Khoa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Nov 2012 07:21:44 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2012-11-30T07:21:44Z</dc:date>
    <item>
      <title>Adding an entity on a new drawing (document) with a new layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/3717274#M52213</link>
      <description>&lt;P&gt;Hi, I am trying to add an entity on a new drawing (document) with a new layer.&lt;/P&gt;&lt;P&gt;Following is the code snippet I have right now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        [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;
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PTPUtils.CreateLayer() method creates a new layer in the given DB.&lt;/P&gt;&lt;P&gt;However, the code gives me an "eKeyNotFound" exception in the line p.Layer = newLayer.&lt;/P&gt;&lt;P&gt;If I remove the p.Layer line, the code works very well. But the entity only goes to the default layer "0".&lt;/P&gt;&lt;P&gt;How could I solve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DynamicScope.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FYI,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ObjectARX 2010 (C#)&lt;/P&gt;&lt;P&gt;VS 2008&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2012 06:44:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/3717274#M52213</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-11-30T06:44:19Z</dc:date>
    </item>
    <item>
      <title>Re: Adding an entity on a new drawing (document) with a new layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/3717308#M52214</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is no database attached to the new created DBPoint entity&amp;nbsp;(derived from&amp;nbsp;DBObject that has Database property), so it throws eKeyNotFound error when you try to set its layer (that&amp;nbsp;requires database to store).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You just swap the code, &lt;U&gt;append the entity to Model Space first&lt;/U&gt; (this will&amp;nbsp;attach database to this entity), &lt;U&gt;then change its layer later&lt;/U&gt;. You code can be rewritten as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Khoa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2012 07:21:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/3717308#M52214</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-11-30T07:21:44Z</dc:date>
    </item>
    <item>
      <title>error CS0103: The name 'PTPUtils' does not exist in the current context</title>
      <link>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/6813156#M52215</link>
      <description>&lt;P&gt;This Scenario works fine but I am getting compilation error for PTPUtils API&lt;/P&gt;&lt;P&gt;error CS0103: The name 'PTPUtils' does not exist in the current context&lt;/P&gt;&lt;P&gt;&amp;nbsp;PTPUtils.CreateLayer(db, layerName, 3);.&lt;/P&gt;&lt;P&gt;Since i was new on this,Please help us on this.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2017 12:19:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/adding-an-entity-on-a-new-drawing-document-with-a-new-layer/m-p/6813156#M52215</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-01-18T12:19:09Z</dc:date>
    </item>
  </channel>
</rss>

