-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathExportToDXF.jsx
375 lines (329 loc) · 11.5 KB
/
ExportToDXF.jsx
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
exportToDXF.jsx for Adobe Illustrator
Description: Export all artboards or selection to separately DXF
Date: May, 2021
Modification date: May, 2023
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
0.1.1 Fixed placeholder insertion for CS6
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale.
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
$.localize = true; // Enabling automatic localization
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
// Main function
function main() {
var SCRIPT = {
name: 'Export To DXF',
version: 'v.0.1.1'
},
CFG = {
defRadio: 1, // Default export: 1 - Artboards, 2 - Selection, 3 - Each selection
abs: '1, 2-4',
allAbs: '%all',
uiMargins: [10, 15, 10, 10],
uiOpacity: .97 // UI window opacity. Range 0-1
},
LANG = {
errDoc: { en: 'Error\nOpen a document and try again',
ru: 'Ошибка\nОткройте документ и запустите скрипт' },
errFolder: { en: "Error\nThis folder doesn't exist",
ru: 'Ошибка\nТакой папки не существует' },
errPrefix: { en: 'Error\nPlease enter file name prefix',
ru: 'Ошибка\nВведите префикс имени' },
folder: { en: 'Output folder', ru: 'Папка назначения' },
select: { en: 'Select', ru: 'Выбрать' },
selectTitle: { en: 'Select a folder to export',
ru: 'Выберите папку для экспорта' },
prefix: { en: 'File name prefix', ru: 'Префикс имен файлов' },
separator: { en: 'Separator', ru: 'Разделитель' },
placeholder: { en: 'all artboards', ru: 'все артборды' },
abs: { en: 'Artboards', ru: 'Артборды' },
totalSel: { en: 'Selection', ru: 'Все выделение' },
eachSel: { en: 'Each selection', ru: 'По отдельности' },
absRange: { en: 'Artboards range', ru: 'Диапазон артбордов' },
wait: { en: 'Waiting...', ru: 'Ожидайте...' },
cancel: { en: 'Cancel', ru: 'Отмена' },
ok: { en: 'Export', ru: 'Экспортировать' }
};
if (!documents.length) {
alert(LANG.errDoc);
return;
}
// Default variables for dialog box
var doc = activeDocument,
fileName = doc.name.replace(/\.[^\.]+$/, ''),
exportOptions = getOptions(),
separator = '-',
outFolder = (doc.path != '') ? doc.path : Folder.desktop;
// INTERFACE
var dialog = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
dialog.alignChildren = ['fill', 'fill'];
dialog.opacity = CFG.uiOpacity;
var outPnl = dialog.add('panel', undefined, LANG.folder);
outPnl.orientation = 'row';
outPnl.margins = CFG.uiMargins;
var outFolderBtn = outPnl.add('button', undefined, LANG.select);
var outFolderLbl = outPnl.add('edittext', undefined);
outFolderLbl.text = decodeURI(outFolder);
outFolderLbl.characters = 22;
var fileNameGrp = dialog.add('group');
var namePnl = fileNameGrp.add('panel', undefined, LANG.prefix);
namePnl.orientation = 'row';
namePnl.margins = CFG.uiMargins;
var namePrefix = namePnl.add('edittext', undefined, fileName);
namePrefix.characters = 22;
var separatorPnl = fileNameGrp.add('panel', undefined, LANG.separator);
separatorPnl.margins = CFG.uiMargins;
var separatorInp = separatorPnl.add('edittext', undefined, separator);
separatorInp.characters = 4;
var optionPnl = dialog.add('group');
optionPnl.orientation = 'row';
var rbAbs = optionPnl.add('radiobutton', undefined, LANG.abs);
var rbSel = optionPnl.add('radiobutton', undefined, LANG.totalSel);
var rbSelSep = optionPnl.add('radiobutton', undefined, LANG.eachSel);
switch (CFG.defRadio) {
case 1:
default:
rbAbs.value = true;
break;
case 2:
rbSel.value = true;
break;
case 3:
rbSelSep.value = true;
break;
}
if (!selection.length || selection.typename == 'TextRange') {
rbSel.enabled = rbSelSep.enabled = false;
rbSel.value = rbSelSep.value = false;
rbAbs.value = true;
}
var abPanel = dialog.add('panel', undefined, LANG.absRange);
abPanel.orientation = 'column';
abPanel.alignChildren = ['fill','fill'];
abPanel.margins = CFG.uiMargins;
var absInput = abPanel.add('edittext', undefined, CFG.abs);
var absDescr = abPanel.add('statictext', undefined, CFG.allAbs + ' - ' + LANG.placeholder);
absDescr.justify = 'left';
rbSel.onClick = rbSelSep.onClick = function () {
abPanel.enabled = false;
}
rbAbs.onClick = function () {
abPanel.enabled = true;
}
var btns = dialog.add('group');
btns.alignChildren = ['center', 'center'];
var cancel = btns.add('button', undefined, LANG.cancel, { name: 'cancel' });
var ok = btns.add('button', undefined, LANG.ok, { name: 'ok' });
var copyright = dialog.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold/');
});
outFolderBtn.onClick = function () {
var userFolder = Folder.selectDialog(LANG.selectTitle);
if (userFolder !== null) {
outFolderLbl.text = decodeURI(userFolder);
outFolder = userFolder;
}
}
absDescr.addEventListener('mousedown', function () {
rbAbs.active = true;
dialog.update();
absInput.active = true;
absInput.textselection = CFG.allAbs;
});
cancel.onClick = dialog.close;
ok.onClick = okClick;
function okClick() {
outFolder = decodeURI(outFolderLbl.text);
if (isEmpty(outFolder.toString())) {
outFolderLbl.text = (doc.path != '') ? doc.path : Folder.desktop;
return;
}
if (!Folder(outFolder).exists) {
alert(LANG.errFolder);
outFolderLbl.text = (doc.path != '') ? doc.path : Folder.desktop;
return;
}
if (!isEmpty(namePrefix.text)) {
fileName = namePrefix.text.trim();
} else {
alert(LANG.errPrefix);
return;
}
ok.text = LANG.wait;
dialog.update();
var docSel = selection,
tmpRange = absInput.text,
absRange = []; // Range of artboards indexes
// Prepare
tmpRange = tmpRange.replace(/\s/g, ''); // Remove whitespaces
tmpRange = tmpRange.split(','); // Split string to array
absRange = getArtboardsRange(tmpRange, CFG.allAbs);
if (!isEmpty(separatorInp.text)) separator = separatorInp.text;
var rootDest = outFolder + '/' + fileName + separator;
if (rbAbs.value) {
selection = null;
redraw();
for (var i = 0, abLen = absRange.length; i < abLen; i++) {
var idx = absRange[i];
var abName = doc.artboards[idx].name.replace(/\s/g, separator);
doc.artboards.setActiveArtboardIndex(idx);
doc.selectObjectsOnActiveArtboard();
exportDXF(rootDest + abName, exportOptions);
selection = null;
redraw();
}
doc.artboards.setActiveArtboardIndex(currBoardIdx);
}
if (rbSel.value) {
exportDXF(rootDest.slice(0, -1 * separator.length), exportOptions);
}
if (rbSelSep.value) {
selection = null;
for (var i = 0, sLen = docSel.length; i < sLen; i++) {
docSel[i].selected = true;
exportDXF(rootDest + (i + 1), exportOptions);
selection = null;
}
}
selection = docSel;
dialog.close();
}
dialog.center();
dialog.show();
};
/**
* Check empty string
* @param {string} str - input string
* @return {boolean} answer
*/
function isEmpty(str) {
return str.replace(/\s/g, '').length == 0;
}
// Remove whitespaces from start and end of string
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
}
}
/**
* Get document artboards from user input
* @param {array} arr - raw array of artboards
* @param {string} placeholder - keyword for get all artboards
* @return {array} array of artboard indexes
*/
function getArtboardsRange(arr, placeholder) {
var parsedStr = [];
forEach(arr, function (e) {
if (e.match(placeholder) != null) {
for (var i = 0, absLen = activeDocument.artboards.length; i <= absLen; i++) {
parsedStr.push(i);
}
return;
};
if (e.match('-') == null) {
parsedStr.push(e * 1);
return;
};
var extremeVal = e.split('-'); // Min & max value in range
for (var j = (extremeVal[0] * 1); j <= extremeVal[1]; j++) {
parsedStr.push(j);
}
});
return intersect(activeDocument.artboards, parsedStr);
}
/**
* Calls a provided callback function once for each element in an array
* @param {array} collection
* @param {function} fn - the callback function
*/
function forEach(collection, fn) {
for (var i = 0; i < collection.length; i++) {
fn(collection[i]);
}
}
/**
* Search for common elements in arrays
* @param {array} arr1
* @param {array} arr2
* @return {array} array of common elements
*/
function intersect(arr1, arr2) {
var tmp = [];
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(i + 1) !== -1) tmp.push(i);
}
return tmp;
}
// Polyfill indexOf() for Array
Array.prototype.indexOf = function (item) {
for (var i = 0 ; i < this.length; i++ ) {
if ( this[i] === item ) return i;
}
return -1;
}
/**
* Get AutoCAD options for export to DXF
* @return {object} options
*/
function getOptions() {
var options = new ExportOptionsAutoCAD();
options.exportFileFormat = AutoCADExportFileFormat.DXF;
// exportOptions.exportOption = AutoCADExportOption.PreserveAppearance;
options.exportOption = AutoCADExportOption.MaximumEditability;
// AutoCADCompatibility.AutoCADRelease13
// AutoCADCompatibility.AutoCADRelease14
// AutoCADCompatibility.AutoCADRelease15
// AutoCADCompatibility.AutoCADRelease18
// AutoCADCompatibility.AutoCADRelease21
// AutoCADCompatibility.AutoCADRelease24
options.version = AutoCADCompatibility.AutoCADRelease14;
options.unit = AutoCADUnit.Millimeters;
options.scaleLineweights = false;
options.exportSelectedArtOnly = true;
options.convertTextToOutlines = false;
options.generateThumbnails = false;
return options;
}
/**
* Export to file
* @param {string} dest - folder path
* @param {object} options - AutoCAD export options
*/
function exportDXF(dest, options) {
var file = new File(dest + '.dxf');
activeDocument.exportFile(file, ExportType.AUTOCAD, options);
}
/**
* Open link in browser
* @param {string} url - website adress
*/
function openURL(url) {
var html = new File(Folder.temp.absoluteURI + '/aisLink.html');
html.open('w');
var htmlBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' + url + '"></head><body> <p></body></html>';
html.write(htmlBody);
html.close();
html.execute();
}
// Run script
try {
main();
} catch (e) {}