Can someone help me optimize this prepress script?

New Here ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

This is an automatic imposition script for printing
It automatically calculates the number of horizontal and vertical imposition plates
and covers the canvas, but the original script does not support masking objects.
So I used a very dumb way to achieve masked objects by automatically creating outer borders for the objects and checking the box for stitching with content.
But in practice this is very slow and laggy.

Maybe there's something wrong with the way I'm doing it, it's always unresponsive for a while, and then it feels laggy.
Thanks to any seniors who can give me advice on how to modify

TOPICS
Scripting

Views

109

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

//This is an automatic imposition script for printing
//It automatically calculates the number of horizontal and vertical imposition plates
//and covers the canvas, but the original script does not support masking objects.
//So I used a very dumb way to achieve masked objects by automatically creating outer borders for the objects and checking the box for stitching with content.
//But in practice this is very slow and laggy.
// Salty Fish 2022.5.24 Hope someone can help me.
// The following operation is creating the outer border
var doc = app.activeDocument;
var theSelect = doc.selection;

var objGBCheck = false;
var addLayerCheck = false; 
var addColorCheck = false; 
var layName = "Border Layer"; 

if (theSelect.length > 0) {
	for (i = 0; i < theSelect.length; i++) { 
		var bound = NO_CLIP_BOUNDS(theSelect[i]);
		var left = bound[0]; 
		var tops = bound[1]; 
		var width = bound[2] - bound[0]; 
		var height = bound[1] - bound[3]; 
		if (addLayerCheck == true) {
			try {
			var newLayer = doc.layers[layName];
			newLayer.locked = false;
			newLayer.visible = true;
		} catch(err) {
			var newLayer = doc.layers.add();
			newLayer.name = layName; 
		}
			var rect = newLayer.pathItems.rectangle(tops, left, width, height); 
		} else {
			var rect = doc.activeLayer.pathItems.rectangle(tops, left, width, height); 
		}

		var newColor = new CMYKColor(); 
			newColor.cyan = 0;
			newColor.magenta = 0;
			newColor.yellow = 0;
			newColor.black = 100;
		if (addColorCheck == true) {
			rect.stroked = true; 
			rect.strokeWidth = 0.25; 
			rect.strokeColor = newColor; 
		} else {
			rect.stroked = false; 
		}
			rect.filled = false; 
			rect.fillColor = NoColor; 
			rect.selected = true;
	}
} else {
	alert("Hello!\nMust first select the object file Oh!", "Error");
}


function NO_CLIP_BOUNDS(the_obj) {
	var NO_CLIP_OBJECTS_AND_MASKS = new Array();
	GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj);
	var v_left = new Array();
	var g_left = new Array();
	var v_top = new Array();
	var g_top = new Array();
	var v_right = new Array();
	var g_right = new Array();
	var v_bottom = new Array();
	var g_bottom = new Array();
	for (var i = 0; i < NO_CLIP_OBJECTS_AND_MASKS.length; i += 1) {
		g_left[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[0];
		v_left[i] = NO_CLIP_OBJECTS_AND_MASKS[i].visibleBounds[0];
		g_top[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[1];
		v_top[i] = NO_CLIP_OBJECTS_AND_MASKS[i].visibleBounds[1];
		g_right[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[2];
		v_right[i] = NO_CLIP_OBJECTS_AND_MASKS[i].visibleBounds[2];
		g_bottom[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[3];
		v_bottom[i] = NO_CLIP_OBJECTS_AND_MASKS[i].visibleBounds[3];
	}
	
	var GV_Bounds = objGBCheck ? "true": "false";
	switch (GV_Bounds) {
	case "true":
		var g_L = MIN_IN_ARRAY(g_left);
		var g_T = MAX_IN_ARRAY(g_top);
		var g_R = MAX_IN_ARRAY(g_right);
		var g_B = MIN_IN_ARRAY(g_bottom);
		return [g_L, g_T, g_R, g_B];
		break;

	case "false":
		var v_L = MIN_IN_ARRAY(v_left);
		var v_T = MAX_IN_ARRAY(v_top);
		var v_R = MAX_IN_ARRAY(v_right);
		var v_B = MIN_IN_ARRAY(v_bottom);
		return [v_L, v_T, v_R, v_B];
		break;
	}

	function GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj) {
		if (IS_CLIP(the_obj)) {
			NO_CLIP_OBJECTS_AND_MASKS.push(the_obj.pageItems[0]);
			return;
		}
		if (the_obj.constructor.name == "GroupItem") {
			try {
				var N_sub_obj = the_obj.pageItems.length;
				for (var i = 0; i < N_sub_obj; i += 1) {
					GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj.pageItems[i]);
				}
			} catch(error) {

			}
			return;
		}
		NO_CLIP_OBJECTS_AND_MASKS.push(the_obj);
		return;
	}
}

function IS_CLIP(the_obj) {
	try {
		if (the_obj.constructor.name == "GroupItem") {
			if (the_obj.clipped) {
				return true;
			}
		}
	} catch(error) {

	}
	return false;
}

function MAX_IN_ARRAY(the_array) {
	var MAX = the_array[0];
	for (var i = 0; i < the_array.length; i += 1) {
		if (the_array[i] > MAX) {
			MAX = the_array[i]
		}
	}
	return MAX;
}

function MIN_IN_ARRAY(the_array) {
	var MIN = the_array[0];
	for (var i = 0; i < the_array.length; i += 1) {
		if (the_array[i] < MIN) {
			MIN = the_array[i]
		}
	}
	return MIN;
}
if (app.documents.length == 0) {
    Mydoc = app.documents.add();
} else {
    Mydoc = app.activeDocument;
}

function MyfistDialog() {
    this.windowRef = null;
}
MyfistDialog.prototype.run = function() {
    var MyDocSelected = app.activeDocument.selection;
    if (MyDocSelected.length > 0) {
        var AutoX = Math.round((app.activeDocument.selection[0].width * 25.4) / 72, 2).toString();
        AutoY = Math.round((app.activeDocument.selection[0].height * 25.4) / 72, 2).toString();
        AutoXQty = Math.floor((Mydoc.width - 5) / app.activeDocument.selection[0].width).toString();
        AutoYQty = Math.floor((Mydoc.height - 5) / app.activeDocument.selection[0].height).toString();
    } else {
        AutoX = "0";
        AutoY = "0";
        AutoXQty = "1";
        AutoYQty = "1";
    }
    res = "dialog { \n\t\ttext: 'Salty modification auto-spell version', \n\t\tmypnl: Panel { orientation:'column', alignChildren:['right', 'top'],\n\t\t\ttext: 'Plating information:', \n\t\t\tmyX: Group { orientation: 'row', \n\t\t\t\tst: StaticText { text:'Horizontal size of finished product' }, \n\t\t\t\tmyXSize: EditText { characters: 5,text:" + AutoX + ", justify:'right' } \n\t\t\t\tst: StaticText { text:'mm      Horizontal co-spelling' }, \n\t\t\t\tmyXQty : EditText { characters: 3,text:" + AutoXQty + ", justify:'right' } \n\t\t\t\tst: StaticText { text:'Quantity' }, \n\t\t\t}, \n\t\t\tmyY: Group { orientation: 'row', \n\t\t\t\tst: StaticText { text:'Vertical size of finished product' }, \n\t\t\t\tmyYSize: EditText { characters: 5,text:" + AutoY + ", justify:'right' } \n\t\t\t\tst: StaticText { text:'mm      Vertical co-spelling' }, \n\t\t\t\tmyYQty : EditText { characters: 3,text:" + AutoYQty + ", justify:'right' } \n\t\t\t\tst: StaticText { text:'Quantity' }, \n\t\t\t}, \n\t\t}, \n\t\tmypnl2: Panel { orientation:'column', alignChildren:['right', 'top'],\n\t\t\ttext: 'Marker Information:(mm)', \n\t\t\tmyOther: Group { orientation: 'row', \n\t\t\tst: StaticText { text:'Marker Offset' }, \n\t\t\tmyCropSpace : EditText { characters: 3,text:2, justify:'right' } \n\t\t\tst: StaticText { text:'Marker line length' }, \n\t\t\tmyCropLength : EditText { characters: 3,text:3, justify:'right' } \n\t\t\tst: StaticText { text:'interval' }, \n\t\t\tmyBlood : EditText { characters: 3,text:0, justify:'right' } \n\t\t\t}, \n\t\t}, \n\t\tCheckboxs: Group { orientation: 'row', \n\t\t\tCheckContent: Checkbox { \n\t\t\t\torientation:'row', text:'With content collage     .', value:true \n\t\t\t}, \n\t\t\tCheckCutline: Checkbox { \n\t\t\t\torientation:'row', text:'Automatic Marking', value:true \n\t\t\t}, \n\t\t}, \n\t\tmypnl3: Group { orientation: 'row', \n\t\t\tst: StaticText { text:'Note: Horizontal and vertical dimensions are required>0, Spellings required≥1,Other values required≥0' }, \n\t\t} \n\t\tbuttons: Group { orientation: 'row', \n\t\t\tcancelBtn: Button { text:'cancel', properties:{name:'cancel'} } \n\t\t\tokBtn: Button { text:'ok', properties:{name:'ok'} }, \n\t\t} \n}";
    myDialog = new Window(res);
    this.windowRef = myDialog;
    myDialog.buttons.okBtn.onClick = function() {
        myXSizeV = (Number(myDialog.mypnl.myX.myXSize.text) * 72) / 25.4;
        myXQtyV = Math.round(Number(myDialog.mypnl.myX.myXQty.text));
        myYSizeV = (Number(myDialog.mypnl.myY.myYSize.text) * 72) / 25.4;
        myYQtyV = Math.round(Number(myDialog.mypnl.myY.myYQty.text));
        myCropSpaceV = (Number(myDialog.mypnl2.myOther.myCropSpace.text) * 72) / 25.4;
        myCropLengthV = (Number(myDialog.mypnl2.myOther.myCropLength.text) * 72) / 25.4;
        myBloodV = (Number(myDialog.mypnl2.myOther.myBlood.text) * 72) / 25.4;
        StartpositionX = 0;
        StartpositionY = 0;
        if (app.activeDocument.selection.length > 0) {
            StartpositionX = app.activeDocument.selection[0].position[0];
            StartpositionY = (app.activeDocument.selection[0].position[1] - ((myYSizeV + myBloodV) * myYQtyV)) + myBloodV;
        }
        var isInputOk = false;
        if (myXSizeV > 0 && myXQtyV >= 1 && myYSizeV > 0 && myYQtyV >= 1 && myCropSpaceV >= 0 && myCropLengthV >= 0 && myBloodV >= 0) {
            isInputOk = true;
        }
        if ((myXQtyV * myYQtyV) > 5000) {
            isInputOk = false;
        }
        if (isInputOk == true) {
            myDialog.close();
            if (myDialog.Checkboxs.CheckContent.value) {
                AutoCopy(myXSizeV, myXQtyV, myYSizeV, myYQtyV, myBloodV, StartpositionX, StartpositionY);
            }
            if (myDialog.Checkboxs.CheckCutline.value) {
                AddCutline(myXSizeV, myXQtyV, myYSizeV, myYQtyV, myCropSpaceV, myCropLengthV, myBloodV, StartpositionX, StartpositionY);
            }
        }
    };
    myDialog.buttons.cancelBtn.onClick = function() {
        myDialog.close();
    };
    myDialog.show();
    return true;
};
if (typeof MyfistDialog_unitTest == "undefined") {
    new MyfistDialog().run();
}

function AutoCopy(myXSizeV, myXQtyV, myYSizeV, myYQtyV, myBloodV, StartpositionX, StartpositionY) {
    var MyDocSelected = app.activeDocument.selection;
    if (MyDocSelected.length > 0) {
        for (var v = 1; v <= myYQtyV; v += 1) {
            for (var h = 1; h <= myXQtyV; h += 1) {
                if (MyDocSelected.length > 0) {
                    for (var i = 0; i < MyDocSelected.length; i += 1) {
                        MyDocSelected[i].selected = false;
                        if (v > 1 || h > 1) {
                            newItem = MyDocSelected[i].duplicate(app.activeDocument, ElementPlacement.PLACEATEND);
                            newItem.translate((myXSizeV + myBloodV) * (h - 1), -(myYSizeV + myBloodV) * (v - 1));
                        }
                    }
                } else {
                    MyDocSelected.selected = false;
                    newItem = MyDocSelected.parent.duplicate(app.activeDocument, ElementPlacement.PLACEATEND);
                    newItem.left += ((myXSizeV + myBloodV) * (h - 1));
                    newItem.top += (-(myYSizeV + myBloodV) * (v - 1));
                }
            }
        }
    }
}

function AddCutline(myXSizeV, myXQtyV, myYSizeV, myYQtyV, myCropSpaceV, myCropLengthV, myBloodV, StartpositionX, StartpositionY) {
    var MyDocSelected = app.activeDocument.selection;
    if (MyDocSelected.length > 0) {
        for (var v = 1; v <= 2; v += 1) {
            for (var h = 1; h <= 2; h += 1) {
                if (MyDocSelected.length > 0) {
                    for (var i = 0; i < MyDocSelected.length; i += 1) {
                        MyDocSelected[i].selected = false;
                        if (v > 1 || h > 1) {

                        }
                    }
                } else {
                    MyDocSelected.selected = false;
                }
            }
        }
    }
    var cmykColor = new CMYKColor();
    cmykColor.cyan = 0;
    cmykColor.yellow = 0;
    cmykColor.magenta = 0;
    cmykColor.black = 100;
    var MyCutlineGroup = activeDocument.groupItems.add();
    if (myBloodV != 0) {
        for (var i = 0; i < myXQtyV + 1; i += 1) {
            if (i > 0) {
                var MyTestPathBottom1 = MyCutlineGroup.pathItems.add();
                MyTestPathBottom1.setEntirePath(new Array(new Array((StartpositionX + ((myXSizeV + myBloodV) * i)) - myBloodV, StartpositionY - myCropSpaceV), new Array((StartpositionX + ((myXSizeV + myBloodV) * i)) - myBloodV, (StartpositionY - myCropSpaceV) - myCropLengthV)));
                MyTestPathBottom1.strokeColor = cmykColor;
                MyTestPathBottom1.stroked = true;
                MyTestPathBottom1.filled = false;
                MyTestPathBottom1.strokeWidth = 0.425196850393701;
                MyTestPathBottom1.selected = true;
                var MyTestPathUp1 = MyCutlineGroup.pathItems.add();
                MyTestPathUp1 = MyTestPathBottom1.duplicate();
                MyTestPathUp1.top += (((myYSizeV + myBloodV) * myYQtyV) - myBloodV) + (myCropSpaceV * 2) + myCropLengthV;
                MyTestPathUp1.strokeColor = cmykColor;
                MyTestPathUp1.stroked = true;
                MyTestPathUp1.filled = false;
                MyTestPathUp1.strokeWidth = 0.425196850393701;
                MyTestPathUp1.selected = true;
            }
            if (i < myXQtyV) {
                var MyTestPathBottom2 = MyCutlineGroup.pathItems.add();
                MyTestPathBottom2.setEntirePath(new Array(new Array(StartpositionX + ((myXSizeV + myBloodV) * i), StartpositionY - myCropSpaceV), new Array(StartpositionX + ((myXSizeV + myBloodV) * i), (StartpositionY - myCropSpaceV) - myCropLengthV)));
                MyTestPathBottom2.strokeColor = cmykColor;
                MyTestPathBottom2.stroked = true;
                MyTestPathBottom2.filled = false;
                MyTestPathBottom2.strokeWidth = 0.425196850393701;
                MyTestPathBottom2.selected = true;
                var MyTestPathUp2 = MyCutlineGroup.pathItems.add();
                MyTestPathUp2 = MyTestPathBottom2.duplicate();
                MyTestPathUp2.top += (((myYSizeV + myBloodV) * myYQtyV) - myBloodV) + (myCropSpaceV * 2) + myCropLengthV;
                MyTestPathUp2.strokeColor = cmykColor;
                MyTestPathUp2.stroked = true;
                MyTestPathUp2.filled = false;
                MyTestPathUp2.strokeWidth = 0.425196850393701;
                MyTestPathUp2.selected = true;
            }
        }
        for (var i = 0; i < myYQtyV + 1; i += 1) {
            if (i > 0) {
                var MyTestPathLeft1 = MyCutlineGroup.pathItems.add();
                MyTestPathLeft1.setEntirePath(new Array(new Array(StartpositionX - myCropSpaceV, (StartpositionY + ((myYSizeV + myBloodV) * i)) - myBloodV), new Array((StartpositionX - myCropSpaceV) - myCropLengthV, (StartpositionY + ((myYSizeV + myBloodV) * i)) - myBloodV)));
                MyTestPathLeft1.strokeColor = cmykColor;
                MyTestPathLeft1.stroked = true;
                MyTestPathLeft1.filled = false;
                MyTestPathLeft1.strokeWidth = 0.425196850393701;
                MyTestPathLeft1.selected = true;
                var MyTestPathRight1 = MyCutlineGroup.pathItems.add();
                MyTestPathRight1 = MyTestPathLeft1.duplicate();
                MyTestPathRight1.left += (((myXSizeV + myBloodV) * myXQtyV) - myBloodV) + (myCropSpaceV * 2) + myCropLengthV;
                MyTestPathRight1.strokeColor = cmykColor;
                MyTestPathRight1.stroked = true;
                MyTestPathRight1.filled = false;
                MyTestPathRight1.strokeWidth = 0.425196850393701;
                MyTestPathRight1.selected = true;
            }
            if (i < myYQtyV) {
                var MyTestPathLeft2 = MyCutlineGroup.pathItems.add();
                MyTestPathLeft2.setEntirePath(new Array(new Array(StartpositionX - myCropSpaceV, StartpositionY + ((myYSizeV + myBloodV) * i)), new Array((StartpositionX - myCropSpaceV) - myCropLengthV, StartpositionY + ((myYSizeV + myBloodV) * i))));
                MyTestPathLeft2.strokeColor = cmykColor;
                MyTestPathLeft2.stroked = true;
                MyTestPathLeft2.filled = false;
                MyTestPathLeft2.strokeWidth = 0.425196850393701;
                MyTestPathLeft2.selected = true;
                var MyTestPathRight2 = MyCutlineGroup.pathItems.add();
                MyTestPathRight2 = MyTestPathLeft2.duplicate();
                MyTestPathRight2.left += (((myXSizeV + myBloodV) * myXQtyV) - myBloodV) + (myCropSpaceV * 2) + myCropLengthV;
                MyTestPathRight2.strokeColor = cmykColor;
                MyTestPathRight2.stroked = true;
                MyTestPathRight2.filled = false;
                MyTestPathRight2.strokeWidth = 0.425196850393701;
                MyTestPathRight2.selected = true;
            }
        }
    } else {
        for (var i = 0; i < myXQtyV + 1; i += 1) {
            var MyTestPathBottom1 = MyCutlineGroup.pathItems.add();
            MyTestPathBottom1.setEntirePath(new Array(new Array((StartpositionX + ((myXSizeV + myBloodV) * i)) - myBloodV, StartpositionY - myCropSpaceV), new Array((StartpositionX + ((myXSizeV + myBloodV) * i)) - myBloodV, (StartpositionY - myCropSpaceV) - myCropLengthV)));
            MyTestPathBottom1.strokeColor = cmykColor;
            MyTestPathBottom1.stroked = true;
            MyTestPathBottom1.filled = false;
            MyTestPathBottom1.strokeWidth = 0.425196850393701;
            MyTestPathBottom1.selected = true;
            var MyTestPathUp1 = MyCutlineGroup.pathItems.add();
            MyTestPathUp1 = MyTestPathBottom1.duplicate();
            MyTestPathUp1.top += (((myYSizeV + myBloodV) * myYQtyV) - myBloodV) + (myCropSpaceV * 2) + myCropLengthV;
            MyTestPathUp1.strokeColor = cmykColor;
            MyTestPathUp1.stroked = true;
            MyTestPathUp1.filled = false;
            MyTestPathUp1.strokeWidth = 0.425196850393701;
            MyTestPathUp1.selected = true;
        }
        for (var i = 0; i < myYQtyV + 1; i += 1) {
            var MyTestPathLeft1 = MyCutlineGroup.pathItems.add();
            MyTestPathLeft1.setEntirePath(new Array(new Array(StartpositionX - myCropSpaceV, (StartpositionY + ((myYSizeV + myBloodV) * i)) - myBloodV), new Array((StartpositionX - myCropSpaceV) - myCropLengthV, (StartpositionY + ((myYSizeV + myBloodV) * i)) - myBloodV)));
            MyTestPathLeft1.selected = true;
            var MyTestPathRight1 = MyCutlineGroup.pathItems.add();
            MyTestPathRight1 = MyTestPathLeft1.duplicate();
            MyTestPathRight1.left += (((myXSizeV + myBloodV) * myXQtyV) - myBloodV) + (myCropSpaceV * 2) + myCropLengthV;
            MyTestPathRight1.selected = true;
        }
    }
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2022 May 27, 2022

Copy link to clipboard

Copied

LATEST

Can anyone help with optimization? Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines