Author Topic: Can we get the LayerTableRecord straight with layerName from LayerTable  (Read 3076 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Hello guys.

Is there any better way to figure out if the layer name is equal to "0" in the down posted codes ?
Codes working without any error but I receive a warning in the error list says: ( possible mistaken empty statement ) but this message does not hurt and the program built successfully.

Code - C#: [Select]
  1. LayerTable tbl = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  2.                     foreach (ObjectId item in tbl)
  3.                     {
  4.                         LayerTableRecord Layrec = (LayerTableRecord)tr.GetObject(item, OpenMode.ForWrite);
  5.                         if (Layrec.Name == "0"); // This semicolon is underlined in green color which related to the error that I am referring to.
  6.                         {  
  7.                             Layrec.LineWeight = LineWeight.LineWeight005;
  8.                             break;
  9.                         }
  10.                     }      
  11.  

Can I get the LayerTableRecord straight from the layerTableLayer without looping into the table ?

Many thanks in advance.

Vildar

  • Mosquito
  • Posts: 2
  • AutoCAD C#
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #1 on: January 14, 2016, 02:28:37 AM »
Code - C#: [Select]
  1. LayerTableRecord Layrec = (LayerTableRecord)tbl["0"].GetObject(OpenMode.ForWrite);

Coder

  • Swamp Rat
  • Posts: 827
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #2 on: January 14, 2016, 02:47:57 AM »
Code - C#: [Select]
  1. LayerTableRecord Layrec = (LayerTableRecord)tbl["0"].GetObject(OpenMode.ForWrite);

Great , thank you.

What about searching for a bunch of layer names ? because the same underlined error message appears again.
example:

Code - C#: [Select]
  1.             using (Transaction tr = db.TransactionManager.StartTransaction())
  2.             {
  3.                 try
  4.                 {
  5.                     List<string> LayerNames = new List<string> { "Layer1", "layer2"};
  6.                     LayerTable tbl = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  7.                     foreach (ObjectId item in tbl)
  8.                     {
  9.                         LayerTableRecord Layrec = (LayerTableRecord)tr.GetObject(item, OpenMode.ForWrite);
  10.                         if (LayerNames.Contains(Layrec.Name));
  11.                         {
  12.                             Layrec.LineWeight = LineWeight.LineWeight005;
  13.                             break;
  14.                         }  
  15.                     }
  16.                 }
  17.                 catch (System.Exception ex)
  18.                 {
  19.                     ed.WriteMessage("\nError : " + ex.Message);
  20.                 }
  21.                 tr.Commit();
  22.             }
  23.  
  24.  

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #3 on: January 14, 2016, 02:59:56 AM »

Check the MSDN help.
Remove the  semicolon after the if conditional test.

Otherwise the block of code is run either way ..

Look at this :

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #4 on: January 14, 2016, 03:12:05 AM »


Reference Compiler Warning (level 3) CS0642 :
https://msdn.microsoft.com/en-us/library/9x19t380.aspx
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Coder

  • Swamp Rat
  • Posts: 827
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #5 on: January 14, 2016, 03:19:06 AM »
Thank you so much Kerry for your help ,
Now the culprit is known.  :wink:

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #6 on: January 14, 2016, 01:38:38 PM »
For SymbolTableRecords like LayerTableRecord, BlockTableRecord, etc.... the name is stored inside the object so you have open the object it to read its name.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #7 on: January 14, 2016, 10:52:39 PM »
Coder,
Now that the error is rectified,
I took a moment to look at what you were actually trying to achieve.

Your routine could be more economical. At the moment you are iterating the entire LayerTable and only acting in 2 instances.

The attached code will iterate the smaller list LayerNames  check if the layer exists and act accordingly.

This is considerably less processor intensive.

For giggles I also added an initializer message.

Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4.  
  5. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  6.  
  7. [assembly: CommandClass(typeof(FindLayer.MyCommands))]
  8.  
  9. namespace FindLayer {
  10.     using System.Collections.Generic;
  11.  
  12.     public class MyCommands {
  13.  
  14.         [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  15.         public void MyCommand() {
  16.             var doc = AcadApp.DocumentManager.MdiActiveDocument;
  17.             var ed = doc.Editor;
  18.             var db = doc.Database;
  19.             List<string> layerNames = new List<string> { "Layer1", "layer2" };
  20.  
  21.             using(var tr = db.TransactionManager.StartTransaction()) {
  22.                 try {
  23.                     var layerTbl = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  24.                     foreach(string layerName in layerNames) {
  25.                         if(layerTbl.Has(layerName)) {
  26.                             var layerRecord = (LayerTableRecord)layerTbl[layerName].GetObject(OpenMode.ForWrite);
  27.                             layerRecord.LineWeight = LineWeight.LineWeight005;
  28.                         }
  29.                     }
  30.                 }
  31.                 catch(System.Exception ex) {
  32.                     ed.WriteMessage("\nError : " + ex.Message);
  33.                 }
  34.                 tr.Commit();
  35.             }
  36.         }
  37.     }
  38.  
  39.     public class InitMyCommands : Autodesk.AutoCAD.Runtime.IExtensionApplication {
  40.         public void Initialize() {
  41.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  42.                 "\n\t\t\t*** type MyCommand to modify Layer1 and Layer2 LineWeight to 0.05 \t***");
  43.         }
  44.  
  45.         public void Terminate() {
  46.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Cleaning up...");
  47.         }
  48.     }
  49. }
  50.  
  51.  

Add Piccy:
« Last Edit: January 14, 2016, 11:20:33 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Coder

  • Swamp Rat
  • Posts: 827
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #8 on: January 15, 2016, 04:19:35 AM »
For SymbolTableRecords like LayerTableRecord, BlockTableRecord, etc.... the name is stored inside the object so you have open the object it to read its name.

Thank you jeff H , actually that wasn't clear to me until Vildar posted the answer that I was looking for.

Coder,
Now that the error is rectified,
I took a moment to look at what you were actually trying to achieve.

Your routine could be more economical. At the moment you are iterating the entire LayerTable and only acting in 2 instances.

The attached code will iterate the smaller list LayerNames  check if the layer exists and act accordingly.

This is considerably less processor intensive.

Yeah that is beautiful Kerry. thank you for the hard work you did for me.


kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Can we get the LayerTableRecord straight with layerName from LayerTable
« Reply #9 on: January 15, 2016, 04:40:33 AM »
< .. > thank you for the hard work you did for me.

No problems, I just pound the keys really hard until the code bits lines up ....
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.