Author Topic: The feature of the "IConfigurationSection.Save" event's registration  (Read 2668 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Maybe It will be interesting for anybody.

Code - C#: [Select]
  1. /* © Andrey Bushman, 2013
  2.  * AutoCAD 2014 x64 SP1
  3.  * The sample, how to save the some settings of your custom controls into the AWS file.
  4.  * I showed the feature of the "IConfigurationSection.Save" event's registration if the palette has not the child controls.
  5.  */
  6.  
  7. using System;
  8. using Microsoft.Win32;
  9.  
  10. using cad = Autodesk.AutoCAD.ApplicationServices.Application;
  11. using App = Autodesk.AutoCAD.ApplicationServices;
  12. using Db = Autodesk.AutoCAD.DatabaseServices;
  13. using Ed = Autodesk.AutoCAD.EditorInput;
  14. using Rtm = Autodesk.AutoCAD.Runtime;
  15. using Win = Autodesk.AutoCAD.Windows;
  16.  
  17. [assembly: Rtm.CommandClass(typeof(Bushman.CAD.Problems.Commands))]
  18.  
  19. namespace Bushman.CAD.Problems {
  20.  
  21.         public class Commands {
  22.  
  23.                 [Rtm.CommandMethod("cmd_4", Rtm.CommandFlags.Modal)]
  24.                 public void Command_4() {
  25.                         Ed.Editor ed = cad.DocumentManager.MdiActiveDocument.Editor;
  26.  
  27.                         // This settings will be saved into the
  28.                         // %AppData%\Autodesk\AutoCAD 2014\R19.1\enu\Support\Profiles\Unnamed Profile\Profile.aws
  29.                         // if the current profile is the <<Unnamed Profile>>.
  30.                         Win.PaletteSet palette_set = new Win.PaletteSet("My Palette",
  31.                                         new Guid("{49DBC66F-21BE-4D9F-A5A9-BA750543042E}"));
  32.                         palette_set.Load += new Win.PalettePersistEventHandler(palette_set_Load);
  33.                         palette_set.Save += new Win.PalettePersistEventHandler(palette_set_Save);
  34.  
  35.                         // WARNING! If you will comment the next code line, the 'Save' event not occurs!
  36.                         // I think it is a bug of API.
  37.                         palette_set.Saving += new Win.PalettePersistEventHandler(palette_set_Saving);
  38.  
  39.                         palette_set.KeepFocus = true;
  40.                         palette_set.Visible = true;
  41.                 }
  42.  
  43.                 void palette_set_Save(object sender, Win.PalettePersistEventArgs e) {
  44.                         palette_set_Save(e.ConfigurationSection);
  45.                 }
  46.  
  47.                 void palette_set_Saving(object sender, Win.PalettePersistEventArgs e) {
  48.                         // The empty implementation...
  49.                 }
  50.  
  51.                 // This names can't to contain the spaces (it will used as the xml
  52.                 // element's and attribute's names)!
  53.                 const String name = "AAA";
  54.                 const String name2 = "BBB";
  55.                 const String val_1_name = "Value_1";
  56.                 const String val_2_name = "Value_2";
  57.  
  58.                 void palette_set_Load(object sender, Win.PalettePersistEventArgs e) {
  59.                         App.IConfigurationSection section = e.ConfigurationSection;
  60.                         App.IConfigurationSection subsection = null;
  61.                         App.IConfigurationSection subsection2 = null;
  62.  
  63.                         if (!section.IsReadOnly) {
  64.                                 Boolean n = section.ContainsSubsection(name);
  65.                                 if (n)
  66.                                         subsection = section.OpenSubsection(name);
  67.                                 else
  68.                                         subsection = section.CreateSubsection(name);
  69.  
  70.                                 if (!subsection.IsReadOnly) {
  71.                                         Boolean b = subsection.ContainsSubsection(name2);
  72.                                         if (b)
  73.                                                 subsection2 = subsection.OpenSubsection(name2);
  74.                                         else
  75.                                                 subsection2 = subsection.CreateSubsection(name2);
  76.                                 }
  77.  
  78.                                 Int32 val_1 = (Int32)subsection2.ReadProperty(val_1_name, 100);
  79.                                 Int32 val_2 = (Int32)subsection2.ReadProperty(val_2_name, 200);
  80.                         }
  81.                 }
  82.  
  83.                 void palette_set_Save(App.IConfigurationSection section) {
  84.                         if (section == null || section.IsReadOnly)
  85.                                 return;
  86.                         App.IConfigurationSection subsection = null;
  87.                         App.IConfigurationSection subsection2 = null;
  88.  
  89.                         Boolean b = section.ContainsSubsection(name);
  90.                         if (b)
  91.                                 subsection = section.OpenSubsection(name);
  92.                         else
  93.                                 subsection = section.CreateSubsection(name);
  94.  
  95.                         if (subsection != null && !subsection.IsReadOnly) {
  96.                                 Boolean n = subsection.ContainsSubsection(name2);
  97.                                 if (n)
  98.                                         subsection2 = subsection.OpenSubsection(name2);
  99.                                 else
  100.                                         subsection2 = subsection.CreateSubsection(name2);
  101.                         }
  102.  
  103.                         if (subsection2 != null && !subsection2.IsReadOnly) {
  104.                                 subsection2.WriteProperty(val_1_name, 123);
  105.                                 subsection2.WriteProperty(val_2_name, 456);
  106.                         }
  107.                 }
  108.         }
  109. }

Fragment of the Profile.asw file:

Code - XML: [Select]
  1. ...
  2. <Tool CLSID="{49DBC66F-21BE-4D9F-A5A9-BA750543042E}">
  3.   <CAdUiDockControlBar Orientation="-1" AllowDocking="1">
  4.     <FloatInfo Left="458" Top="295" Width="447" Height="384"/>
  5.     <DockInfo Left="0" Top="6732" Width="300" Height="798" MRUDockID="59420"/>
  6.   </CAdUiDockControlBar>
  7.   <CAdUiPaletteSet/>
  8.   <AAA>
  9.     <BBB Value_1="123" Value_2="456"/>
  10.   </AAA>
  11. </Tool>
  12. ...
« Last Edit: December 09, 2013, 03:24:16 AM by Andrey Bushman »

Locke

  • Guest
Re: The feature of the "IConfigurationSection.Save" event's registration
« Reply #1 on: March 28, 2014, 07:49:23 AM »
Thanks for sharing Andrey, I've never seen this before.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: The feature of the "IConfigurationSection.Save" event's registration
« Reply #2 on: March 31, 2014, 03:04:07 PM »
My previous message has the old code. I don't publish new code versions if it is interesting to nobody.

The Save event (if subscription for Saving event is absent) isn't generated for the case when the palette is empty. If the palette has some control, the Save event works fine (without the Saving event subscription even). If the subscription for the Saving event is exists, then the Save event is generate twice. I.e. we have the next sequence generation of events:

1. Save
2. Saving
3. Save

Excess chain of generation of events, in my opinion.

So, newer version of code:
Code - C#: [Select]
  1.     /* © Andrey Bushman, 2013
  2.      * AutoCAD 2014 x64 SP1
  3.      * The sample, how to save the some settings of your custom controls into the AWS file.
  4.      */
  5.      
  6.     using System;
  7.     using Microsoft.Win32;
  8.      
  9.     using cad = Autodesk.AutoCAD.ApplicationServices.Application;
  10.     using App = Autodesk.AutoCAD.ApplicationServices;
  11.     using Db = Autodesk.AutoCAD.DatabaseServices;
  12.     using Ed = Autodesk.AutoCAD.EditorInput;
  13.     using Rtm = Autodesk.AutoCAD.Runtime;
  14.     using Win = Autodesk.AutoCAD.Windows;
  15.     using Int = System.Windows.Forms.Integration;
  16.     using WF = System.Windows.Forms;
  17.     [assembly: Rtm.CommandClass(typeof(Bushman.CAD.Samples.Commands))]
  18.      
  19.     namespace Bushman.CAD.Samples {
  20.      
  21.             public class Commands {
  22.      
  23.                     [Rtm.CommandMethod("cmd_5", Rtm.CommandFlags.Modal)]
  24.                     public void Command_5() {
  25.                             Ed.Editor ed = cad.DocumentManager.MdiActiveDocument.Editor;
  26.      
  27.                             // This settings will be saved into the
  28.                             // %AppData%\Autodesk\AutoCAD 2014\R19.1\enu\Support\Profiles\Unnamed Profile\Profile.aws
  29.                             // if the current profile is the <<Unnamed Profile>>.
  30.                             Win.PaletteSet palette_set = new Win.PaletteSet("My Palette",
  31.                                             new Guid("{49DBC66F-21BE-4D9F-A5A9-BA750543042E}"));
  32.      
  33.                             // Add the some Control item on the palette (the Label for example)...
  34.                             WF.Label label = new WF.Label();
  35.                             label.Text = "Hello, World";
  36.                             palette_set.Add("123", label);
  37.      
  38.                             palette_set.Load += new Win.PalettePersistEventHandler(palette_set_Load);
  39.                             palette_set.Save += new Win.PalettePersistEventHandler(palette_set_Save);
  40.      
  41.                             palette_set.KeepFocus = true;
  42.                             palette_set.Visible = true;
  43.                     }
  44.      
  45.                     void palette_set_Save(object sender, Win.PalettePersistEventArgs e) {
  46.                             palette_set_Save(e.ConfigurationSection);
  47.                     }
  48.      
  49.                     // This names can't to contain the spaces (it will used as the xml
  50.                     // element's and attribute's names)!
  51.                     const String name = "AAA";
  52.                     const String name2 = "BBB";
  53.                     const String val_1_name = "Value_1";
  54.                     const String val_2_name = "Value_2";
  55.      
  56.                     void palette_set_Load(object sender, Win.PalettePersistEventArgs e) {
  57.                             App.IConfigurationSection section = e.ConfigurationSection;
  58.                             App.IConfigurationSection subsection = null;
  59.                             App.IConfigurationSection subsection2 = null;
  60.      
  61.                             if (!section.IsReadOnly) {
  62.                                     Boolean n = section.ContainsSubsection(name);
  63.                                     if (n)
  64.                                             subsection = section.OpenSubsection(name);
  65.                                     else
  66.                                             subsection = section.CreateSubsection(name);
  67.      
  68.                                     if (!subsection.IsReadOnly) {
  69.                                             Boolean b = subsection.ContainsSubsection(name2);
  70.                                             if (b)
  71.                                                     subsection2 = subsection.OpenSubsection(name2);
  72.                                             else
  73.                                                     subsection2 = subsection.CreateSubsection(name2);
  74.                                     }
  75.      
  76.                                     Int32 val_1 = (Int32)subsection2.ReadProperty(val_1_name, 100);
  77.                                     Int32 val_2 = (Int32)subsection2.ReadProperty(val_2_name, 200);
  78.                             }
  79.                     }
  80.      
  81.                     void palette_set_Save(App.IConfigurationSection section) {
  82.                             if (section == null || section.IsReadOnly)
  83.                                     return;
  84.                             App.IConfigurationSection subsection = null;
  85.                             App.IConfigurationSection subsection2 = null;
  86.      
  87.                             Boolean b = section.ContainsSubsection(name);
  88.                             if (b)
  89.                                     subsection = section.OpenSubsection(name);
  90.                             else
  91.                                     subsection = section.CreateSubsection(name);
  92.      
  93.                             if (subsection != null && !subsection.IsReadOnly) {
  94.                                     Boolean n = subsection.ContainsSubsection(name2);
  95.                                     if (n)
  96.                                             subsection2 = subsection.OpenSubsection(name2);
  97.                                     else
  98.                                             subsection2 = subsection.CreateSubsection(name2);
  99.                             }
  100.      
  101.                             if (subsection2 != null && !subsection2.IsReadOnly) {
  102.                                     subsection2.WriteProperty(val_1_name, 123);
  103.                                     subsection2.WriteProperty(val_2_name, 456);
  104.                             }
  105.                     }
  106.             }
  107.     }

I've a hope this notes will be interesting for you.
« Last Edit: April 01, 2014, 09:24:25 AM by Andrey Bushman »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: The feature of the "IConfigurationSection.Save" event's registration
« Reply #3 on: April 02, 2014, 03:01:18 AM »

Thanks Andrey,
Bookmarked for the weekend.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: The feature of the "IConfigurationSection.Save" event's registration
« Reply #4 on: April 02, 2014, 03:13:52 AM »
Additional info about location, docking and sizes of PaletteSet is here (it maybe interesting too).