Getting notified when a property changes
This example provides information on comparing two objects defined as properties in Smart
The topic considers that a SmartComponent project created from Visual Studio exists.
Refer to topic Creating a Smart
The steps that will be performed in this topic are:
Add a signal and the properties to the SmartComponents XML file.
Add the description of the signal and the properties to the XML resource file.
Modify the code in 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. Declare two properties and one signal, as shown below:<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>
Open
SmartComponent1.en.xml
file. Provide descriptions for the component, its properties, and the signal.<SmartComponent name="SmartComponent1" 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="Set to high (1) if the objects are equal"/> </SmartComponent>
Open the "CodeBehind.cs" file and modify OnPropertyValueChanged. When the value of a property changes, On
Property gets triggered. The objects are compared. If the objects are equal, the Output signal is set to high; otherwise, it is set to low.Value Changed(Smart Component, Dynamic Property, object) Here are the steps to be followed to achieve the above mentioned task:
Get the value of property ObjectA.
public override void OnPropertyValueChanged(SmartComponent component, DynamicProperty changedProperty, object oldValue) { ProjectObject a = (ProjectObject)component.Properties["ObjectA"].Value; }
Get the value of property ObjectB.
public override void OnPropertyValueChanged(SmartComponent component, DynamicProperty changedProperty, object oldValue) { ProjectObject a = (ProjectObject)component.Properties["ObjectA"].Value; ProjectObject b = (ProjectObject)component.Properties["ObjectB"].Value; }
Set the signal to 1 if ObjectA value equals ObjectB value, otherwise 0.
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.
"%ProgramFiles(X86)%\ABB\RobotStudio {version}\bin\LibraryCompiler.exe" "$(ProjectDir)\SmartComponent1.xml" "$(ProjectDir)\"
Required Namespaces
ABB.