Hi, sure... Didn't we address this via PM?
You can indeed
Chat about anything related to QCAD here.
Rather a topic for the
QCAD Programming, Script Programming and Contributing forum.
Simply add any sourced position, any valid
RVector that you want to the position list.
But lets build in polyline support and some extra validation, shift some things, add comments, ...
Code: Select all
include("scripts/EAction.js");
include("scripts/library.js");
// User configurable parameters:
var distance = 2.0; // A valid number
var doFromStart = true; // true/false
var doFromEnd = true; // true/false
var document = EAction.getDocument();
var positions = [];
if (!isNull(document) && document.hasSelection() && isNumber(distance)) {
var ids = document.querySelectedEntities();
var id, entity, length, polyShape, points;
// Process all selected entities by ID:
for (var i=0; i<ids.length; i++) {
id = ids[i];
entity = document.queryEntityDirect(id); // No change required
// Only process entities with a length(+) and longer than the distance in absolute:
length = entity.getLength();
if (isNumberGreaterZero(length) && length > Math.abs(distance)) {
// Include position from start of entity:
if (doFromStart === true) {
if (isPolylineEntity(entity)) {
polyShape = entity.castToShape();
points = polyShape.getPointsWithDistanceToEnd(distance, RS.FromStart|RS.AlongPolyline);
}
else {
points = entity.getPointsWithDistanceToEnd(distance, RS.FromStart|RS.AlongPolyline);
}
// If any, only include valid positions:
if (!isNull(points) && points.length > 0 && isValidVector(points[0])) {
positions.push(points[0]);
}
}
// Include position from end of entity:
if (doFromEnd === true) {
if (isPolylineEntity(entity)) {
polyShape = entity.castToShape();
points = polyShape.getPointsWithDistanceToEnd(distance, RS.FromEnd|RS.AlongPolyline);
}
else {
points = entity.getPointsWithDistanceToEnd(distance, RS.FromEnd|RS.AlongPolyline);
}
// If any, only include valid positions:
if (!isNull(points) && points.length > 0 && isValidVector(points[0])) {
positions.push(points[0]);
}
}
// Add additional positions here:
// e.g. entity.getStartPoint() | entity.getMiddlePoint() ...
// Zero from start is also equal to the start point itself
// A negative distance is before/after the entity in normal sense
// As implemented it would skip -(distance(-)) <= entity length
}
}
// If any, store positions to reuse as coordinates list for drawing tools:
if (positions.length > 0) {
var appWin = EAction.getMainWindow();
if (!isNull(appWin)) {
appWin.setProperty("InfoStorePositions", positions);
EAction.handleUserInfo("Stored %1 positions.".arg(positions.length));
}
else {
EAction.handleUserWarning("Failed to store positions.");
}
}
else {
EAction.handleUserWarning("No positions to store.");
}
}
else {
// Warn user on critical faults:
EAction.handleUserWarning("No document, no selection or not a valid distance.");
}
Tested as JS script but it would fail in the Script Shell (
GE) for the last
else {...} clause with:
SyntaxError: Parse error
Then don't include the complete last else clause including the warning for use with
GE.
Or save the complete script as a *.js file and execute it with Misc .. Development .. Run Script (
XC)
Regards,
CVH