Manage simulation states
This topic describes how to override the "OnSimulationStart" and "OnSimulationStop" methods from a SmartComponent.
This methods are called when RobotStudio starts or resumes the simulation respectively. The developer can use the passing argument, which contains the SmartComponent itself, to modify it or interact with other elements of the Station.
In this example, we show a very simple use case in which the SmartComponent prints a message when the simulation starts or stops.
Note
The code in this topic can be tested using the default RobotStudio Smart Component template.
Solution
Override the "OnSimulationStart" method on the "CodeBehind.cs" file of the SmartComponent, as shown in the following snippet:
public override void OnSimulationStart(SmartComponent component) { Logger.AddMessage(new LogMessage("Simulation starting")); base.OnSimulationStart(component); }Override the "OnSimulationStop" method on the "CodeBehind.cs" in a similar fashion:
public override void OnSimulationStop(SmartComponent component) { Logger.AddMessage(new LogMessage("Simulation stopping")); base.OnSimulationStart(component); }To be able to react to the Simulation callback functions, the SmartComponent needs to be able to be simulated. To achieve so, modify the "canBeSimulated" attribute in the SmartComponents XML file as follows:
<SmartComponent name="SmartComponent" icon="SmartComponent.png" codeBehind="SmartComponent.CodeBehind,SmartComponent.dll" canBeSimulated="true">
Example
The complete files for the SmartComponent are presented here. When executed, the component will display the predefined messages when the simulation is started or stopped.
using System;
using System.Collections.Generic;
using System.Text;
using ABB.Robotics.Math;
using ABB.Robotics.RobotStudio;
using ABB.Robotics.RobotStudio.Stations;
using System.Diagnostics;
namespace SmartComponent
{
public class CodeBehind : SmartComponentCodeBehind
{
public override void OnSimulationStart(SmartComponent component)
{
Logger.AddMessage(new LogMessage("Simulation starting"));
base.OnSimulationStart(component);
}
public override void OnSimulationStop(SmartComponent component)
{
Logger.AddMessage(new LogMessage("Simulation stopping"));
base.OnSimulationStop(component);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<lc:LibraryCompiler xmlns:lc="urn:abb-robotics-robotstudio-librarycompiler"
xmlns="urn:abb-robotics-robotstudio-graphiccomponent"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:abb-robotics-robotstudio-librarycompiler file:///C:\Program%20Files%20(x86)\ABB\SDK\RobotStudio%202020%20SDK\LibraryCompilerSchema.xsd
urn:abb-robotics-robotstudio-graphiccomponent file:///C:\Program%20Files%20(x86)\ABB\SDK\RobotStudio%202020%20SDK\GraphicComponentSchema.xsd">
<lc:Library fileName="SmartComponent.rslib">
<lc:DocumentProperties>
<lc:Author>SEERVIE</lc:Author>
<lc:Image source="SmartComponent.png"/>
</lc:DocumentProperties>
<SmartComponent name="SmartComponent" icon="SmartComponent.png"
codeBehind="SmartComponent.CodeBehind,SmartComponent.dll"
canBeSimulated="true">
<Properties>
<DynamicProperty name="SampleProperty" valueType="System.Double" value="10">
<Attribute key="MinValue" value="0"/>
<Attribute key="Quantity" value="Length"/>
</DynamicProperty>
</Properties>
<Bindings>
</Bindings>
<Signals>
<IOSignal name="SampleSignal" signalType="DigitalInput"/>
</Signals>
<GraphicComponents>
</GraphicComponents>
<Assets>
<Asset source="SmartComponent.dll"/>
</Assets>
</SmartComponent>
</lc:Library>
</lc:LibraryCompiler>
Required Namespaces
ABB.Robotics.RobotStudio.Stations