Author Topic: Best way of coding  (Read 1490 times)

0 Members and 1 Guest are viewing this topic.

WOWENS

  • Newt
  • Posts: 54
Best way of coding
« on: November 08, 2012, 11:31:20 AM »
as you can tell from post ogf my post's i'm not a programer, just a drafter that tries to write code. that being said if using the com what would be better
option1 or option2. no i'm not trying to do a Regen it's just an example.
any guidance will be appreciated

Option 1
Public ReadOnly Property ThisDrawing() As AcadDocument
     Get
          Return DirectCast(Application.DocumentManager.MdiActiveDocument.AcadDocument, AcadDocument)
     End Get
End Property


Public Sub Test1()
     ThisDrawing.Regen(Common.AcRegenType.acAllViewports)
End sub


Option 2
Public Sub Test2()
     Dim ThisDrawing As AcadDocument = Application.DocumentManager.MdiActiveDocument.AcadDocument

     ThisDrawing.Regen(Common.AcRegenType.acAllViewports)

     System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisDrawing)
End sub

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Re: Best way of coding
« Reply #1 on: November 10, 2012, 09:31:09 AM »
For me, and you can bet others have a different opinion, it depends upon how often you need to use ThisDocument in your code.

If you find that you frequently get the AcadDocument and put it into a variable, then perhaps it might be best to create a private global in your class that is filled with the current document when the class is instantiated (and disposed with the class). This way it can be utilized by any code using the class ... it also makes the code more readable is you can eliminate many complex calls with a simple property

However, if you only ever need to use AcadDocument once or infrequently, your second option will work fine as it is self explanatory.

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

WOWENS

  • Newt
  • Posts: 54
Re: Best way of coding
« Reply #2 on: November 10, 2012, 12:24:54 PM »
Thank you.

TheMaster

  • Guest
Re: Best way of coding
« Reply #3 on: November 10, 2012, 08:46:01 PM »
as you can tell from post ogf my post's i'm not a programer, just a drafter that tries to write code. that being said if using the com what would be better
option1 or option2. no i'm not trying to do a Regen it's just an example.
any guidance will be appreciated

Option 1
Public ReadOnly Property ThisDrawing() As AcadDocument
     Get
          Return DirectCast(Application.DocumentManager.MdiActiveDocument.AcadDocument, AcadDocument)
     End Get
End Property


Public Sub Test1()
     ThisDrawing.Regen(Common.AcRegenType.acAllViewports)
End sub


Option 2
Public Sub Test2()
     Dim ThisDrawing As AcadDocument = Application.DocumentManager.MdiActiveDocument.AcadDocument

     ThisDrawing.Regen(Common.AcRegenType.acAllViewports)

     System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisDrawing)
End sub

They're essentially the same, except that you're eliminating a bit of code.

Normally, when the just-in-time compiler encounters a simple method like the one you show, it will 'inline' the method in the compiled code, meaning that the property you use in Option 1 will end up generating almost the exact same executable code that would be generated if you had directly accessed the AcadDocument, as you do in Option 2.

As to your question about what would be better, my answer would be neither of the two options you show. The reason is, each of them require three property references to get the active AcadDocument object. 

The best option IMO, is to cache a reference to the AcadApplication COM object in a shared member of your class, and just read its ActiveDocument property when you need the active AcadDocument, which entails only one property reference rather than three.

The other issue is that your Property in option 1 does not have any dependence on any instance of your class or instance data, so you should probably make it Shared, as that would allow it to be used from instance members and other Shared members of your class.
« Last Edit: November 10, 2012, 09:36:25 PM by TT »

fixo

  • Guest
Re: Best way of coding
« Reply #4 on: November 12, 2012, 04:39:20 PM »
Not sure about it would be usefull but this way it works

Code: [Select]
public static class AcadInteropApp
    {
       public  static Autodesk.AutoCAD.Interop.AcadApplication AcadApp =
            (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

        private static AcadDocument acdoc()
        {
            if (AcadApp != null)
            {
                return AcadApp.ActiveDocument;
            }
            else
            {
                return null;
            }
        }
    }


        usage:

Code: [Select]
        [CommandMethod("myAdoc")]
        public static void doit()
        {
            Autodesk.AutoCAD.Interop.AcadDocument doc = AcadInteropApp.AcadApp.ActiveDocument;
          // do your usual VBA-like work here:
            AcadCircle cir=  doc.ModelSpace.AddCircle(new double[] { 100, 200, 0 }, 25.4);
            AcadAcCmColor accol= new AcadAcCmColor();
            accol.ColorMethod = AcColorMethod.acColorMethodByACI;
            accol.ColorIndex = AcColor.acMagenta;
            cir.TrueColor = accol;
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(doc.FullName);
            accol = null;
            doc = null;
           // AcadInteropApp.AcadApp.Quit();

        }

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Best way of coding
« Reply #5 on: November 12, 2012, 05:29:23 PM »
Personally I use the word com in variables and methods relying on Autodesk.AutoCAD.Interop

ie:
public  static Autodesk.AutoCAD.Interop.AcadApplication AcadComApp = < ... >

private static AcadDocument acComDoc()  {
<.. >
}

.. helps me identify the derivation at a glance  ... very handy 6 months later :)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

WOWENS

  • Newt
  • Posts: 54
Re: Best way of coding
« Reply #6 on: November 13, 2012, 11:07:49 AM »
thank you all so much for your input.

fixo

  • Guest
Re: Best way of coding
« Reply #7 on: November 13, 2012, 01:41:58 PM »
Personally I use the word com in variables and methods relying on Autodesk.AutoCAD.Interop

ie:
public  static Autodesk.AutoCAD.Interop.AcadApplication AcadComApp = < ... >

private static AcadDocument acComDoc()  {
<.. >
}


.. helps me identify the derivation at a glance  ... very handy 6 months later :)
Thanks to pointing me that out I will be keep it in mind, good idea
Regards,