var mindist=4;
function getDist(pt1,pt2)
{
var dx=(pt1.anchor[0]-pt2.anchor[0]);
var dy=(pt1.anchor[1]-pt2.anchor[1]);
return Math.sqrt(dx*dx+dy*dy);
}
function addObj(mas,obj)
{
for (var i=0;i<mas.length;i++)
if (mas[i]==obj) return mas;
mas.push(obj);
return mas;
}
// ------------------------------------------------
// reverse the order of pathPoints in PathItem "pi"
function pireverse(pi){
var p = pi.pathPoints;
var arr = [];
for (var i=0; i<p.length; i++) {
with(p[i]) arr.unshift( [anchor, rightDirection, leftDirection, pointType] );
}
for (i=0; i<arr.length; i++) {
with(p[i]){
anchor = arr[i][0];
leftDirection = arr[i][1];
rightDirection = arr[i][2];
pointType = arr[i][3];
}
}
}
// -----------------------------------------
// join the 1st anchor of PathItem "pi2"
// and the last anchor of PathItem "pi1"
function pijoin(pi1, pi2){
if (getDist(pi1.pathPoints[0],pi2.pathPoints[0])<=mindist) pireverse(pi1); else
if (getDist(pi1.pathPoints[pi1.pathPoints.length-1],pi2.pathPoints[pi2.pathPoints.length-1])<=mindist) pireverse(pi2); else
if (getDist(pi1.pathPoints[0],pi2.pathPoints[pi2.pathPoints.length-1])<=mindist) {pireverse(pi1);pireverse(pi2);} else
if (getDist(pi1.pathPoints[pi1.pathPoints.length-1],pi2.pathPoints[0])>mindist) return false;
var p1 = pi1.pathPoints;
var p2 = pi2.pathPoints;
p1[p1.length-1].rightDirection = p1[p1.length-1].anchor;
p2[0].leftDirection = p2[0].anchor;
var pp1;
for(var i=0; i<p2.length; i++){
pp1 = p1.add();
with(pp1){
anchor = p2[i].anchor;
rightDirection = p2[i].rightDirection;
leftDirection = p2[i].leftDirection;
pointType = p2[i].pointType;
}
}
pi2.remove();
return true;
}
mySelection = activeDocument.selection;
if (mySelection.length>0){
if (mySelection instanceof Array){
var pnts=new Array();
for (i=0;i<mySelection.length-1;i++)
{
var obj1=mySelection[i];
for (j=i+1;j<mySelection.length;j++)
{
var obj2=mySelection[j];
if ((getDist(obj1.pathPoints[0],obj2.pathPoints[0])<=mindist) ||
(getDist(obj1.pathPoints[obj1.pathPoints.length-1],obj2.pathPoints[0])<=mindist) ||
(getDist(obj1.pathPoints[obj1.pathPoints.length-1],obj2.pathPoints[obj2.pathPoints.length-1])<=mindist) ||
(getDist(obj1.pathPoints[0],obj2.pathPoints[obj2.pathPoints.length-1])<=mindist) )
{
var flag=true;
for (k=0;k<mySelection.length;k++)
if( (k!=i) && (k!=j))
{
var obj3=mySelection[k];
if ((getDist(obj1.pathPoints[0],obj3.pathPoints[0])<=mindist) ||
(getDist(obj1.pathPoints[obj1.pathPoints.length-1],obj3.pathPoints[0])<=mindist) ||
(getDist(obj1.pathPoints[obj1.pathPoints.length-1],obj3.pathPoints[obj3.pathPoints.length-1])<=mindist) ||
(getDist(obj1.pathPoints[0],obj3.pathPoints[obj3.pathPoints.length-1])<=mindist) )
{
flag=false;
break;
}
}
if (flag) {
pnts=addObj(pnts,obj1);
pnts=addObj(pnts,obj2);
}
}
}
}
var i=0
while (i<pnts.length)
{
var j=i+1;
while (j<pnts.length)
{
if (pijoin(pnts[i],pnts[j])) pnts.splice(j,1); else j++;
}
i++;
}
}
}