UNPKG

8 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4/*
5* This is an extension and not part of the main GoJS library.
6* Note that the API for this class may change with any version, even point releases.
7* If you intend to use an extension in production, you should copy the code to your own source directory.
8* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
9* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
10*/
11import * as go from '../release/go-module.js';
12/**
13 * A custom routed {@link Link} for showing the distances between a point on one node and a point on another node.
14 *
15 * Note that because this is a Link, the points being measured must be on {@link Node}s, not simple {@link Part}s.
16 * The exact point on each Node is determined by the {@link Link#fromSpot} and {@link Link#toSpot}.
17 *
18 * Several properties of the DimensioningLink customize the appearance of the dimensioning:
19 * {@link #direction}, for orientation of the dimension line and which side it is on,
20 * {@link #extension}, for how far the dimension line is from the measured points,
21 * {@link #inset}, for leaving room for a text label, and
22 * {@link #gap}, for distance that the extension line starts from the measured points.
23 *
24 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/Dimensioning.html">Dimensioning</a> sample.
25 * @category Part Extension
26 */
27export class DimensioningLink extends go.Link {
28 /**
29 * Constructs a DimensioningLink and sets the following properties:
30 * - {@link #isLayoutPositioned} = false
31 * - {@link #isTreeLink} = false
32 * - {@link #routing} = {@link Link.Orthogonal}
33 */
34 constructor() {
35 super();
36 this._direction = 0;
37 this._extension = 30;
38 this._inset = 10;
39 this._gap = 10;
40 this.isLayoutPositioned = false;
41 this.isTreeLink = false;
42 this.routing = go.Link.Orthogonal;
43 }
44 /**
45 * Copies properties to a cloned DimensioningLink.
46 */
47 cloneProtected(copy) {
48 super.cloneProtected(copy);
49 copy._direction = this._direction;
50 copy._extension = this._extension;
51 copy._inset = this._inset;
52 copy._gap = this._gap;
53 }
54 /**
55 * The general angle at which the measurement should be made.
56 *
57 * The default value is 0, meaning to go measure only along the X axis,
58 * with the dimension line and label above the two nodes (at lower Y coordinates).
59 * New values must be one of: 0, 90, 180, 270, or NaN.
60 * The value NaN indicates that the measurement is point-to-point and not orthogonal.
61 */
62 get direction() { return this._direction; }
63 set direction(val) {
64 if (isNaN(val) || val === 0 || val === 90 || val === 180 || val === 270) {
65 this._direction = val;
66 }
67 else {
68 throw new Error('DimensioningLink: invalid new direction: ' + val);
69 }
70 }
71 /**
72 * The distance at which the dimension line should be from the points being measured.
73 *
74 * The default value is 30.
75 * Larger values mean further away from the nodes.
76 * The new value must be greater than or equal to zero.
77 */
78 get extension() { return this._extension; }
79 set extension(val) { this._extension = val; }
80 /**
81 * The distance that the dimension line should be "indented" from the ends of the
82 * extension lines that are orthogonal to the dimension line.
83 *
84 * The default value is 10.
85 */
86 get inset() { return this._inset; }
87 set inset(val) {
88 if (val >= 0) {
89 this._inset = val;
90 }
91 else {
92 throw new Error('DimensionLink: invalid new inset: ' + val);
93 }
94 }
95 /**
96 * The distance that the extension lines should come short of the measured points.
97 *
98 * The default value is 10.
99 */
100 get gap() { return this._gap; }
101 set gap(val) {
102 if (val >= 0) {
103 this._gap = val;
104 }
105 else {
106 throw new Error('DimensionLink: invalid new gap: ' + val);
107 }
108 }
109 /**
110 * Constructs the link's route by modifying {@link #points}.
111 * @return {boolean} true if it computed a route of points
112 */
113 computePoints() {
114 const fromnode = this.fromNode;
115 if (!fromnode)
116 return false;
117 const fromport = this.fromPort;
118 const fromspot = this.computeSpot(true);
119 const tonode = this.toNode;
120 if (!tonode)
121 return false;
122 const toport = this.toPort;
123 const tospot = this.computeSpot(false);
124 const frompoint = this.getLinkPoint(fromnode, fromport, fromspot, true, true, tonode, toport);
125 if (!frompoint.isReal())
126 return false;
127 const topoint = this.getLinkPoint(tonode, toport, tospot, false, true, fromnode, fromport);
128 if (!topoint.isReal())
129 return false;
130 this.clearPoints();
131 let ang = this.direction;
132 if (isNaN(ang)) {
133 ang = frompoint.directionPoint(topoint);
134 const p = new go.Point(this.extension, 0);
135 p.rotate(ang + 90);
136 const q = new go.Point(this.extension - this.inset, 0);
137 q.rotate(ang + 90);
138 const g = new go.Point(this.gap, 0);
139 g.rotate(ang + 90);
140 this.addPointAt(frompoint.x + g.x, frompoint.y + g.y);
141 this.addPointAt(frompoint.x + p.x, frompoint.y + p.y);
142 this.addPointAt(frompoint.x + q.x, frompoint.y + q.y);
143 this.addPointAt(topoint.x + q.x, topoint.y + q.y);
144 this.addPointAt(topoint.x + p.x, topoint.y + p.y);
145 this.addPointAt(topoint.x + g.x, topoint.y + g.y);
146 }
147 else {
148 const dist = this.extension;
149 let r = 0.0;
150 let s = 0.0;
151 let t0 = 0.0;
152 let t1 = 0.0;
153 if (ang === 0 || ang === 180) {
154 if (ang === 0) {
155 r = Math.min(frompoint.y, topoint.y) - this.extension;
156 s = r + this.inset;
157 t0 = frompoint.y - this.gap;
158 t1 = topoint.y - this.gap;
159 }
160 else {
161 r = Math.max(frompoint.y, topoint.y) + this.extension;
162 s = r - this.inset;
163 t0 = frompoint.y + this.gap;
164 t1 = topoint.y + this.gap;
165 }
166 this.addPointAt(frompoint.x, t0);
167 this.addPointAt(frompoint.x + 0.01, r);
168 this.addPointAt(frompoint.x, s);
169 this.addPointAt(topoint.x, s);
170 this.addPointAt(topoint.x - 0.01, r);
171 this.addPointAt(topoint.x, t1);
172 }
173 else if (ang === 90 || ang === 270) {
174 if (ang === 90) {
175 r = Math.max(frompoint.x, topoint.x) + this.extension;
176 s = r - this.inset;
177 t0 = frompoint.x + this.gap;
178 t1 = topoint.x + this.gap;
179 }
180 else {
181 r = Math.min(frompoint.x, topoint.x) - this.extension;
182 s = r + this.inset;
183 t0 = frompoint.x - this.gap;
184 t1 = topoint.x - this.gap;
185 }
186 this.addPointAt(t0, frompoint.y);
187 this.addPointAt(r, frompoint.y + 0.01);
188 this.addPointAt(s, frompoint.y);
189 this.addPointAt(s, topoint.y);
190 this.addPointAt(r, topoint.y - 0.01);
191 this.addPointAt(t1, topoint.y);
192 }
193 }
194 return true;
195 }
196}