locked
code abstraction RRS feed

  • Question

  • using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.EditorInput;
    
    [CommandMethod("AccessSpace")]
    public static void AccessSpace()
    {
        // Get the current document and database
        Document acDoc = Application.DocumentManager.MdiActiveDocument;
        Database acCurDb = acDoc.Database;
    
        // Start a transaction
        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            // Open the Block table for read
            BlockTable acBlkTbl;
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
    
            // Open the Block table record for read
            BlockTableRecord acBlkTblRec;
    
            // Request which table record to open
            PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
            pKeyOpts.Message = "\nEnter which space to create the line in ";
            pKeyOpts.Keywords.Add("Model");
     
    
            PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);
    
            if (pKeyRes.StringResult == "Model")
            {
                // Get the ObjectID for Model space from the Block table
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
            }
    
            // Create a line that starts at 2,5 and ends at 10,7
            Line acLine = new Line(new Point3d(2, 5, 0), new Point3d(10, 7, 0));
    
            acLine.SetDatabaseDefaults();
    
            // Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acLine);
            acTrans.AddNewlyCreatedDBObject(acLine, true);
    
            // Save the new line to the database
            acTrans.Commit();
        }
    }
    is there a way to apply ABSTRACTION to this code, i want it to be more simpe and REUSABLE, if it possible to generalize the code and make it simple and looks elegant ?
    Monday, April 15, 2019 5:30 PM

Answers

  • //In an extension class
    public static class DocumentExtensions
    {
       public static void AddLines ( this Document source, IEnumerable<Line> lines )
       {
          using (var transaction = source.Database.TransactionManager.StartTransaction())
          {
             foreach (var line in lines)
               transaction.AddNewlyCreatedDBObject(line, true);
    
             transaction.Commit();
          };       
       }
    
       public static void AddLine ( this Document source, Line line )
       {
          source.AddLines(new [] { line });
       }
    }
    
    [CommandMethod("AccessSpace")]
    public static void AccessSpace()
    {
        // Get the current document and database
        Document acDoc = Application.DocumentManager.MdiActiveDocument;
        Database acCurDb = acDoc.Database;
    
        //Removed all the code related to the block stuff as it didn't seem relevant for this post nor does it seem to work properly in all cases
    
       // Create a line that starts at 2,5 and ends at 10,7
       Line acLine = new Line(new Point3d(2, 5, 0), new Point3d(10, 7, 0));
       acLine.SetDatabaseDefaults();
    
       // Add the new object
       acDoc.AddLine(acLine);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by essamce Monday, December 16, 2019 9:05 AM
    Sunday, December 15, 2019 8:12 PM

All replies

  • Abstraction doesn't make code simpler. Abstraction helps with reuse. Since you have a single function here there really isn't much room for abstraction because there doesn't seem to be any reuse going on. Abstraction comes into play when you have multiple implementations and can see where they share functionality. Since you have a single method here there really isn't anything to abstract.

    The only things I see that you could refactor that might add some value (if you're doing this elsewhere).

    1) Create extension methods or wrap the transaction stuff to provide helper methods for doing common things like adding a line.

    2) The whole pKeyRes stuff doesn't seem to be used for anything so it seems like it can go away.

    3) Create an extension method for opening a specific block for reading.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 15, 2019 6:13 PM

  • 1) Create extension methods or wrap the transaction stuff to provide helper methods for doing common things like adding a line.

    how can we do that, could you show me some code?
    • Edited by essamce Sunday, December 15, 2019 5:01 AM
    Sunday, December 15, 2019 5:00 AM
  • //In an extension class
    public static class DocumentExtensions
    {
       public static void AddLines ( this Document source, IEnumerable<Line> lines )
       {
          using (var transaction = source.Database.TransactionManager.StartTransaction())
          {
             foreach (var line in lines)
               transaction.AddNewlyCreatedDBObject(line, true);
    
             transaction.Commit();
          };       
       }
    
       public static void AddLine ( this Document source, Line line )
       {
          source.AddLines(new [] { line });
       }
    }
    
    [CommandMethod("AccessSpace")]
    public static void AccessSpace()
    {
        // Get the current document and database
        Document acDoc = Application.DocumentManager.MdiActiveDocument;
        Database acCurDb = acDoc.Database;
    
        //Removed all the code related to the block stuff as it didn't seem relevant for this post nor does it seem to work properly in all cases
    
       // Create a line that starts at 2,5 and ends at 10,7
       Line acLine = new Line(new Point3d(2, 5, 0), new Point3d(10, 7, 0));
       acLine.SetDatabaseDefaults();
    
       // Add the new object
       acDoc.AddLine(acLine);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by essamce Monday, December 16, 2019 9:05 AM
    Sunday, December 15, 2019 8:12 PM
  • hi Michael

    that's what I was asking about. 

    Thank you so much 

    Monday, December 16, 2019 9:05 AM