//polychain2curve.ls // //february 12 2010 //clinton reese //converter single 2 point polychain to a curve // // //select the start point of the poly chain and run the script //curve will be created in the same layer // @version 2.2 @warnings @script modeler //global var pntindex = 0; //point index and curve point count fgLayer = 0; //forground layer emptyLyr = 0; //empty layers main { //NEED SINGLE POINT TEST // note fg layer fgLayer = lyrfg(); numfg = size(fgLayer); if(numfg>1) { error("More than 1 foreground layer is active"); } // note bg layer bgLayer = lyrbg(); if(bgLayer==nil) { error("No background layer selected."); } numbg = size(bgLayer); if(numbg != 1) { error("More than 1 background layer is active"); } pntindex = 1;//initialize point index selmode(USER); //save first point and first polygon index list editbegin(); pnts[pntindex] = points[1];//save first point mypolyindexlist = points[1].polygon();//polygon index list editend(); selpoint(CLEAR); // clear any existing selections //select all layers into the foreground then do it //need all layers so polgons array has all info needed alllayers = lyrdata(); lyrsetfg(alllayers); selmode(GLOBAL); editbegin(); //get points for the current polygon curpoly = polygons[mypolyindexlist[1]]; prevpolyindex = mypolyindexlist[1]; //make sure 2 point polygons only numpolypoints = polypointcount(curpoly); if(numpolypoints != 2) error("Not a 2 point poly chain."); //get new point if(curpoly.points[1] != pnts[pntindex]) { newpoint = curpoly.points[1]; } if(curpoly.points[2] != pnts[pntindex]) { newpoint = curpoly.points[2]; } editend(); //add the new point to the list pntindex = pntindex + 1; pnts[pntindex] = newpoint;//add to list of points //now have first polygon and first 2 points //loop until new point only has 1 polygon == end of the chain mypolycount=2; editbegin(); while(mypolycount==2) { //editbegin(); //get next polygon mypolyindexlist = pnts[pntindex].polygon();//list of polygons indexes for point if(mypolyindexlist[1] != prevpolyindex) { newpolyindex = mypolyindexlist[1]; } if(mypolyindexlist[2] != prevpolyindex) { newpolyindex = mypolyindexlist[2]; } prevpolyindex = newpolyindex; //get next vertex using the polygon newpoly = polygons[newpolyindex]; //make sure 2 point polygons only numpolypoints = polypointcount(newpoly); if(numpolypoints != 2) error("Not a 2 point poly chain."); if(pnts[pntindex] != newpoly.points[1]) { newpoint = newpoly.points[1]; } if(pnts[pntindex] != newpoly.points[2]) { newpoint = newpoly.points[2]; } pntindex = pntindex + 1; pnts[pntindex] = newpoint;// add new vertex to the list //get polygon count value for this vertex mypolycount = pnts[pntindex].polygon; //editend(); } editend(); lyrsetfg(fgLayer);//back to 2 point poly chain layer //clear selection and select points in order selmode(USER); curvepointcount = pntindex; selpoint(CLEAR); for(j=1;j<=curvepointcount;j++) { selpoint(SET, POINTID, pnts[j]); } //create a curve editbegin(); curveid = addcurve(points); editend(); //selpoint(CLEAR);//must deselect points before select curve selpolygon(SET, CURVE); //selpolygon(SET, POLYID, curveid);//this does not work for some reason cut(); lyrsetfg(bgLayer); paste(); lyrsetfg(fgLayer);//back to 2 point poly chain layer lyrsetbg(bgLayer);//restore background //selpoint(CLEAR); } mainOLD { // note fg layer fgLayer = lyrfg(); numfg = size(fgLayer); if(numfg>1) { error("More than 1 foreground layer is active"); } // note free layers emptyLyr = lyrempty(); // first and second in [1] and [2] alllayers = lyrdata(); selmode(DIRECT); //save list of all selected end points count = editbegin(); mypoints = points; editend(); selmode(USER); //check for point selection if(count==0) error("Must select points first"); selpoint(CLEAR); // clear any existing selections //for each 2 point poly chain create a curve for(i=1;i<=count;i++) { //build list for current curve selList = buildSelectionList(mypoints[i]); //clear selection and select points in order selmode(USER); curvepointcount = pntindex; selpoint(CLEAR); for(j=1;j<=curvepointcount;j++) { selpoint(SET, POINTID, selList[j]); } //create a curve and move it to a new layer editbegin(); addcurve(points); editend(); selpolygon(SET, CURVE); cut(); lyrsetfg(emptyLyr[1]); paste(); lyrsetfg(fgLayer);//back to 2 point poly chain layer selpoint(CLEAR); } //name the new layer lyrsetfg(emptyLyr[1]); setlayername("curveFromPoly"); lyrsetfg(fgLayer); } buildSelectionList: firstPoint { pntindex = 1;//initialize point index //select the first point selmode(USER); selpoint(SET,POINTID,firstPoint); //save first point and first polygon index list editbegin(); pnts[pntindex] = points[1];//save first point mypolyindexlist = points[1].polygon();//polygon index list editend(); selpoint(CLEAR); // clear any existing selections //get polygon count of layers above the foreground layer polyOffset = 0; //if(fgLayer[1] != 1) //{ // k=1; // while(k