Strict Standards: Only variables should be passed by reference in /home/abeall/public_html/fireworks/download.php on line 28

Warning: Cannot modify header information - headers already sent by (output started at /home/abeall/public_html/fireworks/download.php:28) in /home/abeall/public_html/fireworks/download.php on line 44
// Fireworks JavaScript Command // Install by copying to Fireworks/Configuration/Commands/ // Aaron Beall 2008 - http://abeall.com // Version 1.2 /* - [CHANGED-v1.1] copyObject/pasteObject to copyNode/pasteNode - [CHANGED-v1.2] changed all for..in loops to strict loops */ var dom = fw.getDocumentDOM(); var sel = [].concat(fw.selection); function WeldPoints(){ // require active document if (!dom) return false; // validate selection var nod, p1, p2, points = [], selectedNodes = []; function validateSelection(){ p1 = null; p2 = null; points = []; selectedNodes = []; var s = sel.length; while(s--){ if(sel[s] == '[object Path]'){ var c = sel[s].contours.length; while(c--){ var n = sel[s].contours[c].nodes.length; while(n--){ nod = sel[s].contours[c].nodes[n]; if(nod.isSelectedPoint){ selectedNodes[n] = true; if(!p1){ if(n == 0 || n == sel[s].contours[c].nodes.length - 1){ p1 = {s:s, c:c, n:n, node:nod}; points.push(p1); }else{ points.push({s:s, c:c, n:n, node:nod}); } }else if(!p2){ if(n == 0 || n == sel[s].contours[c].nodes.length - 1){ p2 = {s:s, c:c, n:n, node:nod}; points.push(p2); }else{ points.push({s:s, c:c, n:n, node:nod}); } }else{ points.push({s:s, c:c, n:n, node:nod}); } } } } } } } validateSelection(); if(points.length < 2) return alert('To weld, you must select two open endpoints, or at least two adjacent points on the same contour.'); // if two endpoints are selected on different paths, join the paths if((p1 && p2) && (p1.s != p2.s && p1.s != p2.s)){ // welding emdpoints on different paths completely // join paths fw.selection = [sel[p1.s],sel[p2.s]]; sel[p1.s].contours[p1.c].nodes[p1.n].isSelectedPoint = true; // restore point selection after fw.selection resets it sel[p2.s].contours[p2.c].nodes[p2.n].isSelectedPoint = true; dom.joinPaths(); // re-evaluate selection sel = [].concat(fw.selection); validateSelection(); /*p1 = null; p2 = null; points = []; selectedPoints = []; for(var s in sel){ for(var c in sel[s].contours){ for(var n in sel[s].contours[c].nodes){ nod = sel[s].contours[c].nodes[n]; if(nod.isSelectedPoint){ selectedNodes[n] = true; points.push({s:s,c:c,n:n,node:nod}); } } } }*/ } // weld points if((!p1 || !p2) || (p1.c == p2.c && p1.s == p2.s)){ // not welding endpoints, or endepoints on the same contour // sort points by node position function byNode(a,b){ return a.n - b.n; } points.sort(byNode); // check to see if all nodes are on the same contour, and adjacent var weldLoop = points[0].n == 0 && points[points.length-1].n == sel[points[0].s].contours[points[0].c].nodes.length-1; var prevPoint = points[0]; var plen = points.length; for(var p = 0; p < plen; p++){ if(points[p].s != prevPoint.s || points[p].c != prevPoint.c) return alert('Unable to weld, because the points you have selected are not on the same contour and are not endpoints.'); prevPoint = points[p]; } prevPoint = points[points.length - 1], gap = false; for(var p = points.length - 1; p >= 0; p--){ if(Number(prevPoint.n) - Number(points[p].n) > 1){ if(weldLoop && !gap) gap = true; else return alert('Unable to weld, because the points you have selected are not adjacent.'); } prevPoint = points[p]; } // make sure contour has enough points in the first place if(sel[points[0].s].contours[points[0].c].nodes.length < 3) return alert('Unable to weld, because the contour must have at least 3 points.'); // find the average of all adjacent points, this will be the weld result var x = 0, y = 0, plen = points.length; for(var i = 0; i < plen; i++){ x += points[i].node.x; y += points[i].node.y; } var avgX = x/points.length, avgY = y/points.length; var pred = weldLoop ? 'succ' : 'pred'; var succ = weldLoop ? 'pred' : 'succ'; var firstPoint, lastPoint; if(weldLoop){ var plen = points.length; for(var p = 1; p < plen; p++){ if(Number(points[p].n) - Number(points[p-1].n) != 1 && !firstPoint){ firstPoint = points[p-1]; lastPoint = points[p]; var switchHandles = true; break; } } }else{ firstPoint = points[0]; lastPoint = points[points.length-1]; } var weldPoint = { x:avgX, y:avgY, predX:avgX + (firstPoint.node[pred+'X'] - firstPoint.node.x), predY:avgY + (firstPoint.node[pred+'Y'] - firstPoint.node.y), succX:avgX + (lastPoint.node[succ+'X'] - lastPoint.node.x), succY:avgY + (lastPoint.node[succ+'Y'] - lastPoint.node.y), isSelectedPoint:true, isCurvePoint:false, name:null, randomSeed:0, dynamicInfo:[] } weldPoint.isCurvePoint = Math.abs(getAngleRadians({x:weldPoint.predX,y:weldPoint.predY},weldPoint) - getAngleRadians(weldPoint,{x:weldPoint.succX,y:weldPoint.succY})) < 0.00001; if(switchHandles) flipHandles(weldPoint); // weld the adjacent points on the contour var cont = sel[points[0].s].contours[points[0].c]; var nodes = cont.nodes, weldN = points[0].n, newNodes = []; var nlen = nodes.length; for(var n = 0; n < nlen; n++){ if(!selectedNodes[n]) newNodes.push(copyNode(nodes[n])); else if(n == weldN) newNodes.push(weldPoint); } // apply the welded nodes var isClosed = cont.isClosed || (p1!=undefined && p2!=undefined && p1.c == p2.c); sel[points[0].s].contours[points[0].c] = new Contour(); cont = sel[points[0].s].contours[points[0].c]; cont.isClosed = isClosed; var nlen = newNodes.length; for(var n = 0; n < nlen; n++){ cont.nodes[n] = new ContourNode(); pasteNode(cont.nodes[n], newNodes[n]); } }else{ // welding two endpoints // first, create the average weld point var x = (p1.node.x + p2.node.x)/2, y = (p1.node.y + p2.node.y)/2; var pred = p1.n == '0' ? 'succ' : 'pred'; var succ = p2.n == '0' ? 'succ' : 'pred'; var weldPoint = { x:x, y:y, predX:x + (p1.node[pred+'X'] - p1.node.x), predY:y + (p1.node[pred+'Y'] - p1.node.y), succX:x + (p2.node[succ+'X'] - p2.node.x), succY:y + (p2.node[succ+'Y'] - p2.node.y) } weldPoint.isCurvePoint = Math.abs(getAngleRadians({x:weldPoint.predX,y:weldPoint.predY},weldPoint) - getAngleRadians(weldPoint,{x:weldPoint.succX,y:weldPoint.succY})) < 0.00001; // now weld them var cont1 = sel[p1.s].contours[p1.c], cont2 = sel[p2.s].contours[p2.c]; //if(p1.s == p2.s && p1.c != p2.c){ // welding on the same path, but different contours // flip the weldpoint handles if necessary; this was determine through brute testing if(p1.n == 0) flipHandles(weldPoint); // merge contours into one contour, in correct node order var newNodes = []; if(p1.n == 0){ if(p2.n == 0){ //alert('p1 start, p2 start'); for(var n = cont2.nodes.length - 1; n >= 0; n--){ if(n == p2.n) continue; newNodes.push(copyNode(cont2.nodes[n])); flipHandles(newNodes[newNodes.length-1]); } }else{ //alert('p1 start, p2 end'); for(var n = 0; n < cont2.nodes.length; n++){ if(n == p2.n) continue; newNodes.push(copyNode(cont2.nodes[n])); } } //newNodes[newNodes.length-1].succX = newNodes[newNodes.length-1].x; //newNodes[newNodes.length-1].succY = newNodes[newNodes.length-1].y; //cont1.nodes[0].predX = cont1.nodes[0].x; //cont1.nodes[0].predY = cont1.nodes[0].y; for(var n = 0; n < cont1.nodes.length; n++){ newNodes.push(n == p1.n ? weldPoint : copyNode(cont1.nodes[n])); } }else{ if(p2.n == 0){ //alert('p1 end, p2 start'); for(var n = cont2.nodes.length - 1; n >= 0; n--){ if(n == p2.n) continue; newNodes.push(copyNode(cont2.nodes[n])); flipHandles(newNodes[newNodes.length - 1]); } }else{ //alert('p1 end, p2 end'); for(var n = 0; n < cont2.nodes.length; n++){ if(n == p2.n) continue; newNodes.push(copyNode(cont2.nodes[n])); } } //newNodes[newNodes.length-1].succX = newNodes[newNodes.length-1].x; //newNodes[newNodes.length-1].succY = newNodes[newNodes.length-1].y; //cont1.nodes[cont1.nodes.length-1].succX = cont1.nodes[cont1.nodes.length-1].x; //cont1.nodes[cont1.nodes.length-1].succY = cont1.nodes[cont1.nodes.length-1].y; for(var n = cont1.nodes.length - 1; n >= 0; n--){ newNodes.push(copyNode(n == p1.n ? weldPoint : cont1.nodes[n])); flipHandles(newNodes[newNodes.length - 1]); } } // apply merged contour to contour 1 var isClosed = cont1.isClosed; sel[p1.s].contours[p1.c] = new Contour(); sel[p1.s].contours[p1.c].isClosed = isClosed; for(var n = 0; n < newNodes.length; n++){ sel[p1.s].contours[p1.c].nodes[n] = new ContourNode(); pasteNode(sel[p1.s].contours[p1.c].nodes[n], newNodes[n]); } // delete old contour 2 if(p2.c == sel[p2.s].contours.length - 1){ sel[p2.s].contours.length = sel[p2.s].contours.length - 1; }else{ fw.selection = [sel[p2.s]]; for(var c = 0; c < sel[s].contours.length; c++){ var nlen = sel[p2.s].contours[c].nodes.length; for(var n = 0; n < nlen; n++) sel[p2.s].contours[c].nodes[n].isSelectedPoint = false; } var nlen = cont2.nodes.length; for(var n = 0; n < nlen; n++){ cont2.nodes[n].isSelectedPoint = true; } dom.deleteSelection(false); } //} } } //try{ WeldPoints(); //}catch(e){ alert([e, e.lineNumber, e.fileName].join("\n")) }; // copy node function copyNode(pt){ var ptCopy = {} copyProps(ptCopy, pt, ["x", "y", "succX", "succY", "predX", "predY", "randomSeed", "isSelectedPoint", "isCurvePoint", "name"]); ptCopy.dynamicInfo = []; for(var i = 0; i < pt.dynamicInfo.length; i++) ptCopy.dynamicInfo[i] = copyProps({}, pt.dynamicInfo[i], ["pressure", "duration", "velocity"]); return ptCopy; } // paste node function pasteNode(pt, ptCopy){ copyProps(pt, ptCopy, ["x", "y", "succX", "succY", "predX", "predY", "randomSeed", "isSelectedPoint", "isCurvePoint", "name"]); var dynamicInfo = []; for(var i = 0; i < ptCopy.dynamicInfo.length; i++) dynamicInfo.push(copyProps(new ContourNodeDynamicInfo(), ptCopy.dynamicInfo[i], ["pressure", "duration", "velocity"])); pt.dynamicInfo = dynamicInfo; return pt; } // copy props by name function copyProps(targetObj, sourceObj, props){ var p = props.length; while(p--) targetObj[props[p]] = sourceObj[props[p]]; return targetObj; } // flip handles function flipHandles(pt){ var predX = pt.predX, predY = pt.predY, succX = pt.succX, succY = pt.succY; pt.predX = succX; pt.predY = succY; pt.succX = predX; pt.succY = predY; } // get angle in radians between p1 and p2 function getAngleRadians(p1,p2){ return -Math.atan2((p1.x-p2.x), (p1.y-p2.y)); }