Creating a SmartComponent
This section provides information on creating a Smart Component with code-behind using Microsoft Visual Studio. RobotStudio adds a Smart Component project template to Visual Studio. Creating a project based on this template is the easiest way to get started authoring your Smart Component with code-behind. The required references, build events, and template files will be created.
The new project contains the following source code files.
File Name | Description |
---|---|
CodeBehind.cs | Contains the template code behind C# source. |
SmartComponent1.xml | Contains the template Library XML source. |
SmartComponent1.en.xml | Contains the template Library Resource XML source. |
Creating a SmartComponent from Visual Studio
In Visual Studio, select Create a new project and choose the RobotStudio version Smart Component template. Write the desired name for the project and press Next.
Open the "SmartComponent.xml" file, where "SmartComponent" is the name of your project, and change the name of the digital signal to "MySignal" as shown below:
<Signals> <IOSignal name="MySignal" signalType="DigitalInput"/> </Signals>
Open the "CodeBehind.cs" file and add the following method to the "OnIOSignalValueChanged" method:
public override void OnIOSignalValueChanged( SmartComponent component, IOSignal changedSignal) { if (changedSignal.Name == "MySignal") { Logger.AddMessage(new LogMessage("Someone just pressed MySignal!")); } }
Build the SmartComponent with the "Build Solution" command from Visual Studio. An .rslib file will be automatically generated in the projects directory.
Start RobotStudio and create an Empty Station. Select Import Library from the Home tab and open the .rslib file generated from your SmartComponent. The SmartComponent will be displayed in the left side panel.
Double click the SmartComponent to display its menu. Click the button labeled as MySignal. You should see a message in RobotStudio's log window.
Example
CodeBehind.cs:
using System;
using System.Collections.Generic;
using System.Text;
using ABB.Robotics.Math;
using ABB.Robotics.RobotStudio;
using ABB.Robotics.RobotStudio.Stations;
namespace SmartComponent
{
/// <summary>
/// Code-behind class for the SmartComponent 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 a dynamic property value has changed.
/// </summary>
/// <param name="component">Component that owns the changed property.</param>
/// <param name="changedProperty">Changed property.</param>
/// <param name="oldValue">Previous value of the changed property.</param>
public override void OnPropertyValueChanged(
SmartComponent component, DynamicProperty changedProperty, Object oldValue)
{
}
/// <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 == "MySignal")
{
Logger.AddMessage(new LogMessage("Someone just pressed MySignal!"));
}
}
/// <summary>
/// Called during simulation.
/// </summary>
/// <param name="component">Simulated component.</param>
/// <param name="simulationTime">Time (in ms) for the current simulation step.</param>
/// <param name="previousTime">Time (in ms) for the previous simulation step.</param>
/// <remarks>
/// For this method to be called, the component must be marked with
/// simulate="true" in the xml file.
/// </remarks>
public override void OnSimulationStep(
SmartComponent component, double simulationTime, double previousTime)
{
}
}
}
SmartComponent.xml:
<?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="SmartComponent.rslib">
<lc:DocumentProperties>
<lc:Author>USER_NAME</lc:Author>
<lc:Image source="SmartComponent.png"/>
</lc:DocumentProperties>
<SmartComponent name="SmartComponent" icon="SmartComponent.png"
codeBehind="SmartComponent.CodeBehind,SmartComponent.dll"
canBeSimulated="false">
<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="MySignal" signalType="DigitalInput"/>
</Signals>
<GraphicComponents>
</GraphicComponents>
<Assets>
<Asset source="SmartComponent.dll"/>
</Assets>
</SmartComponent>
</lc:Library>
</lc:LibraryCompiler>