Author Topic: Select on layer .. ensure thawed layer  (Read 2400 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Select on layer .. ensure thawed layer
« on: July 14, 2019, 12:09:55 AM »
Anyone care to break this.

Select objects on a specific layer. Ensure the layer is Thawed.
Sample Drawing attached.

Comments welcome.

added: EDIT Code style changes as per Gilles comments.

Code - C#: [Select]
  1. // (C) Codehimbelonga kdub 2019-07-14
  2. //
  3. using System;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using Autodesk.AutoCAD.Geometry;
  8. using Autodesk.AutoCAD.Runtime;
  9. using cadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  10.  
  11.  
  12. [assembly: CommandClass(typeof(Test190714.MyCommands))]
  13.  
  14. namespace Test190714
  15. {
  16.     public class MyCommands
  17.     {
  18.         [CommandMethod("Doit", CommandFlags.Modal)]
  19.         public void MyCommand()
  20.         {
  21.             var layerName = "Cabling";
  22.  
  23.             var isLayerThawed = FreezeLayer(layerName, freeze: false);
  24.             SelectionSet selectedLines;
  25.  
  26.             if (isLayerThawed) {
  27.                 selectedLines = GrabLines(layerName);
  28.             } else {
  29.                 Active.WriteMessage($"Layer '{layerName}' is not accessable");
  30.                 return;
  31.             }
  32.  
  33.             if (selectedLines != null && selectedLines.Count > 0) {
  34.                 OutputLineData(selectedLines);
  35.             } else {
  36.                 Active.WriteMessage("Nothing to report");
  37.             }
  38.         }
  39.  
  40.         /// <summary>Freezes the layer.</summary>
  41.         /// <param name="layerName">Name of the layer.</param>
  42.         /// <param name="freeze">if set to <c>true</c> [freeze].</param>
  43.         /// <returns></returns>
  44.         private bool FreezeLayer(string layerName, bool freeze)
  45.         {
  46.             using (var tr = Active.Database.TransactionManager.StartTransaction()) {
  47.                 var layerTable = (LayerTable)tr.GetObject(Active.Database.LayerTableId,
  48.                                   OpenMode.ForRead);
  49.  
  50.                 if (!layerTable.Has(layerName)) {
  51.                     // No Layer, So can't be frozen or thawed
  52.                     return false;
  53.                 }
  54.  
  55.                 var layerRecord = (LayerTableRecord)tr.GetObject(layerTable[layerName],
  56.                                    OpenMode.ForWrite);
  57.  
  58.                 if (layerRecord.Id != Active.Database.Clayer) {
  59.                     layerRecord.IsFrozen = freeze;
  60.                 }
  61.                 tr.Commit();
  62.                 Active.Editor.Regen();
  63.             }
  64.             return true;
  65.         }
  66.  
  67.         /// <summary>Grabs the lines.</summary>
  68.         /// <param name="layerName">Name of the layer.</param>
  69.         /// <returns></returns>
  70.         public SelectionSet GrabLines(string layerName)
  71.         {
  72.             TypedValue[] filterValues = new[] {
  73.                 new TypedValue((int)DxfCode.Start, "LINE"),
  74.                 new TypedValue((int)DxfCode.LayerName, layerName)
  75.             };
  76.             var selectionFilter = new SelectionFilter(filterValues);
  77.             var selectionOptions = new PromptSelectionOptions {
  78.                 MessageForAdding = $"Select Lines on Layer {layerName}",
  79.                 AllowDuplicates = true
  80.             };
  81.             var selectionResult = Active.Editor.GetSelection(selectionOptions, selectionFilter);
  82.  
  83.             if (!(selectionResult.Status == PromptStatus.OK)) {
  84.                 return null;
  85.             }
  86.  
  87.             return selectionResult.Value;
  88.         }
  89.  
  90.  
  91.         /// <summary>Outputs the line data.</summary>
  92.         /// <param name="ss">The ss.</param>
  93.         private void OutputLineData(SelectionSet ss)
  94.         {
  95.             ObjectId[] idarray = ss.GetObjectIds();
  96.             using (Transaction tr = Active.Database.TransactionManager.StartTransaction()) {
  97.                 foreach (var id in idarray) {
  98.                     var line = (Line)tr.GetObject(id, OpenMode.ForRead, true);
  99.                     Active.WriteMessage(
  100.                         $"\nYou selected: {line.GetType().Name} with Length = {line.Length }");
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     /// <summary>
  107.     /// Provides easy access to several "active" objects in the
  108.     /// AutoCAD runtime environment.
  109.     /// Thanks to : Scott McFarlane - Programming AutoCAD with C#: Best Practices
  110.     /// </summary>
  111.     public static class Active
  112.     {
  113.         public static Document Document => cadApp.DocumentManager.MdiActiveDocument;
  114.         public static Database Database => Document.Database;
  115.         public static Editor Editor => Document.Editor;
  116.  
  117.         public static void WriteMessage(
  118.             string message)
  119.             => Editor.WriteMessage(message);
  120.  
  121.         public static void WriteMessage(
  122.             string message, params object[] parameter)
  123.             => Editor.WriteMessage(message, parameter);
  124.     }
  125. }
  126.  
  127.  


« Last Edit: July 14, 2019, 03:27:55 AM 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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Select on layer .. ensure thawed layer
« Reply #1 on: July 14, 2019, 03:05:43 AM »
Hi,

Nice code Kerry.
Just 2 things about coding style:

Code - C#: [Select]
  1. if (layerTable.Has(layerName) == false)
reminds me of the verbose VB style.
You'd never write:
Code - C#: [Select]
  1. if (layerTable.Has(layerName) == true)
so why not simply write:
Code - C#: [Select]
  1. if (!layerTable.Has(layerName))

Code - C#: [Select]
  1.             using (Transaction tr = Active.Database.TransactionManager.StartTransaction()) {
  2.                 try {
  3.                     foreach (var id in idarray) {
  4.                         var line = (Line)tr.GetObject(id, OpenMode.ForRead, true);
  5.                         Active.WriteMessage(
  6.                             $"\nYou selected: {line.GetType().Name} with Length = {line.Length }");
  7.                     }
  8.                 }
  9.                 finally {
  10.                     tr.Dispose();
  11.                 }
  12.             }
It seems to me the finally block is useless.
The using statement will take care of disposing of the transaction even if an exception occured. So, as you neither have a catch block, the try{...} also is useless.
Speaking English as a French Frog

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Select on layer .. ensure thawed layer
« Reply #2 on: July 14, 2019, 03:13:23 AM »
Yes, you are correct :)

Thanks for the scan Gilles


Edit: Code revised as commented.
« Last Edit: July 14, 2019, 03:36:01 AM 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.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Select on layer .. ensure thawed layer
« Reply #3 on: July 14, 2019, 03:48:43 AM »
@gile
I liked your option of using an OpenCloseTransaction with this :

Code - C#: [Select]
  1.  
  2.                     var layer = (LayerTableRecord)tr.GetObject(layerTable["Cabling"], OpenMode.ForRead);
  3.                     if (layer.IsFrozen)
  4.                     {
  5.                         layer.UpgradeOpen();
  6.                         layer.IsFrozen = false;
  7.                     }
  8.  

Very clean !
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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Select on layer .. ensure thawed layer
« Reply #4 on: July 14, 2019, 09:02:00 AM »
Thanks Kerry.
Clean, this is the way I try to write.
Speaking English as a French Frog