Author Topic: Making use of ExtensionMethods and generics  (Read 1568 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • class keyThumper<T>:ILazy<T>
Making use of ExtensionMethods and generics
« on: April 28, 2022, 12:52:36 AM »
I finally found some time to study a couple lectures by Scott McFarlane from AU2015.
https://www.autodesk.com/autodesk-university/class/Being-Remarkable-C-NET-AutoCAD-Developer-2015

Each of these 4 methods essentially produce the same result. Pretty neat!

Code - C#: [Select]
  1. // (C) Copyright 2022 by  kdub
  2. //
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Kdub.Common;
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  12.  
  13. [assembly: CommandClass(typeof(AutoCAD2023_ExtensionTest_01.TestCommands))]
  14.  
  15. namespace AutoCAD2023_ExtensionTest_01
  16. {
  17.     public class TestCommands
  18.     {
  19.         [CommandMethod("Test01")]
  20.         public static void Test01()
  21.         {
  22.             AddLine_01();
  23.         }
  24.         [CommandMethod("Test02")]
  25.         public static void Test02()
  26.         {
  27.             AddLine_02();
  28.         }
  29.         [CommandMethod("Test03")]
  30.         public static void Test03()
  31.         {
  32.             AddLine_03();
  33.         }
  34.  
  35.         [CommandMethod("Test04")]
  36.         public static void Test04()
  37.         {
  38.             AddLine_04();
  39.         }
  40.  
  41.  
  42.         /// <summary>
  43.         /// Active class and .UsingModelSpace()
  44.         /// and Create<T>()>
  45.         /// </summary>
  46.         internal static void AddLine_04()
  47.         {
  48.             // could be returned to caller.
  49.             var objId = ObjectId.Null;
  50.  
  51.             Active.Database.UsingModelSpace(
  52.                 (transaction, modelSpace) =>
  53.                 {
  54.                     objId = modelSpace.Create<Line>(
  55.                         transaction, acLine =>
  56.                         {
  57.                             acLine.StartPoint = new Point3d(500, 800, 0);
  58.                             acLine.EndPoint = new Point3d(1200, 600, 0);
  59.                             acLine.ColorIndex = 4;
  60.                         });
  61.                 });
  62.         }
  63.  
  64.  
  65.         /// <summary>
  66.         /// Active class and .UsingModelSpace()
  67.         /// </summary>
  68.         internal static void AddLine_03()
  69.         {
  70.             // Start a transaction            
  71.             Active.Database.UsingModelSpace(
  72.                 (transaction, modelSpace) =>
  73.             {
  74.                 // Create a line
  75.                 using (Line acLine = new Line(new Point3d(500, 700, 0),
  76.                                              new Point3d(1200, 500, 0)))
  77.                 {
  78.                     // Add the new object to the block table record and the transaction
  79.                     acLine.SetDatabaseDefaults();
  80.                     acLine.ColorIndex = 3;
  81.                     modelSpace.AppendEntity(acLine);
  82.                     transaction.AddNewlyCreatedDBObject(acLine, true);
  83.                 }
  84.             });
  85.         }
  86.  
  87.  
  88.         /// <summary>
  89.         /// Active class and .UsingTransaction()
  90.         /// </summary>
  91.         internal static void AddLine_02()
  92.         {
  93.             // Start a transaction            
  94.             Active.Database.UsingTransaction(transaction =>
  95.            {
  96.                // Open the Database Block table
  97.                var dbBlockTable = (BlockTable)transaction.GetObject(
  98.                   Active.Database.BlockTableId, OpenMode.ForRead);
  99.  
  100.                // Open the Block table record Model space    
  101.                var modelSpaceRecord = (BlockTableRecord)transaction.GetObject(
  102.                   dbBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  103.  
  104.                // Create a line
  105.                using (Line acLine = new Line(new Point3d(500, 600, 0),
  106.                                             new Point3d(1200, 400, 0)))
  107.                {
  108.                    // Add the new object to the block table record and the transaction
  109.                    acLine.SetDatabaseDefaults();
  110.                    acLine.ColorIndex = 2;
  111.                    modelSpaceRecord.AppendEntity(acLine);
  112.                    transaction.AddNewlyCreatedDBObject(acLine, true);
  113.                }
  114.            });
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Traditional code
  119.         /// </summary>
  120.         internal static void AddLine_01()
  121.         {
  122.             // Get the current document and database
  123.             var document = AcadApp.DocumentManager.MdiActiveDocument;
  124.             var database = document.Database;
  125.  
  126.             // Start a transaction
  127.             using (Transaction transaction = database.TransactionManager.StartTransaction())
  128.             {
  129.                 // Open the Database Block table
  130.                 var dbBlockTable = (BlockTable)transaction.GetObject(
  131.                     database.BlockTableId, OpenMode.ForRead);
  132.  
  133.                 // Open the Block table record Model space    
  134.                 var modelSpaceRecord = (BlockTableRecord)transaction.GetObject(
  135.                     dbBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  136.  
  137.                 // Create a line
  138.                 using (Line acLine = new Line(new Point3d(500, 500, 0),
  139.                                               new Point3d(1200, 300, 0)))
  140.                 {
  141.                     // Add the new object to the block table record and the transaction
  142.                     acLine.SetDatabaseDefaults();
  143.                     acLine.ColorIndex = 1;
  144.                     modelSpaceRecord.AppendEntity(acLine);
  145.                     transaction.AddNewlyCreatedDBObject(acLine, true);
  146.                 }
  147.  
  148.                 // Save the new object to the database
  149.                 transaction.Commit();
  150.             }
  151.         }
  152.     }
  153. }
  154.  
  155.  
  156.  


Code - C#: [Select]
  1.  
  2. using Autodesk.AutoCAD.Runtime;
  3. using Kdub.Common;
  4.  
  5. using System;
  6.  
  7. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  8.  
  9. [assembly: ExtensionApplication(typeof(AutoCAD2023_ExtensionTest_01.Initialization))]
  10.  
  11. namespace AutoCAD2023_ExtensionTest_01
  12. {
  13.     public class Initialization : IExtensionApplication
  14.     {
  15.         public void Initialize()
  16.         {
  17.             AcadApp.Idle += OnIdle;
  18.         }
  19.  
  20.         private void OnIdle(object sender, EventArgs e)
  21.         {
  22.             if (Active.Document != null)
  23.             {
  24.                 AcadApp.Idle -= OnIdle;
  25.                 Active.WriteMessage("\nAutoCAD2023_ExtensionTest_01 loaded.\n");
  26.                 Active.WriteMessage("Get on with it !!\n");
  27.  
  28.                 Active.WriteMessage("\nCOMMANDS:\n");
  29.                 Active.WriteMessage("Test01 :- DrawLine Traditional\n");
  30.                 Active.WriteMessage("Test02 :- DrawLine usingTransaction and Active\n");
  31.                 Active.WriteMessage("Test03 :- DrawLine Active class and .UsingModelSpace()\n");
  32.                 Active.WriteMessage("Test04 :- DrawLine ^^ and Create<T>()>\n");
  33.             }
  34.         }
  35.  
  36.         public void Terminate()
  37.         { }
  38.     }
  39. }
  40.  
  41.  
« Last Edit: April 28, 2022, 01:01:16 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.

pkohut

  • Bull Frog
  • Posts: 483
Re: Making use of ExtensionMethods and generics
« Reply #1 on: April 30, 2022, 02:02:29 AM »
Thanks kdub, these examples are pretty excellent.
New tread (not retired) - public repo at https://github.com/pkohut