unite 2 scripts

Drop in here to discuss whatever you want.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
fruit 89
Newbie Member
Posts: 7
Joined: Sat Jul 13, 2024 6:46 am

unite 2 scripts

Post by fruit 89 » Fri Sep 05, 2025 11:47 am

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)

CVH
Premier Member
Posts: 4879
Joined: Wed Sep 27, 2017 4:17 pm

Re: unite 2 scripts

Post by CVH » Fri Sep 05, 2025 2:42 pm

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

fruit 89
Newbie Member
Posts: 7
Joined: Sat Jul 13, 2024 6:46 am

Re: unite 2 scripts

Post by fruit 89 » Fri Sep 05, 2025 8:02 pm

hi
thanks this works :D

Regards
Flo

CVH
Premier Member
Posts: 4879
Joined: Wed Sep 27, 2017 4:17 pm

Re: unite 2 scripts

Post by CVH » Sat Sep 06, 2025 1:25 am

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

Post Reply

Return to “Chat”