Hello
win 8.1
QCAD/CAM Professional 3.14.0.1
Revision: b77f907
I'm new to scripting in Qcad where I'm having problems getting a simplistic parabola to draw on the sheet. I've used the spiral example as a template but it is creating the drawing parametrically where as I would just like to draw the simple function f(x)=x^2.
I think my problem in in my approach to which classes I should be using but unfortunately the tutorials and reference is not making this clear to me.
Please look at the code below and any suggestions to get me started would be appreciated. Thanks
include("../MathExamples.js");
/**
* This action draws a simple spiral (center at 0/0).
*/
function parabolic(guiAction) {
MathExamples.call(this, guiAction);
}
parabolic.prototype = new MathExamples();
/**
* Draws a spiral and terminates.
*/
parabolic.prototype.beginEvent = function() {
MathExamples.prototype.beginEvent.call(this);
var document = this.getDocument();
var di = this.getDocumentInterface();
var operation = new RAddObjectsOperation(true);
// see the spiral example as this has changed
var xDelta = 0.1;
var c = 0;
for (var x = -10.0; x < 10 ; x += xDelta) {
var lineData = new RLineData(
new RVector(
Math.pow(x, 2)
)
);
var line = new RLineEntity(document, lineData);
operation.addObject(line);
// ++c;
// x += xDelta;
}
di.applyOperation(operation);
this.terminate();
};
/**
* Adds a menu for this action to Examples/Math Examples/Spiral.
*/
parabolic.init = function(basePath) {
var action = new RGuiAction(qsTr("&Parab"), RMainWindowQt.getMainWindow());
action.setRequiresDocument(true);
action.setScriptFile(basePath + "/parabolic.js");
action.setGroupSortOrder(79700);
action.setSortOrder(100);
action.setWidgetNames(["MathExamplesMenu"]);
};
Script, basic concepts math spiral
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files and screenshots.
Post one question per topic.
Re: Script, basic concepts math spiral
RLineData needs two RVector arguments (start and end point).
RVector needs two number arguments (x and y).
Code: Select all
var lineData = new RLineData(new RVector(someXValue, someYValue), new RVector(Math.pow(x, 2), y));
You might find it easer to use the QCAD simple API:
Code: Select all
addLine(x1, y1, x2, y2);
Code: Select all
addPolyline([x1, y1, x2, y2, x3, y3, ...]);
Re: Script, basic concepts math spiral
Thanks Andrew,
That was most helpful and I feel a bit foolish for not picking that up.
What I didn't realize was that the spiral script was creating a lot of small line segments
Anyway my first ever script is now working with the code block below. (probably not that efficiently)
var xDelta = 0.1;
for (var x = -10.0; x < 10 ; x += xDelta) {
var lineData = new RLineData(
new RVector( x - xDelta , Math.pow(x - xDelta, 2)), //start point
new RVector( x, Math.pow(x, 2)) // end point
);
var line = new RLineEntity(document, lineData);
operation.addObject(line);
}
That was most helpful and I feel a bit foolish for not picking that up.
What I didn't realize was that the spiral script was creating a lot of small line segments
Anyway my first ever script is now working with the code block below. (probably not that efficiently)
var xDelta = 0.1;
for (var x = -10.0; x < 10 ; x += xDelta) {
var lineData = new RLineData(
new RVector( x - xDelta , Math.pow(x - xDelta, 2)), //start point
new RVector( x, Math.pow(x, 2)) // end point
);
var line = new RLineEntity(document, lineData);
operation.addObject(line);
}
Re: Script, basic concepts math spiral
If you want to produce a polyline instead, you can use the following approach:
Code: Select all
var vertices = [];
for (...) {
vertices.push([x, y]);
}
addPolyline(vertices);