Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Do I need a foreach loop?

Explorer ,
Feb 28, 2020 Feb 28, 2020

Hello,

 

my ultimite goal is to loop through all my paths to grab all the Compound paths.  For each compound path I want to release it.  Then I want to merge the paths of that came out of that compound path. 

 

Right now I can loop through my paths and select all the Compount paths. I can't figure out to merge the paths so i setled on grouping the paths.  But when I do all the paths from all the compound paths group together rather than grouping each grouping of the paths from when the compound path is released.

 

var doc = app.activeDocument;
var compound = doc.activeLayer.compoundPathItems;

 

function setColor();

 

function setColor() {

try { /* in case that name doesn't exists */

for (var i=0; i < compound.length; i++) {
var currentLayer = compound[i];

currentLayer.selected = true;
if (currentLayer.selected == true) {
app.executeMenuCommand ("noCompoundPath");
app.executeMenuCommand ("group");
}
}
}
catch (e){alert("doesn't exist")}
}

 

Any ideas one what I need to do?  I'm tinking of a foreach loop but I can't figure out how to convert my for loop to a foreach loop.

 

Thank you,

Amber

TOPICS
Scripting
1.3K
Translate
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

correct answers 1 Correct answer

Explorer , Mar 06, 2020 Mar 06, 2020

I found this script that I modified slightly, but I'm running into a few problems.  Like it doesn't apply the script to all the compond paths, so I have to run the script a lot.  I want to get rid of the compound paths without changing the order of the path in my layer.

 

#target illustrator

MergeAll();
MergeAll();
MergeAll();
MergeAll();
MergeAll();


function MergeAll() {
var doc = app.activeDocument;
var compound = doc.activeLayer.compoundPathItems;


for (var i=0; i < compound.length; i++) {
var currentLaye

...
Translate
Adobe
Community Expert ,
Feb 28, 2020 Feb 28, 2020

I'm not sure if I understand you right.

You want:

release all compound paths of a layer

and

create a new compound path of all the released paths?

 

Are these "only simple" compound paths  –  or compound paths of groups of compound paths (nested or deep nested) ?

 

Do you have an example file?

Translate
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
Explorer ,
Feb 28, 2020 Feb 28, 2020

say I have 30 paths in a layer and within that 10 of them are compound paths.

 

I want to be able to take each compound path and realease it and then put them into a group.

 

I've acomplished selecting them all and releasing the compound path, but the grouping isn't working.  I feel like the loop is selecting them all, then releasing the compound paths, then grouping all the selected paths together.

 

I don't want to get the paths out of order.  I have thousands of paths I'm working with.

 

I want the loop to select one compound path, release it, group the paths, then move onto the next compound path.

 

How can I send a sample file?

Translate
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
Community Expert ,
Feb 28, 2020 Feb 28, 2020

Upload the file to a hoster of your choice (dropbox, xup.in or other) and link to here.

 

My main question is:

But what is the final goal after grouping?

Translate
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
Explorer ,
Mar 02, 2020 Mar 02, 2020

I want to merge the pathItems in each group together

Translate
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
Explorer ,
Mar 02, 2020 Mar 02, 2020
Translate
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
Community Expert ,
Mar 02, 2020 Mar 02, 2020

Thank you for your example file.

 

I would try to go another way:

// release all compound paths in the layer "Layer 1" and merge the selection
// regards pixxxel_schubser
var aDoc = app.activeDocument;
aDoc.selection = null;
// select all items in that layer
var aLay = aDoc.layers.getByName('Layer 1');
aLay.hasSelectedArtwork = true;

app.executeMenuCommand("noCompoundPath");

app.executeMenuCommand("group");
app.executeMenuCommand("Live Pathfinder Add");
app.executeMenuCommand("expandStyle");

 

If that works for you

have fun

😉

Translate
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
Explorer ,
Mar 06, 2020 Mar 06, 2020

Thank you for the reply but that is not quite what I was going for.

 

Translate
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
Explorer ,
Mar 06, 2020 Mar 06, 2020

I found this script that I modified slightly, but I'm running into a few problems.  Like it doesn't apply the script to all the compond paths, so I have to run the script a lot.  I want to get rid of the compound paths without changing the order of the path in my layer.

 

#target illustrator

MergeAll();
MergeAll();
MergeAll();
MergeAll();
MergeAll();


function MergeAll() {
var doc = app.activeDocument;
var compound = doc.activeLayer.compoundPathItems;


for (var i=0; i < compound.length; i++) {
var currentLayer = compound[i];

currentLayer.selected = true;
if (currentLayer.selected == true) {
app.executeMenuCommand("noCompoundPath");

function merge() {

var doc = app.activeDocument;

// ---

//if (doc.selection.length === 0) return "JOIN PATHS WITH OVERLAPPING POINTS\n\nABORT: There is no selection.";
// var tolerance = prompt("JOIN PATHS WITH OVERLAPPING POINTS\n\nEnter overlap tolerance (in points):", 0);
// if (tolerance === null) return "JOIN PATHS WITH OVERLAPPING POINTS\n\nABORT: Script execution cancelled.";
//if (!(tolerance >= 0)) return "JOIN PATHS WITH OVERLAPPING POINTS\n\nABORT: The overlap tolerance must be a number of points equal to or larger than 0.";

// ---

var numberOfPoints = 0;
var numberOfStrayPoints = 0;
var numberOfOpenPaths = 0;
var numberOfClosedPaths = 0;
var numberOfSplitPointsMerged = 0;
var numberOfOverlappingPathsJoined = 0;
var numberOfResultingPaths = 0;

// ---

function execute(itemList) {

itemList = Array.prototype.slice.call(itemList);

var items = [];
var overlapClusters = [];

for (var i = 0; i < itemList.length; i++) {
var item = itemList[i];
if (item.typename === "GroupItem") {
execute(item.pageItems);
} else if (item.typename === "PathItem") {
if (item.pathPoints.length > 1) {
var points = Array.prototype.slice.call(item.pathPoints);
for (var j = 0; j < points.length; j++) {
var point = points[j];
var nextPoint = points[j + ((!item.closed || (j + 1 < points.length)) ? 1 : 1 - points.length)];
if (nextPoint) {
if (
point.anchor[0] === nextPoint.anchor[0] &&
point.anchor[1] === nextPoint.anchor[1] &&
point.rightDirection[0] === point.anchor[0] &&
point.rightDirection[1] === point.anchor[1] &&
nextPoint.leftDirection[0] === point.anchor[0] &&
nextPoint.leftDirection[1] === point.anchor[1]
) {
nextPoint.leftDirection = point.leftDirection;
point.remove();
numberOfSplitPointsMerged++;
}
}
numberOfPoints++;
}
}
if (item.pathPoints.length === 1) {
item.remove();
numberOfStrayPoints++;
numberOfOpenPaths++;
} else if (!item.closed) {
items.push(item);
numberOfOpenPaths++;
} else {
numberOfClosedPaths++;
}
}
}

numberOfOpenPaths += items.length;

for (var i = 0; i < items.length; i++) {
var itemA = items[i];
var pointsA = itemA.pathPoints;
for (var j = 0; j < pointsA.length; j++) {
var pointA = pointsA[j];
for (var k = i + 1; k < items.length; k++) {
var itemB = items[k];
var pointsB = itemB.pathPoints;
for (var l = 0; l < pointsB.length; l++) {
var pointB = pointsB[l];
var overlap = tolerance === 0 ?
(pointA.anchor[0] === pointB.anchor[0] && pointA.anchor[1] === pointB.anchor[1]) :
(tolerance >= Math.sqrt(Math.pow(pointA.anchor[0] - pointB.anchor[0], 2) + Math.pow(pointA.anchor[1] - pointB.anchor[1], 2)));
if (overlap) {
if (tolerance > 0) {
var d0 = (pointA.anchor[0] - pointB.anchor[0]) / 2;
var d1 = (pointA.anchor[1] - pointB.anchor[1]) / 2;
pointA.anchor = [pointA.anchor[0] - d0, pointA.anchor[1] - d1];
pointA.leftDirection = [pointA.leftDirection[0] - d0, pointA.leftDirection[1] - d1];
pointA.rightDirection = [pointA.rightDirection[0] - d0, pointA.rightDirection[1] - d1];
pointB.anchor = [pointB.anchor[0] + d0, pointB.anchor[1] + d1];
pointB.leftDirection = [pointB.leftDirection[0] + d0, pointB.leftDirection[1] + d1];
pointB.rightDirection = [pointB.rightDirection[0] + d0, pointB.rightDirection[1] + d1];
}
if (itemA.overlapCluster === undefined) {
if (itemB.overlapCluster === undefined) {
itemA.overlapCluster = [];
itemB.overlapCluster = itemA.overlapCluster;
itemA.overlapCluster.push(itemA);
itemA.overlapCluster.push(itemB);
overlapClusters.push(itemA.overlapCluster);
} else {
itemA.overlapCluster = itemB.overlapCluster;
itemA.overlapCluster.push(itemA);
}
} else {
itemB.overlapCluster = itemA.overlapCluster;
itemA.overlapCluster.push(itemB);
}
}
}
}
}
}

for (var i = 0; i < overlapClusters.length; i++) {
var overlapCluster = overlapClusters[i];
doc.selection = overlapCluster;
numberOfOverlappingPathsJoined += doc.selection.length;
numberOfResultingPaths++;
app.executeMenuCommand("join");
var joinedItem = doc.selection[0];
delete joinedItem.overlapCluster;
}

}

// ---

execute(doc.selection);

// ---

doc.selection = [];

// ---

//return "JOIN PATHS WITH OVERLAPPING POINTS\n\n" +
// numberOfPoints + " points in " + (numberOfOpenPaths + numberOfClosedPaths) + " paths ("+ numberOfOpenPaths +" open and " + numberOfClosedPaths + " closed) were processed.\n\n" +
// numberOfStrayPoints + " stray points were removed.\n" +
// numberOfSplitPointsMerged + " split points were merged.\n" +
// numberOfOverlappingPathsJoined + " paths with overlapping points were joined to " + numberOfResultingPaths + " paths.";

};

merge();

}
}
}

 

I forgot to save the link on where I got this script.

Translate
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
Explorer ,
Mar 17, 2020 Mar 17, 2020
LATEST

So the strange thing is that when I select all and then run this script it seems to work.  I still get an error, but it still does what I need it to do.

 

Thank you to everyone for their input.

Translate
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
Community Expert ,
Mar 06, 2020 Mar 06, 2020

Sorry to hear that. I thought that ist exactly what you described. 

 

And for me this script snippet works very well in your example file.

 

-------------------------

 

Your last question:

IMO far to complicated for your project.

But anyway:

Don't loop forward if the amount of items can change.

Always loop backwards in such cases!

Translate
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
Community Expert ,
Mar 06, 2020 Mar 06, 2020

you were almost there, as pixxxel mentioned you need to loop backwards and the step you were missing: you need to deselect before each iteration

 

here's your original script with the updates

 

var doc = app.activeDocument;
var compound = doc.activeLayer.compoundPathItems;

 

setColor(); // *******************UPDATED

 

function setColor() {

try { /* in case that name doesn't exists */

for (var i=compound.length-1; i>=0; i--) { // ************UPDATED
    doc.selection = null; // **************************ADDED
var currentLayer = compound[i];

currentLayer.selected = true;
if (currentLayer.selected == true) {
app.executeMenuCommand ("noCompoundPath");
app.executeMenuCommand ("group");
}
}
}
catch (e){alert("doesn't exist")}
}
Translate
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
Explorer ,
Mar 17, 2020 Mar 17, 2020

This goes work....Thank you 

Translate
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