« Back to University of Vienna - OMILAB

Using an attribute for a calculation in GraphRep won't work as epxected

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Hi

While working with a notation that uses a diamond shape I wanted to properly set the size for displaying a text inside of the shape. The user is supposed to be able to set the desired width for the text (through the attribute "Text width" which has the type DOUBLE) and then the available height should be automatically calculated. Unfortunately this isn't working as expected. So my question now is: What have I done wrong and why isn't it working?

Following is the commented GraphRep code. Line 35 contains the calculation of the height based on the width (, triangles and the relation between the sides). Line 41 draws a rectangle that visualizes the box that should be used for the text (from line 38). Removing the '#' from line 30 changes the behavior to use a text width value based on the object's size instead of the attribute (proving that the formula is actually working, as long as the value is not taken from an attribute). So the question might be reuced to "Why is it working when I use SET to set the value, but not when I use AVAL (with a DOUBLE attribute)?"
 1GRAPHREP sizing:asymmetrical
 2
 3#-- COLOR DEFINITIONS --
 4SET fontcolor:("black")
 5SET colBrd:("black")
 6SET colFFm:("white")
 7
 8
 9# We use a table so it is properly resizable
10TABLE x:-1.6cm y:-0.9cm w:3.2cm h:1.8cm cols:2 rows:2
11      w1:50% w2:50% h1:50% h2:50%
12STRETCH off
13
14SET inx:(tabx0)
15SET iny:(taby0)
16SET inw:(tabw1) # Because we use a diamond this is only /2
17SET inh:(tabh1) # Because we use a diamond this is only /2
18
19PEN color:(colBrd)
20FILL color:(colFFm)
21POLYGON 4
22  x1:(inx+inw) y1:(iny)
23  x2:(inx+(2*inw)) y2:(iny+inh)
24  x3:(inx+inw) y3:(iny+(2*inh))
25  x4:(inx) y4:(iny+inh)
26
27
28# This is the width, the user should be able to specify it
29AVAL tw:"Text width"
30#SET tw:(CMS tabw1)
31
32# This formula calculates the available height for a given (tw) width
33# It is based on the relation between two (not drawn) "legs"/"catheti" of the
34# right-angled triangle. Each of the sides of the drawn shape is a "hypotenuse".
35SET th:(2*(inh-(CM ( (tw/2)*(CMS inh)/(CMS inw) ))))
36FONT color:(fontcolor)
37AVAL nametext:"Name"
38TEXT (nametext) x:(tabx1) y:(taby1) w:c:(CM tw) h:c:(th) line-break:rigorous
39
40# This rectangle is painted for debug purposes
41RECTANGLE x:(tabx1-(CM tw/2)) y:(taby1-(th/2)) w:(CM tw) h:(th)

Hi Patrik,

the reason for that is how AVAL values are interpreted when reading in the attribute value. By default, they are returned as STRINGs, and not automatically recognized as the data type specified in GraphRep. You can try the following solution to transform to the value:

1AVAL tw_str:"Text width"
2SET tw: (VAL tw_str)

You can also use the parameter  as-original-type  of the AVAL statement. In this case, the variable will not be of type STRING, but of the same type as the attribute
read.
1AVAL tw:"Text width" as-original-type

RE: Using an attribute for a calculation in GraphRep won't work as epxected
Answer
10/29/14 8:10 AM as a reply to Sabin Popescu.
Thanks for the answers.

Unfortunately the following code using the "as-original-type" did not work for me in the Development Toolkit (maybe I missed to change something?):
1AVAL tw:"Text width" as-original-type # so tw should be "DOUBLE"
2SET th:(2*(inh-(CM ( (tw/2)*(CMS inh)/(CMS inw) ))))
3# Print the "debug-recatngle"
4RECTANGLE x:(tabx1-(CM tw/2)) y:(taby1-(th/2)) w:(CM tw) h:(th)

Changing the type did the trick, and this is (part of) the code that I am currently using:
 1AVAL tw_str:"Text width"
 2SET tw:(CM (tw_str))
 3SET th:(2*(inh-(CM ( (CMS tw/2)*(CMS inh)/(CMS inw) ))))
 4# Print the text
 5FONT color:(fontcolor)
 6AVAL nametext:"Name"
 7TEXT (nametext) x:(tabx1) y:(taby1) w:c:(tw) h:c:(th) line-break:rigorous
 8# Print the "debug-recatngle"
 9#RECTANGLE x:(tabx1-(tw/2)) y:(taby1-(th/2)) w:(tw) h:(th)

I decided to use CM instead of VAL so that both tw and th are measured values in hopes of making the whole thing a bit easier to read/understand.