ai js简单拼版 支持间距 脚线 间距可以不一样 上下左右不同也可以

本文档详细描述了一个JavaScript函数库,用于在AdobeIllustrator中实现图形的拼版操作,包括选择图形、设置边距、计算尺寸、绘制裁切线和走刀路线,以及处理颜色。
摘要由CSDN通过智能技术生成
var doc = activeDocument;
var pt = 72 / 25.4;


new Imposition(4, 3, 0).work();


/**
 * 选中图形 复制拼版 拼版的尺寸以 最大的尺寸作为 拼版尺寸
 * @param col 行数
 * @param row 列数
 * @param lcx 左出血
 * @param tcx 上出血
 * @param rcx 右出血
 * @param bcx 下出血
 * @constructor
 */
function Imposition(col, row, lcx, tcx, rcx, bcx) {
    this.lw = 0.1;
    this.jx = 3 * pt;
    this.cx = 2 * pt;
    initAroundSpace.call(this);
    this.w = 0;
    this.h = 0;
    this.xCx = 0;
    this.yCx = 0;
    this.left = 0;
    this.bottom = 0;
    //左右间距
    this.ws = this.lcx + this.rcx;
    //上下间距
    this.hs = this.bcx + this.tcx;
    this.row = row;
    this.col = col;
    this.ss = doc.selection;
    this.jiaoXianGroup = doc.groupItems.add();
    var that = this;
    setWHLeftBottomXY(0, 0);

    this.w -= this.lcx + this.rcx;
    this.h -= this.tcx + this.bcx;
    this.xCx += this.lcx;
    this.yCx += this.bcx;
    //页面宽度
    this.pw = (this.w + this.ws) * this.col + (this.cx + this.jx) * 2 - this.ws;
    //页面高度
    this.ph = (this.h + this.hs) * this.row + (this.cx + this.jx) * 2 - this.hs;


    this.work = function () {
        imp.call(this);
        addCropLine.call(this);
        addCutLine.call(this);
    }

    /**
     * 画走刀路线
     */
    function addCutLine() {
        var yulian = 3 * pt;
        var pw = (this.w + this.ws) * this.col - this.ws+yulian*2;
        var ph = (this.h + this.hs) * this.row - this.hs+yulian*2;


        var x = this.left;
        var y = this.bottom - yulian;
        var len = this.col;
        var f= true;
        if(this.col %2 ==0){
            f = false;
            len += 0.5;
        }
        var listCol = [];
        for (var c = 0; c < len; c=c+2) {
            listCol.push([x+c*this.w, y])
            listCol.push([x+c*this.w, y+ph])
            if(c+1>=len){
                if(f){
                    listCol.push([x+(c+1)*this.w, y+ph])
                    listCol.push([x+(c+1)*this.w, y])
                }
            }else{
                listCol.push([x+(c+1)*this.w, y+ph])
                listCol.push([x+(c+1)*this.w, y])
            }
        }

        addLineByList(listCol);


        addRow.call(this);



        function addLineByList(list) {
            var line = doc.pathItems.add();
            line.setEntirePath(list);
            line.filled = false; // 无填充色
            line.stroked = true; // 描边
            line.strokeWidth = that.lw*2; //指定标线宽度
            line.strokeColor = getCMYKColor(0, 99, 0, 0);
        }
        function addRow() {
            var len = this.row;
            var f = true;

            var listRow = [];
            var x = this.left - yulian;
            var y = this.bottom - yulian+this.jx;
            if (this.row % 2 == 0) {
                f = false;
                len += 0.5;
            }
            for (var r = 0; r < len; r = r + 2) {
                listRow.push([x, y + r * this.h])
                listRow.push([x + pw, y + r * this.h])
                if (r + 1 >= len) {
                    if (f) {
                        listRow.push([x + pw, y + (r + 1) * this.h])
                        listRow.push([x, y + (r + 1) * this.h])
                    }
                } else {
                    listRow.push([x + pw, y + (r + 1) * this.h])
                    listRow.push([x, y + (r + 1) * this.h])
                }

            }
            addLineByList(listRow);
        }


    }

    /**
     * 设置周围的边距
     */
    function initAroundSpace() {
        if (lcx == undefined && tcx == undefined && rcx == undefined && bcx == undefined) {
            this.lcx = 0;
            this.bcx = 0;
            this.rcx = 0;
            this.tcx = 0;
        } else if (tcx == undefined && rcx == undefined && bcx == undefined) {
            this.lcx = lcx * pt;
            this.bcx = lcx * pt;
            this.rcx = lcx * pt;
            this.tcx = lcx * pt;
        } else if (rcx == undefined && bcx == undefined) {
            this.lcx = lcx * pt;
            this.bcx = tcx * pt;
            this.rcx = lcx * pt;
            this.tcx = tcx * pt;
        } else if (lcx != undefined && tcx != undefined && rcx != undefined && bcx != undefined) {
            this.lcx = lcx * pt;
            this.bcx = bcx * pt;
            this.rcx = rcx * pt;
            this.tcx = tcx * pt;
        }
    }

    /**
     * 拼版
     */
    function imp() {
        for (var c = 0; c < this.col; c++) {
            for (var r = 0; r < this.row; r++) {
                if (!(c == 0 && r == 0)) {
                    for (var i = 0; i < this.ss.length; i++) {
                        newItem = this.ss[i].duplicate(app.activeDocument, ElementPlacement.PLACEATEND);
                        newItem.translate(c * (this.w + this.ws), r * (this.h + this.hs))
                    }
                }
            }
        }
    }

    /**
     * 添加裁切线
     */
    function addCropLine() {
        for (var c = 0; c < this.col; c++) {
            var x1 = this.xCx + this.left + c * (this.w + this.ws);
            var x2 = this.xCx + this.left + c * (this.w + this.ws);
            var y1 = this.yCx + this.bottom - this.jx - this.cx;
            var y2 = this.yCx + this.bottom - this.cx;
            addLine(x1, y1, x2, y2)
            addLine(x1 + this.w, y1, x2 + this.w, y2)
            addLine(
                x1,
                y1 + this.ph - this.jx,
                x2,
                y2 + this.ph - this.jx)
            addLine(
                x1 + this.w,
                y1 + this.ph - this.jx,
                x2 + this.w,
                y2 + this.ph - this.jx)
        }
        for (var r = 0; r < this.row; r++) {
            var x1 = this.xCx + this.left - this.jx - this.cx;
            var x2 = this.xCx + this.left - this.cx;
            var y1 = this.yCx + this.bottom + r * (this.h + this.hs);
            var y2 = this.yCx + this.bottom + r * (this.h + this.hs);
            addLine(x1, y1, x2, y2)
            addLine(
                x1 + this.pw - this.jx,
                y1,
                x2 + this.pw - this.jx,
                y2
            )
            addLine(x1, y1 + this.h, x2, y2 + this.h)
            addLine(
                x1 + this.pw - this.jx,
                y1 + this.h,
                x2 + this.pw - this.jx,
                y2 + this.h
            )
        }

        function addLine(x1, y1, x2, y2) {
            var line = that.jiaoXianGroup.pathItems.add();
            line.setEntirePath([[x1, y1], [x2, y2]]);
            line.filled = false; // 无填充色
            line.stroked = true; // 描边
            line.strokeWidth = that.lw; //指定标线宽度
            line.strokeColor = getCMYKColor(0, 0, 0, 99);
        }
    }


    /**
     * 获取颜色
     * @param c
     * @param m
     * @param y
     * @param k
     * @returns {CMYKColor}
     */
    function getCMYKColor(c, m, y, k) {
        var cmykColor = new CMYKColor();
        cmykColor.cyan = c;//设置颜色的值
        cmykColor.magenta = m;//设置颜色的值
        cmykColor.yellow = y;//设置颜色的值
        cmykColor.black = k;//设置颜色的值
        return cmykColor;
    }

    /**
     * 设置 宽度 高度 x y
     * @param w
     * @param h
     */
    function setWHLeftBottomXY(w, h) {
        for (var i = 0; i < that.ss.length; i++) {

            setWAndH(that.ss[i], w, h);
        }

        function ShapeSize(shape, w, h) {
            var sh = shape;
            if (shape.typename == 'GroupItem' && shape.pageItems.length > 0) {
                if (shape.clipped) {
                    sh = shape.pageItems[0];
                }
            }

            var b = sh.geometricBounds;
            if (w == 0 && h == 0) {
                this.w = b[2] - b[0];
                this.h = b[1] - b[3];
                this.xCx = 0;
                this.yCx = 0;
            } else {
                this.w = w * pt;
                this.h = h * pt;
                this.xCx = ((b[2] - b[0]) - this.w) / 2;
                this.yCx = ((b[1] - b[3]) - this.h) / 2;
            }
            this.left = b[0];
            this.bottom = b[3];
        }

        function setWAndH(shape, w, h) {
            var s = new ShapeSize(shape, w, h);
            if (that.w < s.w && that.h < s.h) {
                that.w = that.w > s.w ? that.w : s.w;
                that.h = that.h > s.h ? that.h : s.h;
                that.left = s.left;
                that.bottom = s.bottom;
                that.xCx = s.xCx;
                that.yCx = s.yCx;
            }
        }
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值