Author Topic: Object Events  (Read 12666 times)

0 Members and 1 Guest are viewing this topic.

BlackBox

  • King Gator
  • Posts: 3770
Re: Object Events
« Reply #15 on: October 07, 2012, 05:29:52 AM »
I am just learning this API, and am here to learn good coding practices, so any and all constructive criticism is welcomed.

In my other thread the topic of having only one instance of the event handler(s) was discussed, and it was my understanding by all of the comments that when registering an event adding immediately after removing was not uncommon.

Sorry to not quote specific comments here, I'm on a mobile device at the moment.

** Edit to add - the only alternatives that I recall, were to store each Document to a list for those already having registered the desired events, or to add a pseudo Loaded Propery to a Class for same.

Regardless, I want any code to be as efficient as possible within reason given my inexperience with the API. I have no expectation that anyone would just offer me a re-write in is entirety, but perhaps if you could describe where I have gone wrong, I can attempt to revise the code myself?

TIA
« Last Edit: October 07, 2012, 05:34:44 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Object Events
« Reply #16 on: October 07, 2012, 06:18:13 AM »
Tony,
Quote
the removing and then immediately adding back event handlers
This is a practice I use to insure an event handler is not registered more than once for example within a Documentactivated event handler :
Code - C#: [Select]
  1.         void docMan_DocumentActivated(object sender, DocumentCollectionEventArgs e)
  2.         {
  3.             e.Document.CommandEnded -= doc_CommandEnded;
  4.             e.Document.CommandEnded += doc_CommandEnded;
  5.         }
Let me know if you think it is not a good practice and which one one you think is a better one.

RenderMan,
IMO, using both Documentactivated and DocumentCreated handlers is redundant.
If you used DocumentCreated, you do not need to remove the handler before adding it as the handler sould be add only once for a newly opened or created document.
If i do not misunderstand what you are trying to do, you want to register a CommandEnded event handler to all already opened documents when your application is loaded and to all those which may be created after.
Here's a way:
Code - C#: [Select]
  1.     public class Events : IExtensionApplication
  2.     {
  3.         private DocumentCollection acDocs;
  4.  
  5.         public void Initialize()
  6.         {
  7.             acDocs = acApp.DocumentManager;
  8.  
  9.             // register doc_CommandEnded for all already opened documents
  10.             foreach (Document doc in acDocs)
  11.             {
  12.                 doc.CommandEnded += doc_CommandEnded;
  13.             }
  14.  
  15.             // register acDocs_DocumentCreated for all futurely created documents
  16.             acDocs.DocumentCreated += acDocs_DocumentCreated;
  17.         }
  18.  
  19.         public void Terminate()
  20.         {
  21.         }
  22.  
  23.         void acDocs_DocumentCreated(object sender, DocumentCollectionEventArgs e)
  24.         {
  25.             // register doc_CommandEnded for a newly created document
  26.             e.Document.CommandEnded += doc_CommandEnded;
  27.         }
  28.  
  29.         void doc_CommandEnded(object sender, CommandEventArgs e)
  30.         {
  31.             if (Utils.WcMatch(e.GlobalCommandName.ToUpper(),
  32.                "*CHSPACE,*MSPACE,*PSPACE,*LAYOUT_CONTROL,U,*UNDO,*VPMAX,*VPMIN"))
  33.             {
  34.                 Reconcile();
  35.             }
  36.         }
  37.  
  38.         private void Reconcile()
  39.         {
  40.             Document doc = acDocs.MdiActiveDocument;
  41.             Editor ed = doc.Editor;
  42.  
  43.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  44.  
  45.             // Model space
  46.             if (tilemode == 1)
  47.             {
  48.                 ed.WriteMessage("\n** ModelSpace active ** \n");
  49.             }
  50.  
  51.             else
  52.             {
  53.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  54.                 // Pviewport
  55.                 if (cvport > 1)
  56.                 {
  57.                     ed.WriteMessage("\n** PViewport active ** \n");
  58.                 }
  59.  
  60.                 // Paper space
  61.                 else
  62.                 {
  63.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  64.                 }
  65.             }
  66.         }
  67.     }
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Object Events
« Reply #17 on: October 07, 2012, 06:26:29 AM »
Here's, IMO, a simpler way:
Code - C#: [Select]
  1.         public void Initialize()
  2.         {
  3.             LayoutManager.Current.LayoutSwitched += OnLayoutSwitched;
  4.         }
  5.  
  6.         public void Terminate()
  7.         {
  8.         }
  9.  
  10.         void OnLayoutSwitched(object sender, LayoutEventArgs e)
  11.         {
  12.             Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;
  13.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  14.             if (tilemode == 1)
  15.             {
  16.                 ed.WriteMessage("\n** ModelSpace active ** \n");
  17.             }
  18.             else
  19.             {
  20.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  21.                 if (cvport > 1)
  22.                 {
  23.                     ed.WriteMessage("\n** PViewport active ** \n");
  24.                 }
  25.                 else
  26.                 {
  27.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  28.                 }
  29.             }
  30.         }
  31.     }
« Last Edit: October 07, 2012, 10:15:45 AM by gile »
Speaking English as a French Frog

TheMaster

  • Guest
Re: Object Events
« Reply #18 on: October 07, 2012, 08:42:45 AM »
Tony,
Quote
the removing and then immediately adding back event handlers
This is a practice I use to insure an event handler is not registered more than once for example within a Documentactivated event handler :
Code - C#: [Select]
  1.         void docMan_DocumentActivated(object sender, DocumentCollectionEventArgs e)
  2.         {
  3.             e.Document.CommandEnded -= doc_CommandEnded;
  4.             e.Document.CommandEnded += doc_CommandEnded;
  5.         }
Let me know if you think it is not a good practice and which one one you think is a better one.

RenderMan,
IMO, using both Documentactivated and DocumentCreated handlers is redundant.
If you used DocumentCreated, you do not need to remove the handler before adding it as the handler sould be add only once for a newly opened or created document.
If i do not misunderstand what you are trying to do, you want to register a CommandEnded event handler to all already opened documents when your application is loaded and to all those which may be created after.
Here's a way:
Code - C#: [Select]
  1.     public class Events : IExtensionApplication
  2.     {
  3.         private DocumentCollection acDocs;
  4.  
  5.         public void Initialize()
  6.         {
  7.             acDocs = acApp.DocumentManager;
  8.  
  9.             // register doc_CommandEnded for all already opened documents
  10.             foreach (Document doc in acDocs)
  11.             {
  12.                 doc.CommandEnded += doc_CommandEnded;
  13.             }
  14.  
  15.             // register acDocs_DocumentCreated for all futurely created documents
  16.             acDocs.DocumentCreated += acDocs_DocumentCreated;
  17.         }
  18.  
  19.         public void Terminate()
  20.         {
  21.         }
  22.  
  23.         void acDocs_DocumentCreated(object sender, DocumentCollectionEventArgs e)
  24.         {
  25.             // register doc_CommandEnded for a newly created document
  26.             e.Document.CommandEnded += doc_CommandEnded;
  27.         }
  28.  
  29.         void doc_CommandEnded(object sender, CommandEventArgs e)
  30.         {
  31.             if (Utils.WcMatch(e.GlobalCommandName.ToUpper(),
  32.                "*CHSPACE,*MSPACE,*PSPACE,*LAYOUT_CONTROL,U,*UNDO,*VPMAX,*VPMIN"))
  33.             {
  34.                 Reconcile();
  35.             }
  36.         }
  37.  
  38.         private void Reconcile()
  39.         {
  40.             Document doc = acDocs.MdiActiveDocument;
  41.             Editor ed = doc.Editor;
  42.  
  43.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  44.  
  45.             // Model space
  46.             if (tilemode == 1)
  47.             {
  48.                 ed.WriteMessage("\n** ModelSpace active ** \n");
  49.             }
  50.  
  51.             else
  52.             {
  53.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  54.                 // Pviewport
  55.                 if (cvport > 1)
  56.                 {
  57.                     ed.WriteMessage("\n** PViewport active ** \n");
  58.                 }
  59.  
  60.                 // Paper space
  61.                 else
  62.                 {
  63.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  64.                 }
  65.             }
  66.         }
  67.     }

Gile, as I mentioned, I've seen managed objects throw exceptions when trying to remove an event handler that was never added, and I don't recall which events they were, but that's good enough reason for me to avoid removing handlers that weren't added.

I think the best way to deal with that problem is to structure the code so that what must be done once and un-done once is isolated in separate methods, and can be controlled using a boolean field.


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Object Events
« Reply #19 on: October 07, 2012, 10:26:06 AM »
Thank you for your reply Tony.
May be a  try/finally block would be easier than a boolen field in some cases.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Object Events
« Reply #20 on: October 07, 2012, 01:20:08 PM »
RenderMan,

The last code snippet I posted do not catch switches between PViewport and PaperSpace by double clicking.
If you need to catch this too, you can play with both LayoutManager.LayoutSwitched and Application.SystemVariableChanged events:

Code - C#: [Select]
  1. public class Events : IExtensionApplication
  2.     {
  3.         private DocumentCollection acDocs;
  4.  
  5.         public void Initialize()
  6.         {
  7.             acDocs = acApp.DocumentManager;
  8.             LayoutManager.Current.LayoutSwitched += OnLayoutSwitched;
  9.             acApp.SystemVariableChanged += acApp_SystemVariableChanged;
  10.         }
  11.  
  12.         public void Terminate()
  13.         {
  14.         }
  15.  
  16.         void acApp_SystemVariableChanged(
  17.             object sender,
  18.             Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
  19.         {
  20.             if (e.Name == "CVPORT")
  21.             {
  22.                 Editor ed = acDocs.MdiActiveDocument.Editor;
  23.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  24.                 if (cvport > 1)
  25.                 {
  26.                     ed.WriteMessage("\n** PViewport active ** \n");
  27.                 }
  28.                 else
  29.                 {
  30.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  31.                 }
  32.             }
  33.         }
  34.  
  35.         void OnLayoutSwitched(object sender, LayoutEventArgs e)
  36.         {
  37.             Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;
  38.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  39.             if (tilemode == 1)
  40.             {
  41.                 ed.WriteMessage("\n** ModelSpace active ** \n");
  42.             }
  43.             else
  44.             {
  45.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  46.                 if (cvport == 1)
  47.                 {
  48.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  49.                 }
  50.             }
  51.         }
  52.     }
Speaking English as a French Frog

BlackBox

  • King Gator
  • Posts: 3770
Re: Object Events
« Reply #21 on: October 07, 2012, 03:16:09 PM »
First - I just wanted to say thanks, Tony, and Gile.

As for identifying which Document has been registered using a bool Property is one of the samples I first found when lookin into all of this. I'll revisit that.

As for using LayoutSwitched && SystemVariableChanged; this will not function properly in MDI, as when one Document is in ModelSpace, and a second is in Layout with PViewport Active, switching between them does not change a Document level System Variable. Not sure if LayoutSwitched will fire, as I have not tested.

Hence my original need for both the DocumentCreated event (to register), and the DocumentActivated event (to evaluate the current space, or reconcile)... Each performed a different function.

Further adding to the mix, as I am attempting to enhance my VL routine, is that I have just learned that not even .NET API has direct exposure to the Preferences Objects, and must use COM first before accessing PreferencesDisplay, etc.

Not a big deal on the surface, but I am again disappointed to learn that I cannot simply monitor a Preferences*Modified event. If I am correct in my understanding, then even my .NET adaptation suffers similar limitations for accounting for user changes via Options dialog or VL functions which change Preferences* Objects outside of this plug-in.

** Edit to add - I know that I could simply monitor the Options CommandEnded && CommandWillStart, and all Lisp* event(s), but I guess I feel that I shouldn't have to. Such a critical object (such as preferences) should have a means by which to monitor changes. Am I just oblivious to a known method for doing this?

I'm going to have to give this more thought moving forward. Thank you again, for all of your assistance and feedback.
« Last Edit: October 07, 2012, 03:25:08 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Object Events
« Reply #22 on: October 07, 2012, 03:34:31 PM »
I've only, what I consider to be, recently started learning the .NET API... Given that so much of this is new to me anyway, I wonder if I should just start learning C++/ObjectARX. I know that there's development concepts and some syntax that overlap or are similar respectively; perhaps if I'm going to start the long, long process of learning a higher level language, I should just go for the biggest, baddest one.

I know that Tony has ARX experience (not sure about Jeff & Gile?)... Perhaps you could offer some input? I'm seeking to transition from CAD Production into more development. I'm sure I must be over simplifying the tasks for the purposes of my questions here, but I could use some guidance, as I simply do not know enough for myself (yet).

Cheers! :beer:
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Object Events
« Reply #23 on: October 07, 2012, 04:40:51 PM »
Quote
As for using LayoutSwitched && SystemVariableChanged; this will not function properly in MDI, as when one Document is in ModelSpace, and a second is in Layout with PViewport Active, switching between them does not change a Document level System Variable.

If needed, you can handle the DocumentManager.DocumentActivated too:

Code - C#: [Select]
  1. public class Events : IExtensionApplication
  2.     {
  3.         private DocumentCollection acDocs;
  4.  
  5.         public void Initialize()
  6.         {
  7.             acDocs = acApp.DocumentManager;
  8.             LayoutManager.Current.LayoutSwitched += OnLayoutSwitched;
  9.             acApp.SystemVariableChanged += acApp_SystemVariableChanged;
  10.             acDocs.DocumentActivated += acDocs_DocumentActivated;
  11.         }
  12.  
  13.         public void Terminate()
  14.         {
  15.         }
  16.  
  17.         void acDocs_DocumentActivated(object sender, DocumentCollectionEventArgs e)
  18.         {
  19.             Document doc = e.Document;
  20.             Editor ed = doc.Editor;
  21.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  22.             if (tilemode == 1)
  23.             {
  24.                 ed.WriteMessage("\n** ModelSpace active ** \n");
  25.             }
  26.             else
  27.             {
  28.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  29.                 if (cvport == 1)
  30.                 {
  31.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  32.                 }
  33.                 else
  34.                 {
  35.                     ed.WriteMessage("\n** PViewport active ** \n");
  36.                 }
  37.             }
  38.  
  39.         }
  40.  
  41.         void acApp_SystemVariableChanged(
  42.             object sender,
  43.             Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
  44.         {
  45.             if (e.Name == "CVPORT")
  46.             {
  47.                 Editor ed = acDocs.MdiActiveDocument.Editor;
  48.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  49.                 if (cvport == 1)
  50.                 {
  51.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  52.                 }
  53.                 else
  54.                 {
  55.                     ed.WriteMessage("\n** PViewport active ** \n");
  56.                 }
  57.             }
  58.         }
  59.  
  60.         void OnLayoutSwitched(object sender, LayoutEventArgs e)
  61.         {
  62.             Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor;
  63.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  64.             if (tilemode == 1)
  65.             {
  66.                 ed.WriteMessage("\n** ModelSpace active ** \n");
  67.             }
  68.             else
  69.             {
  70.                 short cvport = (short)acApp.GetSystemVariable("CVPORT");
  71.                 if (cvport == 1)
  72.                 {
  73.                     ed.WriteMessage("\n** PaperSpace active ** \n");
  74.                 }
  75.             }
  76.         }
  77.     }
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Object Events
« Reply #24 on: October 07, 2012, 05:26:08 PM »
The same as upper with some refactorisation:

Code - C#: [Select]
  1.     public class Events : IExtensionApplication
  2.     {
  3.         private DocumentCollection acDocs;
  4.  
  5.         public void Initialize()
  6.         {
  7.             acDocs = acApp.DocumentManager;
  8.             LayoutManager.Current.LayoutSwitched += OnLayoutSwitched;
  9.             acApp.SystemVariableChanged += OnSystemVariableChanged;
  10.             acDocs.DocumentActivated += OnDocumentActivated;
  11.         }
  12.  
  13.         public void Terminate()
  14.         {
  15.         }
  16.  
  17.         private void OnDocumentActivated(object sender, DocumentCollectionEventArgs e)
  18.         {
  19.             if (!(IsModelSpace() || IsPaperSpace()))
  20.             {
  21.                 SendMessage("PViewport");
  22.             }
  23.         }
  24.  
  25.         private void OnSystemVariableChanged(
  26.             object sender,
  27.             Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
  28.         {
  29.             if (e.Name == "CVPORT" && !IsPaperSpace())
  30.             {
  31.                 SendMessage("PViewport");
  32.             }
  33.         }
  34.  
  35.         private void OnLayoutSwitched(object sender, LayoutEventArgs e)
  36.         {
  37.             if (!IsModelSpace())
  38.             {
  39.                 IsPaperSpace();
  40.             }
  41.         }
  42.  
  43.         private bool IsModelSpace()
  44.         {
  45.             short tilemode = (short)acApp.GetSystemVariable("TILEMODE");
  46.             if (tilemode == 1)
  47.             {
  48.                 SendMessage("ModelSpace");
  49.                 return true;
  50.             }
  51.             return false;
  52.         }
  53.  
  54.         private bool IsPaperSpace()
  55.         {
  56.             short cvport = (short)acApp.GetSystemVariable("CVPORT");
  57.             if (cvport == 1)
  58.             {
  59.                 SendMessage("PaperSpace");
  60.                 return true;
  61.             }
  62.             return false;
  63.         }
  64.  
  65.         private void SendMessage(string msg)
  66.         {
  67.             acDocs.MdiActiveDocument.Editor.WriteMessage("\n** {0} active ** \n", msg);
  68.         }
  69.     }
« Last Edit: October 07, 2012, 05:36:00 PM by gile »
Speaking English as a French Frog

TheMaster

  • Guest
Re: Object Events
« Reply #25 on: October 07, 2012, 07:40:16 PM »

Further adding to the mix, as I am attempting to enhance my VL routine, is that I have just learned that not even .NET API has direct exposure to the Preferences Objects, and must use COM first before accessing PreferencesDisplay, etc.

Not a big deal on the surface, but I am again disappointed to learn that I cannot simply monitor a Preferences*Modified event. If I am correct in my understanding, then even my .NET adaptation suffers similar limitations for accounting for user changes via Options dialog or VL functions which change Preferences* Objects outside of this plug-in.


Application.UserConfigurationManager.CurrentProfileChanged  perhaps?

BlackBox

  • King Gator
  • Posts: 3770
Re: Object Events
« Reply #26 on: October 07, 2012, 08:14:18 PM »
I'll have to look into Application.UserConfigurationManager.CurrentProfileChanged; as always, thank you kindly for the suggestion, Tony.
"How we think determines what we do, and what we do determines what we get."

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Object Events
« Reply #27 on: October 13, 2012, 07:35:54 PM »
Further adding to the mix, as I am attempting to enhance my VL routine, is that I have just learned that not even .NET API has direct exposure to the Preferences Objects, and must use COM first before accessing PreferencesDisplay, etc.

I just ran across this which I guess I started and forgot about. Might be helpful and could put in whatever settings you need so not to have to reference the Interop assemblies.
 
I think the DLR will check if it has created the object previously to help performance, and I do not think the way I did it helps anything, but it makes me feel better by believing if I put each of the main objects in a property that once jitted it will help it remember it and run quicker(Just superstition).
 
Need 4.0
Code - C#: [Select]
  1. using System;
  2. using System.IO;
  3. namespace Autodesk.AutoCAD.ApplicationServices
  4. {
  5.     public class AcadApplication
  6.     {
  7.         private static dynamic _acadApplication { get { return Application.AcadApplication; } }
  8.         public class Preferences
  9.         {
  10.             private static dynamic _preferences { get { return _acadApplication.Preferences; } }
  11.            
  12.  
  13.             public class Files
  14.             {
  15.                 private static dynamic _files { get { return _preferences.Files; } }
  16.                 public static string QNewTemplateFile
  17.                 {
  18.                     get
  19.                     {
  20.                         return _files.QNewTemplateFile;
  21.                     }
  22.                     set
  23.                     {
  24.                         string filePath = string.Empty;
  25.                         if (!value.IsNullOrWhiteSpace())
  26.                         {
  27.                             if (Path.GetExtension(value).ToLower() != ".dwt")
  28.                             {
  29.                                 throw new ArgumentException("Must be a .dwt file", value);
  30.                             }
  31.                             if (!File.Exists(value))
  32.                             {
  33.                                 throw new FileNotFoundException(value);
  34.                             }
  35.                             filePath = value;
  36.                         }
  37.  
  38.                         _files.QNewTemplateFile = filePath;
  39.                     }
  40.                 }
  41.                 public static string TemplateDwgPath
  42.                 {
  43.                     get
  44.                     {
  45.                         return _files.TemplateDwgPath;
  46.                     }
  47.                     set
  48.                     {
  49.                         if (!Directory.Exists(value))
  50.                         {
  51.                             throw new FileNotFoundException(value);
  52.                         }
  53.  
  54.                         _files.TemplateDwgPath = value;
  55.                     }
  56.                 }
  57.                 public static string TempFilePath
  58.                 {
  59.                     get
  60.                     {
  61.                         return _files.TempFilePath;
  62.                     }
  63.                     set
  64.                     {
  65.                         if (!Directory.Exists(value))
  66.                         {
  67.                             throw new FileNotFoundException(value);
  68.                         }
  69.  
  70.                         _files.TempFilePath = value;
  71.                     }
  72.                 }
  73.  
  74.             }
  75.             public class Profiles
  76.             {
  77.                 private static dynamic _profiles { get { return _preferences.Profiles; } }
  78.                 public static string ActiveProfile
  79.                 {
  80.                     get
  81.                     {
  82.                         return _profiles.ActiveProfile;
  83.                     }
  84.                     set
  85.                     {
  86.                         _profiles.ActiveProfile = value;
  87.                     }
  88.                 }
  89.                 public static string[] GetAllProfileNames()
  90.                 {
  91.                     string[] names;
  92.                     _profiles.GetAllProfileNames(out names);
  93.                     return names;
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
  99.  

So it can be used as
Code - C#: [Select]
  1.  
  2.         //Set path no nothing
  3.         [CommandMethod("Testes")]
  4.         public void Testes()
  5.         {
  6.             AcadApplication.Preferences.Files.QNewTemplateFile = string.Empty;
  7.         }
  8.         //Not a real path
  9.         [CommandMethod("Testes1")]
  10.         public void Testes1()
  11.         {
  12.             AcadApplication.Preferences.Files.QNewTemplateFile = @"J:\Not\A\Real\Path\Base.dwt"; //Throw error
  13.         }
  14.         //Not a .dwt
  15.         [CommandMethod("Testes2")]
  16.         public void Testes2()
  17.         {
  18.             AcadApplication.Preferences.Files.QNewTemplateFile = @"J:\CadMgd\HGCE\Imperial\Templates\Base.dwg"; //Throw error
  19.  
  20.         }
  21.         //Set the path
  22.         [CommandMethod("Testes3")]
  23.         public void Testes3()
  24.         {
  25.             AcadApplication.Preferences.Files.QNewTemplateFile = @"J:\CadMgd\HGCE\Imperial\Templates\Base.dwt";
  26.         }
  27.         //Write Active Profile to commandLine
  28.         [CommandMethod("Testes4")]
  29.         public void Testes4()
  30.         {
  31.             Ed.WriteLine(AcadApplication.Preferences.Profiles.ActiveProfile);
  32.         }
  33.  

TheMaster

  • Guest
Re: Object Events
« Reply #28 on: October 13, 2012, 08:43:43 PM »
Further adding to the mix, as I am attempting to enhance my VL routine, is that I have just learned that not even .NET API has direct exposure to the Preferences Objects, and must use COM first before accessing PreferencesDisplay, etc.

I just ran across this which I guess I started and forgot about. Might be helpful and could put in whatever settings you need so not to have to reference the Interop assemblies.
 

Other than for the sake of limited Intellisense support, I'm not sure all of that is necessary

Code - C#: [Select]
  1.  
  2. public static void Example()
  3. {
  4.     string path = ( (dynamic) Application.Preferences ).Files.AutoSavePath;    
  5. }
  6.  
  7.  

All you have to do is cast the AcadApplication object to dynamic.

BlackBox

  • King Gator
  • Posts: 3770
Re: Object Events
« Reply #29 on: October 13, 2012, 09:14:30 PM »
Jeff / Tony -

Forgive my elementary question here; just trying to better understand....

I've read that when using the dynamic type, "you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else," but I do not understand how or why one would not need to reference the Interop assembly.

How does one access the Application.AcadApplication Object without the appropriate reference, and only System[.*] using statements?
"How we think determines what we do, and what we do determines what we get."