Skip to main content
karth80472045
Inspiring
June 18, 2019
Question

join overlap anchor

  • June 18, 2019
  • 0 replies
  • 293 views

Hello all,

i have written a script that joins overlapping anchor points of .cgm file , by taking join reasonably script as reference but i am facing the problem of extremely slowness if the cgm file has more than 5,000 pathItems , it is really hard for us joining overlap anchor points for 100 cgm files manually. can anyone suggest me an idea how to approach for joining overlap anchor points without looping. i have attached .CGM file as image for reference.

var conf = {};

conf.close = false;

var mpi = Math.PI;

var hpi = mpi / 2;

conf.dontAddRevHan *= mpi / 180;

function mm2pt(mm){  return mm * 2.83464567;  }

main();

function main(){

  var s = [];

  var s_satisfied=0;

  getPathItemsInSelection(1, s);

  if(s.length < 2) return;

  for(var i = 0, iend = s.length; i < iend; i++){

    readjustAnchors(s, 0);

  }

Loop1:

var pitem = s.shift();

var pitems = [pitem];

  var p  = pitem.pathPoints;

  var z = p.length - 1;

  var pinfo = {

      d : null,       

      child : null,  

      cIdx : null,   

      pPntIdx : null,

      cPntIdx : null  

  };

var p2, z2;

  while(s.length > 0){

    pinfo.child = null;

  

     for(var i = 0, iend = s.length; i < iend; i++){

               p2 = s.pathPoints;

      z2 = p2.length - 1;

cmpLen(pinfo, p, p2, i, 0, 0);

cmpLen(pinfo, p, p2, i, 0, z2);

cmpLen(pinfo, p, p2, i, z, 0);

cmpLen(pinfo, p, p2, i, z, z2);

if(pinfo.d == 0)

{

     s_satisfied=1;

     s.splice( pinfo.cIdx, 1 );

if(pinfo.pPntIdx == 0 && pinfo.cPntIdx==0)

{

     pireverse(pinfo.child);

    pijoin_1(pitem, pinfo.child);

    z = p.length - 1;

    }

else if(pinfo.pPntIdx == 0 && pinfo.cPntIdx==1)

{

  

    pijoin_1(pitem, pinfo.child);

  

    z = p.length - 1;

  

    }

else if(pinfo.pPntIdx == (p.length-1) && pinfo.cPntIdx==0)

{

    pijoin(pitem, pinfo.child);

    z = p.length - 1;

   

     }

else if(pinfo.pPntIdx ==(p.length-1) && pinfo.cPntIdx==1)

{

     pireverse(pinfo.child);

     pijoin(pitem, pinfo.child);

     z = p.length - 1;

     }

else

{

    }

break;

    }

}

if(s.length>0 && s_satisfied==0)

{

    var pitem = s.shift();

var pitems = [pitem];

  var p  = pitem.pathPoints;

  var z = p.length - 1;

  var pinfo = {

      d : null,       

      child : null,  

      cIdx : null,   

      pPntIdx : null,

      cPntIdx : null  

  };

  

    }

}

}

function pijoin_1(pi1, pi2)

{

var p1 = pi1.pathPoints;

  var p2 = pi2.pathPoints;

  var d = dist(p1[0].anchor, p2[1].anchor);

  if (d==0)

  {

  

    p1[0].remove();

  

  

    var pp1 = p1.add();

    var p2i = p2[1];

    pp1.anchor         = p2i.anchor;

    pp1.rightDirection = p2i.rightDirection;

    pp1.leftDirection  = p2i.leftDirection;

    pp1.pointType      = p2i.pointType;

  var pp1=p1.add();

  var p2i=p2[0];

  pp1.anchor         = p2i.anchor;

    pp1.rightDirection = p2i.rightDirection;

    pp1.leftDirection  = p2i.leftDirection;

    pp1.pointType      = p2i.pointType;

p2[1].remove();

// pi2.remove();

  }

}

  function pijoin(pi1, pi2){

  var p1 = pi1.pathPoints;

  var p2 = pi2.pathPoints;

  var d = dist(p1[p1.length-1].anchor, p2[0].anchor);

  if (d==0)

  {

  

    p1[p1.length-1].remove();

  

  

    var pp1 = p1.add();

    var p2i = p2[0];

    pp1.anchor         = p2i.anchor;

    pp1.rightDirection = p2i.rightDirection;

    pp1.leftDirection  = p2i.leftDirection;

    pp1.pointType      = p2i.pointType;

  var pp1=p1.add();

  var p2i=p2[1];

  pp1.anchor         = p2i.anchor;

    pp1.rightDirection = p2i.rightDirection;

    pp1.leftDirection  = p2i.leftDirection;

    pp1.pointType      = p2i.pointType;

p2[0].remove();

  }

else

{

  

}

}

  function dist(p1, p2) {

  return Math.sqrt(dist2(p1, p2));

}

function cmpLen(pinfo, p, p2, i, idx1, idx2){

  var d = dist2( p[idx1].anchor, p2[idx2].anchor);

  if(!pinfo.child || d < pinfo.d){

    pinfo.d = d;               // squared distance

    pinfo.child = p2.parent;   // PathItem

    pinfo.cIdx = i;            // child index in extracted selection

    pinfo.pPntIdx = idx1;      // pathPoints index of parent

    pinfo.cPntIdx = idx2;      // pathPoints index of child

  }

}

function dist2(p1, p2) {

  var dx = p1[0] - p2[0];

  var dy = p1[1] - p2[1];

  return dx*dx + dy*dy;

}

  function readjustAnchors(pitem, marge_if_nearer_than){

  var pp = pitem.pathPoints;

  if(marge_if_nearer_than == 0) return;

  if(pp.length < 2) return;

}

  function getPathItemsInSelection(n, paths){

  if(documents.length < 1) return;

  var s = activeDocument.selection;

  if (!(s instanceof Array) || s.length < 1) return;

  extractPaths(s, n, paths);

}

function pireverse(pitem){

  var pp = pitem.pathPoints;

  var arr = new Array(pp.length);

  var z = pp.length - 1;

  

  for(var i=0; i <= z; i++) {

    var p = pp;

    arr[z - i] = [p.anchor, p.rightDirection, p.leftDirection, p.pointType];

  }

  for(var i=0; i <= z; i++) {

    var p = pp;

    var r = arr;

    p.anchor         = r[0];

    p.leftDirection  = r[1];

    p.rightDirection = r[2];

    p.pointType      = r[3];

  }

}

function extractPaths(s, pp_length_limit, paths){

  for(var i = 0, iend = s.length; i < iend; i++){

    if(s.locked || s.hidden){

      continue;

    } else if(s.typename == "PathItem"){

   

      paths.push(s);

    

    } else if(s.typename == "GroupItem"){

   

      extractPaths(s.pageItems, pp_length_limit, paths);

    

    } else if(s.typename == "CompoundPathItem"){

   

      extractPaths(s.pathItems, pp_length_limit , paths);

    }

  }

}

This topic has been closed for replies.