Using the Saving and Saved callbacks
The CodeBehind of a SmartComponent can access the Saved and Saving Station events to bind a callback method that executes an action before and after a Station has been saved, respectively.
Note
The code in this topic can be tested using the default RobotStudio Smart Component template.
Using the Saved and Saving callbacks
Add the callback functions as methods of the SmartComponent:
public class CodeBehind : SmartComponentCodeBehind { public static void BeforeSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("About to save the station...")); } public static void AfterSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("Station saved!")); } }We will bind the callback functions as soon as the SmartComponent is loaded, for this, we will override the OnLoad() function with the commands needed for doing so.
public class CodeBehind : SmartComponentCodeBehind { public static void BeforeSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("About to save the station...")); } public static void AfterSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("Station saved!")); } public override void OnLoad(SmartComponent component) { base.OnLoad(component); } }Since the Saved event is handled using the built-in EventHandler we use that Handler to create a new instance of the event and bind it to the Saved object, that represents the station in which the SmartComponent will run.
public class CodeBehind : SmartComponentCodeBehind { public static void BeforeSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("About to save the station...")); } public static void AfterSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("Station saved!")); } public override void OnLoad(SmartComponent component) { Station.ActiveStation.Saved += new EventHandler(AfterSaveCallback); base.OnLoad(component); } }The Saving event is handled using the SavingProjectEventHandler, so we need to use that handler to create a new instance of the event and bind it to the Saving event.
public class CodeBehind : SmartComponentCodeBehind { public static void BeforeSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("About to save the station...")); } public static void AfterSaveCallback(object o, EventArgs e) { Logger.AddMessage(new LogMessage("Station saved!")); } public override void OnLoad(SmartComponent component) { Station.ActiveStation.Saved += new EventHandler(AfterSaveCallback); Station.ActiveStation.Saving += new SavingProjectEventHandler(BeforeSaveCallback); base.OnLoad(component); } }
Required Namespaces
ABB.Robotics.RobotStudio.Stations