Script, basic concepts math spiral

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules
Always indicate Operating system, QCAD version
Attach Drawing files, Screenshots
Post one question per topic.
Post Reply
rickiiy
Registered Member
Posts: 2
Joined: Thu May 24, 2018 4:55 am

Script, basic concepts math spiral

Post by rickiiy » Thu May 24, 2018 5:51 am

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"]);
};

User avatar
andrew
Site Admin
Posts: 5552
Joined: Fri Mar 30, 2007 6:07 am

Re: Script, basic concepts math spiral

Post by andrew » Thu May 24, 2018 8:58 am

rickiiy wrote:
Thu May 24, 2018 5:51 am
var lineData = new RLineData(new RVector(Math.pow(x, 2)));
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));
Where someXValue, someYValue are your x,y values for the start point of the line.

You might find it easer to use the QCAD simple API:

Code: Select all

addLine(x1, y1, x2, y2);
or for a single polyline:

Code: Select all

addPolyline([x1, y1, x2, y2, x3, y3, ...]);
No need for operations, etc. in the QCAD Simple API.

rickiiy
Registered Member
Posts: 2
Joined: Thu May 24, 2018 4:55 am

Re: Script, basic concepts math spiral

Post by rickiiy » Thu May 24, 2018 9:48 am

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);

}

User avatar
andrew
Site Admin
Posts: 5552
Joined: Fri Mar 30, 2007 6:07 am

Re: Script, basic concepts math spiral

Post by andrew » Thu May 24, 2018 9:57 am

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);

Post Reply

Return to “QCAD 'How Do I' Questions”