Getting notified when a property changes
This example provides information on comparing two objects defined as properties in SmartComponent thereby explaining the use of OnPropertyValueChanged(SmartComponent, DynamicProperty, Object) method.
This example provides snippets of XML and C# that could be used within the smart component project. An active station 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.
Add the properties in the SmartComponents XML file.
Add the description of the properties in the SmartComponent.en.xml file.
Modify the code of the CodeBehind.cs file.
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="ObjectCompare.rslib"> <SmartComponent icon="ObjectCompare.png" codeBehind="ObjectCompare.ObjectCompare"> <Properties> <DynamicProperty name="ObjectA" valueType="ABB.Robotics.RobotStudio.ProjectObject"/> <DynamicProperty name="ObjectB" valueType="ABB.Robotics.RobotStudio.ProjectObject"/> </Properties> <Signals> <IOSignal name="Output" signalType="DigitalOutput" readOnly="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="ObjectCompare" description="Sets a digital signal as a result of an object comparison"> <DynamicProperty name="ObjectA" description="First object"/> <DynamicProperty name="ObjectB" description="Second object"/> <IOSignal name="Output" description="Goes high (1) if the objects are equal"/> </SmartComponent>
Open "CodeBehind.cs" file and add the following code as events.When Property changes, OnPropertyValueChanged(SmartComponent, DynamicProperty, Object) gets triggered. The objects are compared. If the objects are equal the Output signal goes high else goes low.
Following are the steps to be followed to achieve the above mentioned task:
Get the value of property ObjectA.
ProjectObject a = (ProjectObject)component.Properties["ObjectA"].Value;
Get the value of property ObjectB.
ProjectObject b = (ProjectObject)component.Properties["ObjectB"].Value;
Set the signal if ObjectA value equals ObjectB value.
component.IOSignals["Output"].Value = a == b ? 1 : 0;
Example
The complete files for this SmartComponent are shown now:
public override void OnPropertyValueChanged(SmartComponent component,
DynamicProperty changedProperty, object oldValue)
{
ProjectObject a = (ProjectObject)component.Properties["ObjectA"].Value;
ProjectObject b = (ProjectObject)component.Properties["ObjectB"].Value;
component.IOSignals["Output"].Value = a == b ? 1 : 0;
}
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_2RobotStudioReleaseNumber\bin\LibraryCompiler.exe" "$(ProjectDir)\SmartComponent1.xml" "$(ProjectDir)\"
Required Namespaces
ABB.Robotics.RobotStudio.Stations