# How to bind "Stroke Style" to a boolean property

**URL:** <https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534>\
**Category:** General Discussion\
**Created:** [January 9, 2017, 4:15pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534 "2017-01-09T16:15:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijustdunnoaname](https://avatars.discourse-cdn.com/v4/letter/i/5e9695/32.png) [@ijustdunnoaname](https://forum.inductiveautomation.com/u/ijustdunnoaname)\
**Post date:** [January 9, 2017, 4:15pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/1 "2017-01-09T16:15:29Z")

</div>

I’m in the beginning stages of a project and am currently building motor templates. Each template has the following custom properties:  
running\_color (color)  
stopped\_color (color)  
alarm\_color (color)  
running\_indication (Boolean)  
alarm\_indication (Boolean)

I would like to know how I could use the Boolean “alarm\_indication” to cause the motor’s “Stroke style” to increase to a width of 5.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [January 16, 2017, 5:54pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/2 "2017-01-16T17:54:47Z")

</div>

As far as I can see this is not possible with binding - you’ll have to use Python to do this. The information about setting stroke style in code came from tebright1’s post [here](https://inductiveautomation.com/forum/viewtopic.php?f=25&t=14815).

1. Right click on the background of your motor template and select ‘Scripting’
2. Select the propertyChange / propertyChange event
3. Enter the following code:

`if event.propertyName == 'alarm_indication':
	from java.awt import BasicStroke
	rectangle = event.source.getComponent('Rectangle')
	if event.newValue == 1:
		rectangle.strokeStyle = BasicStroke(5)
	else:
		rectangle.strokeStyle = BasicStroke(1)
`  
You will obviously have to change what object you are getting a reference for. Mine is called ‘Rectangle’. Your motor might consist of a number of shapes - you’ll have to set the strokeStyle for all the ones you want to increase in width.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 16, 2017, 7:06pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/3 "2017-01-16T19:06:04Z")

</div>

This is why I created the objectScript() expression function in the [Simulation Aids module](https://www.automation-pros.com/modulesales.html) – I wanted to efficiently use jython within bindings.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [January 16, 2017, 7:10pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/4 "2017-01-16T19:10:16Z")

</div>

What would the given example look like using objectScript() Phil?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 17, 2017, 12:35am UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/5 "2017-01-17T00:35:00Z")

</div>

[quote=“AlThePal”]What would the given example look like using objectScript() Phil?[/quote]First, you’d create a project or shared script that converts a boolean to a stroke:`from java.awt import BasicStroke
def alarmStroke(thick):
   return BasicStroke(5) if thick else BasicStroke(1)`Then call it from the binding expression with the boolean as an extra argument:`objectScript('project.stroke.alarmStroke(args[0])', {Root Container.alarm_indication})`Note that arguments after the string of code are presented as a tuple named ‘args’.  
The other major advantage of this approach is it always runs – no need to switch the designer to preview mode.

---

<div class="post-metadata">

**Author:** ![systemparadox](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@systemparadox](https://forum.inductiveautomation.com/u/systemparadox)\
**Post date:** [January 17, 2017, 9:05am UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/6 "2017-01-17T09:05:52Z")

</div>

Note that in Ignition 7.8 you can pass arguments directly to runScript:

```auto
runScript('project.stroke.alarmStroke', 0, {Root Container.alarm_indication})
```

(You still have to specify the polling rate, but if you set it to 0 that’s the same as no polling)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 17, 2017, 12:51pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/7 "2017-01-17T12:51:29Z")

</div>

Take a look at [this thread](https://inductiveautomation.com/forum/viewtopic.php?f=70&t=14114) for other ideas, as well. Files have been refreshed, so all the downloadable content will work.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 17, 2017, 12:55pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/8 "2017-01-17T12:55:15Z")

</div>

Unfortunately, runScript() can only pass arguments when the expression string is a function name. I often want to use one-liners for extra functionality. Say one rectangle needs a different alarm thickness – with objectScript, you can put that customization right in the expression:`objectScript('project.stroke.BasicStroke(10) if args[0] else project.stroke.BasicStroke(1)',
{Root Container.alarm_indication})`Note that java.awt.BasicStroke was imported into project.stroke, so it’s available to Ignition scripts with no further import.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [January 30, 2017, 6:04pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/9 "2017-01-30T18:04:53Z")

</div>

Just for completeness, here’s how you do this with runScript and Custom Methods on templates:

1. Create a template e.g. Test
2. Create an alarm\_indication Template Parameter on the template
3. Put a custom method on the root of the template:

`
def setBorderStyle(self, alarm_indication):
    from java.awt import BasicStroke
    return BasicStroke(5) if alarm_indication else BasicStroke(1)
`4. Put the following expression on the Stroke Style of the object you wish to animate:

```auto
runScript('self.parent.setBorderStyle(' + {Test.alarm_indication} + ')', 0)
```

Note the path to access the template root’s custom method from the object. Putting the custom method on the object itself and using a path of self.setBorderStyle works with objects on windows but not with templates.

As Phil points out in the following discussion, this form of the code is faster: `runScript('self.parent.setBorderStyle', 0, {Test.alarm_indication})`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 30, 2017, 7:37pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/10 "2017-01-30T19:37:38Z")

</div>

[quote=“AlThePal”]`runScript('self.parent.setBorderStyle(' + {Test.alarm_indication} + ')', 0)`[/quote]You can do this, but it won’t scale. Changing the executable string that runScript uses (or objectScript) will increase the execution time by a couple orders of magnitude. I’ve seen tens of milliseconds vs. hundreds of microseconds in typical cases. Don’t make jython compile things on the fly. That’s why argument passing is so important.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [January 30, 2017, 7:40pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/11 "2017-01-30T19:40:35Z")

</div>

How can you tell how long this is taking?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 30, 2017, 8:22pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/12 "2017-01-30T20:22:12Z")

</div>

[quote=“AlThePal”]How can you tell how long this is taking?[/quote]I instrumented objectScript() and view() while I developed them. Getting rid of jython compiler overhead wasn’t the original motivator for these functions, but that objective was quickly added. Oh, and the instrumentation is still present in the view() function and can be displayed by setting its logger to DEBUG mode.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [January 30, 2017, 8:24pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/13 "2017-01-30T20:24:56Z")

</div>

I can understand that you might have instrumented your own code, but how can you tell how long an internal Ignition function like runScript is taking?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 30, 2017, 8:32pm UTC](https://forum.inductiveautomation.com/t/how-to-bind-stroke-style-to-a-boolean-property/12534/14 "2017-01-30T20:32:28Z")

</div>

You can nest it inside of a do-nothing expression function that you have instrumented.
