/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2021, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { IHttpClient } from "./IHttpClient"; import { IShortUserDescription } from "./IUser"; import { waitFor, userFullName, userInitials } from "./impl/Utils"; /** * The base class provides information about the file/assembly clash detection test and test results. */ export class ClashTest { private _data: any; private basePath: string; private httpClient: IHttpClient; /** * @param data - Raw test data. * @param basePath - Clash tests API base path for the owner file/assembly. * @param httpClient - Http client. */ constructor(data: any, basePath: string, httpClient: IHttpClient) { this.httpClient = httpClient; this.basePath = basePath; this.data = data; } protected internalGet(relativePath: string): Promise { return this.httpClient.get(`${this.basePath}/clashes/${this.id}${relativePath}`); } protected internalPut( relativePath: string, body?: ArrayBuffer | Blob | globalThis.File | FormData | object | string | null ): Promise { return this.httpClient.put(`${this.basePath}/clashes/${this.id}${relativePath}`, body); } protected internalDelete(relativePath: string): Promise { return this.httpClient.delete(`${this.basePath}/clashes/${this.id}${relativePath}`); } /** * The type of the clashes that the test detects: * * - `Сlearance clash` (true) - a clash in which the entity A may or may not intersect with * entity B, but comes within a distance of less than the `tolerance`. * - `Hard clash` (false) - a clash in which the entity A intersects with entity B by a * distance of more than the `tolerance`. * * @readonly */ get clearance(): boolean { return this.data.clearance; } /** * Test creation time (UTC) in the format specified in ISO 8601. * * @readonly */ get createdAt(): string { return this.data.createdAt; } /** * Raw test data received from the server. * * @readonly */ get data(): any { return this._data; } private set data(value: any) { this._data = value; this._data.owner.avatarUrl = `${this.httpClient.serverUrl}/users/${this._data.owner.userId}/avatar`; this._data.owner.fullName = userFullName(this._data.owner); this._data.owner.initials = userInitials(this._data.owner.fullName); } /** * Unique test ID. * * @readonly */ get id(): string { return this.data.id; } /** * Test last update (UTC) time in the format specified in ISO 8601. * * @readonly */ get lastModifiedAt(): string { return this.data.lastModifiedAt; } /** * Test name. */ get name(): string { return this.data.name; } set name(value: string) { this.data.name = value; } /** * Test owner information. * * @property {string} userId - User ID. * @property {string} userName - User name. * @property {string} name - First name. * @property {string} lastName - Last name. * @property {string} fullName - Full name. * @property {string} initials - Initials. * @property {string} email - User email. * @property {string} avatarUrl - User avatar image URL. * @readonly */ get owner(): IShortUserDescription { return this.data.owner; } /** * First selection set for clash detection. Entities from `selectionSetA` will be tested * against each others by entities from the `selectionSetB` during the test. * * @readonly */ get selectionSetA(): string[] { return this.data.selectionSetA; } /** * The type of first selection set for clash detection. Can be one of: * * - `all` - All file/assembly entities. * - `handle` - Entities with handles specified in the `selectionSetA`. * - `models` - Entities of file/assembly models specified in `selectionSetA`. * - `searchquery` - Entities retrieved by the search queries specified in `selectionSetA`. * * @readonly */ get selectionTypeA(): string { return this.data.selectionTypeA; } /** * Second selection set for clash detection. Entities from `selectionSetB` will be tested * against each others by entities from the `selectionSetA` during the test. * * @readonly */ get selectionSetB(): string[] { return this.data.selectionSetB; } /** * The type of second selection set for clash detection. Can be one of: * * - `all` - All file/assembly entities. * - `handle` - Entities with handles specified in the `selectionSetB`. * - `models` - Entities of file/assembly models specified in `selectionSetB`. * - `searchquery` - Entities retrieved by the search queries specified in `selectionSetB`. * * @readonly */ get selectionTypeB(): string { return this.data.selectionTypeB; } /** * Test status. Can be `none`, `waiting`, `inprogress`, `done` or `failed`. * * @readonly */ get status(): string { return this.data.status; } /** * The distance of separation between entities at which test begins detecting clashes. * * @readonly */ get tolerance(): number { return this.data.tolerance; } /** * Refresh test data. * * @async */ checkout(): Promise { return this.internalGet("") .then((response) => response.json()) .then((data) => { this.data = data; return this; }); } /** * Update test data on the server. * * @async * @param data - Raw test data. */ update(data: any): Promise { return this.internalPut("", data) .then((response) => response.json()) .then((data) => { this.data = data; return this; }); } /** * Remove a test from a file. * * @async * @returns Returns the raw data of a deleted test. */ delete(): Promise { return this.internalDelete("").then((response) => response.json()); } /** * Save test data changes to the server. Call this method to update test data on the server * after any changes. * * @async */ save(): Promise { return this.update(this.data); } /** * Wait for test to complete. Test is done when it changes to `done` or `failed` status. * * @async * @param params - An object containing waiting parameters. * @param params.timeout - The time, in milliseconds that the function should wait test. If * test is not complete during this time, the `TimeoutError` exception will be thrown. * @param params.interval - The time, in milliseconds, the function should delay in between * checking test status. * @param params.signal- An AbortController * signal object instance, which can be used to abort waiting as desired. */ waitForDone(params?: { timeout?: number; interval?: number; signal?: AbortSignal }) { const checkDone = () => this.checkout().then((test) => ["done", "failed"].includes(test.status)); return waitFor(checkDone, params).then(() => this); } /** * Returns a report with the clash detection results for this test. * * @async */ getReport(): Promise { return this.internalGet("/report").then((response) => response.json()); } }