-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-notes.ts
128 lines (116 loc) · 5.4 KB
/
edit-notes.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { mainLog } from './utils/logger.js';
import { ApiClient } from './utils/apiclient.js';
import { BasicNode, DrawingObjectType, Note, GetDrawingJsonExportResponse, Annotation, ModifyStatusResponseOutput, SingleRequestResultStatus } from './utils/onshapetypes.js';
import { usage, waitForModifyToFinish, DrawingScriptArgs, parseDrawingScriptArgs, validateBaseURLs } from './utils/drawingutils.js';
import { getDrawingJsonExport, getAllDrawingAnnotationsInViewsFromExportData } 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('edit-notes');
}
if (validArgs) {
try {
LOG.info(`documentId=${drawingScriptArgs.documentId}, workspaceId=${drawingScriptArgs.workspaceId}, elementId=${drawingScriptArgs.elementId}`);
/**
* Retrieve annotations in the drawing that are associated with views (to avoid annotations in borders, titleblock, etc.).
* NOTE - THIS MEANS NOTES THAT ARE NOT ASSOCIATED WITH A VIEW (e.g. do not have a leader attached to a view edge) WILL NOT BE EDITED.
*/
let drawingJsonExport: GetDrawingJsonExportResponse = await getDrawingJsonExport(apiClient, drawingScriptArgs.documentId, 'w', drawingScriptArgs.workspaceId, drawingScriptArgs.elementId) as GetDrawingJsonExportResponse;
let viewAnnotations: Annotation[] = getAllDrawingAnnotationsInViewsFromExportData(drawingJsonExport);
/**
* Loop through annotations and create edit requests to move each note 1 unit to the right and add ' +' to the last line of note text.
*/
let editAnnotations: Annotation[] = null;
for (let indexAnnotation = 0; indexAnnotation < viewAnnotations.length; indexAnnotation++) {
let annotation: Annotation = viewAnnotations[indexAnnotation];
let editAnnotation: Annotation = null;
switch (annotation.type) {
case DrawingObjectType.NOTE: {
// Add a ' +' at the end of the note text.
// It's best to preserve the {\\pxql; <note text> } structure of note text by putting new text inside of the {}'s.
// pxql or pql is left justified, pxqc or pqc is center justified, pxqr or pqr is right justified, pxqj or pqj is justified.
let lastBraceRegEx = /}$/g; // Regular expression to find the brace at end of the contents string
let newContents = annotation.note.contents.replace(lastBraceRegEx, ' +}')
editAnnotation = {
note: {
logicalId: annotation.note.logicalId,
position: {
coordinate: [
annotation.note.position.coordinate[0] + 1.0,
annotation.note.position.coordinate[1],
annotation.note.position.coordinate[2]
],
type: 'Onshape::Reference::Point'
},
contents: newContents
},
type: DrawingObjectType.NOTE
};
break;
}
default: {
editAnnotation = null;
break;
}
}
if (editAnnotation) {
if (editAnnotations === null) {
editAnnotations = [editAnnotation];
} else {
editAnnotations.push(editAnnotation);
}
}
}
if (editAnnotations && editAnnotations.length > 0) {
const requestBody = {
description: 'Edit notes',
jsonRequests: [ {
messageName: 'onshapeEditAnnotations',
formatVersion: '2021-01-01',
annotations: editAnnotations
} ]
};
/**
* Modify the drawing to edit the dimensions
*/
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) {
let countSucceeded = 0;
let countFailed = 0;
for (let iResultCount: number = 0; iResultCount < responseOutput.results.length; iResultCount++) {
let currentResult = responseOutput.results[iResultCount];
if (currentResult.status === SingleRequestResultStatus.RequestSuccess) {
countSucceeded++;
} else {
countFailed++;
}
}
console.log(`Successfully edited ${countSucceeded} of ${editAnnotations.length} notes.`);
if (countFailed > 0) {
console.log(`Failed to edit ${countFailed} notes.`);
}
if (editAnnotations.length !== (countSucceeded + countFailed)) {
let countTotal = countSucceeded + countFailed;
console.log(`Mismatch in number of note edits requested (${editAnnotations.length}) and response (${countTotal}).`);
}
} else {
console.log('Edit notes failed waiting for modify to finish.');
LOG.info('Edit notes failed waiting for modify to finish.');
}
} else {
console.log('No notes found to be edited.');
LOG.error('No notes found to be edited.');
}
} catch (error) {
console.error(error);
LOG.error('Edit notes failed', error);
}
}