/** * Simple Creation API, mainly designed for use in the ECMAScript console. */ /** * Sets the current color for newly added entities * \ingroup ecma_simple * * \code * setCurrentColor(new RColor(255, 255, 255)) * setCurrentColor(new RColor("white")) * setCurrentColor(255, 255, 255) * \endcode */ function setCurrentColor(color) { var di = getTransactionDocumentInterface(); if (isNull(di)) { di = getTransactionDocument(); } if (isNull(di)) { qWarning("no document interface or document"); return; } // r, g, b: if (arguments.length===3) { setCurrentColor(new RColor(arguments[0], arguments[1], arguments[2])); return; } di.setCurrentColor(color); } /** * Checks if the given layer exists. * \ingroup ecma_simple * * \code * hasLayer("MyLayer1") * \endcode */ function hasLayer(name) { var doc = getTransactionDocument(); if (isNull(doc)) { return false; } return doc.hasLayer(name); } /** * Adds a layer to the drawing. * \ingroup ecma_simple * * \code * addLayer("MyLayer1") * addLayer("MyLayer2", "white", "CONTINUOUS", RLineweight.Weight025) * \endcode */ function addLayer(name, colorName, linetypeName, lineWeight) { var doc = getTransactionDocument(); if (isNull(doc)) { return undefined; } if (isNull(colorName)) { colorName = "white"; } if (isNull(linetypeName)) { linetypeName = "CONTINUOUS"; } if (isNull(lineWeight)) { lineWeight = RLineweight.Weight000; } if (doc.hasLayer(name)) { return undefined; } var layer = new RLayer( doc, name, false, false, new RColor(colorName), doc.getLinetypeId(linetypeName), lineWeight, false ); return addObject(layer); } /** * Sets the current layer to the given layer. * \ingroup ecma_simple * * \param layerName Layer name or ID. */ function setCurrentLayer(layerName) { var di = getTransactionDocumentInterface(); if (isNull(di)) { return false; } di.setCurrentLayer(layerName); } /** * Adds a point to the drawing. * \ingroup ecma_simple * * \code * addPoint(x,y) * addPoint([x,y]) * addPoint(new RVector(x,y)) * \endcode */ function addPoint(position) { if (arguments.length===2) { return addPoint(new RVector(arguments[0], arguments[1])); } if (isArray(position)) { return addPoint(new RVector(position)); } return addShape(new RPoint(position)); } /** * Adds a line to the drawing. * \ingroup ecma_simple * * \code * addLine(x1,y1, x2,y2) * addLine([x1,y1], [x2,y2]) * addLine(new RVector(x1,y1), new RVector(x2,y2)) * \endcode */ function addLine(startPoint, endPoint) { if (arguments.length===4) { return addLine(new RVector(arguments[0], arguments[1]), new RVector(arguments[2], arguments[3])); } if (isArray(startPoint)) { startPoint = new RVector(startPoint); } if (isArray(endPoint)) { endPoint = new RVector(endPoint); } return addShape(new RLine(startPoint, endPoint)); } /** * Adds an infinite line to the drawing. * The two vectors are the base point and the direction vector * (point relative to base point) * \ingroup ecma_simple * * \code * addXLine(x1,y1, dx,dy) * addXLine([x1,y1], [dx,dy]) * addXLine(new RVector(x1,y1), new RVector(dx,dy)) * \endcode */ function addXLine(startPoint, directionVector) { if (arguments.length===4) { return addXLine(new RVector(arguments[0], arguments[1]), new RVector(arguments[2], arguments[3])); } if (isArray(startPoint)) { startPoint = new RVector(startPoint); } if (isArray(directionVector)) { directionVector = new RVector(directionVector); } return addShape(new RXLine(startPoint, directionVector)); } /** * Adds an arc to the drawing. * \ingroup ecma_simple * * \code * addArc(cx,cy, radius, startAngle, endAngle, reversed) * addArc([cx,cy], radius, startAngle, endAngle, reversed) * addArc(new RVector(cx,cy), radius, startAngle, endAngle, reversed) * \endcode */ function addArc(center, radius, startAngle, endAngle, reversed) { if (arguments.length===6) { return addArc(new RVector(arguments[0], arguments[1]), arguments[2], arguments[3], arguments[4], arguments[5]); } if (isArray(center)) { center = new RVector(center); } if (!isVector(center)) { qWarning("invalid center"); throw("invalid center"); } return addShape(new RArc(center, radius, deg2rad(startAngle), deg2rad(endAngle), reversed)); } /** * Adds a circle to the drawing. * \ingroup ecma_simple * * \code * addCircle(cx,cy, radius) * addCircle([cx,cy], radius) * addCircle(new RVector(cx,cy), radius) * \endcode */ function addCircle(center, radius) { if (arguments.length===3) { return addCircle(new RVector(arguments[0], arguments[1]), arguments[2]); } if (isArray(center)) { center = new RVector(center); } return addShape(new RCircle(center, radius)); } /** * Adds a polyline to the drawing. * \ingroup ecma_simple * \author Andrew Mustun * \author tukuyomi * * \param p Array of type [ [ x, y, bulge, relative ], ..., [ ... ] ] * or [ [ vector, bulge, relative ], ..., [ ... ] ] * Where: * x: X coordinate * y: Y coordinate * vector: RVector with position of vertex * bulge: Bulge for next segment after vertex, defaults to 0.0. * relative: True if this vector's coordinates are relative to the previous coordinates, * defaults to false. * * \param closed True for an implicitly closed polyline. Default is false. * \param relative True to treat all coordinates as relative. * The first coordinate is always absolute. Default is false. * * \code * addPolyline([[x1,y1],[x2,y2],[x3,y3]], false) * addPolyline([ [ 100 , 0 ], [ 20 , -10 , 1 ], [ 0 , 40 , 0 , true ], [ -20 , -10 , 1 , true ] ]) * addPolyline([new RVector(x1,y1),new RVector(x2,y2),new RVector(x3,y3)], closed, relative) * addPolyline([[new RVector(x1,y1),bulge1,rel1],[new RVector(x2,y2),bulge2,rel2],[new RVector(x3,y3),bulge3,rel3]], closed, relative) * \endcode */ function addPolyline(points, closed, relative) { if (isNull(closed)) { closed = false; } if (isNull(relative)) { relative = false; } var pl = new RPolyline(); pl.setClosed(closed); var v = new RVector(0,0); for (var i=0; i