public class SerializationExamples
{
///////////////////////////////////////////////////////////
// Examples of saving and loading MPX object configuration
///////////////////////////////////////////////////////////
// Save an entire Axis configuration, including child objects.
public void SaveAxis()
{
Mpx.Controller controller = new Mpx.Controller(0);
Mpx.ConfigSaver saver = new Mpx.ConfigSaver();
// Have the OnSavePropertyError method handle PropertyError events
saver.PropertyErrorEvent += OnSavePropertyError;
saver.SaveToFile(controller.Axis[0], "axisConfig.xml");
}
// Save Axis configuration, but exclude child objects.
public void SaveAxisOnly()
{
Mpx.Controller controller = new Mpx.Controller(0);
Mpx.Axis axis0 = controller.Axis[0];
Mpx.ConfigSaver saver = new Mpx.ConfigSaver();
// Don't save child objects
saver.IncludeChildren = false;
// Have the OnSavePropertyError method handle PropertyError events
saver.PropertyErrorEvent += OnSavePropertyError;
saver.SaveToFile(axis0, "axisOnlyConfig.xml");
}
// Save only the Controller configuration, excluding child objects.
public void SaveControllerOnly()
{
Mpx.Controller controller = new Mpx.Controller(0);
Mpx.ConfigSaver saver = new Mpx.ConfigSaver();
// Don't save child objects
saver.IncludeChildren = false;
// Have the OnSavePropertyError method handle PropertyError events
saver.PropertyErrorEvent += OnSavePropertyError;
saver.SaveToFile(controller, "controllerOnlyConfig.xml");
}
// Save the entire Controller configuration, including child objects.
//
// Handle ConfigSavePropertyStatus events in order to display
// progress and exclude Can and CanNode objects from being saved.
//
// Handle PropertyError events in order to display errors.
public void SaveController()
{
Mpx.Controller controller = new Mpx.Controller(0);
Mpx.ConfigSaver saver = new Mpx.ConfigSaver();
// Have the OnSavePropertyError method handle PropertyError events
saver.PropertyErrorEvent += OnSavePropertyError;
// Have the OnSavePropertyStatus method handle
// ConfigSavePropertyStatus events
saver.PropertyStatusEvent += OnSavePropertyStatus;
saver.SaveToFile(controller, "controllerConfig.xml");
}
/////////////////////////////////////////////////////////////////////////
// Handlers for events that occur while saving MPX object configuration.
/////////////////////////////////////////////////////////////////////////
// This method handles PropertyError events that occur while saving
// the configuration of an object.
public void OnSavePropertyError(Mpx.ConfigSaver saver,
Mpx.ConfigSavePropertyErrorEventArgs e)
{
// Example output:
// ERROR: Controller.Axis[2]: Axis is not enabled
string msg = "ERROR: "
+ e.PropertyPath
+ ": "
+ e.Message
+ "\n";
System.Console.Write(msg);
if (e.ErrorCode == Mpx.ConfigSaveErrorCode.Internal)
{
// Cancel the save operation
saver.Cancel();
System.Console.Write("Canceled!\n");
}
}
// This method handles PropertyStatus events that occur while loading
// configuration to object.
public void OnSavePropertyStatus(Mpx.ConfigSaver saver,
Mpx.ConfigSavePropertyStatusEventArgs e)
{
string msg;
// Print the current property and the percentage done
// Example output:
// 25%: Controller.Axis[2].AmpDisableAction
// skipped
msg = e.PercentageDone
+ "%: "
+ e.PropertyPath
+ "\n";
System.Console.Write(msg);
// Don't save CAN or CAN Node objects
if (e.PropertyPath.StartsWith("Controller.Can"))
{
System.Console.Write(" skipped\n");
e.SkipProperty = true;
}
}
///////////////////////////////
// Load axis configuration onto 2 different axes
public void LoadAxes()
{
Mpx.Controller controller = new Mpx.Controller(0);
Mpx.ConfigLoader loader = new Mpx.ConfigLoader();
// Have the OnLoadPropertyError method handle PropertyError events
loader.PropertyErrorEvent += OnLoadPropertyError;
loader.LoadFromFile(controller.Axis[0], "axisOnlyConfig.xml");
loader.LoadFromFile(controller.Axis[1], "axisOnlyConfig.xml");
}
// Load controller configuration.
// Handle PropertyError events in order to display errors and warnings.
// Handle PropertyStatus events in order to display progress.
public void LoadController()
{
Mpx.Controller controller = new Mpx.Controller(0);
Mpx.ConfigLoader loader = new Mpx.ConfigLoader();
// Have the OnLoadPropertyError method handle PropertyError events
loader.PropertyErrorEvent += OnLoadPropertyError;
// Have the OnLoadPropertyStatus method handle PropertyStatus
// events.
loader.PropertyStatusEvent += OnLoadPropertyStatus;
loader.LoadFromFile(controller, "controllerConfig.xml");
}
///////////////////////////////////////////////////////////
// Handlers for events that occur while loading MPX object
// configuration.
///////////////////////////////////////////////////////////
// This method handles PropertyError events that occur while loading
// configuration to an object.
public void OnLoadPropertyError(Mpx.ConfigLoader loader,
Mpx.ConfigLoadPropertyErrorEventArgs e)
{
string msg;
if (e.ErrorCode == Mpx.ConfigLoadErrorCode.Missing)
{
if (e.PropertyPath.StartsWith("Controller.Can"))
{
// Ignore missing Can and CanNode objects
return;
}
// A property is missing in the config file. Print a warning.
msg = "Warning: ";
}
else
{
// For all other errors, print an error message and cancel the save.
msg = "Error: ";
// Cancel the load operation
loader.Cancel();
}
// Print an informative message
// Example output:
// ERROR: Controller.Axis[2]: Axis is not enabled
msg += e.PropertyPath
+ ": "
+ e.Message
+ "\n";
System.Console.Write(msg);
}
// This method handles PropertyStatus events that occur while loading
// configuration to an object.
public void OnLoadPropertyStatus(Mpx.ConfigLoader loader,
Mpx.ConfigLoadPropertyStatusEventArgs e)
{
string msg;
// Print the current property and the percentage done
// Example output:
// 25%: Controller.Axis[2].AmpDisableAction
// Current Value: None
// New Value: CmdEqAct
msg = e.PercentageDone
+ "%: "
+ e.PropertyPath
+ "\n";
if (e.NewValueText != e.CurrentValueText)
{
// The property value is going to change. Display the current
// value and the new value.
msg += " Current Value: "
+ e.CurrentValueText
+ "\n New Value: "
+ e.NewValueText
+ "\n";
}
System.Console.Write(msg);
}
public void DelagateExample()
{
Mpx.ConfigLoader loader = new Mpx.ConfigLoader();
Mpx.ConfigLoader.PropertyErrorEventHandler loaderPropertyErrorEvent;
loaderPropertyErrorEvent = OnLoadPropertyError;
loader.PropertyErrorEvent += loaderPropertyErrorEvent;
Mpx.ConfigLoader.PropertyStatusEventHandler loaderPropertyStatusEvent;
loaderPropertyStatusEvent = OnLoadPropertyStatus;
loader.PropertyStatusEvent += loaderPropertyStatusEvent;
Mpx.ConfigSaver saver = new Mpx.ConfigSaver();
Mpx.ConfigSaver.PropertyErrorEventHandler saverPropertyErrorEvent;
saverPropertyErrorEvent = OnSavePropertyError;
saver.PropertyErrorEvent += saverPropertyErrorEvent;
Mpx.ConfigSaver.PropertyStatusEventHandler saverPropertyStatusEvent;
saverPropertyStatusEvent = OnSavePropertyStatus;
saver.PropertyStatusEvent += saverPropertyStatusEvent;
}
}
|