<?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: Access Drawing database without Opening it then Create Objects Inside then Save in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464683#M15811</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You should use the LayerId property instead.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        public static void CreateFileWithText(string templatePath, string outputDwgPath)
        {
            // create a new Database (within a using statement to ensure the Database to be disposed)
            using (var db = new Database(false, true))
            {
                // read the template dwg file
                db.ReadDwgFile(templatePath, FileOpenMode.OpenForReadAndAllShare, false, null);

                // start a transaction
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    // check if a layer named "Character" already exists and create it if not
                    string layerName = "Character";
                    ObjectId layerId;
                    var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                    if (layerTable.Has(layerName))
                    {
                        layerId = layerTable[layerName];
                    }
                    else
                    {
                        tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                        var layer = new LayerTableRecord
                        {
                            Name = layerName,
                            Color = Color.FromRgb(200, 30, 80)
                        };
                        layerId = layerTable.Add(layer);
                        tr.AddNewlyCreatedDBObject(layer, true);
                    }

                    // get the Model space
                    var modelSpace = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);

                    // create a text
                    var text = new DBText
                    {
                        Position = new Point3d(2.0, 2.0, 0.0),
                        LayerId = layerId,
                        Height = 500.0,
                        TextString = "Hello, World."
                    };
                    modelSpace.AppendEntity(text);
                    tr.AddNewlyCreatedDBObject(text, true);

                    // save changes to the database
                    tr.Commit();
                }

                // save the new drawing
                db.SaveAs(outputDwgPath, DwgVersion.Current);
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Jul 2021 12:12:07 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2021-07-13T12:12:07Z</dc:date>
    <item>
      <title>Access Drawing database without Opening it then Create Objects Inside then Save</title>
      <link>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464538#M15810</link>
      <description>&lt;P&gt;Good day Everyone. I'm new to AutoCAD C# Programming asking for your kind assistance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm trying to do is&lt;/P&gt;&lt;P&gt;1. Access Drawing Database without Opening it in the Editor ( Template Drawing )&lt;/P&gt;&lt;P&gt;2. Create Objects Inside It&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Assign Layer Property to the Created Objects&lt;/P&gt;&lt;P&gt;4. Save as New Drawing&amp;nbsp; ( Output Drawing )&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I followed this Solution and It works, I can already Access the drawing database and Create Objects Inside it and then save into a different drawing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/how-to-open-an-autocad-file-update-and-close-with-save-using-c/td-p/8281653" target="_blank"&gt;https://forums.autodesk.com/t5/net/how-to-open-an-autocad-file-update-and-close-with-save-using-c/td-p/8281653&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I try to assign a layer property in the new object I get eKeyNotFound Error even though the Template drawing that I access has that layer.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also followed this Link but still no Luck.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/how-change-layer-of-specific-entity/td-p/8577610" target="_blank"&gt;https://forums.autodesk.com/t5/net/how-change-layer-of-specific-entity/td-p/8577610&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my Source Code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;[CommandMethod("CHANGENEW")]
public static void CreateText()
{
    //Get the template plant database
    Database acCurDb = new Database(false, true);
    string dwgFlpath = @"D:\Users\johnrey.d.malulan\Downloads\Hotaka_New_Type_Test.dwg";

    //Set the output Drawing Directory
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    string strDWGName = acDoc.Name;
    strDWGName = @"D:\Users\johnrey.d.malulan\Downloads\Output.dwg";


    acCurDb.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);




    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                           OpenMode.ForRead) as BlockTable;
        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                              OpenMode.ForWrite) as BlockTableRecord;

        var layerName = "Character";
        // check if the layer already exists
        var lt = (LayerTable)acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead);
        if (!lt.Has(layerName))
        {
            // if not create it
            var layer = new LayerTableRecord()
            {
                Name = layerName,
                Color = Color.FromRgb(200, 30, 80)
            };
            lt.UpgradeOpen();
            lt.Add(layer);
            acTrans.AddNewlyCreatedDBObject(layer, true);
            Application.ShowAlertDialog("Layer Created");
        }


        // Create a single-line text object
        DBText acText = new DBText();
        //acText.SetDatabaseDefaults();
        acText.Position = new Point3d(2, 2, 0);
        acText.Layer = layerName;
        acText.Height = 500;
        acText.TextString = "Hello, World.";
        acBlkTblRec.AppendEntity(acText);
        acTrans.AddNewlyCreatedDBObject(acText, true);
        // Save the changes and dispose of the transaction
        acTrans.Commit();
    }
    acCurDb.SaveAs(strDWGName, DwgVersion.Current);
}
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help me &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 11:10:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464538#M15810</guid>
      <dc:creator>john-malulan</dc:creator>
      <dc:date>2021-07-13T11:10:14Z</dc:date>
    </item>
    <item>
      <title>Re: Access Drawing database without Opening it then Create Objects Inside then Save</title>
      <link>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464683#M15811</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You should use the LayerId property instead.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        public static void CreateFileWithText(string templatePath, string outputDwgPath)
        {
            // create a new Database (within a using statement to ensure the Database to be disposed)
            using (var db = new Database(false, true))
            {
                // read the template dwg file
                db.ReadDwgFile(templatePath, FileOpenMode.OpenForReadAndAllShare, false, null);

                // start a transaction
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    // check if a layer named "Character" already exists and create it if not
                    string layerName = "Character";
                    ObjectId layerId;
                    var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                    if (layerTable.Has(layerName))
                    {
                        layerId = layerTable[layerName];
                    }
                    else
                    {
                        tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                        var layer = new LayerTableRecord
                        {
                            Name = layerName,
                            Color = Color.FromRgb(200, 30, 80)
                        };
                        layerId = layerTable.Add(layer);
                        tr.AddNewlyCreatedDBObject(layer, true);
                    }

                    // get the Model space
                    var modelSpace = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);

                    // create a text
                    var text = new DBText
                    {
                        Position = new Point3d(2.0, 2.0, 0.0),
                        LayerId = layerId,
                        Height = 500.0,
                        TextString = "Hello, World."
                    };
                    modelSpace.AppendEntity(text);
                    tr.AddNewlyCreatedDBObject(text, true);

                    // save changes to the database
                    tr.Commit();
                }

                // save the new drawing
                db.SaveAs(outputDwgPath, DwgVersion.Current);
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 12:12:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464683#M15811</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2021-07-13T12:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: Access Drawing database without Opening it then Create Objects Inside then Save</title>
      <link>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464748#M15812</link>
      <description>&lt;P&gt;Change:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="general"&gt;acText.Layer = layerName;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="general"&gt;acText.LayerId = layerId; // ObjectId of your layer&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S.: Oops! I did not see the &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;answer.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 12:32:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10464748#M15812</guid>
      <dc:creator>Alexander.Rivilis</dc:creator>
      <dc:date>2021-07-13T12:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Access Drawing database without Opening it then Create Objects Inside then Save</title>
      <link>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10467973#M15814</link>
      <description>You're a life saver Sir, Thank you so much. Have a nice day!</description>
      <pubDate>Wed, 14 Jul 2021 11:46:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/access-drawing-database-without-opening-it-then-create-objects/m-p/10467973#M15814</guid>
      <dc:creator>john-malulan</dc:creator>
      <dc:date>2021-07-14T11:46:36Z</dc:date>
    </item>
  </channel>
</rss>

