You can trigger an AdoScript code that calculates the length of the relation and save the result into a hidden attribute, if the relation is created with the event handler "AfterCreateModelingConnector".
To calculate the length of the relation you neeed the position information of the source and target object. The AdoScript below shows yo how you can implement this scenario, which you also can find in the attached library as an example.
1ON_EVENT "AfterCreateModelingConnector" {
2
3#Save the pregiven values
4 SET nModelId: (modelid)
5 SET nObjId: (objid)
6 SET nClassId: (classid)
7 SET nFromObjId: (fromobjid)
8 SET nToObjId: (toobjid)
9
10#Get position of the source instance
11 CC "Core" GET_ATTR_VAL objid: (nFromObjId) attrname:"Position"
12 SET nFromObjPosition: (val)
13
14#Get position of the target instance
15 CC "Core" GET_ATTR_VAL objid: (nToObjId) attrname:"Position"
16 SET nToObjPosition: (val)
17
18#Parse the position attributes of the source and target instance and save the values you need in variables
19 LEO parse: (nFromObjPosition) get-tmm-value:n_x_fromobj:"x" get-tmm-value:n_y_fromobj:"y"
20 get-tmm-value:n_w_fromobj:"w" get-tmm-value:n_h_fromobj:"h"
21 LEO parse: (nToObjPosition) get-tmm-value:n_x_toobj:"x" get-tmm-value:n_y_toobj:"y"
22 get-tmm-value:n_w_toobj:"w" get-tmm-value:n_h_toobj:"h"
23
24# If your source and target instance are resizebale then request also the height and the width of the instances.
25# Otherwise take the values you defined in the GraphRep.
26
27# Differenciate if the target and source instance are in the same line -vertically and horizontally.
28# Note: These are simple conditions for demonstration
29 IF (n_y_fromobj=(n_y_toobj)) {
30 SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj))-(CMS(n_w_fromobj/2)+CMS(n_w_toobj/2)))
31 SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))
32 } ELSIF (n_x_fromobj=n_x_toobj) {
33 SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))-(CMS(n_h_fromobj/2)+CMS(n_h_toobj/2)))
34 SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj)))
35 } ELSE {
36 SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj))-(CMS(n_w_fromobj/2)+CMS(n_w_toobj/2)))
37 SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))-(CMS(n_h_fromobj/2)+CMS(n_h_toobj/2)))
38 }
39
40# Calculate the distance with the formula of PythagorasSET nDistance: (sqrt(pow(n_xDist, 2)+pow(n_yDist, 2)))
41# Set the value of the calculated distance into a hidden attribute so you can call it from the graphrep of the relation
42 CC "Core" GET_ATTR_ID classid: (nClassId) attrname:"Distance"
43 SET nDistanceAttrId: (attrid)
44 CC "Core" SET_ATTR_VAL objid: (nObjId) attrid: (nDistanceAttrId) val: (nDistance)
45}
Now you can call the attribute value of the Distance attribute in the GraphRep and set the width of the text as follows:
1GRAPHREP
2
3AVAL nDistance:"Distance"
4SET vDistance: (CM nDistance)
5
6MIDDLE
7ATTR "Name" x:0.1cm w:c: (vDistance) h:b
.