Initializing persistent state
This topic shows the necessary steps to override the OnInitialize(SmartComponent) method from a SmartComponent's CodeBehind.
This method gets called when the SmartComponent initializes its CodeBehind by using the method InitializeCodeBehind() .
In this example, we show a very simple use case in which triggering a signal resets the SmartComponent, making it clear its StateCache by calling the InitializeCodeBehind() , which subsequently calls the OnInitialize(SmartComponent) method.
Add a public override of the OnInitialize(SmartComponent) method.
Declare a signal in the XMl file of the component.
Testing the component in RobotStudio.
Note
The code in this topic can be tested using the default RobotStudio Smart Component template.
Solution
Open the CodeBehind.cs source file of the SmartComponent and, inside the SmartComponentCodeBehind class, edit the OnIOSignalValueChanged method to look like this:
public override void OnIOSignalValueChanged(SmartComponent component, IOSignal changedSignal) { if (changedSignal.Name == "Reset" && changedSignal.Value.Equals(1)) { component.InitializeCodeBehind(); } base.OnIOSignalValueChanged(component, changedSignal); }Add the overriding OnInitialize method:
public override void OnInitialize(SmartComponent component) { Logger.AddMessage(new LogMessage("Initializing: cleaning the StateCache.")); component.StateCache.Clear(); base.OnInitialize(component); }Declare a signal in the XML file as shown here:
<Signals> <IOSignal name="Reset" signalType="DigitalInput" autoReset="true"/> </Signals>Using this component and pressing the Reset button will produce the following output:
Initializing: cleaning the StateCache.
Example
Relevant methods in the CodeBehind file:
using System;
using System.Collections.Generic;
using System.Text;
using ABB.Robotics.Math;
using ABB.Robotics.RobotStudio;
using ABB.Robotics.RobotStudio.Stations;
namespace SmartComponent1
{
/// <summary>
/// Code-behind class for the SmartComponent1 Smart Component.
/// </summary>
/// <remarks>
/// The code-behind class should be seen as a service provider used by the
/// Smart Component runtime. Only one instance of the code-behind class
/// is created, regardless of how many instances there are of the associated
/// Smart Component.
/// Therefore, the code-behind class should not store any state information.
/// Instead, use the SmartComponent.StateCache collection.
/// </remarks>
public class CodeBehind : SmartComponentCodeBehind
{
/// <summary>
/// Called when the value of an I/O signal value has changed.
/// </summary>
/// <param name="component"> Component that owns the changed signal. </param>
/// <param name="changedSignal"> Changed signal. </param>
public override void OnIOSignalValueChanged(SmartComponent component, IOSignal changedSignal)
{
if (changedSignal.Name == "Reset" && changedSignal.Value.Equals(1))
{
component.InitializeCodeBehind();
}
base.OnIOSignalValueChanged(component, changedSignal);
}
public override void OnInitialize(SmartComponent component)
{
Logger.AddMessage(new LogMessage("Initializing: cleaning the StateCache."));
component.StateCache.Clear();
base.OnInitialize(component);
}
// Additional methods are defined here.
}
The XML file:
<?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%202024%20SDK\LibraryCompilerSchema.xsd
urn:abb-robotics-robotstudio-graphiccomponent file:///C:\Program%20Files%20(x86)\ABB\SDK\RobotStudio%202024%20SDK\GraphicComponentSchema.xsd">
<lc:Library fileName="SmartComponent1.rslib">
<lc:DocumentProperties>
<lc:Author>USER_NAME</lc:Author>
<lc:Image source="SmartComponent1.png"/>
</lc:DocumentProperties>
<SmartComponent name="SmartComponent1" icon="SmartComponent1.png"
codeBehind="SmartComponent1.CodeBehind,SmartComponent1.dll"
canBeSimulated="false">
<Properties>
</Properties>
<Bindings>
</Bindings>
<Signals>
<IOSignal name="Reset" signalType="DigitalInput" autoReset="true"/>
</Signals>
<GraphicComponents>
</GraphicComponents>
<Assets>
<Asset source="SmartComponent1.dll"/>
</Assets>
</SmartComponent>
</lc:Library>
</lc:LibraryCompiler>
Required Namespaces
ABB.Robotics.RobotStudio.Stations