Illustrator腳本開發(fā)CY-標(biāo)注尺寸
發(fā)布時(shí)間:2024-11-08 10:35 [ 我要自學(xué)網(wǎng)原創(chuàng) ] 發(fā)布人: 快樂小女 閱讀: 121

var five = $.getenv("bz_five") + "" !== 'null' ? ($.getenv("bz_five") === 'false' ? false : true) : true


var count = $.getenv("bz_count") ? $.getenv("bz_count") : 1

function main(_flagMargin, _flagFontSize, _arrowSize, _arrowWidth, isTop, isBottom, isLeft, isRight) {
    var doc = app.activeDocument; // 當(dāng)前文檔

    var theSelect = doc.selection; // 選中的內(nèi)容

    var _datumPointUnit = 2.834645 // 基準(zhǔn)單位

    var flagMargin = _flagMargin * _datumPointUnit;// 標(biāo)尺距離物體邊沿寬度

    var flagFontSize = _flagFontSize * _datumPointUnit; // 標(biāo)尺字體大小

    var arrowSize = _arrowSize * _datumPointUnit // 箭頭尺寸

    doc.selection = null; // 取消選擇所有內(nèi)容

    var newSpot
    var newColor
    try {
        newSpot = app.activeDocument.spots.getByName("Dimension Line")
    } catch (error) {
        // 創(chuàng)建專色
        newSpot = app.activeDocument.spots.add();
        newColor = new CMYKColor();
        newColor.cyan = 100;
        newColor.magenta = 0;
        newColor.yellow = 0;
        newColor.black = 0;

        newSpot.name = "Dimension Line";
        newSpot.colorType = ColorModel.SPOT;
        newSpot.color = newColor;
    }

    function createColor() {
        var newSpotColor = new SpotColor();
        newSpotColor.spot = newSpot;
        return newSpotColor
    }

    doc.defaultFilled = false; // 默認(rèn)填充: 無(wú)
    doc.defaultStroked = true; // 默認(rèn)描邊: 打開
    doc.defaultStrokeWidth = _arrowWidth * _datumPointUnit; // 描邊0.3
    doc.defaultStrokeColor = createColor();

    for (var index = 0; index < theSelect.length; index++) {
        var _this = theSelect[index];

        // -------獲取元素信息
        var strokeWidth = 0 // 元素描邊
        if (_this == '[PathItem ]' && _this.stroked) {
            strokeWidth = _this.strokeWidth
        }

        var left = _this.left; // 元素距離左邊的距離
        var top = _this.top;   // 元素距離頂部邊的距離
        var width = _this.width + strokeWidth; // 元素寬度
        var height = _this.height + strokeWidth; // 元素高度

        // -------獲取元素信息

        var flagGroup = doc.groupItems.add();// 創(chuàng)建一個(gè)標(biāo)尺組
        if (isLeft) {
            // --------------------左 - 畫高度標(biāo)尺--------------------

            // 畫線條
            var line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath([
                [left - flagMargin, top],
                [left - flagMargin, top - height]
            ]);

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left - flagMargin - flagMargin / 2, top],
                    [left - flagMargin + flagMargin / 2, top]
                ]
            );

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left - flagMargin - flagMargin / 2, top - height],
                    [left - flagMargin + flagMargin / 2, top - height]
                ]
            );
            // 畫線條
            // 畫箭頭
            var arrow1 = flagGroup.pathItems.add(); //畫箭頭上
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left - flagMargin, top],
                [left - flagMargin - arrowSize / 2, top - arrowSize],
                [left - flagMargin + arrowSize / 2, top - arrowSize]]
            );
            arrow1.closed = true

            arrow1 = flagGroup.pathItems.add(); //畫箭頭下
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left - flagMargin, top - height],
                [left - flagMargin - arrowSize / 2, top - height + arrowSize],
                [left - flagMargin + arrowSize / 2, top - height + arrowSize]]
            );
            arrow1.closed = true
            // 畫箭頭

            // 尺寸文字
            var text1 = flagGroup.textFrames.add()
            text1.contents = mathNum(height / _datumPointUnit, five, count) + " mm"
            text1.textRange.characterAttributes.size = flagFontSize;

            for (var i = 0; i < app.textFonts.length; i++) {
                var e = app.textFonts[i];
                if (e.family === "Adobe 宋體 Std L") {
                    text1.textRange.characterAttributes.textFont = app.textFonts[i]
                }
            }

            text1.textRange.fillColor = createColor();
            text1.textRange.characterAttributes.overprintFill = true
            text1.left = left - flagMargin - text1.width - flagMargin / 2;
            text1.top = top - height / 2 + text1.height / 2;
            // ---------------------左 - 畫高度標(biāo)尺--------------------
        }

        if (isRight) {
            // --------------------右 - 畫高度標(biāo)尺--------------------
            // 畫線條
            var line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath([
                [left + width + flagMargin, top],
                [left + width + flagMargin, top - height]
            ]);

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left + width + flagMargin - flagMargin / 2, top],
                    [left + width + flagMargin + flagMargin / 2, top]
                ]
            );

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left + width + flagMargin - flagMargin / 2, top - height],
                    [left + width + flagMargin + flagMargin / 2, top - height]
                ]
            );
            // 畫線條
            // 畫箭頭
            var arrow1 = flagGroup.pathItems.add(); //畫箭頭上
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left + width + flagMargin, top],
                [left + width + flagMargin - arrowSize / 2, top - arrowSize],
                [left + width + flagMargin + arrowSize / 2, top - arrowSize]]
            );
            arrow1.closed = true

            arrow1 = flagGroup.pathItems.add(); //畫箭頭下
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left + width + flagMargin, top - height],
                [left + width + flagMargin - arrowSize / 2, top - height + arrowSize],
                [left + width + flagMargin + arrowSize / 2, top - height + arrowSize]]
            );
            arrow1.closed = true
            // 畫箭頭

            // 尺寸文字
            var text1 = flagGroup.textFrames.add()
            text1.contents = mathNum(height / _datumPointUnit, five, count) + " mm"
            text1.textRange.characterAttributes.size = flagFontSize;

            for (var i = 0; i < app.textFonts.length; i++) {
                var e = app.textFonts[i];
                if (e.family === "Adobe 宋體 Std L") {
                    text1.textRange.characterAttributes.textFont = app.textFonts[i]
                }
            }

            text1.textRange.fillColor = createColor();
            text1.textRange.characterAttributes.overprintFill = true
            text1.left = left + width + flagMargin + flagMargin / 2;
            text1.top = top - height / 2 + text1.height / 2;
            // ---------------------左 - 畫高度標(biāo)尺--------------------
        }

        if (isBottom) {
            // ---------------------下 - 畫寬度標(biāo)尺--------------------
            // 畫線條
            var line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath([
                [left, top - height - flagMargin],
                [left + width, top - height - flagMargin]
            ]);

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left, top - height - flagMargin / 2],
                    [left, top - height - flagMargin - flagMargin / 2]
                ]
            );

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left + width, top - height - flagMargin / 2],
                    [left + width, top - height - flagMargin - flagMargin / 2]
                ]
            );
            // 畫線條

            // 畫箭頭
            var arrow1 = flagGroup.pathItems.add(); //畫箭頭左
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left, top - height - flagMargin],
                [left + arrowSize, top - height - flagMargin + arrowSize / 2],
                [left + arrowSize, top - height - flagMargin - arrowSize / 2]]
            );
            arrow1.closed = true

            var arrow1 = flagGroup.pathItems.add(); //畫箭頭右
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left + width, top - height - flagMargin],
                [left + width - arrowSize, top - height - flagMargin + arrowSize / 2],
                [left + width - arrowSize, top - height - flagMargin - arrowSize / 2]]
            );
            arrow1.closed = true
            // 畫箭頭

            // 尺寸文字
            var text1 = flagGroup.textFrames.add()
            text1.contents = mathNum(width / _datumPointUnit, five, count) + " mm"
            text1.textRange.characterAttributes.size = flagFontSize;
            text1.textRange.fillColor = createColor();
            text1.textRange.characterAttributes.overprintFill = true
            text1.left = left + width / 2 - text1.width / 2;
            text1.top = top - height - flagMargin - text1.height / 2;

            for (var i = 0; i < app.textFonts.length; i++) {
                var e = app.textFonts[i];
                if (e.family === "Adobe 宋體 Std L") {
                    text1.textRange.characterAttributes.textFont = app.textFonts[i]
                }
            }
            // ---------------------下 - 畫寬度標(biāo)尺--------------------
        }

        if (isTop) {
            // ---------------------上 - 畫寬度標(biāo)尺--------------------
            // 畫線條
            var line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath([
                [left, top + flagMargin],
                [left + width, top + flagMargin]
            ]);

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left, top + flagMargin / 2],
                    [left, top + flagMargin + flagMargin / 2]
                ]
            );

            line1 = flagGroup.pathItems.add();
            line1.strokeOverprint = true
            line1.setEntirePath(
                [
                    [left + width, top + flagMargin / 2],
                    [left + width, top + flagMargin + flagMargin / 2]
                ]
            );
            // 畫線條

            // 畫箭頭
            var arrow1 = flagGroup.pathItems.add(); //畫箭頭左
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left, top + flagMargin],
                [left + arrowSize, top + flagMargin + arrowSize / 2],
                [left + arrowSize, top + flagMargin - arrowSize / 2]]
            );
            arrow1.closed = true

            var arrow1 = flagGroup.pathItems.add(); //畫箭頭右
            arrow1.stroked = false;
            arrow1.fillColor = createColor();
            arrow1.fillOverprint = true
            arrow1.setEntirePath([
                [left + width, top + flagMargin],
                [left + width - arrowSize, top + flagMargin + arrowSize / 2],
                [left + width - arrowSize, top + flagMargin - arrowSize / 2]]
            );
            arrow1.closed = true
            // 畫箭頭

            // 尺寸文字
            var text1 = flagGroup.textFrames.add()
            text1.contents = mathNum(width / _datumPointUnit, five, count) + " mm"
            text1.textRange.characterAttributes.size = flagFontSize;
            text1.textRange.fillColor = createColor();
            text1.textRange.characterAttributes.overprintFill = true
            text1.left = left + width / 2 - text1.width / 2;
            text1.top = top + flagMargin + text1.height + text1.height / 2;

            for (var i = 0; i < app.textFonts.length; i++) {
                var e = app.textFonts[i];
                if (e.family === "Adobe 宋體 Std L") {
                    text1.textRange.characterAttributes.textFont = app.textFonts[i]
                }
            }
            // ---------------------下 - 畫寬度標(biāo)尺--------------------
        }

    }
}

// 入口函數(shù)
function init() {
    // 在屏幕左上角附近創(chuàng)建一個(gè)空對(duì)話框窗口
    dlg = new Window('dialog', '尺寸標(biāo)注@冰鎮(zhèn)果凍');
    dlg.center()
    dlg.frameLocation = [400, 400];
    dlg.msgPnl = dlg.add('panel', undefined, '配置');
    dlg.msgPnl.alignChildren = "center";

    dlg.positionPnl = dlg.add('panel', undefined, '數(shù)值/位置');
    dlg.positionPnl.alignChildren = "center";


    dlg.msgPnl._margin = dlg.msgPnl.add('group');
    dlg.msgPnl._fontSize = dlg.msgPnl.add('group');
    dlg.msgPnl._alSize = dlg.msgPnl.add('group');
    dlg.msgPnl._alWidth = dlg.msgPnl.add('group');
    dlg.msgPnl._alNum = dlg.msgPnl.add('group');

    with (dlg.msgPnl) {
        _margin.st = _margin.add('statictext', undefined, '標(biāo)注與對(duì)象的距離(mm)');
        _margin.et = _margin.add('edittext', undefined, $.getenv("bz_margin") ? $.getenv("bz_margin") : "1");
        _margin.et.preferredSize = [60, 30];

        _fontSize.st = _fontSize.add('statictext', undefined, '標(biāo)注文字字體大小(mm)');
        _fontSize.et = _fontSize.add('edittext', undefined, $.getenv("bz_fontSize") ? $.getenv("bz_fontSize") : "2");
        _fontSize.et.preferredSize = [60, 30];

        _alSize.st = _alSize.add('statictext', undefined, '標(biāo)注箭頭大小尺寸(mm)');
        _alSize.et = _alSize.add('edittext', undefined, $.getenv("bz_alSize") ? $.getenv("bz_alSize") : "0.1");
        _alSize.et.preferredSize = [60, 30];

        _alWidth.st = _alWidth.add('statictext', undefined, '標(biāo)注線條粗細(xì)尺寸(mm)');
        _alWidth.et = _alWidth.add('edittext', undefined, $.getenv("bz_alWidth") ? $.getenv("bz_alWidth") : "0.1");
        _alWidth.et.preferredSize = [60, 30];

        _alNum.st = _alNum.add('statictext', undefined, '標(biāo)注數(shù)值小數(shù)個(gè)數(shù)(cs)');
        _alNum.et = _alNum.add('edittext', undefined, count);
        _alNum.et.preferredSize = [60, 30];
    }

    dlg.positionPnl._direction = dlg.positionPnl.add('group');
    with (dlg.positionPnl) {
        _five = _direction.add("checkbox", undefined, "四舍五入");
        _five.value = five

        _top = _direction.add("checkbox", undefined, "上");
        _top.value = $.getenv("bz_top") + "" !== 'null' ? ($.getenv("bz_top") === 'false' ? false : true) : false

        _bottom = _direction.add("checkbox", undefined, "下");
        _bottom.value = $.getenv("bz_bottom") + "" !== 'null' ? ($.getenv("bz_bottom") === 'false' ? false : true) : false

        _left = _direction.add("checkbox", undefined, "左");
        _left.value = $.getenv("bz_left") + "" !== 'null' ? ($.getenv("bz_left") === 'false' ? false : true) : true

        _right = _direction.add("checkbox", undefined, "右");
        _right.value = $.getenv("bz_right") + "" !== 'null' ? ($.getenv("bz_right") === 'false' ? false : true) : false
    }

    dlg.btnPnl = dlg.add('panel', undefined, '操作');
    dlg.btnPnl.orientation = "row";
    dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, '標(biāo)注選中對(duì)象', { name: 'ok' });
    dlg.btnPnl.build2Btn = dlg.btnPnl.add('button', undefined, '標(biāo)注橫向距離', { name: '_width' });
    dlg.btnPnl.build3Btn = dlg.btnPnl.add('button', undefined, '標(biāo)注縱向距離', { name: '_height' });


    dlg.btnPnl.ok.onClick = function () {
        count = Number(dlg.msgPnl._alNum.et.text)
        five = _five.value
        var _margin = Number(dlg.msgPnl._margin.et.text)
        var _fontSize = Number(dlg.msgPnl._fontSize.et.text)
        var _alSize = Number(dlg.msgPnl._alSize.et.text)
        var _alWidth = Number(dlg.msgPnl._alWidth.et.text)

        $.setenv("bz_count", count)
        $.setenv("bz_five", five)
        $.setenv("bz_margin", _margin)
        $.setenv("bz_fontSize", _fontSize)
        $.setenv("bz_alSize", _alSize)
        $.setenv("bz_alWidth", _alWidth)

        $.setenv("bz_left", _left.value)
        $.setenv("bz_top", _top.value)
        $.setenv("bz_bottom", _bottom.value)
        $.setenv("bz_right", _right.value)

        main(_margin, _fontSize, _alSize, _alWidth, _top.value, _bottom.value, _left.value, _right.value)
        dlg.fivelose()
    }

    dlg.btnPnl.build2Btn.onClick = function () {
        count = Number(dlg.msgPnl._alNum.et.text)
        five = _five.value
        var _margin = Number(dlg.msgPnl._margin.et.text)
        var _fontSize = Number(dlg.msgPnl._fontSize.et.text)
        var _alSize = Number(dlg.msgPnl._alSize.et.text)
        var _alWidth = Number(dlg.msgPnl._alWidth.et.text)

        $.setenv("bz_count", count)
        $.setenv("bz_five", five)
        $.setenv("bz_margin", _margin)
        $.setenv("bz_fontSize", _fontSize)
        $.setenv("bz_alSize", _alSize)
        $.setenv("bz_alWidth", _alWidth)
        main2("標(biāo)注橫向距離", _margin, _fontSize, _alSize, _alWidth)
        dlg.close()
    }

    dlg.btnPnl.build3Btn.onClick = function () {
        count = Number(dlg.msgPnl._alNum.et.text)
        five = _five.value
        var _margin = Number(dlg.msgPnl._margin.et.text)
        var _fontSize = Number(dlg.msgPnl._fontSize.et.text)
        var _alSize = Number(dlg.msgPnl._alSize.et.text)
        var _alWidth = Number(dlg.msgPnl._alWidth.et.text)

        $.setenv("bz_count", count)
        $.setenv("bz_five", five)
        $.setenv("bz_margin", _margin)
        $.setenv("bz_fontSize", _fontSize)
        $.setenv("bz_alSize", _alSize)
        $.setenv("bz_alWidth", _alWidth)
        main2("標(biāo)注縱向距離", _margin, _fontSize, _alSize, _alWidth)
        dlg.close()
    }
    dlg.show()
}

init()

// --------下面是增強(qiáng)代碼--------
function elDistanceLabeling(el1, el2, type, flagMargin, flagFontSize, arrowSize, arrowWidth) {
    var config = getConfig(flagMargin, flagFontSize, arrowSize, arrowWidth) // 獲取配置

    // 獲取兩個(gè)元素的位置信息
    var el1_leftX = el1.geometricBounds[0]
    var el1_leftY = el1.geometricBounds[1]
    var el1_rightX = el1.geometricBounds[2]
    var el1_rightY = el1.geometricBounds[3]

    var el2_leftX = el2.geometricBounds[0]
    var el2_leftY = el2.geometricBounds[1]
    var el2_rightX = el2.geometricBounds[2]
    var el2_rightY = el2.geometricBounds[3]
    // 獲取兩個(gè)元素的位置信息

    // 中心點(diǎn)
    var align = (el2_rightY - el1_leftY) / 2 + el1_leftY
    var center = (el2_rightX - el1_leftX) / 2 + el1_leftX

    // 獲得組
    var _group = getGroupAndLayer()

    if (type === "標(biāo)注橫向距離") {
        var margin = (el2_leftX - el1_rightX)
        widthFlag(_group, center, align, margin, el1_leftX, el1_leftY, el1_rightX, el1_rightY, el2_leftX, el2_leftY, el2_rightX, el2_rightY, config)
    }

    if (type === "標(biāo)注縱向距離") {
        var margin = el2_leftY - el1_rightY
        heightFlag(_group, center, align, margin, el1_leftX, el1_leftY, el1_rightX, el1_rightY, el2_leftX, el2_leftY, el2_rightX, el2_rightY, config)
    }


}

function main2(type, flagMargin, flagFontSize, arrowSize, arrowWidth) {
    var theSelect = activeDocument.selection;
    if (theSelect.length < 2) {
        alert('選擇的物體數(shù)量小于2, 無(wú)法計(jì)算', "提示")
        return
    }

    // 排序
    if (type === "標(biāo)注橫向距離") {
        my_sort(theSelect)
    } else {
        my_sort2(theSelect)

    }
    for (var i = 0; i < theSelect.length - 1; i++) {
        elDistanceLabeling(theSelect[i], theSelect[i + 1], type, flagMargin, flagFontSize, arrowSize, arrowWidth)
    }
}

function getConfig(flagMargin, flagFontSize, arrowSize, arrowWidth) {
    var newSpot
    var newColor
    try {
        newSpot = app.activeDocument.spots.getByName("Dimension Line")
    } catch (error) {
        // 創(chuàng)建專色
        newSpot = app.activeDocument.spots.add();
        newColor = new CMYKColor();
        newColor.cyan = 100;
        newColor.magenta = 0;
        newColor.yellow = 0;
        newColor.black = 0;

        newSpot.name = "Dimension Line";
        newSpot.colorType = ColorModel.SPOT;
        newSpot.color = newColor;
    }
    var newSpotColor = new SpotColor();
    newSpotColor.spot = newSpot;

    return {
        flagMargin: toMM(flagMargin),
        flagFontSize: toMM(flagFontSize),
        arrowSize: toMM(arrowSize),
        arrowWidth: toMM(arrowWidth),
        spot: newSpotColor
    }

}

function getGroupAndLayer() {
    var name = "@PDG_標(biāo)注圖層"
    var name2 = "@PDG_標(biāo)注圖層_組"
    var doc = app.activeDocument
    for (var i = 0; i < doc.layers.length; i++) {
        var lay = doc.layers[i];
        if (lay.name === name) {
            for (var j = 0; j < doc.groupItems.length; j++) {  // 查找是否有路徑組
                var group = doc.groupItems[j];
                if (group.name === name2)
                    return group
            }

            // 有圖層沒組
            var _my_group = lay.groupItems.add()
            _my_group.name = name2
            return _my_group
        }
    }

    var cutlayer = activeDocument.layers.add();
    cutlayer.name = name; //設(shè)置圖層名稱
    var my_group = cutlayer.groupItems.add();
    my_group.name = name2

    return my_group
}

function widthFlag(_group, center, align, margin, el1_leftX, el1_leftY, el1_rightX, el1_rightY, el2_leftX, el2_leftY, el2_rightX, el2_rightY, config) {
    var _datumPointUnit = 2.834646
    // ---------------------畫寬度標(biāo)尺--------------------
    // 畫線條
    var line1 = _group.pathItems.add();
    line1.strokeColor = config.spot
    line1.strokeOverprint = true
    line1.filled = false
    line1.setEntirePath([
        [el1_rightX, align],
        [el1_rightX + margin, align]
    ]);

    line1 = _group.pathItems.add();
    line1.strokeColor = config.spot
    line1.strokeOverprint = true
    line1.filled = false
    line1.setEntirePath(
        [
            [el1_rightX, align - config.flagMargin / 2],
            [el1_rightX, align + config.flagMargin / 2]
        ]
    );

    line1 = _group.pathItems.add();
    line1.strokeColor = config.spot
    line1.strokeOverprint = true
    line1.filled = false
    line1.setEntirePath(
        [
            [el2_leftX, align - config.flagMargin / 2],
            [el2_leftX, align + config.flagMargin / 2]
        ]
    );
    // 畫線條
    var arrow1 = _group.pathItems.add(); //畫箭頭左
    arrow1.stroked = false;
    arrow1.fillColor = config.spot;
    arrow1.fillOverprint = true
    arrow1.setEntirePath([
        [el1_rightX, align],
        [el1_rightX + config.arrowSize, align + config.arrowSize / 2],
        [el1_rightX + config.arrowSize, align - config.arrowSize / 2]]
    );
    arrow1.closed = true

    var arrow1 = _group.pathItems.add(); //畫箭頭右
    arrow1.stroked = false;
    arrow1.fillColor = config.spot;
    arrow1.fillOverprint = true
    arrow1.setEntirePath([
        [el2_leftX, align],
        [el2_leftX - config.arrowSize, align + config.arrowSize / 2],
        [el2_leftX - config.arrowSize, align - config.arrowSize / 2]]
    );
    arrow1.closed = true

    // 尺寸文字
    var text1 = _group.textFrames.add()
    text1.contents = mathNum(margin / _datumPointUnit, five, count) + " mm"
    text1.textRange.characterAttributes.size = config.flagFontSize;
    text1.textRange.fillColor = config.spot;
    text1.textRange.characterAttributes.overprintFill = true
    text1.left = el1_rightX + margin / 2 - text1.width / 2;
    text1.top = align + text1.height;

    for (var i = 0; i < app.textFonts.length; i++) {
        var e = app.textFonts[i];
        if (e.family === "Adobe 宋體 Std L") {
            text1.textRange.characterAttributes.textFont = app.textFonts[i]
        }
    }
}

function heightFlag(_group, center, align, margin, el1_leftX, el1_leftY, el1_rightX, el1_rightY, el2_leftX, el2_leftY, el2_rightX, el2_rightY, config) {
    var _datumPointUnit = 2.834646
    // ---------------------畫高度標(biāo)尺--------------------
    // 畫線條
    var line1 = _group.pathItems.add();
    line1.strokeColor = config.spot
    line1.strokeOverprint = true
    line1.filled = false
    line1.setEntirePath([
        [center, el1_rightY],
        [center, el2_leftY]
    ]);

    var line1 = _group.pathItems.add();
    line1.strokeColor = config.spot
    line1.strokeOverprint = true
    line1.filled = false
    line1.setEntirePath([
        [center - config.flagMargin / 2, el2_leftY],
        [center + config.flagMargin / 2, el2_leftY]
    ]);

    var line1 = _group.pathItems.add();
    line1.strokeColor = config.spot
    line1.strokeOverprint = true
    line1.filled = false
    line1.setEntirePath([
        [center - config.flagMargin / 2, el1_rightY],
        [center + config.flagMargin / 2, el1_rightY]
    ]);
    var arrow1 = _group.pathItems.add(); //畫箭頭上
    arrow1.stroked = false;
    arrow1.fillColor = config.spot;
    arrow1.fillOverprint = true
    arrow1.setEntirePath([
        [center, el1_rightY],
        [center - config.arrowSize / 2, el1_rightY - config.arrowSize],
        [center + config.arrowSize / 2, el1_rightY - config.arrowSize]]
    );
    arrow1.closed = true

    var arrow1 = _group.pathItems.add(); //畫箭頭上
    arrow1.stroked = false;
    arrow1.fillColor = config.spot;
    arrow1.fillOverprint = true
    arrow1.setEntirePath([
        [center, el2_leftY],
        [center - config.arrowSize / 2, el2_leftY + config.arrowSize],
        [center + config.arrowSize / 2, el2_leftY + config.arrowSize]]
    );
    arrow1.closed = true

    // 尺寸文字
    var text1 = _group.textFrames.add()
    text1.contents = mathNum(margin / _datumPointUnit, five, count) + " mm"
    text1.textRange.characterAttributes.size = config.flagFontSize;
    text1.textRange.fillColor = config.spot;
    text1.textRange.characterAttributes.overprintFill = true
    text1.left = center + config.flagMargin / 2;
    text1.top = align + text1.height / 2;

    for (var i = 0; i < app.textFonts.length; i++) {
        var e = app.textFonts[i];
        if (e.family === "Adobe 宋體 Std L") {
            text1.textRange.characterAttributes.textFont = app.textFonts[i]
        }
    }

}

function my_sort(arr) {
    for (var i = 0; i < arr.length - 1; ++i) {
        // 假設(shè)最小的值是當(dāng)前的下標(biāo)
        var indexMin = i;
        //遍歷剩余長(zhǎng)度找到最小下標(biāo)
        for (var j = i; j < arr.length; ++j) {
            if (arr[j].geometricBounds[0] < arr[indexMin].geometricBounds[0] || arr[j].geometricBounds[2] < arr[indexMin].geometricBounds[2]) {
                indexMin = j;
            }
        }
        if (indexMin !== i) {
            //交換當(dāng)前下標(biāo)i與最小下標(biāo)的
            var temp = arr[i];
            arr[i] = arr[indexMin];
            arr[indexMin] = temp;
        }
    }

    return arr
};

function my_sort2(arr) {
    for (var i = 0; i < arr.length - 1; ++i) {
        // 假設(shè)最小的值是當(dāng)前的下標(biāo)
        var indexMin = i;
        //遍歷剩余長(zhǎng)度找到最小下標(biāo)
        for (var j = i; j < arr.length; ++j) {
            if (arr[j].geometricBounds[1] > arr[indexMin].geometricBounds[1] || arr[j].geometricBounds[3] > arr[indexMin].geometricBounds[3]) {
                indexMin = j;
            }
        }
        if (indexMin !== i) {
            //交換當(dāng)前下標(biāo)i與最小下標(biāo)的
            var temp = arr[i];
            arr[i] = arr[indexMin];
            arr[indexMin] = temp;
        }
    }

    return arr
};

function toMM(num) {
    var _baseUnit = 2.834646
    return num * _baseUnit
}

function mathNum(_value, _five, _count) {

    if (_five) {
        _value = _value.toFixed(_count)
    } else {
        _value = _value + ""
        // 截取
        _start = _value.split(".")[0].length
        _value = _value.slice(0, _start + 1 + _count)
    }

    return Number(_value)
}

Illustrator腳本開發(fā)教程
我要自學(xué)網(wǎng)商城 ¥70 元
進(jìn)入購(gòu)買
文章評(píng)論
0 條評(píng)論 按熱度排序 按時(shí)間排序 /350
添加表情
遵守中華人民共和國(guó)的各項(xiàng)道德法規(guī),
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。
V
特惠充值
聯(lián)系客服
APP下載
官方微信
返回頂部
分類選擇:
電腦辦公 平面設(shè)計(jì) 室內(nèi)設(shè)計(jì) 室外設(shè)計(jì) 機(jī)械設(shè)計(jì) 工業(yè)自動(dòng)化 影視動(dòng)畫 程序開發(fā) 網(wǎng)頁(yè)設(shè)計(jì) 會(huì)計(jì)課程 興趣成長(zhǎng) AIGC