<?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 How to add new layers to a side db? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-add-new-layers-to-a-side-db/m-p/12637541#M5056</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to add a new layer A to a side db.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;And there are some dbtexts in the side db.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to set the dbtext's layers&amp;nbsp; to A.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;thanks.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    [CommandMethod("W7")]
    public void W7()
    {
      Database db = HostApplicationServices.WorkingDatabase;

      using (Transaction trans = db.TransactionManager.StartTransaction())
      {
        try
        {
          using (Database dbInsert = new Database(false, true))
          {
            string sDwg = @"D:\B.dwg";
            dbInsert.ReadDwgFile(sDwg, System.IO.FileShare.ReadWrite, true, null);

            Autodesk.AutoCAD.DatabaseServices.TransactionManager sideTm = dbInsert.TransactionManager;
            using (Transaction sideTr = sideTm.StartTransaction())
            {
              ObjectId id = dbInsert.NewLayer("A", 5, "continues", "acadiso.lin", LineWeight.LineWeight015, false, false, false, false, false);
              sideTr.Commit();
            }

            using (Transaction sideTr = sideTm.StartTransaction())
            {
              BlockTable bt = (BlockTable)sideTm.GetObject(dbInsert.BlockTableId, OpenMode.ForRead, false);
              BlockTableRecord modelspace = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);

              foreach (ObjectId id in modelspace)
              {
                try
                {
                  if (id.IsErased) continue;

                  Entity ent = (Entity)sideTr.GetObject(id, OpenMode.ForWrite, false);
                  ent.Layer = "B";
                  ent.DowngradeOpen();
                }
                catch
                {

                }
              }

              sideTr.Commit();
            }

            dbInsert.RetainOriginalThumbnailBitmap = true;
            dbInsert.SaveAs(sDwg, DwgVersion.AC1027);

            trans.Commit();
          }
        }
        catch
        {
        }
      }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public static ObjectId NewLayer(this Database db, string layerName, short colorIndex, string lineType, string lineTypeFile, LineWeight lineWeight, bool bIsPlot, bool bIsOff, bool bIsFreezed, bool bIsLocked, bool bIsCurrent)
    {
      Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
      colorIndex = (short)(colorIndex % 256);
      ObjectId idRet = ObjectId.Null;
      try
      {
        using (Transaction trans = (Transaction)db.TransactionManager.StartTransaction())
        {
          LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
          LayerTableRecord ltr = new LayerTableRecord();
          ltr.Name = layerName;

          ltr.IsOff = bIsOff;
          ltr.IsFrozen = bIsFreezed;
          ltr.IsLocked = bIsLocked;
          ltr.IsPlottable = bIsPlot;
          ltr.Color = CadColor.FromColorIndex(CadColors.ColorMethod.ByColor, colorIndex);
          ltr.LineWeight = lineWeight;
          ltr.LinetypeObjectId = db.LoadLineType(lineType, lineTypeFile);
          idRet = lt.Add(ltr);
          trans.AddNewlyCreatedDBObject(ltr, true);
          if (bIsCurrent)
            db.Clayer = idRet;
          trans.Commit();
        }
      }
      catch (System.Exception ex)
      {

      }
      return idRet;
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Mar 2024 12:01:54 GMT</pubDate>
    <dc:creator>swaywood</dc:creator>
    <dc:date>2024-03-13T12:01:54Z</dc:date>
    <item>
      <title>How to add new layers to a side db?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-add-new-layers-to-a-side-db/m-p/12637541#M5056</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to add a new layer A to a side db.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;And there are some dbtexts in the side db.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to set the dbtext's layers&amp;nbsp; to A.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;thanks.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    [CommandMethod("W7")]
    public void W7()
    {
      Database db = HostApplicationServices.WorkingDatabase;

      using (Transaction trans = db.TransactionManager.StartTransaction())
      {
        try
        {
          using (Database dbInsert = new Database(false, true))
          {
            string sDwg = @"D:\B.dwg";
            dbInsert.ReadDwgFile(sDwg, System.IO.FileShare.ReadWrite, true, null);

            Autodesk.AutoCAD.DatabaseServices.TransactionManager sideTm = dbInsert.TransactionManager;
            using (Transaction sideTr = sideTm.StartTransaction())
            {
              ObjectId id = dbInsert.NewLayer("A", 5, "continues", "acadiso.lin", LineWeight.LineWeight015, false, false, false, false, false);
              sideTr.Commit();
            }

            using (Transaction sideTr = sideTm.StartTransaction())
            {
              BlockTable bt = (BlockTable)sideTm.GetObject(dbInsert.BlockTableId, OpenMode.ForRead, false);
              BlockTableRecord modelspace = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);

              foreach (ObjectId id in modelspace)
              {
                try
                {
                  if (id.IsErased) continue;

                  Entity ent = (Entity)sideTr.GetObject(id, OpenMode.ForWrite, false);
                  ent.Layer = "B";
                  ent.DowngradeOpen();
                }
                catch
                {

                }
              }

              sideTr.Commit();
            }

            dbInsert.RetainOriginalThumbnailBitmap = true;
            dbInsert.SaveAs(sDwg, DwgVersion.AC1027);

            trans.Commit();
          }
        }
        catch
        {
        }
      }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public static ObjectId NewLayer(this Database db, string layerName, short colorIndex, string lineType, string lineTypeFile, LineWeight lineWeight, bool bIsPlot, bool bIsOff, bool bIsFreezed, bool bIsLocked, bool bIsCurrent)
    {
      Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
      colorIndex = (short)(colorIndex % 256);
      ObjectId idRet = ObjectId.Null;
      try
      {
        using (Transaction trans = (Transaction)db.TransactionManager.StartTransaction())
        {
          LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
          LayerTableRecord ltr = new LayerTableRecord();
          ltr.Name = layerName;

          ltr.IsOff = bIsOff;
          ltr.IsFrozen = bIsFreezed;
          ltr.IsLocked = bIsLocked;
          ltr.IsPlottable = bIsPlot;
          ltr.Color = CadColor.FromColorIndex(CadColors.ColorMethod.ByColor, colorIndex);
          ltr.LineWeight = lineWeight;
          ltr.LinetypeObjectId = db.LoadLineType(lineType, lineTypeFile);
          idRet = lt.Add(ltr);
          trans.AddNewlyCreatedDBObject(ltr, true);
          if (bIsCurrent)
            db.Clayer = idRet;
          trans.Commit();
        }
      }
      catch (System.Exception ex)
      {

      }
      return idRet;
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 12:01:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-add-new-layers-to-a-side-db/m-p/12637541#M5056</guid>
      <dc:creator>swaywood</dc:creator>
      <dc:date>2024-03-13T12:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to add new layers to a side db?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-add-new-layers-to-a-side-db/m-p/12638041#M5057</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works for me:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;[CommandMethod("TEST2")]
public static void Test2()
{
    string fileName = @"D:\B.dwg";
    using (var db = new Database(false, true))
    {
        db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
        using (var tr = db.TransactionManager.StartTransaction())
        {
            db.AddLayer("A", 5, "Continuous", "acadiso.lin", LineWeight.LineWeight015, false);
            var modelSpace = (BlockTableRecord)tr.GetObject(
                SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
            foreach (ObjectId id in modelSpace)
            {
                if (id.ObjectClass.Name == "AcDbText")
                {
                    var text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
                    text.Layer = "A";
                }
            }
            tr.Commit();
        }
        db.SaveAs(fileName, DwgVersion.AC1027);
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The extension method (it checks if the layer already exists in the LayerTable of the database ans uses some default parameters).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static ObjectId AddLayer(
    this Database db, 
    string layerName, 
    short colorIndex, 
    string lineType, 
    string lineTypeFile, 
    LineWeight lineWeight = LineWeight.ByLineWeightDefault, 
    bool isPlottable = true, 
    bool isOff = false, 
    bool isFrozen = false, 
    bool isLocked = false, 
    bool isCurrent = false)
{
    ObjectId layerId;
    LayerTableRecord layer;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
        if (layerTable.Has(layerName))
        {
            layerId = layerTable[layerName];
            layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForWrite);
        }
        else
        {
            tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
            layer = new LayerTableRecord();
            layer.Name = layerName;
            layerId = layerTable.Add(layer);
            tr.AddNewlyCreatedDBObject(layer, true);
        }
        var linetypeTable = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
        if (!linetypeTable.Has(lineType))
        {
            db.LoadLineTypeFile(lineType, lineTypeFile);
        }
        layer.Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex);
        layer.LinetypeObjectId = linetypeTable[lineType];
        layer.LineWeight = lineWeight;
        layer.IsPlottable = isPlottable;
        layer.IsOff = isOff;
        layer.IsFrozen = isFrozen;
        layer.IsLocked = isLocked;
        if (isCurrent)
            db.Clayer = layerId;
        tr.Commit();
    }
    return layerId;
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 15:39:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-add-new-layers-to-a-side-db/m-p/12638041#M5057</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-03-13T15:39:59Z</dc:date>
    </item>
  </channel>
</rss>

