// © Andrey Bushman, 2013
using System;
using Microsoft.Win32;
using cad = Autodesk.AutoCAD.ApplicationServices.Application;
using App = Autodesk.AutoCAD.ApplicationServices;
using Db = Autodesk.AutoCAD.DatabaseServices;
using Ed = Autodesk.AutoCAD.EditorInput;
using Rtm = Autodesk.AutoCAD.Runtime;
using Win = Autodesk.AutoCAD.Windows;
using System.IO;
[assembly: Rtm.CommandClass(typeof(Bushman.CAD.Problems.Commands))]
[assembly: Rtm.ExtensionApplication(typeof(Bushman.CAD.Problems.Commands))]
namespace Bushman.CAD.Problems {
public class Commands : Rtm.IExtensionApplication {
internal static Win.PaletteSet palette_set = null;
[Rtm.CommandMethod("cmd_1", Rtm.CommandFlags.Modal)]
public void Command_1() {
Ed.Editor ed = cad.DocumentManager.MdiActiveDocument.Editor;
App.IConfigurationSection cp = cad.UserConfigurationManager.OpenCurrentProfile();
const String name = "Bushman";
// This names can to contain the spaces
const String name2 = "My Some Application";
const String propName = "Property 1";
// This settings will created in the registry (HKCU)
App.IConfigurationSection section = cp.ContainsSubsection(name) ?
cp.OpenSubsection(name) : cp.CreateSubsection(name);
App.IConfigurationSection section2 = section.ContainsSubsection(name2) ?
section.OpenSubsection(name2) : section.CreateSubsection(name2);
section2.WriteProperty(propName, "Hello, World!");
String cprofile = cad.GetSystemVariable("cprofile") as String;
String path = Db.HostApplicationServices.Current.UserRegistryProductRootKey
+ @"\Profiles\" + cprofile;
using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(path, false)) {
using (RegistryKey regKey2 = regKey.OpenSubKey(name).OpenSubKey(name2)) {
String value = regKey2.GetValue(propName, String.Empty) as String;
ed.WriteMessage("Register: {0}\n", regKey2.Name);
ed.WriteMessage("The register is successfully changed!\n");
}
}
}
[Rtm.CommandMethod("cmd_2", Rtm.CommandFlags.Modal)]
public void Command_2() {
Ed.Editor ed = cad.DocumentManager.MdiActiveDocument.Editor;
if (palette_set == null) {
// This settings will be saved into the
// %AppData%\Autodesk\AutoCAD 2014\R19.1\enu\Support\Profiles\Unnamed Profile\Profile.aws
// if the current profile is <<Unnamed Profile>>.
palette_set = new Win.PaletteSet("My Palette",
new Guid("{49DBC66F-21BE-4D9F-A5A9-BA750543042E}"));
palette_set.Load += new Win.PalettePersistEventHandler(palette_set_Load);
cad.BeginQuit += new EventHandler(cad_BeginQuit);
}
palette_set.KeepFocus = true;
palette_set.Visible = true;
}
static App.IConfigurationSection _palette_set = null;
void cad_BeginQuit(object sender, EventArgs e) {
palette_set_Save(null, _palette_set);
}
// This names can't to contain the spaces!
const String name = "AAA";
const String name2 = "BBB";
const String val_1_name = "Value_1";
const String val_2_name = "Value_2";
void palette_set_Load(object sender, Win.PalettePersistEventArgs e) {
App.IConfigurationSection section = _palette_set = e.ConfigurationSection;
App.IConfigurationSection subsection = null;
App.IConfigurationSection subsection2 = null;
if (!section.IsReadOnly) {
subsection = section.ContainsSubsection(name) ?
section.OpenSubsection(name) : section.CreateSubsection(name);
if (!subsection.IsReadOnly) {
subsection2 = subsection.ContainsSubsection(name2) ?
subsection.OpenSubsection(name2) : subsection.CreateSubsection(name2);
}
Int32 val_1 = (Int32)subsection2.ReadProperty(val_1_name, 100);
Int32 val_2 = (Int32)subsection2.ReadProperty(val_2_name, 200);
}
}
void palette_set_Save(object sender, App.IConfigurationSection section) {
App.IConfigurationSection subsection = null;
App.IConfigurationSection subsection2 = null;
if (section != null && !section.IsReadOnly) {
// TODO: Problem is here.
// Here execution of code interrupts... Maybe IConfigurationSection is disposed already...
subsection = section.ContainsSubsection(name) ?
section.OpenSubsection(name) : section.CreateSubsection(name);
if (!subsection.IsReadOnly) {
subsection2 = subsection.ContainsSubsection(name2) ?
subsection.OpenSubsection(name2) : subsection.CreateSubsection(name2);
}
subsection2.WriteProperty(val_1_name, 700);
subsection2.WriteProperty(val_2_name, 800);
}
}
public void Initialize() {
Ed.Editor ed = cad.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("The 'Hello World' is loaded... \n");
}
public void Terminate() {
// throw new NotImplementedException();
}
}
}