Author Topic: Options on Object Events  (Read 6173 times)

0 Members and 1 Guest are viewing this topic.

GumbyCAD

  • Newt
  • Posts: 84
Options on Object Events
« on: May 08, 2014, 11:06:56 PM »
Hi Team,

I was wondering if anyone had any opinions of Object Events fired off in AutoCAD.

Autodesk Helps say:
Quote
You can write data to any object in the database, but modifying the object that issued the event should be avoided.
Obviously, any object causing an event to be triggered could still be open and the operation currently in progress. Therefore, avoid modifying an object from an event handler for the same object. However, you can safely read information from the object triggering an event.

i.e.
I was thinking of adding code, which is driven by an AutoCAD Event that when a piece of Text or Dimension is created it gets put onto a layer specific layer.

Would this break the rule as noted above?

And any suggestion on were to start looking for ideas / samples.

Stephan  :mrgreen:

BlackBox

  • King Gator
  • Posts: 3770
Re: Options on Object Events
« Reply #1 on: May 09, 2014, 12:43:06 AM »
The simplest way of handling situations like this that I was taught, is to use the modified events to store the ObjectId(s) for applicable objects, and use the CommandEnded, etc. events to process said ObjectId(s).

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

fixo

  • Guest
Re: Options on Object Events
« Reply #2 on: May 09, 2014, 04:30:13 AM »
Completely borrowed from BlackBox's code:

Code - C#: [Select]
  1. //MyPlugin.cs
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Internal;
  6. using Autodesk.AutoCAD.Runtime;
  7. using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
  8. using System;
  9. using System.IO;
  10. using System.Reflection;
  11.  
  12. [assembly: ExtensionApplication(typeof(ToBrian.LayerEvent))]
  13.  
  14. namespace ToStefan
  15. {
  16.     class LayerEvent : IExtensionApplication
  17.     {
  18.         #region Properties
  19.  
  20.         public static ObjectId ObjectType { get; internal set; }
  21.  
  22.         #endregion
  23.  
  24.         #region Initialization
  25.  
  26.         void IExtensionApplication.Initialize()
  27.         {
  28.             DocumentCollection acDocs = acApp.DocumentManager;
  29.             Document doc = acDocs.MdiActiveDocument;
  30.             Editor ed = doc.Editor;
  31.  
  32.             acDocs.DocumentCreated += OnDocumentCreated;
  33.             doc.CommandWillStart += OnCommandWillStart;
  34.  
  35.             ed.WriteMessage("\n** " +
  36.             Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location)
  37.             + " loaded ** \n");
  38.         }
  39.  
  40.         void IExtensionApplication.Terminate()
  41.         {
  42.         }
  43.  
  44.         #endregion
  45.  
  46.         #region Methods
  47.  
  48.         public static void OnCommandEnded(object sender, CommandEventArgs e)
  49.         {
  50.             Document doc = acApp.DocumentManager.MdiActiveDocument;
  51.             Editor ed = doc.Editor;
  52.  
  53.             UnRegisterEvents(doc);
  54.  
  55.             try
  56.             {
  57.                 using (DocumentLock docLock = doc.LockDocument())
  58.                 {
  59.                     using (Transaction tr =
  60.                     doc.Database.TransactionManager.StartOpenCloseTransaction())
  61.                     {
  62.                         if (ObjectType != null)
  63.                         {
  64.                             Entity ent =
  65.                            (Entity)tr.GetObject(ObjectType, OpenMode.ForWrite) as Entity;
  66.  
  67.                             if (ent == null) return;
  68.  
  69.                            if (ent is Line)
  70.                             {
  71.                                 Line ln = (Line)ent as Line;
  72.                                 ln.Layer = "C-ROAD-LINE";
  73.                                 ln.ColorIndex = 256;
  74.                                 ed.WriteMessage("** Line Entity layer changed to \"C-SSWR-TEXT\" ** \n");
  75.                             }
  76.                            if (ent is Dimension)
  77.                            {
  78.                                Dimension dm = (Dimension)ent as Dimension;
  79.                                dm.Layer = "C-SSWR-TEXT";
  80.                                dm.ColorIndex = 256;
  81.                                ed.WriteMessage("** Dimension Entity layer changed to \"C-SSWR-TEXT\" ** \n");
  82.                            }
  83.                            if (ent is Polyline)
  84.                            {
  85.                                Polyline poly = (Polyline)ent as Polyline;
  86.                                poly.Layer = "C-ROAD-CURV";//C-SSWR-STRC
  87.                                poly.ColorIndex = 256;
  88.                                ed.WriteMessage("** Polyline Entity layer changed to \"C-ROAD-CURV\" ** \n");
  89.                            }
  90.                            if (ent is Circle)
  91.                            {
  92.                                Circle cr = (Circle)ent as Circle;
  93.                                cr.Layer = "C-SSWR-STRC";
  94.                                cr.ColorIndex = 256;
  95.                                ed.WriteMessage("** Circle Entity layer changed to \"C-SSWR-STRC\" ** \n");
  96.                            }
  97.                            if (ent is BlockReference)
  98.                            {
  99.                                BlockReference br = (BlockReference)ent as BlockReference;
  100.                                br.Layer = "0";
  101.                                br.ColorIndex = 5;
  102.                                ed.WriteMessage("** BlockReference  Entity layer changed to \"0\" color blue** \n");
  103.                            }
  104.                           //  ed.WriteMessage("** Entity layer changed to \"C-ROAD-INTS-TEXT\" ** \n");
  105.                         }
  106.  
  107.                         tr.Commit();
  108.                     }
  109.                 }
  110.             }
  111.  
  112.             catch (System.Exception ex)
  113.             {
  114.                 ed.WriteMessage("\nSystem.Exception error: " + ex.Message);
  115.             }
  116.         }
  117.  
  118.         public static void OnCommandWillStart(object sender, CommandEventArgs e)
  119.         {
  120.             string cmd = e.GlobalCommandName.ToString().ToUpper();
  121.  
  122.             if (cmd != null && Utils.WcMatch(cmd, "*DIM*,LINE,PLINE,CIRCLE,INSERT"))
  123.                 RegisterEvents(acApp.DocumentManager.MdiActiveDocument);
  124.         }
  125.  
  126.         public static void OnDocumentCreated(Object sender, DocumentCollectionEventArgs e)
  127.         {
  128.             Document doc = e.Document;
  129.  
  130.             if (doc != null)
  131.                 doc.CommandWillStart += OnCommandWillStart;
  132.         }
  133.  
  134.         public static void OnObjectAppended(object sender, ObjectEventArgs e)
  135.         {
  136.             ObjectId id = e.DBObject.ObjectId;
  137.  
  138.             if (id != null)
  139.                 ObjectType = id;
  140.         }
  141.  
  142.         public static void RegisterEvents(Document doc)
  143.         {
  144.             if (doc != null)
  145.             {
  146.                 Editor ed = doc.Editor;
  147.  
  148.                 // command events
  149.                 doc.CommandCancelled += OnCommandEnded;
  150.                 doc.CommandEnded += OnCommandEnded;
  151.                 doc.CommandFailed += OnCommandEnded;
  152.  
  153.                 // database events
  154.                 doc.Database.ObjectAppended += OnObjectAppended;
  155.  
  156.                 ed.WriteMessage("\n** ObjectType events registered ** \n");
  157.             }
  158.         }
  159.  
  160.         public static void UnRegisterEvents(Document doc)
  161.         {
  162.             if (doc != null)
  163.             {
  164.                 Editor ed = doc.Editor;
  165.  
  166.                 // command events
  167.                 doc.CommandCancelled -= OnCommandEnded;
  168.                 doc.CommandEnded -= OnCommandEnded;
  169.                 doc.CommandFailed -= OnCommandEnded;
  170.  
  171.                 // database events
  172.                 doc.Database.ObjectAppended -= OnObjectAppended;
  173.  
  174.                 ed.WriteMessage("\n** ObjectType events unregistered ** \n");
  175.             }
  176.         }
  177.  
  178.         #endregion
  179.     }
  180. }
  181.  

EDIT:KDUB-> CODE=CSHARP
« Last Edit: May 09, 2014, 05:31:27 AM by Kerry »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Options on Object Events
« Reply #3 on: May 09, 2014, 08:07:38 AM »
I was thinking of adding code, which is driven by an AutoCAD Event that when a piece of Text or Dimension is created it gets put onto a layer specific layer.
My old sample is here. The behavior is set up by means of the configuration file. You can ignore the Russian comments in my code i.e. code is very simple and written for the example.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Options on Object Events
« Reply #4 on: May 09, 2014, 08:43:06 AM »
Code - C#: [Select]
  1. ...
  2. class LayerEvent : IExtensionApplication
  3. ...
  4. void IExtensionApplication.Initialize()
  5.        {
  6.            DocumentCollection acDocs = acApp.DocumentManager;
  7.            Document doc = acDocs.MdiActiveDocument;
  8.            Editor ed = doc.Editor;
  9. ...
  10.  
Why you prefer explicitly implementation of the IExtensionApplication interface?

In AutoCAD 2015 the MdiActiveDocument property now can be null. So it is necessary to check it.

Code - C#: [Select]
  1. [assembly: ExtensionApplication(typeof(ToBrian.LayerEvent))]
  2.  
  3. namespace ToStefan
  4. {
  5.    class LayerEvent : IExtensionApplication
  6.    {
So ToBrian's or ToStefan's namespace? ;)

BlackBox

  • King Gator
  • Posts: 3770
Re: Options on Object Events
« Reply #5 on: May 09, 2014, 08:46:21 AM »
Why you prefer explicitly implementation of the IExtensionApplication interface?

In AutoCAD 2015 the "doc" variable now can be null. So it is necessary to check it.

Good observations, Andrey... However, the original code snippet was not written for 2015 (which just came out)... If memory serves, I was still coding for 2011 & 2012 at that time. :-)

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

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Options on Object Events
« Reply #6 on: May 09, 2014, 08:54:07 AM »
I don't insist, this is a simply small note (it maybe useful for anybody) :). Now all of us need to remember MdiActiveDocument, since sooner or later we will be must to recompile our old code under the newer AutoCAD version. Therefore I add now this a check into my old code too.
Quote
I was still coding for 2011 & 2012 at that time. :-)
I write for AutoCAD 2009, as a rule.
« Last Edit: May 09, 2014, 08:58:26 AM by Andrey Bushman »

BlackBox

  • King Gator
  • Posts: 3770
Re: Options on Object Events
« Reply #7 on: May 09, 2014, 08:58:14 AM »
Now all of us need to remember MdiActiveDocument since sooner or later we will be must to recompile the code under newer AutoCAD version.

No worries, my friend; we are in agreement.



I've been preoccupied for the past few months with my new employer, that I'm just now starting to read up on what else I need to consider for 2015, as I need to publish app updates at Autodesk Exchange, and we're gearing up for IDSP 2015 at work.

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

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Options on Object Events
« Reply #8 on: May 09, 2014, 12:11:38 PM »
To pull from the lessons given by Mr. Tanzillo it is wasteful to reopen the objects after the event has been fired to change them again.  Instead if you structure your overrule to override the Close() method .  I don't have time to set up an example right now but if you wish to play, create an object overrule that targets the desired RX classes and stores the id of the desired layers. Then in the close method check the layer id property of the object and change if needed.  IMO it is a cleaner solution which will use less code in the long run.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Options on Object Events
« Reply #9 on: May 09, 2014, 12:14:05 PM »
from the lessons given by Mr. Tanzillo
?

BlackBox

  • King Gator
  • Posts: 3770
Re: Options on Object Events
« Reply #10 on: May 09, 2014, 12:23:33 PM »
To pull from the lessons given by Mr. Tanzillo it is wasteful to reopen the objects after the event has been fired to change them again.  Instead if you structure your overrule to override the Close() method .  I don't have time to set up an example right now but if you wish to play, create an object overrule that targets the desired RX classes and stores the id of the desired layers. Then in the close method check the layer id property of the object and change if needed.  IMO it is a cleaner solution which will use less code in the long run.

Again, I have no idea where exactly Fixo pulled my legacy code, but I'd agree with your Overrule suggestion (as explained by Tony to me last year sometime; I'm sure others prior to that)... I have the links somewhere, and will post back.

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

BlackBox

  • King Gator
  • Posts: 3770
Re: Options on Object Events
« Reply #11 on: May 09, 2014, 12:25:19 PM »
... I have the links somewhere, and will post back.

I believe this post specifically has what you're after:

http://www.theswamp.org/index.php?topic=43356.msg486228#msg486228
"How we think determines what we do, and what we do determines what we get."

GumbyCAD

  • Newt
  • Posts: 84
Re: Options on Object Events
« Reply #12 on: August 03, 2014, 11:10:53 PM »
Thanks.. A lot to Digest for sure.
 :-D

GumbyCAD

  • Newt
  • Posts: 84
Re: Options on Object Events
« Reply #13 on: August 04, 2014, 12:40:31 AM »
Dare I say this "What is an overrule"  (Be gentle)

Sorry can't see how to implement or use any of these samples.....

Is the beginners guide on this stuff?

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Options on Object Events
« Reply #14 on: August 11, 2014, 10:44:42 PM »
Dare I say this "What is an overrule"  (Be gentle)

Sorry can't see how to implement or use any of these samples.....

Is the beginners guide on this stuff?

Little late on this one but if you're still looking I found the my first plugin material to explain overrules pretty well. www.autodesk.com/myfirstautocadplugin  Basically allows you to overrule default behaviour.