I'm really new to QCAD and I am working on a script to discretize an arc.
I have the mathematical logic and it works on a non-interactive script, but I would like the user to be able to chose the arc they want when the interactive script is running.
And I don't know how to keeps the selecting mouse when running the script and what functions to use to grab the user selection.
Currently, my script looks like that (don't know if there are bugs inside, since I cannot test since I cannot select an arc):
Code: Select all
include("scripts/EAction.js");
function DiscretizeArc(guiAction) {
EAction.call(this, guiAction);
this.nb_points = undefined;
this.arc = undefined;
this.setUiOptions("DiscretizeArc.ui");
}
DiscretizeArc.prototype = new EAction();
DiscretizeArc.prototype.beginEvent = function() {
EAction.prototype.beginEvent.call(this);
this.di = this.getDocumentInterface();
this.di.setClickMode(RAction.PickArc);
};
DiscretizeArc.prototype.pickArc = function(event, entity, preview) {
if (entity.getType() === 22) {
this.arc = entity;
if (preview) {
this.updatePreview();
}
else {
this.applyOperation();
}
}
else {
EAction.handleUserMessage("Select an arc");
}
};
DiscretizeArc.prototype.getOperation = function(preview) {
var doc = this.getDocument();
var arcInfo = {
centerX: this.arc.getCenter().x,
centerY: this.arc.getCenter().y,
startPointX: this.arc.getStartPoint().x,
startPointY: this.arc.getStartPoint().y,
startEndX: this.arc.getEndPoint().x,
startEndY: this.arc.getEndPoint().y,
radius: getRadius(),
startAngle: getStartAngle(),
angle: getAngleLenght()
}
var arc_angle_n = arcInfo.angle/(nb_points-1);
var line_entity;
var op = new RAddObjectOperation();
var x_temp = arcInfo.startPointX; var y_temp = arcInfo.startPointY;
for (i=1; i<this.nb_points-1; i++) {
var Dx = arcInfo.radius*cos(arcInfo.startAngle+arc_angle_n*i)+arcInfo.centerX;
var Dy = arcInfo.radius*sin(arcInfo.startAngle+arc_angle_n*i)+arcInfo.centerY;
line_entity = new RLineEntity(doc, new RLineData(x_temp, y_temp, Dx, Dy));
op.addObject(line_entity);
this.di.applyOperation(op);
x_temp = Dx; y_temp = Dy;
}
line_entity = new RLineEntity(doc, new RLineData(x_temp,y_temp, arcInfo.startEndX,arcInfo.startEndY));
op.addObject(line_entity);
this.di.applyOperation(op);
var id = this.arc.getId();
var entity = doc.queryEntity(id);
op = new RDeleteObjectsOperation();
op.deleteObject(entity);
this.di.applyOperation(op);
return op;
}
DiscretizeArc.prototype.slotNumberOfPointsChanged = function(v) {
this.number = v;
this.updatePreview();
};
DiscretizeArc.init = function(basePath) {
var action = new RGuiAction(qsTr("&DiscretizeArc"), RMainWindowQt.getMainWindow());
action.setRequiresDocument(true);
action.setScriptFile(basePath + "/DiscretizeArc.js");
action.setGroupSortOrder(100000);
action.setSortOrder(0);
action.setWidgetNames(["ExamplesMenu"]);
};