Author Topic: Best way to sync a pallete with current drawing?  (Read 2219 times)

0 Members and 1 Guest are viewing this topic.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Best way to sync a pallete with current drawing?
« on: November 27, 2015, 02:41:01 AM »
I've got a palette that shows assemblies stored in a dictionary in the drawing.

The palette holds a control that is fed a list of objects. I refresh the control when I update the dictionary.

But if I switch active drawings, the pallete doesn't update from the dictionary in the new drawing.

What's the best way to keep the palette synced with the active drawing? I'm thinking I need to capture some drawing switch event and update the pallete. Sound right?

If you have a better architecture, I'd love to hear it.

huiz

  • Swamp Rat
  • Posts: 917
  • Certified Prof C3D
Re: Best way to sync a pallete with current drawing?
« Reply #1 on: November 27, 2015, 08:44:56 AM »
Application.DocumentManager.DocumentActivated += new DocumentCollectionEventHandler(your_function_to_reset_palette);
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Best way to sync a pallete with current drawing?
« Reply #2 on: November 27, 2015, 10:06:09 AM »
I'd use DocumentBecameCurrent, instead of DocumentActivated: DocumentActivated also fires when the MdiActiveDocument is not changed, such as when a modal dialog box or a message box is dismissed, in this case, it may not be necessary to refreshing the Palette view

huiz

  • Swamp Rat
  • Posts: 917
  • Certified Prof C3D
Re: Best way to sync a pallete with current drawing?
« Reply #3 on: November 27, 2015, 10:24:51 AM »
Ah good point. I'll test it with my code.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Best way to sync a pallete with current drawing?
« Reply #4 on: November 27, 2015, 05:34:14 PM »
I'd use DocumentBecameCurrent, instead of DocumentActivated: ...

Thanks guys! I implemented both, but ran into an exception when switching open drawings multiple times after DocumentBecameCurrent. Line 12 below throws the exception: (Autodesk.AutoCAD.Runtime.Exception: eNotFromThisDocument at Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal (AcDbTransactionManager* pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean forceOpenOnLockedLayer) .

If I use DocumentActivated it works fine. The code that's crashing from DocumentBecameCurrent is below. I wonder if it has something to do with executing before the database switch happens completely.

Code - C: [Select]
  1. /// <summary>
  2. /// Returns a the specified dictionary in the NOD. If the dictionary doesn't exist,
  3. /// it is created and the new dictionary returned
  4. /// </summary>
  5. /// <param name="dictionaryName"></param>
  6. /// <returns>The dictionary YOU want.</returns>
  7. public static DBDictionary GetNamedDictionary(string dictionaryName)
  8. {
  9.     DBDictionary myDbDictionary=null;
  10.     using (LockedTransaction tr = Active.Document.TransactionManager.StartLockedTransaction())
  11.     {
  12.         DBDictionary nod = tr.GetObject(HostApplicationServices.WorkingDatabase.NamedObjectsDictionaryId,
  13.             OpenMode.ForWrite) as DBDictionary; //<-- crasharino if called from DocumentBecameCurrent, works fine from DocumentActivated
  14.         if (nod.Contains(dictionaryName))
  15.         {
  16.             myDbDictionary = (DBDictionary) tr.GetObject(nod.GetAt(dictionaryName), OpenMode.ForRead);
  17.             Debug.Print("Dictionary Found: " + dictionaryName);
  18.         }
  19.         else
  20.         {
  21.             myDbDictionary = new DBDictionary();
  22.             nod.SetAt(dictionaryName, myDbDictionary);
  23.             tr.AddNewlyCreatedDBObject(myDbDictionary,true);
  24.             Debug.Print("Dictionary Added: "+dictionaryName);
  25.         }
  26.         tr.Commit();
  27.     }
  28.     return myDbDictionary;
  29. }

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Best way to sync a pallete with current drawing?
« Reply #5 on: November 27, 2015, 05:40:34 PM »
Also, on a related note, where is a good place to put the hook into the Document Manager?

Right now I've got the line:
Code - C: [Select]
  1. Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.DocumentActivated +=
  2.                                 new DocumentCollectionEventHandler(PaletteSync);

as part of a command that is run to show the pallete, but it seems like I should put it in a place where I can get rid of the EventHandler. Right now if the user executes that command multiple times, I'll just stack EventHandlers. Not to mention if the palette is visible at startup, and the command not executed, I won't have the EventHandler processing my syncing.