Getting notified when a signal changes
This example provides information on how to show an Object (for example: GraphicComponent) based on Signal Change. When the signal goes high the selected Object is made visible. Similar approach could be followed if Object has to be hidden.
The steps that will be performed in this topic are:
Add the properties and signals to the SmartComponent's XML file.
Add the properties and signal's descriptions to the XML resource file.
Modify the CodeBehind of the component to add the overriding method the code to execute.
This example provides snippets of XML and C# that could be used within the smart component project. An active station is required for this example.
The topic considers that a SmartComponent project created from Visual Studio exists. Refer to topic Creating a SmartComponent on how to run it.
Note
The code in this topic can be tested using the default RobotStudio Smart Component template.
Solution
Open
SmartComponent1.xml
file. Add the following code in Library XML which defines the properties and signals to be used. Properties and Signals are defined for the SmartComponent to function.<lc:Library fileName="Show.rslib"> <SmartComponent icon="Show.png" codeBehind="Show.Show"> <Properties> <DynamicProperty name="Object" valueType="ABB.Robotics.RobotStudio.ProjectObject"/> </Properties> <Signals> <IOSignal name="Execute" signalType="DigitalInput" autoReset="true"/> <IOSignal name="Executed" signalType="DigitalOutput" autoReset="true"/> </Signals> </SmartComponent> </lc:Library>
Open
SmartComponent1.en.xml
file. Add the following code in Library Resource XML which describes the properties and signals.<SmartComponent name="Show" description="Makes an object visible in the graphics"> <DynamicProperty name="Object" description="The object to show"/> <IOSignal name="Execute" description="Set to high (1) to show the object"/> <IOSignal name="Executed" description="Goes high (1) when the operation is complete"/> </SmartComponent>
Open "CodeBehind.cs" file and add the following code as events. When Signal changes, OnIOSignalValueChanged(SmartComponent, IOSignal) gets triggered. Execute method is called to show the object in graphics. Following are the steps to be followed to achieve the aforementioned task:
Get the signal object "Executed".
IOSignal executed = component.IOSignals["Executed"];
Set the visible property to True and Pulse and signal.
if (Execute(component)) { if (executed != null) executed.Pulse(); }
Example
Open "CodeBehind.cs" file and add the following code. The following code gets the corresponding ProjectObject and sets the Visible property to true. The following code uses reflection to get access to the properties.
public override void OnIOSignalValueChanged(SmartComponent component, IOSignal signal)
{
if (signal.Name == "Execute" && (int)signal.Value != 0)
{
IOSignal executed = component.IOSignals["Executed"];
if (Execute(component))
{
if (executed != null)
executed.Pulse();
}
}
}
protected override bool Execute(SmartComponent component)
{
ProjectObject obj = component.Properties["Object"].Value as ProjectObject;
if (obj == null) return false;
foreach (ProjectObject pObject in SmartComponent.GetCollectionObjects(obj))
{
System.Reflection.PropertyInfo pInfo = pObject.GetType().GetProperty("Visible");
if (pInfo == null)
return false;
pInfo.SetValue(pObject, true, null);
}
return true;
}
Compiling the Code
Note
In order for the Library Compiler to find the code behind assembly asset it needs to be located in the same directory as the Library XML file. The template project contains the following Post-Build Event, that copies the assembly to the right place.
copy /y "$(TargetPath)" "$(ProjectDir)"
The LibraryCompiler.exe
is executed as a Post-Build Event,
which means it is executed after the C# project has been built,
and the code behind asset has been created.
"SDKPath_2RobotStudio ReleaseNumber\bin\LibraryCompiler.exe" "$(ProjectDir)\SmartComponent1.xml" "$(ProjectDir)\"
Required Namespaces
ABB.Robotics.RobotStudio.Stations