Page 1 of 1

unite 2 scripts

Posted: Fri Sep 05, 2025 11:47 am
by fruit 89
hi can i unite 2 scripst?

Code: Select all

    include("scripts/EAction.js"); 
    var distance = 2;
    var document = EAction.getDocument();

    if (document.hasSelection()) {
        var ids = document.querySelectedEntities();
    }

    var positions = [];
    for (var i=0; i<ids.length; i++) {
        var id = ids[i];
        var entity = document.queryEntity(id);
        if (entity.getLength() > distance) {
            var points = entity.getPointsWithDistanceToEnd(distance, RS.FromStart);
            if (isValidVector(points[0])) {
                positions.push(points[0]);
            }
        }
    }

    var appWin = EAction.getMainWindow();
    appWin.setProperty("InfoStorePositions", positions);

Code: Select all

    include("scripts/EAction.js"); 
    var distance = 2;
    var document = EAction.getDocument();

    if (document.hasSelection()) {
        var ids = document.querySelectedEntities();
    }

    var positions = [];
    for (var i=0; i<ids.length; i++) {
        var id = ids[i];
        var entity = document.queryEntity(id);
        if (entity.getLength() > distance) {
            var points = entity.getPointsWithDistanceToEnd(distance, RS.FromEnd);
            if (isValidVector(points[0])) {
                positions.push(points[0]);
            }
        }
    }

    var appWin = EAction.getMainWindow();
    appWin.setProperty("InfoStorePositions", positions);
the difference is only the distance from Start...End (RS.From)

Re: unite 2 scripts

Posted: Fri Sep 05, 2025 2:42 pm
by CVH
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

Re: unite 2 scripts

Posted: Fri Sep 05, 2025 8:02 pm
by fruit 89
hi
thanks this works :D

Regards
Flo

Re: unite 2 scripts

Posted: Sat Sep 06, 2025 1:25 am
by CVH
fruit 89 wrote:
Fri Sep 05, 2025 8:02 pm
thanks this works
It should but I can't fix the Script Shell failing for the last else clause.
- Adding an empty line or a line with a comment doesn't work.
- Converting Windows EOL to Unix EOL has no effect.

It seems that: if (...) {....} and else {....} in the top level are evaluated fully separately as string.
Then the former condition is unknown and a syntax error is thrown for else whatever.
Also see this related topic.


Also see the reply by PM about using RS.FromAny.
getPointsWithDistanceToEnd is expected to return an array, at least empty.
Variable points might then hold more than one item of interest.
Each of those array items might be a valid RVector to include as position.


If considered as solved, please add [SOLVED] to the title of the initial post by editing it.

Regards,
CVH