FWIW - There's a .NET sample for exporting Command Settings to .XML already included in the Sample folder for your installation here:
Code:
(defun c:C3dCsSample (/ folder)
(if
(setq folder
(findfile
(strcat
(vl-registry-read
(strcat "HKEY_LOCAL_MACHINE\\"
(if vlax-user-product-key ; If 2013
(vlax-user-product-key) ; Use 2013 function
(vlax-product-key) ; Use legacy function
)
)
"ACADLOCATION"
)
"\\Sample\\Civil 3D API\\DotNet\\CSharp\\CommandSettingsSample"
)
)
)
(startapp "explorer" folder)
(prompt "\n** Folder not found ** ")
)
(princ)
)
I've taken the liberty of compiling this sample .NET code to .NET 3.5, which should work for Civil 3D 2010 and newer. Simply rename the file from CommandSettings.txt to CommandSettings.dll
Once you've NETLOADed the assembly, simply enter in the CommandMethod name of "ExportCS"
Again, this is only a sample... For those who would prefer (as I do), to compile the source code themselves:
Code:
using System;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.Settings;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections;
using Autodesk.AutoCAD.Windows;
using System.IO;
namespace CommandSettingsSample {
/// <summary>
/// This sample illustrates the Civil 3D .NET Command Settings API
/// It exports a list of all command settings in one Civil document as
/// an xml document.
/// </summary>
public class CSSample {
public Editor ed;
public StringBuilder xml;
public String file;
/// <summary>
/// Command to export command settings.
/// </summary>
[CommandMethod("ExportCS")]
public void ExportCommandSettings() {
var sfd = new System.Windows.Forms.SaveFileDialog();
sfd.Filter = "XML Files (*.xml)|*.xml";
sfd.DefaultExt = "xml";
sfd.FileName = "cmd_settings.xml";
sfd.Title = "Export command settings";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
file = sfd.FileName;
} else {
return;
}
CivilDocument doc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
ed = Application.DocumentManager.MdiActiveDocument.Editor;
xml = new StringBuilder();
// Get all the settings defined in AeccDbMgd using Reflection
Type[] types = new Type[] { typeof(Autodesk.Civil.Settings.SettingsRoot) };
Assembly aeccdbmgd = typeof(CivilDocument).Module.Assembly;
Type[] types2 = aeccdbmgd.GetTypes();
foreach (Type t in types2) {
// A command setting's class name contains "SettingsCmd", and is derived from SettingsAmbient (or
// derived from a child of SettingsAmbient)
if (t.Name.Contains("SettingsCmd")
&& (t.BaseType.Name.Contains("SettingsAmbient")
|| t.BaseType.BaseType.Name.Contains("SettingsAmbient"))) {
ed.WriteMessage("Command Setting found: " + t.Name + "\n");
xml.Append( String.Format("<CommandSetting name=\"{0}\">\n", t.Name));
try {
ConstructorInfo conInfo = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
object[] s4 = new object[] { doc.Settings };
SettingsAmbient settingsAmbient = (SettingsAmbient)conInfo.Invoke(s4);
// This gets the properties of the class and adds it to the xml string
GetPropsOfObj(settingsAmbient, 2);
} catch (System.Exception e) {
ed.WriteMessage("Error: " + e.Message + "\n" + e.GetType() + "\n");
}
xml.Append( "</CommandSetting>\n");
}
}
using (var sw = new StreamWriter(file)) {
sw.Write(xml.ToString());
sw.Close();
}
ed.WriteMessage("DONE\n");
}
/// <summary>
/// Gets the properites for an object using Reflection
/// </summary>
/// <param name="obj">The object to inspect</param>
/// <param name="tab">Tab level for xml indentation</param>
private void GetPropsOfObj(Object obj, int tab) {
String space = " ".PadRight(tab);
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo propInfo in properties) {
object prop = type.InvokeMember(propInfo.Name, BindingFlags.GetProperty, null, obj, new object[0]);
if (prop.GetType().Name.Contains("Settings")) {
xml.Append(String.Format(space + "<Property name=\"{0}\">\n", propInfo.Name));
GetPropsOfObj(prop, tab + 3);
xml.Append( space + "</Property>\n");
} else if (prop.GetType().Name.Contains("Property")) {
Type propType = prop.GetType();
object val = propType.InvokeMember("Value", BindingFlags.GetProperty, null, prop, new object[0]);
xml.Append( String.Format(space + "<Property name=\"{0}\" value=\"{1}\"/>\n", propInfo.Name, val.ToString()));
} else {
ed.WriteMessage("Didn't process: {0} {1}\n", propInfo.Name, propInfo.PropertyType.ToString());
}
}
}
}
}
HTH
Bookmarks