1 Reply Latest reply on Nov 22, 2017 2:18 AM by r-bin

    Add subpath to existing path

    dmitryp35155038 Level 1

      I need to add a subpath to an existing path. The SubPathItems object doesn't provide the add method, so to add the subpath I create a new path that includes old subpaths and the new one, and then delete the old path. This causes flickering and doesn't look like the best solution.

       

      Is there a better way to add a subpath to an existing path?

        • 1. Re: Add subpath to existing path
          r-bin Level 5

          Try this method.

           

          var subpath = example_subpath();
          
          add_subpath(subpath);
          
          ////////////////////////////////////////////////////////////
          function add_subpath(subpath_info)
              {
              try
                  {
                  var d = new ActionDescriptor();
                  var r = new ActionReference();
          
                  r.putEnumerated(charIDToTypeID("Path"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
          
                  var path = executeActionGet(r).getObjectValue(stringIDToTypeID("pathContents"));           
          
                  var list = path.getList(stringIDToTypeID("pathComponents"));           
          
                  for (var i = 0; i < subpath_info.length; i++)
                      list.putObject(stringIDToTypeID("null"), subpath_to_desc(subpath_info[i]));
          
                  d.putReference( stringIDToTypeID( "null" ), r );
                  d.putList(charIDToTypeID("T   "), list);
          
                  executeAction(charIDToTypeID("setd"), d, DialogModes.NO);
          
                  return true;
          
                  function subpath_to_desc(pth)
                      {
                      try
                          {
                          var op = stringIDToTypeID("xor");
          
                          try {        
                              switch (pth.operation)
                                  {
                                  case ShapeOperation.SHAPEADD      : op = stringIDToTypeID("add");       break;
                                  case ShapeOperation.SHAPEINTERSECT: op = stringIDToTypeID("subtract");  break;
                                  case ShapeOperation.SHAPESUBTRACT : op = stringIDToTypeID("intersect"); break;
                                  case ShapeOperation.SHAPEXOR      : op = stringIDToTypeID("xor");       break;
                                  }
                              }
                          catch(e) {}
                  
                          var d = new ActionDescriptor();
                  
                          var subpathlist = new ActionList();              
                  
                          var d1 = new ActionDescriptor();
                  
                          try { d1.putBoolean(stringIDToTypeID("closedSubpath"), pth.closed); } catch(e) {}
                  
                          var points = new ActionList();              
                  
                          var pp = pth.entireSubPath;
                  
                          for (var i = 0; i < pp.length; i++)
                              {
                              var d2 = new ActionDescriptor();
          
                              var anchor = new ActionDescriptor();
                              anchor.putUnitDouble( stringIDToTypeID("horizontal"), charIDToTypeID( "#Rlt" ), pp[i].anchor[0]);
                              anchor.putUnitDouble( stringIDToTypeID("vertical"),   charIDToTypeID( "#Rlt" ), pp[i].anchor[1]);
                              d2.putObject(stringIDToTypeID("anchor"),   stringIDToTypeID("paint"), anchor);
                  
                              try {        
                                  var forward = new ActionDescriptor();
                                  forward.putUnitDouble( stringIDToTypeID("horizontal"), charIDToTypeID( "#Rlt" ), pp[i].leftDirection[0]);
                                  forward.putUnitDouble( stringIDToTypeID("vertical"),   charIDToTypeID( "#Rlt" ), pp[i].leftDirection[1]);
                                  d2.putObject(stringIDToTypeID("forward"),  stringIDToTypeID("paint"), forward);
                                  }
                              catch(e) {}
                  
                              try {        
                                  var backward = new ActionDescriptor();
                                  backward.putUnitDouble( stringIDToTypeID("horizontal"), charIDToTypeID( "#Rlt" ), pp[i].rightDirection[0]);
                                  backward.putUnitDouble( stringIDToTypeID("vertical"),   charIDToTypeID( "#Rlt" ), pp[i].rightDirection[1]);
                                  d2.putObject(stringIDToTypeID("backward"), stringIDToTypeID("paint"), backward);
                                  }
                              catch(e) {}
                  
                              try {        
                                  d2.putBoolean(stringIDToTypeID("smooth"), pp[i].kind==PointKind.SMOOTHPOINT?true:false);
                                  }
                              catch(e) {}
                  
                              points.putObject(stringIDToTypeID("null"), d2)
                              }
                  
                          d1.putList(stringIDToTypeID("points"), points)
                          subpathlist.putObject(stringIDToTypeID("null"), d1)
                          d.putEnumerated(stringIDToTypeID("shapeOperation"), stringIDToTypeID("shapeOperation"), op );
                          d.putList(stringIDToTypeID("subpathListKey"), subpathlist);
                  
                          return d;
                          }
                      catch (e) { alert(e); return null; }
                      }
                  }
              catch (e) { alert(e); return false; }
              }
          
          ////////////////////////////////////////////////////////////
          function example_subpath()
              {
              var pth = new Array();
              var p = new Array();
          
              p[0] = new PathPointInfo;   
              p[0].kind = PointKind.CORNERPOINT;  
              p[0].anchor = [100, 10];
              p[0].leftDirection  = p[0].anchor;
              p[0].rightDirection = p[0].anchor;
          
              p[1] = new PathPointInfo;   
              p[1].kind = PointKind.CORNERPOINT;  
              p[1].anchor = [150, 60]
              p[1].leftDirection  = p[1].anchor;
              p[1].rightDirection = p[1].anchor;
          
              p[2] = new PathPointInfo;   
              p[2].anchor = [50, 60];
          
              pth[0] = new SubPathInfo(); 
              pth[0].operation = ShapeOperation.SHAPEADD;
              pth[0].closed = true;
              pth[0].entireSubPath = p;
          
              return  pth;
              }