@itwin/linear-referencing-backend 5.0.0-dev.90 → 5.0.0-dev.93

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 (35) hide show
  1. package/lib/cjs/LinearReferencingElementAspects.d.ts +1 -1
  2. package/lib/cjs/LinearReferencingElementAspects.d.ts.map +1 -1
  3. package/lib/cjs/LinearReferencingElementAspects.js +4 -4
  4. package/lib/cjs/LinearReferencingElementAspects.js.map +1 -1
  5. package/lib/cjs/LinearReferencingElements.d.ts +2 -2
  6. package/lib/cjs/LinearReferencingElements.d.ts.map +1 -1
  7. package/lib/cjs/LinearReferencingElements.js +10 -10
  8. package/lib/cjs/LinearReferencingElements.js.map +1 -1
  9. package/lib/cjs/LinearReferencingSchema.js +3 -3
  10. package/lib/cjs/LinearReferencingSchema.js.map +1 -1
  11. package/lib/cjs/linear-referencing-backend.d.ts +4 -4
  12. package/lib/cjs/linear-referencing-backend.d.ts.map +1 -1
  13. package/lib/cjs/linear-referencing-backend.js +4 -4
  14. package/lib/cjs/linear-referencing-backend.js.map +1 -1
  15. package/lib/esm/LinearReferencingElementAspects.d.ts +54 -0
  16. package/lib/esm/LinearReferencingElementAspects.d.ts.map +1 -0
  17. package/lib/esm/LinearReferencingElementAspects.js +99 -0
  18. package/lib/esm/LinearReferencingElementAspects.js.map +1 -0
  19. package/lib/esm/LinearReferencingElements.d.ts +202 -0
  20. package/lib/esm/LinearReferencingElements.d.ts.map +1 -0
  21. package/lib/esm/LinearReferencingElements.js +603 -0
  22. package/lib/esm/LinearReferencingElements.js.map +1 -0
  23. package/lib/esm/LinearReferencingRelationships.d.ts +63 -0
  24. package/lib/esm/LinearReferencingRelationships.d.ts.map +1 -0
  25. package/lib/esm/LinearReferencingRelationships.js +78 -0
  26. package/lib/esm/LinearReferencingRelationships.js.map +1 -0
  27. package/lib/esm/LinearReferencingSchema.d.ts +14 -0
  28. package/lib/esm/LinearReferencingSchema.d.ts.map +1 -0
  29. package/lib/esm/LinearReferencingSchema.js +30 -0
  30. package/lib/esm/LinearReferencingSchema.js.map +1 -0
  31. package/lib/esm/linear-referencing-backend.d.ts +11 -0
  32. package/lib/esm/linear-referencing-backend.d.ts.map +1 -0
  33. package/lib/esm/linear-referencing-backend.js +15 -0
  34. package/lib/esm/linear-referencing-backend.js.map +1 -0
  35. package/package.json +23 -12
@@ -0,0 +1,603 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module LinearReferencing
7
+ */
8
+ import { assert, DbResult } from "@itwin/core-bentley";
9
+ import { PhysicalElement, SpatialLocationElement } from "@itwin/core-backend";
10
+ import { Code, IModelError, RelatedElement } from "@itwin/core-common";
11
+ import { ComparisonOption, LinearlyReferencedLocationType, } from "@itwin/linear-referencing-common";
12
+ import { LinearlyReferencedAtLocation, LinearlyReferencedFromToLocation } from "./LinearReferencingElementAspects.js";
13
+ import { ILinearLocationLocatesElement, ILinearlyLocatedAlongILinearElement, IReferentReferencesElement, } from "./LinearReferencingRelationships.js";
14
+ /** Base class for Spatial Location Element subclasses representing properties whose value is located along a Linear-Element and only applies to a portion of an Element.
15
+ * @beta
16
+ */
17
+ export class LinearlyLocatedAttribution extends SpatialLocationElement {
18
+ /** @internal */
19
+ static get className() { return "LinearlyLocatedAttribution"; }
20
+ attributedElement;
21
+ constructor(props, iModel) {
22
+ super(props, iModel);
23
+ this.attributedElement = RelatedElement.fromJSON(props.attributedElement);
24
+ }
25
+ getLinearElementId() {
26
+ return LinearlyLocated.getLinearElementId(this.iModel, this.id);
27
+ }
28
+ }
29
+ /** Base class for Spatial Location Element implementations that are linearly located along a Linear-Element.
30
+ * @beta
31
+ */
32
+ export class LinearLocationElement extends SpatialLocationElement {
33
+ /** @internal */
34
+ static get className() { return "LinearLocationElement"; }
35
+ constructor(props, iModel) {
36
+ super(props, iModel);
37
+ }
38
+ getLinearElementId() {
39
+ return LinearlyLocated.getLinearElementId(this.iModel, this.id);
40
+ }
41
+ }
42
+ /** Linear Referencing Location attached to an Element not inherently Linearly Referenced.
43
+ * @beta
44
+ */
45
+ export class LinearLocation extends LinearLocationElement {
46
+ /** @internal */
47
+ static get className() { return "LinearLocation"; }
48
+ constructor(props, iModel) {
49
+ super(props, iModel);
50
+ }
51
+ static toProps(modelId, categoryId) {
52
+ const props = {
53
+ classFullName: LinearLocation.classFullName,
54
+ category: categoryId,
55
+ model: modelId,
56
+ code: Code.createEmpty(),
57
+ };
58
+ return props;
59
+ }
60
+ static create(iModel, modelId, categoryId) {
61
+ return new LinearLocation(this.toProps(modelId, categoryId), iModel);
62
+ }
63
+ static insertFromTo(iModel, modelId, categoryId, linearElementId, fromToPosition, locatedElementId) {
64
+ const newId = LinearlyLocated.insertFromTo(iModel, this.toProps(modelId, categoryId), linearElementId, fromToPosition);
65
+ ILinearLocationLocatesElement.insert(iModel, newId, locatedElementId);
66
+ return newId;
67
+ }
68
+ insertFromTo(iModel, linearElementId, fromToPosition, locatedElementId) {
69
+ const newId = LinearlyLocated.insertFromTo(iModel, this.toJSON(), linearElementId, fromToPosition);
70
+ ILinearLocationLocatesElement.insert(iModel, newId, locatedElementId);
71
+ return newId;
72
+ }
73
+ static insertAt(iModel, modelId, categoryId, linearElementId, atPosition, locatedElementId) {
74
+ const newId = LinearlyLocated.insertAt(iModel, this.toProps(modelId, categoryId), linearElementId, atPosition);
75
+ ILinearLocationLocatesElement.insert(iModel, newId, locatedElementId);
76
+ return newId;
77
+ }
78
+ insertAt(iModel, linearElementId, atPosition, locatedElementId) {
79
+ const newId = LinearlyLocated.insertAt(iModel, this.toJSON(), linearElementId, atPosition);
80
+ ILinearLocationLocatesElement.insert(iModel, newId, locatedElementId);
81
+ return newId;
82
+ }
83
+ }
84
+ /** Base class for Physical Elements that are inherently linearly located along a Linear-Element.
85
+ * @beta
86
+ */
87
+ export class LinearPhysicalElement extends PhysicalElement {
88
+ /** @internal */
89
+ static get className() { return "LinearPhysicalElement"; }
90
+ constructor(props, iModel) {
91
+ super(props, iModel);
92
+ }
93
+ }
94
+ /** Spatial Location Element that can play the role of a Referent (known location along a Linear-Element).
95
+ * @beta
96
+ */
97
+ export class ReferentElement extends SpatialLocationElement {
98
+ /** @internal */
99
+ static get className() { return "ReferentElement"; }
100
+ referencedElement;
101
+ constructor(props, iModel) {
102
+ super(props, iModel);
103
+ this.referencedElement = RelatedElement.fromJSON(props.referencedElement);
104
+ }
105
+ getLinearElementId() {
106
+ return LinearlyLocated.getLinearElementId(this.iModel, this.id);
107
+ }
108
+ }
109
+ /** Referent-implementation turning any bis:SpatialElement not inherently Linearly-Referenced into a Referent for Linear-Referencing purposes.
110
+ * @beta
111
+ */
112
+ export class Referent extends ReferentElement {
113
+ /** @internal */
114
+ static get className() { return "Referent"; }
115
+ constructor(props, iModel) {
116
+ super(props, iModel);
117
+ }
118
+ static toProps(modelId, categoryId, referencedElementId) {
119
+ const props = {
120
+ classFullName: LinearLocation.classFullName,
121
+ category: categoryId,
122
+ model: modelId,
123
+ code: Code.createEmpty(),
124
+ referencedElement: new IReferentReferencesElement(referencedElementId),
125
+ };
126
+ return props;
127
+ }
128
+ static create(iModel, modelId, categoryId, referencedElementId) {
129
+ return new Referent(this.toProps(modelId, categoryId, referencedElementId), iModel);
130
+ }
131
+ static insertAt(iModel, modelId, categoryId, linearElementId, atPosition, referencedElementId) {
132
+ return LinearlyLocated.insertAt(iModel, this.toProps(modelId, categoryId, referencedElementId), linearElementId, atPosition);
133
+ }
134
+ insertAt(iModel, linearElementId, atPosition) {
135
+ return LinearlyLocated.insertAt(iModel, this.toJSON(), linearElementId, atPosition);
136
+ }
137
+ }
138
+ class ECSQLGenImpl {
139
+ selectDistinct() {
140
+ return false;
141
+ }
142
+ }
143
+ class AtAndFromToECSQLGenImpl extends ECSQLGenImpl {
144
+ genSelect() {
145
+ return "coalesce(AtLocation.AtPosition.DistanceAlongFromStart, FromToLocation.FromPosition.DistanceAlongFromStart) StartDistanceAlong, " +
146
+ "coalesce(AtLocation.AtPosition.DistanceAlongFromStart, FromToLocation.ToPosition.DistanceAlongFromStart) StopDistanceAlong, " +
147
+ "coalesce(AtLocation.ECInstanceId, FromToLocation.ECInstanceId) LocationAspectId ";
148
+ }
149
+ selectDistinct() {
150
+ return true;
151
+ }
152
+ genFromJoin() {
153
+ return "LEFT JOIN LinearReferencing.LinearlyReferencedAtLocation AtLocation ON LinearlyLocated.InstanceId = AtLocation.Element.Id " +
154
+ "LEFT JOIN LinearReferencing.LinearlyReferencedFromToLocation FromToLocation ON LinearlyLocated.InstanceId = FromToLocation.Element.Id ";
155
+ }
156
+ genWhere(bindVals, from, inclusiveFrom, to, inclusiveTo) {
157
+ const fromCompOp = (inclusiveFrom === undefined || inclusiveFrom) ? ">=" : ">";
158
+ const toCompOp = (inclusiveTo === undefined || inclusiveTo) ? "<=" : "<";
159
+ let ecSql = "";
160
+ if (from !== undefined && to !== undefined) {
161
+ ecSql += "(AtLocation.AtPosition.DistanceAlongFromStart ";
162
+ ecSql += fromCompOp;
163
+ ecSql += " ? AND AtLocation.AtPosition.DistanceAlongFromStart ";
164
+ ecSql += toCompOp;
165
+ ecSql += " ?) OR (FromToLocation.FromPosition.DistanceAlongFromStart ";
166
+ ecSql += fromCompOp;
167
+ ecSql += " ? AND FromToLocation.FromPosition.DistanceAlongFromStart ";
168
+ ecSql += toCompOp;
169
+ ecSql += " ?) " + "OR (FromToLocation.ToPosition.DistanceAlongFromStart ";
170
+ ecSql += fromCompOp;
171
+ ecSql += " ? AND FromToLocation.ToPosition.DistanceAlongFromStart ";
172
+ ecSql += toCompOp;
173
+ ecSql += " ?) OR (FromToLocation.FromPosition.DistanceAlongFromStart <= ? AND FromToLocation.ToPosition.DistanceAlongFromStart >= ?) ";
174
+ bindVals.push(from);
175
+ bindVals.push(to);
176
+ bindVals.push(from);
177
+ bindVals.push(to);
178
+ bindVals.push(from);
179
+ bindVals.push(to);
180
+ bindVals.push(from);
181
+ bindVals.push(to);
182
+ }
183
+ else if (from !== undefined) {
184
+ ecSql += "AtLocation.AtPosition.DistanceAlongFromStart ";
185
+ ecSql += fromCompOp;
186
+ ecSql += " ? OR FromToLocation.FromPosition.DistanceAlongFromStart ";
187
+ ecSql += fromCompOp;
188
+ ecSql += " ? OR FromToLocation.ToPosition.DistanceAlongFromStart ";
189
+ ecSql += fromCompOp;
190
+ ecSql += " ? ";
191
+ bindVals.push(from);
192
+ bindVals.push(from);
193
+ bindVals.push(from);
194
+ }
195
+ else if (to !== undefined) {
196
+ ecSql += "AtLocation.AtPosition.DistanceAlongFromStart ";
197
+ ecSql += toCompOp;
198
+ ecSql += " ? OR FromToLocation.FromPosition.DistanceAlongFromStart ";
199
+ ecSql += toCompOp;
200
+ ecSql += " ? OR FromToLocation.ToPosition.DistanceAlongFromStart ";
201
+ ecSql += toCompOp;
202
+ ecSql += " ? ";
203
+ bindVals.push(to);
204
+ bindVals.push(to);
205
+ bindVals.push(to);
206
+ }
207
+ else {
208
+ ecSql += "(AtLocation.AtPosition.DistanceAlongFromStart IS NOT NULL) OR ";
209
+ ecSql += "(FromToLocation.FromPosition.DistanceAlongFromStart IS NOT NULL) ";
210
+ }
211
+ return ecSql;
212
+ }
213
+ genOrderBy() {
214
+ return "coalesce(AtLocation.AtPosition.DistanceAlongFromStart, FromToLocation.FromPosition.DistanceAlongFromStart)";
215
+ }
216
+ }
217
+ class FromToECSQLGenImpl extends ECSQLGenImpl {
218
+ genSelect() {
219
+ return "FromToLocation.FromPosition.DistanceAlongFromStart StartDistanceAlong, FromToLocation.ToPosition.DistanceAlongFromStart StopDistanceAlong, FromToLocation.ECInstanceId LocationAspectId ";
220
+ }
221
+ genFromJoin() {
222
+ return "INNER JOIN LinearReferencing.LinearlyReferencedFromToLocation FromToLocation ON LinearlyLocated.InstanceId = FromToLocation.Element.Id ";
223
+ }
224
+ genWhere(bindVals, from, inclusiveFrom, to, inclusiveTo) {
225
+ const fromCompOp = (inclusiveFrom === undefined || inclusiveFrom) ? ">=" : ">";
226
+ const toCompOp = (inclusiveTo === undefined || inclusiveTo) ? "<=" : "<";
227
+ let ecSql = "";
228
+ if (from !== undefined && to !== undefined) {
229
+ ecSql += "AND ((FromToLocation.FromPosition.DistanceAlongFromStart ";
230
+ ecSql += fromCompOp;
231
+ ecSql += " ? AND FromToLocation.FromPosition.DistanceAlongFromStart ";
232
+ ecSql += toCompOp;
233
+ ecSql += " ?) OR (FromToLocation.ToPosition.DistanceAlongFromStart ";
234
+ ecSql += fromCompOp;
235
+ ecSql += " ? AND FromToLocation.ToPosition.DistanceAlongFromStart ";
236
+ ecSql += toCompOp;
237
+ ecSql += " ?) OR (FromToLocation.FromPosition.DistanceAlongFromStart <= ? AND FromToLocation.ToPosition.DistanceAlongFromStart >= ?)) ";
238
+ bindVals.push(from);
239
+ bindVals.push(to);
240
+ bindVals.push(from);
241
+ bindVals.push(to);
242
+ bindVals.push(from);
243
+ bindVals.push(to);
244
+ }
245
+ else if (from !== undefined) {
246
+ ecSql += "AND (FromToLocation.FromPosition.DistanceAlongFromStart ";
247
+ ecSql += fromCompOp;
248
+ ecSql += " ? OR FromToLocation.ToPosition.DistanceAlongFromStart ";
249
+ ecSql += fromCompOp;
250
+ ecSql += " ?)";
251
+ bindVals.push(from);
252
+ bindVals.push(from);
253
+ }
254
+ else if (to !== undefined) {
255
+ ecSql += "AND (FromToLocation.FromPosition.DistanceAlongFromStart ";
256
+ ecSql += toCompOp;
257
+ ecSql += " ? OR FromToLocation.ToPosition.DistanceAlongFromStart ";
258
+ ecSql += toCompOp;
259
+ ecSql += " ?) ";
260
+ bindVals.push(to);
261
+ bindVals.push(to);
262
+ }
263
+ else {
264
+ ecSql += "FromToLocation.FromPosition.DistanceAlongFromStart IS NOT NULL ";
265
+ }
266
+ return ecSql;
267
+ }
268
+ genOrderBy() {
269
+ return "FromToLocation.FromPosition.DistanceAlongFromStart";
270
+ }
271
+ }
272
+ class AtECSQLGenImpl extends ECSQLGenImpl {
273
+ genSelect() {
274
+ return "AtLocation.AtPosition.DistanceAlongFromStart StartDistanceAlong, AtLocation.AtPosition.DistanceAlongFromStart StopDistanceAlong, AtLocation.ECInstanceId LocationAspectId ";
275
+ }
276
+ genFromJoin() {
277
+ return "INNER JOIN LinearReferencing.LinearlyReferencedAtLocation AtLocation ON LinearlyLocated.InstanceId = AtLocation.Element.Id ";
278
+ }
279
+ genWhere(bindVals, from, inclusiveFrom, to, inclusiveTo) {
280
+ const fromCompOp = (inclusiveFrom === undefined || inclusiveFrom) ? ">=" : ">";
281
+ const toCompOp = (inclusiveTo === undefined || inclusiveTo) ? "<=" : "<";
282
+ let ecSql = "";
283
+ if (from !== undefined && to !== undefined) {
284
+ ecSql += "AtLocation.AtPosition.DistanceAlongFromStart ";
285
+ ecSql += fromCompOp;
286
+ ecSql += " ? AND AtLocation.AtPosition.DistanceAlongFromStart ";
287
+ ecSql += toCompOp;
288
+ ecSql += " ? ";
289
+ bindVals.push(from);
290
+ bindVals.push(to);
291
+ }
292
+ else if (from !== undefined) {
293
+ ecSql += "AtLocation.AtPosition.DistanceAlongFromStart ";
294
+ ecSql += fromCompOp;
295
+ ecSql += " ? ";
296
+ bindVals.push(from);
297
+ }
298
+ else if (to !== undefined) {
299
+ ecSql += "AtLocation.AtPosition.DistanceAlongFromStart ";
300
+ ecSql += toCompOp;
301
+ ecSql += " ? ";
302
+ bindVals.push(to);
303
+ }
304
+ else
305
+ ecSql += "AtLocation.AtPosition.DistanceAlongFromStart IS NOT NULL ";
306
+ return ecSql;
307
+ }
308
+ genOrderBy() {
309
+ return "AtLocation.AtPosition.DistanceAlongFromStart";
310
+ }
311
+ }
312
+ class QueryLinearLocationsECSQLGen {
313
+ _params;
314
+ _ecSql;
315
+ _addSelectClause(impl) {
316
+ let select = "SELECT ";
317
+ if (impl.selectDistinct())
318
+ select += "DISTINCT ";
319
+ select += "LinearlyLocated.InstanceId LinearlyLocatedId, printf('%s:%s', meta.ECSchemaDef.Name, meta.ECClassDef.Name) LinearlyLocatedClassFullName, ";
320
+ select += impl.genSelect();
321
+ this._ecSql += select;
322
+ }
323
+ _parseClassFullName(classFullName) {
324
+ const parts = classFullName.split(":");
325
+ if (parts.length !== 2)
326
+ return undefined;
327
+ return [parts[0], parts[1]];
328
+ }
329
+ _genLinearlyLocated() {
330
+ return "meta.ECSchemaDef JOIN meta.ECClassDef USING meta.SchemaOwnsClasses JOIN " +
331
+ "(SELECT coalesce(Located.TargetECInstanceId, Along.SourceECInstanceId) InstanceId, " +
332
+ "coalesce(Located.TargetECClassId, Along.SourceECClassId) ClassId " +
333
+ "FROM LinearReferencing.ILinearlyLocatedAlongILinearElement Along LEFT JOIN " +
334
+ "LinearReferencing.ILinearLocationLocatesElement Located ON Along.SourceECInstanceId = Located.SourceECInstanceId " +
335
+ "WHERE Along.TargetECInstanceId = ?) LinearlyLocated ON meta.ECClassDef.ECInstanceId = LinearlyLocated.ClassId ";
336
+ }
337
+ _addFromClause(impl /* bvector<double>& bindVals */) {
338
+ let from = "FROM ";
339
+ from += this._genLinearlyLocated();
340
+ from += impl.genFromJoin();
341
+ this._ecSql += from;
342
+ }
343
+ _addWhereClause(impl, bindVals) {
344
+ let where = "WHERE ";
345
+ where += impl.genWhere(bindVals, this._params.fromDistanceAlong, (this._params.fromComparisonOption === undefined ||
346
+ this._params.fromComparisonOption === ComparisonOption.Inclusive), this._params.toDistanceAlong, (this._params.toComparisonOption === undefined ||
347
+ this._params.toComparisonOption === ComparisonOption.Inclusive));
348
+ if (this._params.linearlyLocatedClassFullNames !== undefined) {
349
+ if (where.length > 6)
350
+ where += "AND ";
351
+ if (1 === this._params.linearlyLocatedClassFullNames.length) {
352
+ const classFullName = this._params.linearlyLocatedClassFullNames[0];
353
+ const schemaNameClassName = this._parseClassFullName(classFullName);
354
+ if (schemaNameClassName === undefined)
355
+ throw new IModelError(0, "Invalid full class name");
356
+ where += `meta.ECSchemaDef.Name='${schemaNameClassName[0]}' AND meta.ECClassDef.Name='${schemaNameClassName[1]}' `;
357
+ }
358
+ else if (1 < this._params.linearlyLocatedClassFullNames.length) {
359
+ where += "(";
360
+ for (const classFullName of this._params.linearlyLocatedClassFullNames) {
361
+ if (classFullName === undefined)
362
+ continue;
363
+ const schemaNameClassName = this._parseClassFullName(classFullName);
364
+ if (schemaNameClassName === undefined)
365
+ continue;
366
+ where += `(meta.ECSchemaDef.Name='${schemaNameClassName[0]}' AND meta.ECClassDef.Name='${schemaNameClassName[1]}') OR `;
367
+ }
368
+ where = where.substring(0, where.length - 4); // Removing last OR
369
+ where += ") ";
370
+ }
371
+ }
372
+ this._ecSql += where;
373
+ }
374
+ _addOrderByClause(impl) {
375
+ let orderBy = "ORDER BY ";
376
+ orderBy += impl.genOrderBy();
377
+ this._ecSql += orderBy;
378
+ }
379
+ _createImpl() {
380
+ if (this._params.linearlyReferencedLocationTypeFilter === undefined ||
381
+ this._params.linearlyReferencedLocationTypeFilter === LinearlyReferencedLocationType.Any) {
382
+ return new AtAndFromToECSQLGenImpl();
383
+ }
384
+ else if (this._params.linearlyReferencedLocationTypeFilter === LinearlyReferencedLocationType.FromTo) {
385
+ return new FromToECSQLGenImpl();
386
+ }
387
+ else {
388
+ return new AtECSQLGenImpl();
389
+ }
390
+ }
391
+ constructor(params) {
392
+ this._params = params;
393
+ this._ecSql = "";
394
+ }
395
+ generate(linearElementId) {
396
+ this._ecSql = "";
397
+ const impl = this._createImpl();
398
+ const bindVals = [linearElementId];
399
+ this._addSelectClause(impl);
400
+ this._addFromClause(impl);
401
+ this._addWhereClause(impl, bindVals);
402
+ this._addOrderByClause(impl);
403
+ return [this._ecSql, bindVals];
404
+ }
405
+ }
406
+ /** A class offering services for LinearlyLocated elements.
407
+ * @beta
408
+ */
409
+ export class LinearlyLocated {
410
+ static insertBasic(iModel, elProps, linearElementId) {
411
+ const newId = iModel.elements.insertElement(elProps);
412
+ const linearlyLocatedAlongLinearElement = ILinearlyLocatedAlongILinearElement.create(iModel, newId, linearElementId);
413
+ linearlyLocatedAlongLinearElement.insert();
414
+ return newId;
415
+ }
416
+ /** Insert a new LinearlyLocated element into an iModel at a specific location along an existing Linear-Element.
417
+ * @param iModel The iModel to insert the new element into.
418
+ * @param elProps The properties of the new element.
419
+ * @param linearElementId The Id of the Linear-Element along which the new LinearlyLocated will be inserted.
420
+ * @param atPosition Linear position.
421
+ * @returns The newly inserted element's Id.
422
+ * @throws [[IModelError]] if unable to insert the element.
423
+ */
424
+ static insertAt(iModel, elProps, linearElementId, atPosition) {
425
+ const newId = this.insertBasic(iModel, elProps, linearElementId);
426
+ LinearlyReferencedAtLocation.insert(iModel, newId, atPosition.atPosition, (atPosition.fromReferent === undefined) ? undefined : atPosition.fromReferent.id);
427
+ return newId;
428
+ }
429
+ /** Insert a new LinearlyLocated element into an iModel at a specific from-to location along an existing Linear-Element.
430
+ * @param iModel The iModel to insert the new element into.
431
+ * @param elProps The properties of the new element.
432
+ * @param linearElementId The Id of the Linear-Element along which the new LinearlyLocated will be inserted.
433
+ * @param fromToPosition Linear position.
434
+ * @returns The newly inserted element's Id.
435
+ * @throws [[IModelError]] if unable to insert the element.
436
+ */
437
+ static insertFromTo(iModel, elProps, linearElementId, fromToPosition) {
438
+ const newId = this.insertBasic(iModel, elProps, linearElementId);
439
+ LinearlyReferencedFromToLocation.insert(iModel, newId, fromToPosition.fromPosition, fromToPosition.toPosition, (fromToPosition.fromPositionFromReferent === undefined) ? undefined : fromToPosition.fromPositionFromReferent.id, (fromToPosition.toPositionFromReferent === undefined) ? undefined : fromToPosition.toPositionFromReferent.id);
440
+ return newId;
441
+ }
442
+ static getLinearLocations(iModel, linearlyLocatedElementId, fullClassName) {
443
+ const aspects = iModel.elements.getAspects(linearlyLocatedElementId, fullClassName);
444
+ if (aspects.length === 0)
445
+ return [];
446
+ const retVal = [];
447
+ for (const aspect of aspects) {
448
+ const linearAspect = aspect;
449
+ retVal.push(linearAspect);
450
+ }
451
+ return retVal;
452
+ }
453
+ static queryFirstLinearLocationAspectId(iModel, linearlyLocatedElementId, className) {
454
+ let aspectId;
455
+ iModel.withPreparedStatement(`SELECT ECInstanceId FROM LinearReferencing.${className} WHERE Element.Id=? LIMIT 1`, (stmt) => {
456
+ stmt.bindId(1, linearlyLocatedElementId);
457
+ if (stmt.step() === DbResult.BE_SQLITE_ROW)
458
+ aspectId = stmt.getValue(0).getId();
459
+ });
460
+ return aspectId;
461
+ }
462
+ /** Query for LinearlyReferenced AtLocation aspects owned by the specified LinearlyLocated Element.
463
+ * @param iModel The iModel to query from.
464
+ * @param linearlyLocatedElementId The id of the LinearlyLocated Element to query aspects about.
465
+ * @returns Returns an array of LinearlyReferencedAtLocation.
466
+ * @throws [[IModelError]]
467
+ */
468
+ static getAtLocations(iModel, linearlyLocatedElementId) {
469
+ return this.getLinearLocations(iModel, linearlyLocatedElementId, "LinearReferencing:LinearlyReferencedAtLocation");
470
+ }
471
+ /** Query for the single LinearlyReferenced AtLocation aspect owned by the specified LinearlyLocated Element. If more than one aspect is expected, use [[getAtLocations]] instead.
472
+ * @param iModel The iModel to query from.
473
+ * @param linearlyLocatedElementId The id of the LinearlyLocated Element to query about.
474
+ * @returns Returns an LinearlyReferencedAtLocation or undefined is none is found.
475
+ * @throws [[IModelError]]
476
+ */
477
+ static getAtLocation(iModel, linearlyLocatedElementId) {
478
+ const linearLocations = this.getAtLocations(iModel, linearlyLocatedElementId);
479
+ if (linearLocations.length === 0)
480
+ return undefined;
481
+ else {
482
+ assert(linearLocations.length === 1);
483
+ return linearLocations[0];
484
+ }
485
+ }
486
+ /** Update an existing LinearlyReferencedAtLocation aspect within the iModel.
487
+ * @param iModel The iModel to update.
488
+ * @param linearlyLocatedElementId The Id of the owning Linearly Located Element.
489
+ * @param linearLocationProps The properties to use to update the LinearlyReferencedAtLocation aspect.
490
+ * @param aspectId The Id of the aspect to update. If not known, the first aspectId will be looked-up.
491
+ * @throws [[IModelError]]
492
+ */
493
+ static updateAtLocation(iModel, linearlyLocatedElementId, linearLocationProps, aspectId) {
494
+ let linearLocAspectId;
495
+ if (aspectId !== undefined)
496
+ linearLocAspectId = aspectId;
497
+ else {
498
+ linearLocAspectId = this.queryFirstLinearLocationAspectId(iModel, linearlyLocatedElementId, "LinearlyReferencedAtLocation");
499
+ }
500
+ const linearLocationAspectProps = {
501
+ id: linearLocAspectId,
502
+ element: { id: linearlyLocatedElementId },
503
+ classFullName: "LinearReferencing:LinearlyReferencedAtLocation",
504
+ atPosition: linearLocationProps.atPosition,
505
+ fromReferent: linearLocationProps.fromReferent,
506
+ };
507
+ iModel.elements.updateAspect(linearLocationAspectProps);
508
+ }
509
+ /** Update an existing LinearlyReferencedFromToLocation aspect within the iModel.
510
+ * @param iModel The iModel to update.
511
+ * @param linearlyLocatedElementId The Id of the owning Linearly Located Element.
512
+ * @param linearLocationProps The properties to use to update the LinearlyReferencedFromToLocation aspect.
513
+ * @param aspectId The Id of the aspect to update. If not known, the first aspectId will be looked-up.
514
+ * @throws [[IModelError]]
515
+ */
516
+ static updateFromToLocation(iModel, linearlyLocatedElementId, linearLocationProps, aspectId) {
517
+ let linearLocAspectId;
518
+ if (aspectId !== undefined)
519
+ linearLocAspectId = aspectId;
520
+ else {
521
+ linearLocAspectId = this.queryFirstLinearLocationAspectId(iModel, linearlyLocatedElementId, "LinearlyReferencedFromToLocation");
522
+ }
523
+ const linearLocationAspectProps = {
524
+ id: linearLocAspectId,
525
+ element: { id: linearlyLocatedElementId, relClassName: "LinearReferencing:ILinearlyLocatedOwnsFromToLocations" },
526
+ classFullName: "LinearReferencing:LinearlyReferencedFromToLocation",
527
+ fromPosition: linearLocationProps.fromPosition,
528
+ fromPositionFromReferent: linearLocationProps.fromPositionFromReferent,
529
+ toPosition: linearLocationProps.toPosition,
530
+ toPositionFromReferent: linearLocationProps.toPositionFromReferent,
531
+ };
532
+ iModel.elements.updateAspect(linearLocationAspectProps);
533
+ }
534
+ /** Query for the Id of the Linear-Element along which the specified LinearlyLocated Element is located.
535
+ * @param iModel The iModel to query from.
536
+ * @param linearlyLocatedElementId The id of the LinearlyLocated Element to query a Linear-Element for.
537
+ * @returns Returns the Id of the Linear-Element or undefined is none is assigned.
538
+ */
539
+ static getLinearElementId(iModel, linearlyLocatedElementId) {
540
+ let linearElementId;
541
+ iModel.withPreparedStatement("SELECT TargetECInstanceId FROM LinearReferencing.ILinearlyLocatedAlongILinearElement WHERE SourceECInstanceId = ?", (stmt) => {
542
+ stmt.bindId(1, linearlyLocatedElementId);
543
+ if (DbResult.BE_SQLITE_ROW === stmt.step())
544
+ linearElementId = stmt.getValue(0).getId();
545
+ else
546
+ linearElementId = undefined;
547
+ });
548
+ return linearElementId;
549
+ }
550
+ /** Query for LinearlyReferenced FromToLocation aspects owned by the specified LinearlyLocated Element.
551
+ * @param iModel The iModel to query from.
552
+ * @param linearlyLocatedElementId The id of the LinearlyLocated Element to query aspects about.
553
+ * @returns Returns an array of LinearlyReferencedFromToLocation.
554
+ * @throws [[IModelError]]
555
+ */
556
+ static getFromToLocations(iModel, linearlyLocatedElementId) {
557
+ return this.getLinearLocations(iModel, linearlyLocatedElementId, "LinearReferencing:LinearlyReferencedFromToLocation");
558
+ }
559
+ /** Query for the single LinearlyReferenced FromToLocation aspect owned by the specified LinearlyLocated Element. If more than one aspect is expected, use [[getFromToLocations]] instead.
560
+ * @param iModel The iModel to query from.
561
+ * @param linearlyLocatedElementId The id of the LinearlyLocated Element to query about.
562
+ * @returns Returns an LinearlyReferencedFromToLocation or undefined is none is found.
563
+ * @throws [[IModelError]]
564
+ */
565
+ static getFromToLocation(iModel, linearlyLocatedElementId) {
566
+ const linearLocations = this.getFromToLocations(iModel, linearlyLocatedElementId);
567
+ if (linearLocations.length === 0)
568
+ return undefined;
569
+ else {
570
+ assert(linearLocations.length === 1);
571
+ return linearLocations[0];
572
+ }
573
+ }
574
+ }
575
+ /** A class offering services for linearly-located data along a Linear-Element.
576
+ * @beta
577
+ */
578
+ export class LinearElement {
579
+ /** Query for LinearLocationReferences based on specified query parameters.
580
+ * @returns Returns an array of LinearLocationReferences.
581
+ * @throws [[IModelError]]
582
+ */
583
+ static queryLinearLocations(iModel, linearElementId, queryParams) {
584
+ const ecSqlGen = new QueryLinearLocationsECSQLGen(queryParams);
585
+ const ecsqlAndBindVals = ecSqlGen.generate(linearElementId);
586
+ const linearLocationRefs = [];
587
+ iModel.withPreparedStatement(ecsqlAndBindVals[0], (stmt) => {
588
+ stmt.bindValues(ecsqlAndBindVals[1]);
589
+ while (DbResult.BE_SQLITE_ROW === stmt.step()) {
590
+ const linearLocationRef = {
591
+ linearlyLocatedId: stmt.getValue(0).getId(),
592
+ linearlyLocatedClassFullName: stmt.getValue(1).getString(),
593
+ startDistanceAlong: stmt.getValue(2).getDouble(),
594
+ stopDistanceAlong: stmt.getValue(3).getDouble(),
595
+ locationAspectId: stmt.getValue(4).getId(),
596
+ };
597
+ linearLocationRefs.push(linearLocationRef);
598
+ }
599
+ });
600
+ return linearLocationRefs;
601
+ }
602
+ }
603
+ //# sourceMappingURL=LinearReferencingElements.js.map