@design-core/core 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. package/.eslintrc.json +14 -7
  2. package/.github/workflows/DesignCoreQA.yml +3 -1
  3. package/README.md +11 -4
  4. package/TODO.md +19 -0
  5. package/core/core/core.js +49 -42
  6. package/core/designCore.js +61 -2
  7. package/core/dimensions/alignedDimension.js +142 -0
  8. package/core/dimensions/angularDimension.js +550 -0
  9. package/core/dimensions/baseCircularDimension.js +244 -0
  10. package/core/dimensions/baseDimension.js +558 -0
  11. package/core/dimensions/baseLinearDimension.js +371 -0
  12. package/core/dimensions/diametricDimension.js +82 -0
  13. package/core/dimensions/dimension.js +298 -0
  14. package/core/dimensions/linearDimension.js +29 -0
  15. package/core/dimensions/radialDimension.js +78 -0
  16. package/core/dimensions/rotatedDimension.js +167 -0
  17. package/core/entities/arc.js +314 -60
  18. package/core/entities/arctext.js +642 -0
  19. package/core/entities/basePolyline.js +553 -94
  20. package/core/entities/circle.js +181 -54
  21. package/core/entities/entity.js +154 -74
  22. package/core/entities/hatch.js +807 -0
  23. package/core/entities/insert.js +200 -39
  24. package/core/entities/line.js +196 -134
  25. package/core/entities/lwpolyline.js +21 -44
  26. package/core/entities/point.js +194 -29
  27. package/core/entities/polyline.js +54 -7
  28. package/core/entities/rectangle.js +43 -13
  29. package/core/entities/solid.js +43 -6
  30. package/core/entities/text.js +324 -181
  31. package/core/lib/boundingBox.js +73 -13
  32. package/core/lib/canvas.js +256 -65
  33. package/core/lib/clipboard.js +198 -0
  34. package/core/lib/colour.js +144 -0
  35. package/core/lib/colours.js +367 -301
  36. package/core/lib/commandLine.js +16 -28
  37. package/core/lib/commandManager.js +115 -58
  38. package/core/lib/constants.js +23 -0
  39. package/core/lib/dxf/dxf.js +249 -34
  40. package/core/lib/dxf/dxfFile.js +23 -38
  41. package/core/lib/dxf/dxfIterator.js +17 -5
  42. package/core/lib/dxf/dxfRead.js +23 -7
  43. package/core/lib/dxf/dxfWrite.js +39 -129
  44. package/core/lib/dxf/handleManager.js +97 -0
  45. package/core/lib/dxf/sections/blocks.js +16 -2
  46. package/core/lib/dxf/sections/entities.js +17 -3
  47. package/core/lib/dxf/sections/header.js +10 -1
  48. package/core/lib/dxf/sections/objects.js +36 -0
  49. package/core/lib/dxf/sections/section.js +43 -5
  50. package/core/lib/dxf/sections/tables.js +17 -3
  51. package/core/lib/entityManager.js +178 -0
  52. package/core/lib/fileio.js +14 -4
  53. package/core/lib/headers.js +229 -0
  54. package/core/lib/input.js +46 -0
  55. package/core/lib/inputManager.js +199 -194
  56. package/core/lib/intersect.js +341 -468
  57. package/core/lib/logging.js +25 -1
  58. package/core/lib/matrix.js +6 -8
  59. package/core/lib/mouse.js +24 -79
  60. package/core/lib/patterns.js +690 -0
  61. package/core/lib/promptOptions.js +133 -0
  62. package/core/lib/scene.js +71 -174
  63. package/core/lib/selectionManager.js +74 -48
  64. package/core/lib/selectionWindow.js +24 -10
  65. package/core/lib/settings.js +18 -11
  66. package/core/lib/snapping.js +129 -21
  67. package/core/lib/stateManager.js +288 -0
  68. package/core/lib/strings.js +57 -5
  69. package/core/lib/utils.js +95 -11
  70. package/core/objects/dictionary.js +43 -0
  71. package/core/objects/dictionaryManager.js +80 -0
  72. package/core/properties/dimType.js +117 -0
  73. package/core/properties/flags.js +66 -0
  74. package/core/properties/property.js +26 -0
  75. package/core/properties/propertyManager.js +65 -19
  76. package/core/tables/appId.js +31 -0
  77. package/core/tables/appIdManager.js +44 -0
  78. package/core/tables/block.js +323 -0
  79. package/core/tables/blockManager.js +48 -0
  80. package/core/tables/blockRecord.js +29 -0
  81. package/core/tables/blockRecordManager.js +37 -0
  82. package/core/tables/dimStyle.js +627 -0
  83. package/core/tables/dimStyleManager.js +187 -0
  84. package/core/tables/endblk.js +28 -0
  85. package/core/tables/layer.js +219 -0
  86. package/core/tables/layerManager.js +47 -0
  87. package/core/tables/ltype.js +85 -0
  88. package/core/tables/ltypeManager.js +65 -0
  89. package/core/tables/style.js +145 -0
  90. package/core/tables/styleManager.js +55 -0
  91. package/core/tables/styleManagerBase.js +110 -0
  92. package/core/tables/tableManagerBase.js +354 -0
  93. package/core/tables/ucs.js +28 -0
  94. package/core/tables/ucsManager.js +44 -0
  95. package/core/tables/view.js +28 -0
  96. package/core/tables/viewManager.js +44 -0
  97. package/core/tables/vport.js +70 -0
  98. package/core/tables/vportManager.js +65 -0
  99. package/core/tools/chamfer.js +343 -0
  100. package/core/tools/chamferFilletBase.js +162 -0
  101. package/core/tools/copy.js +68 -40
  102. package/core/tools/copybase.js +65 -0
  103. package/core/tools/copyclip.js +59 -0
  104. package/core/tools/cornerEntity.js +138 -0
  105. package/core/tools/cutclip.js +72 -0
  106. package/core/tools/distance.js +37 -9
  107. package/core/tools/erase.js +43 -13
  108. package/core/tools/explode.js +117 -0
  109. package/core/tools/extend.js +71 -32
  110. package/core/tools/fillet.js +336 -0
  111. package/core/tools/identify.js +33 -6
  112. package/core/tools/matchProp.js +106 -0
  113. package/core/tools/mirror.js +141 -0
  114. package/core/tools/move.js +65 -32
  115. package/core/tools/offset.js +321 -0
  116. package/core/tools/pan.js +54 -0
  117. package/core/tools/pasteclip.js +93 -0
  118. package/core/tools/purge.js +90 -0
  119. package/core/tools/rotate.js +105 -70
  120. package/core/tools/scale.js +166 -0
  121. package/core/tools/tool.js +14 -4
  122. package/core/tools/trim.js +71 -32
  123. package/core/tools/zoom.js +152 -0
  124. package/package.json +3 -305
  125. package/test/dimensions/alignedDimension.test.js +211 -0
  126. package/test/dimensions/angularDimension.test.js +243 -0
  127. package/test/dimensions/baseDimension.test.js +239 -0
  128. package/test/dimensions/diametricDimension.test.js +203 -0
  129. package/test/dimensions/dimension.test.js +102 -0
  130. package/test/dimensions/radialDimension.test.js +203 -0
  131. package/test/dimensions/rotatedDimension.test.js +224 -0
  132. package/test/entities/arc.test.js +463 -41
  133. package/test/entities/arctext.test.js +329 -0
  134. package/test/entities/basePolyline.test.js +459 -10
  135. package/test/entities/circle.test.js +312 -7
  136. package/test/entities/entity.test.js +28 -9
  137. package/test/entities/hatch.test.js +606 -0
  138. package/test/entities/insert.test.js +90 -0
  139. package/test/entities/line.test.js +182 -6
  140. package/test/entities/point.test.js +375 -37
  141. package/test/entities/solid.test.js +156 -0
  142. package/test/entities/text.test.js +533 -7
  143. package/test/integration/exportIntegration.reference.dxf +1706 -0
  144. package/test/integration/exportIntegration.test.js +186 -0
  145. package/test/integration/importExportIntegration.test.js +126 -0
  146. package/test/integration/intersectionIntegration.test.js +880 -0
  147. package/test/integration/userInputIntegration.reference.dxf +1410 -0
  148. package/test/integration/userInputIntegration.test.js +269 -0
  149. package/test/lib/boundingBox.test.js +26 -2
  150. package/test/lib/canvas.test.js +613 -5
  151. package/test/lib/clipboard.test.js +70 -0
  152. package/test/lib/colour.test.js +114 -0
  153. package/test/lib/colours.test.js +63 -18
  154. package/test/lib/commandLine.test.js +159 -22
  155. package/test/lib/commandManager.test.js +4 -4
  156. package/test/lib/constants.test.js +9 -0
  157. package/test/lib/dxf/dxfFile.test.js +6 -2
  158. package/test/lib/dxf/dxfIterator.test.js +1 -1
  159. package/test/lib/dxf/dxfWrite.test.js +173 -0
  160. package/test/lib/dxf/handleManager.test.js +137 -0
  161. package/test/lib/dxf/sections/blocks.test.js +5 -5
  162. package/test/lib/dxf/sections/entities.test.js +4 -4
  163. package/test/lib/dxf/sections/header.test.js +18 -4
  164. package/test/lib/dxf/sections/objects.test.js +132 -0
  165. package/test/lib/dxf/sections/section.test.js +29 -29
  166. package/test/lib/dxf/sections/tables.test.js +3 -3
  167. package/test/lib/entityManager.test.js +163 -0
  168. package/test/lib/headers.test.js +233 -0
  169. package/test/lib/inputManager.test.js +1144 -5
  170. package/test/lib/intersect.test.js +454 -331
  171. package/test/lib/logging.test.js +1 -1
  172. package/test/lib/matrix.test.js +1 -1
  173. package/test/lib/mouse.test.js +327 -4
  174. package/test/lib/patterns.test.js +105 -0
  175. package/test/lib/promptOptions.test.js +179 -0
  176. package/test/lib/scene.test.js +16 -52
  177. package/test/lib/selectionManager.test.js +17 -5
  178. package/test/lib/settings.test.js +111 -5
  179. package/test/lib/snapping.test.js +142 -4
  180. package/test/lib/stateManager.test.js +155 -0
  181. package/test/lib/utils.test.js +81 -1
  182. package/test/objects/dictionary.test.js +81 -0
  183. package/test/objects/dictionaryManager.test.js +34 -0
  184. package/test/properties/dimType.test.js +62 -0
  185. package/test/properties/flags.test.js +164 -0
  186. package/test/properties/property.test.js +13 -0
  187. package/test/properties/propertyManager.test.js +10 -11
  188. package/test/tables/appId.test.js +46 -0
  189. package/test/tables/appIdManager.test.js +72 -0
  190. package/test/tables/block.test.js +157 -0
  191. package/test/tables/blockManager.test.js +52 -0
  192. package/test/tables/blockRecord.test.js +39 -0
  193. package/test/tables/blockRecordManager.test.js +60 -0
  194. package/test/tables/dimstyle.test.js +72 -0
  195. package/test/tables/endblk.test.js +30 -0
  196. package/test/tables/layer.test.js +95 -0
  197. package/test/tables/layerManager.test.js +172 -0
  198. package/test/{styles → tables}/ltype.test.js +7 -7
  199. package/test/{styles → tables}/ltypeManager.test.js +2 -2
  200. package/test/tables/style.test.js +44 -0
  201. package/test/tables/stylemanagerbase.test.js +220 -0
  202. package/test/tables/tableManagerBase.test.js +194 -0
  203. package/test/tables/ucs.test.js +37 -0
  204. package/test/tables/ucsManager.test.js +48 -0
  205. package/test/tables/view.test.js +37 -0
  206. package/test/tables/viewManager.test.js +48 -0
  207. package/test/tables/vport.test.js +117 -0
  208. package/test/tables/vportManager.test.js +54 -0
  209. package/test/test-helpers/test-helpers.js +66 -0
  210. package/test/tools/chamfer.test.js +970 -0
  211. package/test/tools/chamferFilletBase.test.js +446 -0
  212. package/test/tools/copy.test.js +22 -14
  213. package/test/tools/copybase.test.js +93 -0
  214. package/test/tools/copyclip.test.js +92 -0
  215. package/test/tools/cornerEntity.test.js +201 -0
  216. package/test/tools/cutclip.test.js +94 -0
  217. package/test/tools/distance.test.js +140 -18
  218. package/test/tools/erase.test.js +12 -12
  219. package/test/tools/explode.test.js +45 -0
  220. package/test/tools/extend.test.js +326 -44
  221. package/test/tools/fillet.test.js +831 -0
  222. package/test/tools/identify.test.js +115 -16
  223. package/test/tools/matchProp.test.js +84 -0
  224. package/test/tools/mirror.test.js +475 -0
  225. package/test/tools/move.test.js +75 -19
  226. package/test/tools/offset.test.js +748 -0
  227. package/test/tools/pan.test.js +72 -0
  228. package/test/tools/pasteclip.test.js +100 -0
  229. package/test/tools/purge.test.js +89 -0
  230. package/test/tools/rotate.test.js +89 -23
  231. package/test/tools/scale.test.js +529 -0
  232. package/test/tools/trim.test.js +419 -84
  233. package/test/tools/zoom.test.js +504 -0
  234. package/core/entities/alignedDimension.js +0 -279
  235. package/core/entities/angularDimension.js +0 -194
  236. package/core/entities/baseDimension.js +0 -227
  237. package/core/entities/block.js +0 -247
  238. package/core/entities/diametricDimension.js +0 -133
  239. package/core/entities/dimension.js +0 -202
  240. package/core/entities/radialDimension.js +0 -126
  241. package/core/styles/dimStyle.js +0 -719
  242. package/core/styles/dimStyleManager.js +0 -47
  243. package/core/styles/layer.js +0 -133
  244. package/core/styles/layerManager.js +0 -40
  245. package/core/styles/ltype.js +0 -86
  246. package/core/styles/ltypeManager.js +0 -50
  247. package/core/styles/style.js +0 -158
  248. package/core/styles/styleManager.js +0 -43
  249. package/core/styles/styleManagerBase.js +0 -299
  250. package/test/entities/block.test.js +0 -22
  251. package/test/styles/dimstyle.test.js +0 -19
  252. package/test/styles/layerManager.test.js +0 -150
  253. package/test/styles/style.test.js +0 -33
  254. package/test/styles/stylemanagerbase.test.js +0 -219
@@ -0,0 +1,558 @@
1
+ import { Point } from '../entities/point.js';
2
+ import { Block } from '../tables/block.js';
3
+ import { Text } from '../entities/text.js';
4
+ import { Solid } from '../entities/solid.js';
5
+ import { Line } from '../entities/line.js';
6
+ import { Entity } from '../entities/entity.js';
7
+ import { Logging } from '../lib/logging.js';
8
+ import { Property } from '../properties/property.js';
9
+ import { Strings } from '../lib/strings.js';
10
+ import { Utils } from '../lib/utils.js';
11
+
12
+ import { DesignCore } from '../designCore.js';
13
+ import { DimType } from '../properties/dimType.js';
14
+
15
+ /**
16
+ * Base Dimension Entity Class
17
+ * @extends Entity
18
+ */
19
+ export class BaseDimension extends Entity {
20
+ /**
21
+ * Create a Base Dimension
22
+ * @param {Array} data
23
+ */
24
+ constructor(data) {
25
+ super(data);
26
+
27
+ // hide inherited property
28
+ // needs to be enumerable=false to not appear in the object props
29
+ Object.defineProperty(this, 'lineType', {
30
+ enumerable: false,
31
+ });
32
+
33
+ // hide inherited property
34
+ Object.defineProperty(this, 'lineWidth', {
35
+ enumerable: false,
36
+ });
37
+
38
+ Object.defineProperty(this, 'blockName', {
39
+ value: '',
40
+ writable: true,
41
+ });
42
+
43
+ Object.defineProperty(this, 'block', {
44
+ value: new Block({ points: [new Point(), new Point()] }),
45
+ writable: true,
46
+ });
47
+
48
+ /*
49
+ Object.defineProperty(this, 'text', {
50
+ value: new Text(),
51
+ writable: true,
52
+ });
53
+ */
54
+
55
+ Object.defineProperty(this, 'textOverride', {
56
+ value: '',
57
+ writable: true,
58
+ enumerable: true,
59
+ });
60
+
61
+ Object.defineProperty(this, 'dimType', {
62
+ value: new DimType(),
63
+ writable: true,
64
+ });
65
+
66
+ Object.defineProperty(this, 'dimensionStyle', {
67
+ value: 'STANDARD',
68
+ writable: true,
69
+ enumerable: true,
70
+ });
71
+
72
+ Object.defineProperty(this, 'leaderLength', {
73
+ value: 0,
74
+ writable: true,
75
+ });
76
+
77
+ Object.defineProperty(this, 'linearDimAngle', {
78
+ value: 0,
79
+ writable: true,
80
+ });
81
+
82
+ if (data) {
83
+ if (data.hasOwnProperty('1')) {
84
+ // DXF Groupcode 1 - Dimension text
85
+ // The string explicitly entered by the user.
86
+ // Optional; default is the measurement. If null or “<>”, the dimension measurement is drawn as the text,
87
+ // if ““ (one blank space), the text is suppressed. Anything else is drawn as the text
88
+ this.textOverride = Property.loadValue([data[1], data.textOverride], '');
89
+ }
90
+
91
+ /*
92
+ if (data.hasOwnProperty('block')) {
93
+ // Reference to block that contains the entities that make up this dimension
94
+ this.block = data.block;
95
+ }*/
96
+
97
+ if (data.hasOwnProperty('blockName') || data.hasOwnProperty('2')) {
98
+ // DXF Groupcode 2 - Blockname
99
+ this.blockName = Property.loadValue([data.blockName, data[2]], '');
100
+ }
101
+
102
+ if (data.hasOwnProperty('dimensionStyle') || data.hasOwnProperty('3')) {
103
+ // DXF Groupcode 3 - Dimension Style Name
104
+ this.dimensionStyle = Property.loadValue([data.dimensionStyle, data[3]], 'STANDARD');
105
+ }
106
+
107
+ if (data.hasOwnProperty('40')) {
108
+ // DXF Groupcode 40 - Leader length for radius and diameter dimensions
109
+ this.leaderLength = data[40];
110
+ }
111
+
112
+ if (data.hasOwnProperty('41')) {
113
+ // DXF Groupcode 41 - Line Spacing Factor
114
+ // Percentage of default (3-on-5) line spacing to be applied.
115
+ // Valid values range from 0.25 to 4.00
116
+ const err = 'Groupcode 41 not implemented';
117
+ Logging.instance.debug(`${this.type} - ${err}`);
118
+ }
119
+
120
+ if (data.hasOwnProperty('42')) {
121
+ // DXF Groupcode 42 - Actual Measurement
122
+ // Read-only
123
+ const err = 'Groupcode 42 not implemented';
124
+ Logging.instance.debug(`${this.type} - ${err}`);
125
+ }
126
+
127
+ if (data.hasOwnProperty('50')) {
128
+ // DXF Groupcode 50 - Angle of rotated, horizontal, or vertical dimensions
129
+ this.linearDimAngle = Property.loadValue([data[50]], 0);
130
+ }
131
+
132
+ if (data.hasOwnProperty('51')) {
133
+ // DXF Groupcode 51 - Horizontal Direction
134
+ // All dimension types have an optional 51 group code, which indicates the horizontal direction
135
+ // for the dimension entity. The dimension entity determines the orientation of dimension
136
+ // text and lines for horizontal, vertical, and rotated linear dimensions
137
+ // This group value is the negative if the angle between the OCS X axis and the UCS X axis.
138
+ // It is always in the XY plane of the OCS
139
+ const err = 'Groupcode 51 not implemented';
140
+ Logging.instance.warn(`${this.type} - ${err}`);
141
+ }
142
+
143
+ if (data.hasOwnProperty('angle') || data.hasOwnProperty('53')) {
144
+ // DXF Groupcode 53 - Rotation
145
+ // rotation angle of the dimension text away from its default orientation
146
+ this.angle = Property.loadValue([data.angle, data[53]], 0);
147
+ }
148
+
149
+ if (data.hasOwnProperty('dimType') || data.hasOwnProperty('70')) {
150
+ // DXF Groupcode 70 - Dimension Type
151
+ // Values 0-6 are integer values that represent the dimension type. Values 32, 64, and 128
152
+ // are bit values, which are added to the integer values
153
+ // (value 32 is always set in R13 and later releases)
154
+ // 0 = Rotated, horizontal, or vertical;
155
+ // 1 = Aligned;
156
+ // 2 = Angular;
157
+ // 3 = Diameter;
158
+ // 4 = Radius;
159
+ // 5 = Angular 3 point;
160
+ // 6 = Ordinate;
161
+ // 32 = Indicates that the block reference (group code 2) is referenced by this dimension only
162
+ // 64 = Ordinate type. This is a bit value (bit 7) used only with integer value 6.
163
+ // If set, ordinate is X-type; if not set, ordinate is Y-type
164
+ // 128 = This is a bit value (bit 8) added to the other group 70 values if the dimension text
165
+ // has been positioned at a user-defined location rather than at the default location
166
+
167
+ let dimTypeValue = data.dimType;
168
+
169
+ if (data.dimType instanceof DimType) {
170
+ dimTypeValue = data.dimType.getBaseDimType();
171
+ }
172
+
173
+ const dimensionType = Property.loadValue([data[70], dimTypeValue], 0);
174
+
175
+ if (!DimType.isValidDimensionType(dimensionType)) {
176
+ const msg = 'Invalid Dimension Type';
177
+ const err = (`${this.type} - ${msg}: ${dimensionType}`);
178
+ throw Error(err);
179
+ }
180
+
181
+ this.dimType.setDimType(dimensionType);
182
+ }
183
+
184
+ if (data.hasOwnProperty('71')) {
185
+ // DXF Groupcode 71 - Attachment Point
186
+ // 1 = Top left; 2 = Top center; 3 = Top right
187
+ // 4 = Middle left; 5 = Middle center; 6 = Middle right
188
+ // 7 = Bottom left; 8 = Bottom center; 9 = Bottom right
189
+ const err = 'Groupcode 71 not implemented';
190
+ Logging.instance.debug(`${this.type} - ${err}`);
191
+ }
192
+
193
+ if (data.hasOwnProperty('72')) {
194
+ // DXF Groupcode 72 - Line Spacing
195
+ // 1 (or missing) = At least (taller characters will override)
196
+ // 2 = Exact (taller characters will not override)
197
+ const err = 'Groupcode 72 not implemented';
198
+ Logging.instance.debug(`${this.type} - ${err}`);
199
+ }
200
+
201
+ if (data.leaderLength) {
202
+ this.leaderLength = data.leaderLength;
203
+ }
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Get the dimension value formatted according to the dimension style
209
+ * @param {number} dimensionValue - the value to format
210
+ * @return {string} - the formatted dimension value
211
+ */
212
+ getDimensionValue(dimensionValue) {
213
+ // Formatted value to return
214
+ let formattedDimensionValue = '';
215
+
216
+ // Default precision from style
217
+ const precision = this.getDimensionStyle().getValue('DIMDEC');
218
+ const angularPrecision = this.getDimensionStyle().getValue('DIMADEC');
219
+
220
+ // Dimension Rounding - DIMRND
221
+ // rounding is not applied to angular dimensions
222
+ const DIMRND = this.getDimensionStyle().getValue('DIMRND');
223
+ if (DIMRND !== 0 && this.dimType.getBaseDimType() !== 2) {
224
+ dimensionValue = Math.round(dimensionValue / DIMRND) * DIMRND;
225
+ }
226
+
227
+ let linearDimensionValue = Math.abs(dimensionValue).toFixed(precision);
228
+ let angularDimensionValue = Math.abs(dimensionValue).toFixed(angularPrecision);
229
+
230
+ // Zero suppression
231
+ // Values 0-3 affect feet-and-inch dimensions only:
232
+ // 0 = Suppresses zero feet and precisely zero inches
233
+ // 1 = Includes zero feet and precisely zero inches
234
+ // 2 = Includes zero feet and suppresses zero inches
235
+ // 3 = Includes zero inches and suppresses zero feet
236
+ // 4 = Suppresses leading zeros in decimal dimensions (for example, 0.5000 becomes .5000)
237
+ // 8 = Suppresses trailing zeros in decimal dimensions (for example, 12.5000 becomes 12.5)
238
+ // 12 = Suppresses both leading and trailing zeros (for example, 0.5000 becomes .5)
239
+
240
+ const DIMZIN = this.getDimensionStyle().getValue('DIMZIN');
241
+ const suppressLeadingLinear = (DIMZIN === 4 || DIMZIN === 12);
242
+ const suppressTrailingLinear = (DIMZIN === 8 || DIMZIN === 12);
243
+ linearDimensionValue = this.suppressZeros(linearDimensionValue, suppressLeadingLinear, suppressTrailingLinear);
244
+
245
+ const DIMAZIN = this.getDimensionStyle().getValue('DIMAZIN');
246
+ const suppressLeadingAngular = (DIMAZIN === 1 || DIMAZIN === 3);
247
+ const suppressTrailingAngular = (DIMAZIN === 2 || DIMAZIN === 3);
248
+ angularDimensionValue = this.suppressZeros(angularDimensionValue, suppressLeadingAngular, suppressTrailingAngular);
249
+
250
+ switch (this.dimType.getBaseDimType()) {
251
+ case 0: // Rotated, horizontal, or vertical
252
+ case 1: // Aligned
253
+ formattedDimensionValue = `${linearDimensionValue}`;
254
+ break;
255
+ case 2: // Angular
256
+ formattedDimensionValue = `${angularDimensionValue}${Strings.Symbol.DEGREE}`;
257
+ break;
258
+ case 3: // Diameter
259
+ formattedDimensionValue = `${Strings.Symbol.DIAMETER}${linearDimensionValue}`;
260
+ break;
261
+ case 4: // Radius
262
+ formattedDimensionValue = `${Strings.Symbol.RADIUS}${linearDimensionValue}`;
263
+ break;
264
+ case 5: // Angular 3 point
265
+ formattedDimensionValue = `${angularDimensionValue}${Strings.Symbol.DEGREE}`;
266
+ break;
267
+ case 6: // Ordinate
268
+ // Ordinate dimensions are typically used to indicate the X or Y coordinate of a point
269
+ formattedDimensionValue = `${linearDimensionValue} ${Strings.Symbol.UNITS}`;
270
+ break;
271
+ default:
272
+ formattedDimensionValue = `${linearDimensionValue}`;
273
+ }
274
+
275
+ // Decimal separator - DIMDSEP
276
+ const DIMDSEP = this.getDimensionStyle().getValue('DIMDSEP');
277
+ // Replace dot with specified separator
278
+ if (DIMDSEP !== '.') {
279
+ formattedDimensionValue = formattedDimensionValue.replace('.', DIMDSEP);
280
+ }
281
+
282
+ // Handle textOverride
283
+ if (this.textOverride !== '') {
284
+ formattedDimensionValue = this.textOverride.replace(/<>/g, formattedDimensionValue);
285
+ }
286
+
287
+ // TODO: Implement prefix and postsuffix - DIMPOST
288
+ // prefix and suffix included in DIMPOST value separated by <>
289
+ // e.g. prefix<>suffix
290
+ // const DIMPOST = this.getDimensionStyle().getValue('DIMPOST');
291
+ // const DIMAPOST = this.getDimensionStyle().getValue('DIMAPOST');
292
+
293
+ // TODO: Implement Unit format - DIMLUNIT
294
+ // 1 = Scientific; 2 = Decimal; 3 = Engineering;
295
+ // 4 = Architectural; 5 = Fractional; 6 = Windows desktop
296
+ // const DIMLUNIT = this.getDimensionStyle().getValue('DIMLUNIT');
297
+
298
+ return formattedDimensionValue;
299
+ }
300
+
301
+ /**
302
+ * Suppress leading and/or trailing zeros from a dimension value
303
+ * @param {number} dimensionValue
304
+ * @param {boolean} suppressLeading
305
+ * @param {boolean} suppressTrailing
306
+ * @return {string} - the dimension value as a string with suppressed zeros
307
+ */
308
+ suppressZeros(dimensionValue, suppressLeading, suppressTrailing) {
309
+ let numberString = dimensionValue.toString();
310
+
311
+ if (suppressLeading) {
312
+ // Suppress leading zeros
313
+ numberString = numberString.replace(/^0+/, '');
314
+ }
315
+
316
+ if (suppressTrailing) {
317
+ // Suppress trailing zeros
318
+ numberString = numberString.replace(/(\.\d*?[1-9])0+$/g, '$1').replace(/\.0+$/, '');
319
+ }
320
+
321
+ if (numberString === '') {
322
+ // If the result is an empty string return '0'
323
+ numberString = '0';
324
+ }
325
+
326
+ return numberString;
327
+ }
328
+
329
+ /**
330
+ * set the dimensions text value
331
+ * @param {string} textValue - the dimension value to set
332
+ * @param {Point} textPosition - the position of the text
333
+ * @param {number} textRotation - the rotation of the text (radians)
334
+ * @return {Text} Dimension Text
335
+ */
336
+ getDimensionText(textValue, textPosition, textRotation) {
337
+ const text = new Text();
338
+ // get the text height
339
+ const textHeight = this.getDimensionStyle().getValue('DIMTXT');
340
+ // set the text height
341
+ text.height = textHeight;
342
+ // Always set text horizontal alignment to center
343
+ text.horizontalAlignment = 1;
344
+ // Always set text vertical alignment to middle
345
+ text.verticalAlignment = 2;
346
+ // set the text value
347
+ text.string = this.getDimensionValue(textValue);
348
+ // set the text position
349
+ text.points = [textPosition];
350
+ // set the text rotation
351
+ text.setRotation(Utils.radians2degrees(this.getTextDirection(textRotation)));
352
+
353
+ return text;
354
+ }
355
+
356
+ /**
357
+ * Get the text direction ensuring it is always oriented correctly
358
+ * @param {number} textDirection - the direction of the text (radians)
359
+ * @return {number} - the adjusted text direction (radians)
360
+ */
361
+ getTextDirection(textDirection) {
362
+ // Ensure the text is orientated correctly
363
+ if (textDirection > Math.PI / 2 && textDirection <= Math.PI * 1.5) {
364
+ textDirection = textDirection + Math.PI;
365
+ }
366
+ return textDirection;
367
+ }
368
+
369
+ /**
370
+ * Check if two angles are aligned or opposite
371
+ * @param {number} angle1 in radians
372
+ * @param {number} angle2 in radians
373
+ * @return {boolean} true if aligned or opposite
374
+ */
375
+ alignedOrOpposite(angle1, angle2) {
376
+ const firstAngle = Utils.round(angle1 % (Math.PI * 2));
377
+ const secondAngle = Utils.round(angle2 % (Math.PI * 2));
378
+ const reversedSecondAngle = Utils.round((angle2 + Math.PI) % (Math.PI * 2));
379
+
380
+ return (firstAngle === secondAngle || firstAngle === reversedSecondAngle);
381
+ }
382
+
383
+ /**
384
+ * Get the dimension style
385
+ * @return {Object} - the dimension style object
386
+ */
387
+ getDimensionStyle() {
388
+ return DesignCore.DimStyleManager.getItemByName(this.dimensionStyle);
389
+ }
390
+
391
+ /**
392
+ * Get the point for the sequence number
393
+ * @param {Array} points - array of points to search
394
+ * @param {number} sequenceNumber
395
+ * @return {Point}
396
+ */
397
+ getPointBySequence(points, sequenceNumber) {
398
+ const point = points.find((point) => point.sequence === sequenceNumber);
399
+
400
+ if (!point) {
401
+ return null;
402
+ }
403
+
404
+ return point;
405
+ }
406
+
407
+ /**
408
+ * Define the arrow head
409
+ * @param {Point} point
410
+ * @param {number} angle
411
+ * @return {Solid}
412
+ */
413
+ getArrowHead(point, angle) {
414
+ // TODO: implement arrow styles
415
+ // get the arrow size
416
+ const arrowSize = this.getDimensionStyle().getValue('DIMASZ');
417
+ let p1 = new Point(point.x + arrowSize / 4, point.y + arrowSize);
418
+ let p2 = new Point(point.x - arrowSize / 4, point.y + arrowSize);
419
+ const ang = angle - Math.PI / 2;
420
+ p1 = p1.rotate(point, ang);
421
+ p2 = p2.rotate(point, ang);
422
+
423
+ const points = [point, p1, p2];
424
+
425
+ const arrowHead = new Solid({ points: points });
426
+
427
+ return arrowHead;
428
+ }
429
+
430
+ /**
431
+ * Get the geometry for the centre mark
432
+ * @param {Point} point centre point for center mark
433
+ * @return {Array} array of entities representing the center mark
434
+ */
435
+ getCentreMark(point) {
436
+ /*
437
+ * DIMCEN value defines the centre mark type and size
438
+ * 0 = No center marks or lines are drawn
439
+ * <0 = Centerlines are drawn
440
+ * >0 = Center marks are drawn
441
+ */
442
+
443
+ const centreMark = [];
444
+ // get the center mark style
445
+ const markStyle = this.getDimensionStyle().getValue('DIMCENSTYL');
446
+ // get the centre mark size
447
+ const markSize = this.getDimensionStyle().getValue('DIMCENVALUE');
448
+
449
+ if (markStyle === 0) {
450
+ return centreMark;
451
+ }
452
+
453
+ if (markStyle > 0) {
454
+ const lineOne = new Line({ points: [new Point(point.x - markSize, point.y), new Point(point.x + markSize, point.y)] });
455
+ const lineTwo = new Line({ points: [new Point(point.x, point.y - markSize), new Point(point.x, point.y + markSize)] });
456
+ centreMark.push(lineOne, lineTwo);
457
+
458
+ if (markStyle > 1) {
459
+ const lineThree = new Line({ points: [new Point(point.x, point.y + markSize * 2), new Point(point.x, point.y + markSize * 5)] });
460
+ const lineFour = new Line({ points: [new Point(point.x - markSize * 2, point.y), new Point(point.x - markSize * 5, point.y)] });
461
+ const lineFive = new Line({ points: [new Point(point.x, point.y - markSize * 2), new Point(point.x, point.y - markSize * 5)] });
462
+ const lineSix = new Line({ points: [new Point(point.x + markSize * 2, point.y), new Point(point.x + markSize * 5, point.y)] });
463
+
464
+ centreMark.push(lineThree, lineFour, lineFive, lineSix);
465
+ }
466
+ }
467
+
468
+ return centreMark;
469
+ }
470
+
471
+ /**
472
+ * Draw the entity
473
+ * @param {Object} ctx - context
474
+ * @param {number} scale
475
+ */
476
+ draw(ctx, scale) {
477
+ if (this.block.items.length === 0) {
478
+ this.refresh();
479
+ }
480
+
481
+ this.block.draw(ctx, scale, this);
482
+ }
483
+
484
+ /**
485
+ * Get snap points
486
+ * @param {Point} mousePoint
487
+ * @param {number} delta
488
+ * @return {Array} - array of snap points
489
+ */
490
+ snaps(mousePoint, delta) {
491
+ const snaps = [];
492
+ return snaps;
493
+ }
494
+
495
+ /**
496
+ * Get closest point on entity
497
+ * @param {Point} P
498
+ * @return {Array} - [Point, distance]
499
+ */
500
+ closestPoint(P) {
501
+ return this.block.closestPoint(P);
502
+ }
503
+
504
+ /**
505
+ * Return boundingbox for entity
506
+ * @return {BoundingBox}
507
+ */
508
+ boundingBox() {
509
+ return this.block.boundingBox();
510
+ }
511
+
512
+ /**
513
+ * Determine if the entity is within the selection
514
+ * @param {Object} selection - {min: Point, max: Point}
515
+ * @return {boolean} true if within
516
+ */
517
+ within(selection) {
518
+ return this.block.within(selection);
519
+ }
520
+
521
+ /**
522
+ * Determine if the entity is touch the selection window
523
+ * @param {Object} selection - {min: Point, max: Point}
524
+ * @return {boolean} true if touched
525
+ */
526
+ touched(selection) {
527
+ return this.block.touched(selection);
528
+ }
529
+
530
+ /**
531
+ * Refesh the dimension geometry
532
+ */
533
+ refresh() {
534
+ const entities = this.buildDimension();
535
+
536
+ if (entities) {
537
+ this.block.clearItems();
538
+
539
+ entities.forEach((element) => {
540
+ // For colour BYBLOCK for dimensions
541
+ element.setColour('BYBLOCK');
542
+ this.block.addItem(element);
543
+ });
544
+ }
545
+ }
546
+
547
+ /**
548
+ * Set a property if it exists
549
+ * @param {string} property
550
+ * @param {any} value
551
+ */
552
+ setProperty(property, value) {
553
+ if (this.hasOwnProperty(property)) {
554
+ this[property] = value;
555
+ this.refresh();
556
+ }
557
+ }
558
+ }