LanyaAITools.jsx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. #target illustrator
  2. #targetengine main
  3. var bit = 64;//AI软件系统位数,默认64位,如果点击合集面板按钮没有反应,可以将64改为32。
  4. var aiVersion = app.version.split('.')[0];
  5. var vs = "illustrator-" + aiVersion + ".0" + bit;
  6. var IconsFolder = "C:/TSP/icon";
  7. var micro_distance = "1";
  8. // 实际代码建立 buildMsg(code) 函数传送代码
  9. function buildMsg(code) {
  10. try {
  11. var bt = new BridgeTalk;
  12. bt.target = vs;
  13. var msg = code;
  14. bt.body = msg;
  15. bt.send();
  16. } catch (e) { }
  17. }
  18. // 创建面板 使用 new Window("palette") ,需要 BridgeTalk, 它是 Adobe 应用程序之间进行通信的一种机制。
  19. // 它允许不同的 Adobe 应用程序在同一台计算机上进行交互和数据共享。
  20. // var bt = new BridgeTalk();
  21. // bt.target = "photoshop"; // 目标应用程序名称
  22. // bt.body = "alert('Hello from Illustrator!')"; // 要发送的消息或脚本
  23. // bt.send();
  24. icon_panel(); // main_panel();
  25. function main_panel() {
  26. var panel = new Window("palette", "蘭雅 Adobe Illustrator 工具箱© 2023.12.12");
  27. panel.alignChildren = ["left", "top"];
  28. panel.spacing = 2;
  29. panel.margins = 3;
  30. // 创建按钮组
  31. var BtGroup1 = panel.add("group");
  32. var BtGroup2 = panel.add("group");
  33. // 设置按钮组为水平布局
  34. BtGroup1.orientation = "row";
  35. BtGroup2.orientation = "row";
  36. // 设置按钮组的边缘间距
  37. BtGroup1.spacing = 2; // 调整按钮之间的间距
  38. BtGroup2.spacing = 2;
  39. // 添加按钮
  40. var button1 = BtGroup1.add("button", undefined, "标注尺寸");
  41. var button2 = BtGroup1.add("button", undefined, "批量旋转");
  42. var button3 = BtGroup1.add("button", undefined, "文件日期");
  43. var button4 = BtGroup1.add("button", undefined, "尺寸取整-微调-统一");
  44. var button5 = BtGroup2.add("button", undefined, "替换对齐-打包图片");
  45. var button6 = BtGroup2.add("button", undefined, "自动群组-调整尺寸");
  46. var button7 = BtGroup2.add("button", undefined, "尺寸复制");
  47. var button8 = BtGroup2.add("button", undefined, "▲");
  48. button8.preferredSize = [26, 26];
  49. button1.helpTip = "标注尺寸, <Alt>增强标注";
  50. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  51. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  52. button4.helpTip = "尺寸取整, <Alt-Ctrl-Shift>微调统一尺寸";
  53. button5.helpTip = "左上对齐快速替换, <Alt>打包连接图";
  54. button6.helpTip = "自动群组, <Alt>调整尺寸";
  55. button7.helpTip = "尺寸复制, <Alt>包括轮廓";
  56. button8.helpTip = "切换图标界面窗口";
  57. // 设置按钮大小与图片大小相同
  58. button8.preferredSize = [26, 26];
  59. button1.onClick = function () {
  60. if (ScriptUI.environment.keyboardState.ctrlKey) {
  61. buildMsg("shapes_info();");
  62. } else if (ScriptUI.environment.keyboardState.altKey) {
  63. make_size_plus();
  64. } else {
  65. buildMsg("make_size();");
  66. }
  67. };
  68. button2.onClick = function () {
  69. if (ScriptUI.environment.keyboardState.ctrlKey) {
  70. // Ctrl 加鼠标左键,自定义数字
  71. var input = prompt("请输入角度数字:", "45");
  72. if (!isNaN(parseFloat(input)))
  73. buildMsg("shapes_rotate(" + input + ");");
  74. } else if (ScriptUI.environment.keyboardState.altKey) {
  75. buildMsg("shapes_rotate(180);");
  76. } else {
  77. buildMsg("shapes_rotate(90);");
  78. }
  79. };
  80. button3.onClick = function () {
  81. if (ScriptUI.environment.keyboardState.altKey) {
  82. buildMsg("mark_5mm();");
  83. } else {
  84. buildMsg("filename_date();");
  85. }
  86. };
  87. button4.onClick = function () {
  88. if (ScriptUI.environment.keyboardState.ctrlKey) {
  89. buildMsg("modify_size(-" + micro_distance + ", -" + micro_distance + ");");
  90. } else if (ScriptUI.environment.keyboardState.altKey) {
  91. buildMsg("modify_size(" + micro_distance + ", " + micro_distance + ");");
  92. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  93. // alert("ScriptUI.environment.keyboardState.shiftKey");
  94. var input = prompt("请输如宽和高两个数字(例如: 100 80): (或者:99.5 55.5)", "100 80");
  95. // 使用正则表达式匹配数字
  96. var regex = /(\d+(?:\.\d+)?)\s*(\d+(?:\.\d+)?)/;
  97. var match = input.match(regex);
  98. if (match) {
  99. var number1 = Number(match[1]);
  100. var number2 = Number(match[2]);
  101. buildMsg("set_size(" + number1 + ", " + number2 + ");");
  102. } else {
  103. alert("输入格式不正确!");
  104. }
  105. } else {
  106. buildMsg("size_to_integer();");
  107. }
  108. };
  109. button5.onClick = function () {
  110. if (ScriptUI.environment.keyboardState.altKey) {
  111. buildMsg("img_pack_links();");
  112. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  113. // Shift + 鼠标左键 中心对齐替换
  114. buildMsg("replace_align_center(true);");
  115. } else {
  116. buildMsg("replace_align_position();");
  117. }
  118. };
  119. button6.onClick = function () {
  120. if (ScriptUI.environment.keyboardState.ctrlKey) {
  121. alert("Ctrl信息; Alt 调整尺寸; Shift重新加载脚本; 默认自动群组");
  122. } else if (ScriptUI.environment.keyboardState.altKey) {
  123. ResizeToSize();
  124. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  125. reload_aia();
  126. } else {
  127. auto_group();
  128. }
  129. };
  130. button7.onClick = function () {
  131. if (ScriptUI.environment.keyboardState.altKey) {
  132. buildMsg("size_by_controlBounds();");
  133. }else if (ScriptUI.environment.keyboardState.shiftKey) {
  134. // Shift + 鼠标左键 中心对齐
  135. buildMsg("replace_align_center(false);");
  136. } else {
  137. buildMsg("size_by_width_height();");
  138. }
  139. };
  140. button8.onClick = function () {
  141. icon_panel();
  142. panel.close();
  143. };
  144. // 显示面板
  145. panel.show();
  146. }
  147. function icon_panel() {
  148. var panel = new Window("palette", "©蘭雅 Adobe Illustrator 工具箱");
  149. panel.onClose = function () {
  150. saveWindowPosition(panel);
  151. };
  152. panel.alignChildren = ["left", "top"];
  153. panel.spacing = 2;
  154. panel.margins = 3;
  155. // 创建按钮组
  156. var BtGroup1 = panel.add("group");
  157. var BtGroup2 = panel.add("group");
  158. // 设置按钮组为水平布局
  159. BtGroup1.orientation = "row";
  160. BtGroup2.orientation = "row";
  161. // 设置按钮组的边缘间距
  162. BtGroup1.spacing = 2; // 调整按钮之间的间距
  163. BtGroup2.spacing = 2;
  164. // scriptFile = new File($.fileName); // 获取当前脚本文件的路径
  165. // var iconFile = new File(scriptFile.path + "/icon/icon.png"); // 拼接图标文件的完整路径
  166. var iconF1 = IconsFolder + "/size.png";
  167. var iconF2 = IconsFolder + "/icon.png";
  168. var iconF3 = IconsFolder + "/mark.png";
  169. var iconF4 = IconsFolder + "/debug.png";
  170. var iconF5 = IconsFolder + "/replace.png";
  171. var iconF6 = IconsFolder + "/gpucard.png";
  172. var iconF7 = IconsFolder + "/byBounds.png";
  173. var iconF8 = IconsFolder + "/repeat.png";
  174. // 添加图标按钮
  175. var button1 = BtGroup1.add("iconbutton", undefined, iconF1);
  176. var button2 = BtGroup1.add("iconbutton", undefined, iconF2);
  177. var button3 = BtGroup1.add("iconbutton", undefined, iconF3);
  178. var button4 = BtGroup1.add("iconbutton", undefined, iconF4);
  179. var button5 = BtGroup2.add("iconbutton", undefined, iconF5);
  180. var button6 = BtGroup2.add("iconbutton", undefined, iconF6);
  181. var button7 = BtGroup2.add("iconbutton", undefined, iconF7);
  182. var button8 = BtGroup2.add("iconbutton", undefined, iconF8);
  183. button1.helpTip = "标注尺寸, <Alt>增强标注";
  184. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  185. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  186. button4.helpTip = "尺寸取整, <Alt-Ctrl-Shift>微调统一尺寸";
  187. button5.helpTip = "左上对齐快速替换, <Alt>打包连接图";
  188. button6.helpTip = "自动群组, <Alt>调整尺寸";
  189. button7.helpTip = "尺寸复制, <Alt>包括轮廓";
  190. button8.helpTip = "<Ctrl>微调距离, 最小化窗口";
  191. // 设置按钮大小与图片大小相同
  192. button1.preferredSize = [48, 48];
  193. button2.preferredSize = [48, 48];
  194. button3.preferredSize = [48, 48];
  195. button4.preferredSize = [48, 48];
  196. button5.preferredSize = [48, 48];
  197. button6.preferredSize = [48, 48];
  198. button7.preferredSize = [48, 48];
  199. button8.preferredSize = [48, 48];
  200. button1.onClick = function () {
  201. if (ScriptUI.environment.keyboardState.ctrlKey) {
  202. buildMsg("shapes_info();");
  203. } else if (ScriptUI.environment.keyboardState.altKey) {
  204. make_size_plus();
  205. } else {
  206. buildMsg("make_size();");
  207. }
  208. };
  209. button2.onClick = function () {
  210. if (ScriptUI.environment.keyboardState.ctrlKey) {
  211. // Ctrl 加鼠标左键,自定义数字
  212. var input = prompt("请输入角度数字:", "45");
  213. if (!isNaN(parseFloat(input)))
  214. buildMsg("shapes_rotate(" + input + ");");
  215. } else if (ScriptUI.environment.keyboardState.altKey) {
  216. buildMsg("shapes_rotate(180);");
  217. } else {
  218. buildMsg("shapes_rotate(90);");
  219. }
  220. };
  221. button3.onClick = function () {
  222. if (ScriptUI.environment.keyboardState.altKey) {
  223. buildMsg("mark_5mm();");
  224. } else {
  225. buildMsg("filename_date();");
  226. }
  227. };
  228. button4.onClick = function () {
  229. if (ScriptUI.environment.keyboardState.ctrlKey) {
  230. buildMsg("modify_size(-" + micro_distance + ", -" + micro_distance + ");");
  231. } else if (ScriptUI.environment.keyboardState.altKey) {
  232. buildMsg("modify_size(" + micro_distance + ", " + micro_distance + ");");
  233. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  234. // alert("ScriptUI.environment.keyboardState.shiftKey");
  235. var input = prompt("请输如宽和高两个数字(例如: 100 80): (或者:99.5 55.5)", "100 80");
  236. // 使用正则表达式匹配数字
  237. var regex = /(\d+(?:\.\d+)?)\s*(\d+(?:\.\d+)?)/;
  238. var match = input.match(regex);
  239. if (match) {
  240. var number1 = Number(match[1]);
  241. var number2 = Number(match[2]);
  242. buildMsg("set_size(" + number1 + ", " + number2 + ");");
  243. } else {
  244. alert("输入格式不正确!");
  245. }
  246. } else {
  247. buildMsg("size_to_integer();");
  248. }
  249. };
  250. button5.onClick = function () {
  251. if (ScriptUI.environment.keyboardState.altKey) {
  252. buildMsg("img_pack_links();");
  253. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  254. // Shift + 鼠标左键 中心对齐替换
  255. buildMsg("replace_align_center(true);");
  256. } else {
  257. buildMsg("replace_align_position();");
  258. }
  259. };
  260. button6.onClick = function () {
  261. if (ScriptUI.environment.keyboardState.ctrlKey) {
  262. alert("Ctrl信息; Alt 调整尺寸; Shift重新加载脚本; 默认自动群组");
  263. } else if (ScriptUI.environment.keyboardState.altKey) {
  264. ResizeToSize();
  265. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  266. reload_aia();
  267. } else {
  268. auto_group();
  269. }
  270. };
  271. button7.onClick = function () {
  272. if (ScriptUI.environment.keyboardState.altKey) {
  273. buildMsg("size_by_controlBounds();");
  274. }else if (ScriptUI.environment.keyboardState.shiftKey) {
  275. // Shift + 鼠标左键 中心对齐
  276. buildMsg("replace_align_center(false);");
  277. } else {
  278. buildMsg("size_by_width_height();");
  279. }
  280. };
  281. button8.onClick = function () {
  282. if (ScriptUI.environment.keyboardState.ctrlKey) {
  283. micro_distance = prompt("设置微调距离(mm): ", micro_distance);
  284. } else if (ScriptUI.environment.keyboardState.altKey) {
  285. main_panel();
  286. panel.close();
  287. } else {
  288. mini_panel();
  289. panel.close();
  290. }
  291. };
  292. // 显示面板
  293. panel.show();
  294. // 恢复窗口位置
  295. restoreWindowPosition(panel);
  296. }
  297. function mini_panel() {
  298. var panel = new Window("palette", "");
  299. panel.spacing = 0;
  300. panel.margins = [0, 0, 0, 0];;
  301. var icon = IconsFolder + "/repeat.png";
  302. var button_mini = panel.add("iconbutton", undefined, icon);
  303. button_mini.preferredSize = [40, 40];
  304. button_mini.spacing = 0;
  305. button_mini.onClick = function () {
  306. icon_panel();
  307. panel.close();
  308. };
  309. panel.show();
  310. restoreWindowPosition(panel);
  311. panel.bounds.height = 42;
  312. panel.bounds.width = 50;
  313. }
  314. // 保存窗口位置
  315. function saveWindowPosition(window) {
  316. var position = window.bounds;
  317. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  318. settingsFile.open("w");
  319. settingsFile.write(position.left + "," + position.top + "," + position.right + "," + position.bottom);
  320. settingsFile.close();
  321. }
  322. // 恢复窗口位置
  323. function restoreWindowPosition(window) {
  324. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  325. if (settingsFile.exists) {
  326. settingsFile.open("r");
  327. var position = settingsFile.read().split(",");
  328. settingsFile.close();
  329. window.bounds.left = parseInt(position[0]);
  330. window.bounds.top = parseInt(position[1]);
  331. window.bounds.right = parseInt(position[2]);
  332. window.bounds.bottom = parseInt(position[3]);
  333. }
  334. }
  335. //==================================================================================//
  336. // 蘭雅 Adobe Illustrator 工具箱© 2023.12.12 各个按钮功能模块
  337. //==================================================================================//
  338. var mm = 25.4 / 72; // pt 和 mm 转换系数
  339. // 格式化尺寸为 mm 取整数
  340. function formatSize(size) {
  341. return Math.round(size * mm).toFixed(0);
  342. }
  343. // 获得选择对象的边界框
  344. function get_Sel_Bounds() {
  345. var totalBounds = null;
  346. var sr = app.activeDocument.selection;
  347. for (var i = 0; i < sr.length; i++) {
  348. var item = sr[i];
  349. // 获取对象的边界框
  350. var bounds = item.geometricBounds;
  351. // 更新总范围
  352. if (totalBounds === null) {
  353. totalBounds = bounds.slice(); // 创建边界框的副本
  354. } else {
  355. totalBounds[0] = Math.min(totalBounds[0], bounds[0]); // 左边界
  356. totalBounds[1] = Math.max(totalBounds[1], bounds[1]); // 上边界
  357. totalBounds[2] = Math.max(totalBounds[2], bounds[2]); // 右边界
  358. totalBounds[3] = Math.min(totalBounds[3], bounds[3]); // 下边界
  359. }
  360. }
  361. return totalBounds;
  362. }
  363. // 标注尺寸
  364. function make_size() {
  365. // 定义当前激活文档
  366. var docRef = activeDocument;
  367. var mm = 25.4 / 72; // pt 和 mm 转换系数
  368. var myFont = textFonts.getByName("MicrosoftYaHei");
  369. var myFontSize = 24;
  370. var x, y;
  371. // 格式化尺寸为 mm,按每 0.5 取整
  372. function formatSize5(size) {
  373. var rd = Math.round(size * mm * 2) / 2;
  374. return (rd % 1 === 0) ? Math.round(rd) : rd.toFixed(1);
  375. }
  376. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  377. var cmykRed = new CMYKColor();
  378. cmykRed.cyan = 0;
  379. cmykRed.magenta = 100;
  380. cmykRed.yellow = 100;
  381. cmykRed.black = 0;
  382. function writeText(text) {
  383. var textRef = docRef.textFrames.add(); // 建立文本
  384. textRef.contents = text;
  385. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  386. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  387. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  388. textRef.top = y + 15 / mm;
  389. textRef.left = x + 10 / mm;
  390. }
  391. // 遍历选择的物件标注尺寸
  392. if (docRef.selection.length > 0) {
  393. var mySelection = docRef.selection;
  394. for (var i = 0; i < mySelection.length; i++) {
  395. var s = mySelection[i]
  396. x = s.left; y = s.top
  397. var str = formatSize5(s.width) + "x" + formatSize5(s.height) + "mm";
  398. writeText(str)
  399. }
  400. }
  401. }
  402. // 统计物件信息
  403. function shapes_info() {
  404. var sr = app.activeDocument.selection;
  405. var str = "选择物件总数:" + sr.length + "\n";
  406. var text = "";
  407. for (var i = 0; i < sr.length; i++) {
  408. var s = sr[i];
  409. var size = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  410. if (i < 5) str += "第" + (i + 1) + "个尺寸: " + size + "\n";
  411. text += size + " ";
  412. }
  413. alert(str);
  414. // clear the current selection 清除当前选择
  415. app.activeDocument.selection = null;
  416. // add temp objects to hold text for copying 添加临时对象来保存用于复制的文本
  417. var tempObj = app.activeDocument.pathItems.add();
  418. var myText = app.activeDocument.textFrames.add();
  419. myText.contents = text;
  420. tempObj.selected = true;
  421. myText.selected = true;
  422. // copy the text 复制文本 // app.paste(); 粘贴
  423. app.copy();
  424. // remove the two temp object made for copying 删除用于复制的两个临时对象
  425. tempObj.remove();
  426. myText.remove();
  427. }
  428. // 文件名日期
  429. function filename_date() {
  430. // 获取当前时间
  431. function getdate() {
  432. var d = new Date(), month = '' + (d.getMonth() + 1),
  433. day = '' + d.getDate(), year = d.getFullYear();
  434. if (month.length < 2) month = '0' + month;
  435. if (day.length < 2) day = '0' + day;
  436. return [year, month, day].join('-');
  437. }
  438. // 获取 AI文档名称
  439. var docRef = activeDocument;
  440. var str = docRef.name;
  441. str = str + " " + getdate();
  442. var base = new Array();
  443. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  444. // 默认使用文档页面作为范围
  445. var pw = docRef.width; // 文档宽
  446. var ph = docRef.height; // 文档高
  447. var x = base[0]; // 画板左下角 x 坐标
  448. var y = - base[1]; // 画板左下角 y 坐标
  449. x = pw / 2 - x; // 转换x坐标: 画板中下x
  450. // 如果选择物件,使用物件范围
  451. if (app.activeDocument.selection.length > 0) {
  452. var bounds = new Array();
  453. bounds = get_Sel_Bounds();
  454. x = (bounds[0] + bounds[2]) / 2;
  455. y = bounds[3];
  456. }
  457. var myFont = textFonts.getByName("MicrosoftYaHei");
  458. var myFontSize = 8;
  459. function writeText() {
  460. var textRef = docRef.textFrames.add(); // 建立文本
  461. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  462. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  463. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  464. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  465. textRef.top = y + 7.4; // 画板底向上偏移
  466. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  467. textRef.selected = true;
  468. }
  469. writeText();
  470. }
  471. // 借咬口5mm
  472. function mark_5mm() {
  473. // 获取 AI文档名称
  474. var docRef = activeDocument;
  475. var str = docRef.name;
  476. str = "借咬口5mm"
  477. var base = new Array();
  478. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  479. // 默认使用文档页面作为范围
  480. var x = base[0]; // 画板左下角 x 坐标
  481. var y = - base[1]; // 画板左下角 y 坐标
  482. x = docRef.width / 2 - x; // 转换x坐标: 画板中下x
  483. // 如果选择物件,使用物件范围
  484. if (app.activeDocument.selection.length > 0) {
  485. var bounds = new Array();
  486. bounds = get_Sel_Bounds();
  487. x = (bounds[0] + bounds[2]) / 2;
  488. y = bounds[3];
  489. }
  490. var myFont = textFonts.getByName("MicrosoftYaHei");
  491. var myFontSize = 144;
  492. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  493. var cmykRed = new CMYKColor();
  494. cmykRed.cyan = 0;
  495. cmykRed.magenta = 100;
  496. cmykRed.yellow = 100;
  497. cmykRed.black = 0;
  498. function writeText() {
  499. var textRef = docRef.textFrames.add(); // 建立文本
  500. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  501. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  502. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  503. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  504. textRef.top = y - 144;
  505. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  506. textRef.selected = true;
  507. }
  508. writeText();
  509. }
  510. // 批量修改尺寸 使用蒙板几何边界尺寸
  511. function set_size(x, y) {
  512. var sr = app.activeDocument.selection;
  513. for (var i = 0; i < sr.length; i++) {
  514. var s = sr[i];
  515. var bound = NO_CLIP_BOUNDS(s);
  516. // var left = bound[0]; var top = bound[1];
  517. var width = bound[2] - bound[0];
  518. var height = bound[1] - bound[3];
  519. var scale_x = x / mm / width * 100;
  520. var scale_y = y / mm / height * 100;
  521. // X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
  522. s.resize(scale_x, scale_y, true, true, true, true, 100);
  523. }
  524. }
  525. // 批量增加减少尺寸
  526. function modify_size(x, y) {
  527. var sr = app.activeDocument.selection;
  528. for (var i = 0; i < sr.length; i++) {
  529. var s = sr[i];
  530. var bound = NO_CLIP_BOUNDS(s);
  531. var width = bound[2] - bound[0];
  532. var height = bound[1] - bound[3];
  533. var scale_x = (formatSize(width) / mm + x / mm) / width * 100;
  534. var scale_y = (formatSize(height) / mm + y / mm) / height * 100;
  535. s.resize(scale_x, scale_y);
  536. }
  537. }
  538. // 遍历选择的物件尺寸取整
  539. function size_to_integer() {
  540. var sr = app.activeDocument.selection;
  541. for (var i = 0; i < sr.length; i++) {
  542. var s = sr[i];
  543. var bound = NO_CLIP_BOUNDS(s);
  544. var width = bound[2] - bound[0];
  545. var height = bound[1] - bound[3];
  546. var scale_x = formatSize(width) / mm / width * 100;
  547. var scale_y = formatSize(height) / mm / height * 100;
  548. s.resize(scale_x, scale_y);
  549. }
  550. }
  551. // 批量物件旋转角度
  552. function shapes_rotate(angle) {
  553. var sr = app.activeDocument.selection;
  554. for (var i = 0; i < sr.length; i++)
  555. sr[i].rotate(angle);
  556. }
  557. // 物件轮廓边界
  558. function size_by_controlBounds() {
  559. var docRef = activeDocument;
  560. // 判断选择物件2个以上
  561. if (docRef.selection.length > 1) {
  562. // 定义选择物件
  563. mySelection = docRef.selection;
  564. // 最上层物件为替换源
  565. var sourceObj = docRef.selection[0];
  566. // 定义数组保存选择物件controlBounds
  567. var BoundsArray = new Array();
  568. for (var i = 0; i < mySelection.length; i++) {
  569. // PageItem.position 获得物件群组左上角坐标
  570. var sel_Bounds = mySelection[i].controlBounds;
  571. BoundsArray.push(sel_Bounds);
  572. }
  573. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  574. var newGroup = sourceObj.parent.groupItems.add();
  575. for (var i = 1; i < BoundsArray.length; i++) {
  576. var width = BoundsArray[i][2] - BoundsArray[i][0];
  577. var height = BoundsArray[i][1] - BoundsArray[i][3];
  578. sourceObj.width = width;
  579. sourceObj.height = height;
  580. // 移动源文件到目的物件左上角对齐
  581. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  582. sourceObj.position = sel_xy;
  583. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  584. }
  585. sourceObj.remove();
  586. }
  587. }
  588. // 物件几何边界尺寸大小
  589. function size_by_width_height() {
  590. var doc = activeDocument;
  591. // 判断选择物件2个以上
  592. if (doc.selection.length > 1) {
  593. // 定义选择物件
  594. src = doc.selection;
  595. var taget = doc.selection[0];
  596. // PageItem.position 获得物件群组左上角坐标
  597. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  598. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  599. var newGroup = taget.parent.groupItems.add();
  600. for (var i = 1; i < src.length; i++) {
  601. var bound = NO_CLIP_BOUNDS(src[i]);
  602. var left = bound[0]; var top = bound[1];
  603. var width = bound[2] - bound[0];
  604. var height = bound[1] - bound[3];
  605. var sel_xy = new Array(left, top);
  606. taget.width = width;
  607. taget.height = height;
  608. taget.position = sel_xy;
  609. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  610. }
  611. taget.remove();
  612. } else {
  613. make_rectangle();
  614. }
  615. }
  616. function draw_rect(x, y, w, h) {
  617. // 创建一个新的矩形对象
  618. var rect = app.activeDocument.pathItems.rectangle(0, 0, w / mm, h / mm);
  619. // // 设置矩形的位置
  620. rect.position = [x / mm, y / mm];
  621. var M100 = new CMYKColor();
  622. M100.magenta = 100;
  623. // 设置矩形的填充颜色 轮廓颜色和宽度
  624. // rect.fillColor = M100;
  625. rect.filled = false; // 不填充颜色
  626. rect.strokeColor = M100;
  627. rect.strokeWidth = 0.3 / mm;
  628. rect.stroked = true;
  629. // 将矩形添加到文档中
  630. app.activeDocument.pathItems.add(rect);
  631. }
  632. // 从剪贴板数字建立矩形
  633. function make_rectangle() {
  634. // 从剪贴版获得字符串
  635. app.activeDocument.selection = null;
  636. app.paste();
  637. var sr = app.activeDocument.selection;
  638. var str = sr[0].contents;
  639. for (var i = 0; i != sr.length; i++)
  640. sr[i].remove();
  641. // 从字符串中提取储存数字对
  642. // var str = '100x100mm 200x200mm 300x300mm';
  643. var regex = /(\d+)/g;
  644. var match;
  645. var numbers = [];
  646. while ((match = regex.exec(str)) !== null) {
  647. var number = parseInt(match[1]);
  648. numbers.push(number);
  649. }
  650. // 两个数字组成一对尺寸,每个尺寸绘制一个矩形
  651. var sum = 0;
  652. for (var i = 0; i < numbers.length / 2; i++) {
  653. draw_rect(sum, 0, numbers[2 * i], numbers[2 * i + 1]);
  654. sum += numbers[2 * i] + 30;
  655. }
  656. }
  657. // 拼版左上对齐
  658. function replace_align_position() {
  659. var docRef = activeDocument;
  660. // 判断选择物件2个以上
  661. if (docRef.selection.length > 1) {
  662. // 定义选择物件
  663. mySelection = docRef.selection;
  664. // 最上层物件为替换源
  665. var sourceObj = docRef.selection[0];
  666. // 定义数组用来保存选择物件的左上角坐标
  667. var alterObjectArray = new Array();
  668. for (var i = 0; i < mySelection.length; i++) {
  669. // PageItem.position 获得物件群组左上角坐标
  670. var sel_xy = mySelection[i].position
  671. alterObjectArray.push(sel_xy);
  672. }
  673. // 删除用来定位的下层物件
  674. for (var i = 1; i < mySelection.length; i++) {
  675. mySelection[i].remove();
  676. }
  677. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  678. var newGroup = sourceObj.parent.groupItems.add();
  679. for (var i = 1; i < alterObjectArray.length; i++) {
  680. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  681. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  682. }
  683. sourceObj.remove();
  684. }
  685. }
  686. // 拼版中心对齐
  687. function replace_align_center(objremove) {
  688. var docRef = activeDocument;
  689. // 判断选择物件2个以上
  690. if (docRef.selection.length > 1) {
  691. // 定义选择物件
  692. mySelection = docRef.selection;
  693. // 最上层物件为替换源
  694. var sourceObj = docRef.selection[0];
  695. // 定义数组用来保存选择物件的中心坐标 # 计算中心坐标
  696. // x = (left + right) / 2 // y = (top + bottom) / 2
  697. var alterObjectArray = new Array();
  698. for (var i = 0; i < mySelection.length; i++) {
  699. var bound = NO_CLIP_BOUNDS(mySelection[i]);
  700. var sel_xy = new Array((bound[0] + bound[2]) / 2, (bound[1] + bound[3]) / 2);
  701. alterObjectArray.push(sel_xy);
  702. }
  703. // 删除用来定位的下层物件
  704. for (var i = 1; (i < mySelection.length) && objremove; i++) {
  705. mySelection[i].remove();
  706. }
  707. // 获得替换源物件中心坐标
  708. // var bound = NO_CLIP_BOUNDS(sourceObj);
  709. // var src_xy = new Array((bound[0] + bound[2]) / 2, (bound[1] + bound[3]) / 2);
  710. var src_xy = alterObjectArray[0];
  711. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  712. var newGroup = sourceObj.parent.groupItems.add();
  713. for (var i = 1; i < alterObjectArray.length; i++) {
  714. var newobj = sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  715. var move_xy = new Array(alterObjectArray[i][0] - src_xy[0], alterObjectArray[i][1] - src_xy[1]);
  716. newobj.translate(move_xy[0], move_xy[1]);
  717. }
  718. sourceObj.remove();
  719. }
  720. }
  721. // 读取加载jsxbin文件,传递给AI软件
  722. function load_jsxbin(file) {
  723. var file = new File(file);
  724. if (file.open('r')) {
  725. var fileContent = file.read();
  726. file.close();
  727. buildMsg(fileContent);
  728. } else {
  729. alert('文件打开失败: ' + file);
  730. }
  731. }
  732. //========== 以下插件引用使用互联网各位大大的插件 =================//
  733. // 标注尺寸增强版 V2.1
  734. function make_size_plus() { load_jsxbin(IconsFolder + "/makesize.dat"); }
  735. // 自动群组
  736. function auto_group() { load_jsxbin(IconsFolder + "/autogroup.dat"); }
  737. // 调整尺寸
  738. function ResizeToSize() { load_jsxbin(IconsFolder + "/resize.dat"); }
  739. // 打包链接图片
  740. function img_pack_links() { load_jsxbin(IconsFolder + "/packlinks.dat"); }
  741. // 重新加载aia脚本文件
  742. function reload_aia() { load_jsxbin(IconsFolder + "/reloadaia.dat"); }
  743. // 群组物件几何边界 // 左 上 宽 高
  744. // var bound = NO_CLIP_BOUNDS(s);
  745. // var left = bound[0]; var top = bound[1];
  746. // var width = bound[2] - bound[0]; var height = bound[1] - bound[3];
  747. function NO_CLIP_BOUNDS(the_obj) {
  748. var NO_CLIP_OBJECTS_AND_MASKS = new Array();
  749. GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj);
  750. var g_left = [];
  751. var g_top = [];
  752. var g_right = [];
  753. var g_bottom = [];
  754. for (var i = 0; i < NO_CLIP_OBJECTS_AND_MASKS.length; i += 1) {
  755. g_left[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[0];
  756. g_top[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[1];
  757. g_right[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[2];
  758. g_bottom[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[3];
  759. }
  760. var g_L = MIN_IN_ARRAY(g_left);
  761. var g_T = MAX_IN_ARRAY(g_top);
  762. var g_R = MAX_IN_ARRAY(g_right);
  763. var g_B = MIN_IN_ARRAY(g_bottom);
  764. return [g_L, g_T, g_R, g_B];
  765. function GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj) {
  766. if (IS_CLIP(the_obj)) {
  767. NO_CLIP_OBJECTS_AND_MASKS.push(the_obj.pageItems[0]);
  768. return;
  769. }
  770. if (the_obj.constructor.name == "GroupItem") {
  771. try {
  772. var N_sub_obj = the_obj.pageItems.length;
  773. for (var i = 0; i < N_sub_obj; i += 1) {
  774. GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj.pageItems[i]);
  775. }
  776. } catch (error) {
  777. }
  778. return;
  779. }
  780. NO_CLIP_OBJECTS_AND_MASKS.push(the_obj);
  781. return;
  782. }
  783. }
  784. function IS_CLIP(the_obj) {
  785. try {
  786. if (the_obj.constructor.name == "GroupItem") {
  787. if (the_obj.clipped) {
  788. return true;
  789. }
  790. }
  791. } catch (error) {
  792. }
  793. return false;
  794. }
  795. function MAX_IN_ARRAY(the_array) {
  796. var MAX = the_array[0];
  797. for (var i = 0; i < the_array.length; i += 1) {
  798. if (the_array[i] > MAX) {
  799. MAX = the_array[i]
  800. }
  801. }
  802. return MAX;
  803. }
  804. function MIN_IN_ARRAY(the_array) {
  805. var MIN = the_array[0];
  806. for (var i = 0; i < the_array.length; i += 1) {
  807. if (the_array[i] < MIN) {
  808. MIN = the_array[i]
  809. }
  810. }
  811. return MIN;
  812. }