TheSwamp

Code Red => .NET => Topic started by: JONTHEPOPE on July 07, 2014, 03:03:48 PM

Title: Layers and layer names
Post by: JONTHEPOPE on July 07, 2014, 03:03:48 PM
I cut and paste the code from the help file but it has some words underlined.

void line 12

and LayerTableRecord? line 30

Does anyone else have problems with the AutoCAD help file?
 
Code - C#: [Select]
  1.  
  2. [CommandMethod("CreateAndAssignALayer")]
  3. public static [color=red]void[/color] CreateAndAssignALayer()
  4.  
  5. {
  6.     Document acDoc = Application.DocumentManager.MdiActiveDocument;
  7.     Database acCurDb = acDoc.Database;
  8.     using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  9.     {
  10.         LayerTable acLyrTbl;
  11.         acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
  12.                                         OpenMode.ForRead) as LayerTable;
  13.  
  14.         string sLayerName = "Center";
  15.  
  16.         if (acLyrTbl.Has(sLayerName) == false)
  17.         {
  18.             using (LayerTableRecord acLyrTblRec = [color=red]new LayerTableRecord[/color]())
  19.             {
  20.                 acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 3);
  21.                 acLyrTblRec.Name = sLayerName;
  22.                 acLyrTbl.UpgradeOpen();
  23.                 acLyrTbl.Add(acLyrTblRec);
  24.                 acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  25.             }
  26.         }
  27.  
  28.         BlockTable acBlkTbl;
  29.         acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
  30.                                         OpenMode.ForRead) as BlockTable;
  31.  
  32.         BlockTableRecord acBlkTblRec;
  33.         acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  34.                                         OpenMode.ForWrite) as BlockTableRecord;
  35.  
  36.         using (Circle acCirc = new Circle())
  37.         {
  38.             acCirc.Center = new Point3d(2, 2, 0);
  39.             acCirc.Radius = 1;
  40.             acCirc.Layer = sLayerName;
  41.             acBlkTblRec.AppendEntity(acCirc);
  42.             acTrans.AddNewlyCreatedDBObject(acCirc, true);
  43.         }
  44.         acTrans.Commit();
  45.     }
  46. }
  47.  

http://www.theswamp.org/Smileys/aaron/mrgreen.gif

edit:kdub->add code = csharp
Title: Re: Layers and layer names
Post by: gile on July 07, 2014, 03:37:09 PM
Hi,

void (http://msdn.microsoft.com/en-us/library/yah0tteb.aspx) specifies that the method does not return a value.
LayerTableRecord (http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-97955B2B-F823-4787-86C7-97F7E701FD72) is the type (class) for layer objects in the AutoCAD .NET API.
Title: Re: Layers and layer names
Post by: Kerry on July 07, 2014, 09:57:03 PM
Quote
I cut and paste the code from the help file but it has some words underlined.

void line 12

and LayerTableRecord? line 30

Does anyone else have problems with the AutoCAD help file?

What color is the underlining?
What is the message when you pass  the cursor over the words ?
Does the your Solution compile ?

Quote
1>------ Build started: Project: AutoCAD CSharp plug-in23, Configuration: Debug Any CPU ------
1>  AutoCAD CSharp plug-in23 -> W:\AcadCode\_Projects\AutoCAD CSharp plug-in23\AutoCAD CSharp plug-in23\bin\Debug\AutoCAD CSharp plug-in23.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I notice that your posted code is not contained inside a class. It's 'normal' that the class declaration is located immediately after the using statements ... have you forgotten this ?
Title: Re: Layers and layer names
Post by: JONTHEPOPE on July 08, 2014, 01:01:01 AM
Thanks again. I'll give it another go.
It's really nice to get a push in the right direction!
I can get really overwhelmed most days especially when I am forgetting things!
Title: Re: Layers and layer names
Post by: Keith Brown on July 09, 2014, 04:35:04 PM
You are getting errors because you have not declared a class.  Pretty much every type in C# needs to reside inside of a class.  Look at Kerry's code and then look at the screenshot you posted.  The class declaration is missing from the screenshot.


Here is a link to the microsoft virtual academy that has free video training on c#


http://www.microsoftvirtualacademy.com/training-topics/c-app-development (http://www.microsoftvirtualacademy.com/training-topics/c-app-development)


**EDIT** It looks like the post this was replying to was deleted.






Title: Re: Layers and layer names
Post by: JONTHEPOPE on July 09, 2014, 04:55:44 PM
Hi,
Thanks for the link.

how do I create multiple layers when acLyrTbl has no overload method?
Do I cut and paste the same routine over and over again?
There must be a better way  :grazy:

Code: [Select]

      if (acLyrTbl.Has("M-ANNO-TEXT-NEWW") != true)
      {
          acLyrTbl.UpgradeOpen();
 
          using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
          {
              acLyrTblRec.Name = "M-ANNO-TEXT-NEWW";
              acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 3);
Title: Re: Layers and layer names
Post by: Keith Brown on July 09, 2014, 05:14:13 PM
Building on Kerry's code, this is one way to do it.  I chose to pass optional parameters vs a list because i think it is more readable.  If you are planning on creating alot of layers then maybe you should think about doing it another way.


Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.Geometry;
  10. using Autodesk.AutoCAD.Colors;
  11.  
  12.  
  13. [assembly: CommandClass(typeof(Layers.Class1))]
  14. namespace Layers
  15. {
  16.     class Class1
  17.     {
  18.  
  19.  
  20.         [CommandMethod("AddMyLayers")]
  21.         public static void AddMyLayers()
  22.         {
  23.             // Get the current document and database, and start a transaction
  24.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  25.             Database acCurDb = acDoc.Database;
  26.  
  27.  
  28.             using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  29.             {
  30.                 // Returns the layer table for the current database
  31.                 LayerTable acLyrTbl;
  32.                 acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
  33.  
  34.  
  35.                 AddLayers(acTrans, acLyrTbl, "MyLayer", "MyLayer2", "MyLayer3", "MyLayer4" );
  36.  
  37.  
  38.                 // Commit the changes
  39.                 acTrans.Commit();
  40.  
  41.  
  42.                 // Dispose of the transaction
  43.             }
  44.         }
  45.  
  46.  
  47.         public static void AddLayers(Transaction acTrans, LayerTable acLyrTbl, params string[] layers)
  48.         {
  49.             var layerList = layers.ToList();
  50.             foreach (var layer in layerList)
  51.             {
  52.                 // Check to see if MyLayer exists in the Layer table
  53.                 if (!acLyrTbl.Has(layer))
  54.                 {
  55.                     // Open the Layer Table for write
  56.                     acLyrTbl.UpgradeOpen();
  57.  
  58.  
  59.                     // Create a new layer table record and name the layer
  60.                     using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
  61.                     {
  62.                         acLyrTblRec.Name = layer;
  63.  
  64.  
  65.                         // Add the new layer table record to the layer table and the transaction
  66.                         acLyrTbl.Add(acLyrTblRec);
  67.                         acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
Title: Re: Layers and layer names
Post by: JONTHEPOPE on July 09, 2014, 05:25:31 PM
nice!

Here's where I'm at so far .
when I write
AddLayers(acTrans, acLyrTbl, "MyLayer", "MyLayer2", "MyLayer3", "MyLayer4" );
will I also be able to set the colors and line types after?

Keith what does the type Params do? is it some sort of array?

Code: [Select]
   public class newlayertest
    {

        [CommandMethod("AddMyLayer")]
        public static void AddMyLayer()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                             OpenMode.ForRead) as LayerTable;

                if (acLyrTbl.Has("M-ANNO-TEXT-NEWW") != true)
                {
                    acLyrTbl.UpgradeOpen();

                    using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
                    {
                        acLyrTblRec.Name = "M-ANNO-TEXT-NEWW";
                        acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 3);
                        acLyrTbl.Add(acLyrTblRec);
                        acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
                        acCurDb.Clayer = acLyrTbl["M-ANNO-TEXT-NEWW"];
                    }

                    if (acLyrTbl.Has("MyLayer") != true)
                    {
                        acLyrTbl.UpgradeOpen();
                        using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
                        {
                            acLyrTblRec.Name = "MyLayer";
                            acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 3);
                            acLyrTbl.Add(acLyrTblRec);
                            acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
                            acCurDb.Clayer = acLyrTbl["MyLayer"];
                        }
                        acTrans.Commit();
                    }
                }
            }
        }
    }
}
Title: Re: Layers and layer names
Post by: JONTHEPOPE on July 09, 2014, 05:27:32 PM
Also that last post by Keith Brown was alot to chew
Title: Re: Layers and layer names
Post by: JONTHEPOPE on July 10, 2014, 05:32:50 PM
Could I build a class to reference this part of the code so I can reuse it?
How about the layernames can they change in this class like a variable?

Code: [Select]

 if (acLyrTbl.Has("M-HVAC-EQPM-NEWW") != true)
                    {
                        // Open the Layer Table for write
                        acLyrTbl.UpgradeOpen();

                        // Create a new layer table record and name the layer "MyLayer"
                        using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
                        {
                            acLyrTblRec.Name = "M-HVAC-EQPM-NEWW";
                            acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 8);

                            // Add the new layer table record to the layer table and the transaction
                            acLyrTbl.Add(acLyrTblRec);
                            acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
                            acCurDb.Clayer = acLyrTbl["M-HVAC-EQPM-NEWW"];
                        }

Title: Re: Layers and layer names
Post by: WILL HATCH on July 10, 2014, 07:31:02 PM
Code is much easier to read when posted under its type.
To do the equivalent of your code snip here:
Code - C#: [Select]
  1.  if (acLyrTbl.Has("M-HVAC-EQPM-NEWW") != true)
  2.                     {
  3.                         // Open the Layer Table for write
  4.                         acLyrTbl.UpgradeOpen();
  5.  
  6.                         // Create a new layer table record and name the layer "MyLayer"
  7.                         using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
  8.                         {
  9.                             acLyrTblRec.Name = "M-HVAC-EQPM-NEWW";
  10.                             acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 8);
  11.  
  12.                             // Add the new layer table record to the layer table and the transaction
  13.                             acLyrTbl.Add(acLyrTblRec);
  14.                             acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  15.                             acCurDb.Clayer = acLyrTbl["M-HVAC-EQPM-NEWW"];
  16.                         }

You don't want a class here, you want a function.  I like to use extension methods which are held in a separate static class.

Code - C#: [Select]
  1.                public static class ExtensionMethods
  2.                {
  3.                     public static void AddLayer(this LayerTable lt, Transaction tr, string name, int color)
  4.                     {
  5.                         // Create a new layer table record
  6.                         using (LayerTableRecord ltr = new LayerTableRecord())
  7.                         {
  8.                             ltr.Name = LayerName;
  9.                             ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, color);
  10.                             // Add the new layer table record to the layer table and the transaction
  11.                             if (lt.IsReadOnly) lt.UpgradeOpen(); <--coding in the window, don't think this is the correct property to test
  12.                            lt.Add(ltr);
  13.                            tr.AddNewlyCreatedDBObject(ltr, true);
  14.                            
  15.                        }
  16.                    }
  17.               }
  18.  
called like:
Code - C#: [Select]
  1.  if (acLyrTbl.Has("M-HVAC-EQPM-NEWW") != true)
  2. {
  3. acLyrTbl.AddLayer(acTrans,"M-HVAC-EQPM-NEWW",8);
  4. acCurDb.Clayer = acLyrTbl["M-HVAC-EQPM-NEWW"];
  5. ...
  6.  

If you want to create a class, you require a data structure which cannot be supported by standard collections.  Your layer system could possibly be shown in this way but you would be surprised how much data you can access through dictionaries etc. without requiring a special class developed.
Title: Re: Layers and layer names
Post by: JONTHEPOPE on July 10, 2014, 11:00:57 PM
Thanks for the very clean explanation and example, I'll see if I can muscle through an extension method.