-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-callout.ts
79 lines (71 loc) · 3.01 KB
/
create-callout.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { mainLog } from './utils/logger.js';
import { ApiClient } from './utils/apiclient.js';
import { BasicNode, DrawingObjectType, ModifyStatusResponseOutput, SingleRequestResultStatus } from './utils/onshapetypes.js';
import { usage, DrawingScriptArgs, validateBaseURLs, waitForModifyToFinish, parseDrawingScriptArgs, getRandomLocation } from './utils/drawingutils.js';
const LOG = mainLog();
let drawingScriptArgs: DrawingScriptArgs = null;
let validArgs: boolean = true;
let apiClient: ApiClient = null;
try {
drawingScriptArgs = parseDrawingScriptArgs();
apiClient = await ApiClient.createApiClient(drawingScriptArgs.stackToUse);
validateBaseURLs(apiClient.getBaseURL(), drawingScriptArgs.baseURL);
} catch (error) {
validArgs = false;
usage('create-callout');
}
if (validArgs) {
try {
LOG.info(`documentId=${drawingScriptArgs.documentId}, workspaceId=${drawingScriptArgs.workspaceId}, elementId=${drawingScriptArgs.elementId}`);
const randomLocation: number[] = getRandomLocation([1.0, 1.0], [8.0, 8.0]);
const textHeight = 0.12;
const annotationText = 'Callout';
const requestBody = {
description: 'Add callout',
jsonRequests: [
{
messageName: 'onshapeCreateAnnotations',
formatVersion: '2021-01-01',
annotations: [
{
type: DrawingObjectType.CALLOUT,
callout: {
borderShape: 'Circle',
borderSize: 0,
contents: annotationText,
contentsBottom: 'bottom',
contentsLeft: 'left',
contentsRight: 'right',
contentsTop: 'top',
position: {
type: 'Onshape::Reference::Point',
coordinate: randomLocation
},
textHeight: textHeight
}
}
]
}
]
};
const modifyRequest = await apiClient.post(`api/v6/drawings/d/${drawingScriptArgs.documentId}/w/${drawingScriptArgs.workspaceId}/e/${drawingScriptArgs.elementId}/modify`, requestBody) as BasicNode;
const responseOutput: ModifyStatusResponseOutput = await waitForModifyToFinish(apiClient, modifyRequest.id);
if (responseOutput) {
// Only 1 request was made - verify it succeeded
if (responseOutput.results.length == 1 &&
responseOutput.results[0].status === SingleRequestResultStatus.RequestSuccess) {
// Success - logicalId of new callout is available
const newLogicalId = responseOutput.results[0].logicalId;
console.log(`Create callout succeeded and has a logicalId: ${newLogicalId}`);
} else {
console.log(`Create callout failed. Response status code: ${responseOutput.statusCode}.`)
}
} else {
console.log('Create callout with leader failed waiting for modify to finish.');
LOG.info('Create callout with leader failed waiting for modify to finish.');
}
} catch (error) {
console.error(error);
LOG.error('Create callout failed', error);
}
}