Author Topic: Second Add-in Ever... please comment  (Read 1868 times)

0 Members and 1 Guest are viewing this topic.

bchapman

  • Guest
Second Add-in Ever... please comment
« on: January 31, 2013, 02:19:18 AM »
I  am proud to say I have written my second addin.  Will you pro's please give me your input? Constructive criticism is a good thing :) Thanks!

Code: [Select]
namespace BCCGETSTAOFF
{
    public class BCCGETSTAOFF
    {
        [CommandMethod("BCC:GETSTAOFF")]
        public static void BCCSTAOFF()
        {
            Document Doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Doc.Editor;
            PromptEntityOptions BCCGETALIGNMENT = new PromptEntityOptions("\nSelect an Alignment Object: ");
            PromptEntityResult resalignment = ed.GetEntity(BCCGETALIGNMENT);
            ObjectId alignmentId = resalignment.ObjectId;
            var BCCSelectedPoint = ed.GetPoint("\nSelect Point: ");
            Point3d BCCPNT = BCCSelectedPoint.Value;
            var BCCPNTX = BCCPNT.X;
            var BCCPNTY = BCCPNT.Y;
            ed.WriteMessage("\nX={0},Y={1}", BCCPNTX, BCCPNTY);

            using (Transaction ts = Application.DocumentManager.MdiActiveDocument.Database
            .TransactionManager.StartTransaction())
            {
                Alignment alignment = alignmentId.GetObject(OpenMode.ForRead) as Alignment;
                ed.WriteMessage("\nAlignment name: {0}", alignment.Name);
                double FNLSTA = 0.0;
                double FNLOFF = 0.0;
                alignment.StationOffset (BCCPNT.X, BCCPNT.Y, ref FNLSTA, ref FNLOFF);
                ed.WriteMessage("\nSta:{0},Off:{1}", FNLSTA, FNLOFF);

               
            }
       
            }
    }
}
« Last Edit: January 31, 2013, 04:45:21 AM by BC_in_NV »

TheMaster

  • Guest
Re: Second Add-in Ever... please comment
« Reply #1 on: January 31, 2013, 05:57:01 AM »
I  am proud to say I have written my second addin.  Will you pro's please give me your input? Constructive criticism is a good thing :) Thanks!

Code: [Select]
namespace BCCGETSTAOFF
{
    public class BCCGETSTAOFF
    {
        [CommandMethod("BCC:GETSTAOFF")]
        public static void BCCSTAOFF()
        {
            Document Doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Doc.Editor;
            PromptEntityOptions BCCGETALIGNMENT = new PromptEntityOptions("\nSelect an Alignment Object: ");
            PromptEntityResult resalignment = ed.GetEntity(BCCGETALIGNMENT);
            ObjectId alignmentId = resalignment.ObjectId;
            var BCCSelectedPoint = ed.GetPoint("\nSelect Point: ");
            Point3d BCCPNT = BCCSelectedPoint.Value;
            var BCCPNTX = BCCPNT.X;
            var BCCPNTY = BCCPNT.Y;
            ed.WriteMessage("\nX={0},Y={1}", BCCPNTX, BCCPNTY);

            using (Transaction ts = Application.DocumentManager.MdiActiveDocument.Database
            .TransactionManager.StartTransaction())
            {
                Alignment alignment = alignmentId.GetObject(OpenMode.ForRead) as Alignment;
                ed.WriteMessage("\nAlignment name: {0}", alignment.Name);
                double FNLSTA = 0.0;
                double FNLOFF = 0.0;
                alignment.StationOffset (BCCPNT.X, BCCPNT.Y, ref FNLSTA, ref FNLOFF);
                ed.WriteMessage("\nSta:{0},Off:{1}", FNLSTA, FNLOFF);

               
            }
       
            }
    }
}

Try to use the variables you declare in your code, rather than fetching them again.

For example, you set the active document to a variable, but then you use Application.DocumentManager.MdiActiveDocument... to start a transaction.  Others may be more willing to look at code that's succinct and doesn't use fully-qualified names

Unless your code is designed to be usable from some other context besides a command, you should use the Document's TransactionManager rather than the Database's TransactionManager. The former will ensure that graphics are updated in all cases, but the latter may not always do that.
« Last Edit: January 31, 2013, 06:01:39 AM by TT »

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Second Add-in Ever... please comment
« Reply #2 on: January 31, 2013, 01:19:57 PM »
Unless your code is designed to be usable from some other context besides a command, you should use the Document's TransactionManager rather than the Database's TransactionManager. The former will ensure that graphics are updated in all cases, but the latter may not always do that.

Thanks TT! I've become accustomed to using the Database TransactionManager because many of my routines run on drawing sets, but I've been forced to perform manual regen in some situations as a result.

bchapman

  • Guest
Re: Second Add-in Ever... please comment
« Reply #3 on: January 31, 2013, 03:24:44 PM »
Thanks TT!  I'll give that a shot...using the long names to help myself for now... should probably switch before it becomes habit though lol.

Much appreciated.