I want to make some random lines or

This forum is for 'Work-in-Progress' QCAD user developed script Add Ons and Plug-ins.

Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: andrew, Husky, J-J

Post Reply
fdixon
Junior Member
Posts: 21
Joined: Thu Sep 17, 2020 6:22 pm

I want to make some random lines or

Post by fdixon » Sun Jan 02, 2022 1:07 am

edit: I hope I posted this in the right place. If not sorry I will move it.

I would like to make some random likes like I saw on the trial version SVG export I think.
I assume that is not a separate script.

I would like something like that though.

I did a search and browse in the script section, but I will honestly say I did not really understand much.
Could someone point my in the best direction to figure it out.
Is it possible there is a script similar to that?

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

Re: I want to make some random lines or

Post by CVH » Sun Jan 02, 2022 5:32 am

Hi,
Not sure what export you are referring to ...

Freehand lines see:
Menu Draw .. Lines .. Freehand Line (LF).
What are in fact chains of straight lines.

Controlled curving lines:
Menu Draw .. Splines
The Fit Points type (SL) will cross the indicated positions.

Remark that none of those is at random. :wink:

Regards,
CVH

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

Re: I want to make some random lines or

Post by andrew » Sun Jan 02, 2022 6:15 pm

You could whip up a simple script that does that. Have a look at:
https://www.qcad.org/en/tutorial-runnin ... rom-a-file

fdixon
Junior Member
Posts: 21
Joined: Thu Sep 17, 2020 6:22 pm

Re: I want to make some random lines or

Post by fdixon » Thu Jan 06, 2022 12:07 am

using the trial version of qcad, export a drawing to svg. there are random lines embedded in the svg.

I wanted to generate random lines sort of like that. I assume it still does that. I wanted to do something similar with a script.
I am hampered by not know how the scripting works but I can learn. I was looking for a hint for where to look to make random lines and maybe an example script. I am looking through the forum for ideas. just haven't checked for a few days.

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

Re: I want to make some random lines or

Post by CVH » Thu Jan 06, 2022 9:06 am

fdixon wrote:
Sun Jan 02, 2022 1:07 am
I did a search and browse in the script section, but I will honestly say I did not really understand much.
fdixon wrote:
Sun Jan 02, 2022 1:07 am
I assume that is not a separate script.
fdixon wrote:
Thu Jan 06, 2022 12:07 am
I was looking for a hint for where to look to make random lines and maybe an example script.
OK, got it now ... :wink:
Look no further, it is a Pro script and not readily available as open source.

Quite sure the standard function Math.random() comes into play.
What returns a random value from 0 to 1 excluding 1.

So you need a field of play to scale that up limited in X and Y, an RBox under QCAD.
That could be a defined area or whole the document.
In the latter case doc.getBoundingBox();

To add a line one needs a document, an interface, an operation and a valid layer to draw on.
A line is drawn between two vectors with both X & Y ... 4 random values.
Looping N times for N lines.

Code: Select all

    var doc = this.getDocument();
    var di = this.getDocumentInterface();
    // Initiate an operation:
    var op = new RAddObjectsOperation();
    // Set tool title used for undo/redo information:
    op.setText("Random lines");
    
    // Working area:    (Rule one of both options out)
    // A) Or defined by document:    (Requires a not empty document)
    var bbox = doc.getBoundingBox();
    // B) Or by given limits in X & Y:    (Fill in 2 opposite corners)
//    var bbox = new RBox(new RVector(xxx1, yyy1), new RVector(xxx2, yyy2));

    // Define lower left limit:    
    var minX = bbox.getMinimum().x;
    var minY = bbox.getMinimum().y;
    // Define size in X & Y:
    var sizeX = bbox.getWidth();
    var sizeY = bbox.getHeight();
    
    // Initiate end points:
    var startPoint = new RVector();
    var endPoint = new RVector();

    // Adding 10 random lines:
    var n = 10;
    var newLine; 
    for (i=0; i<n; i++) {    // Cycle lines
        // Define end points:
        startPoint.x = minX + Math.random() * sizeX;
        endPoint.x = minX + Math.random() * sizeX;
        startPoint.y = minY + Math.random() * sizeY;
        endPoint.y = minY + Math.random() * sizeY;

        // Create a new line entity:
        newLine = new RLineEntity(doc, new RLineData(startPoint, endPoint));

       // Add entity to operation:
       op.addObject(newLine);
    } // Loop lines
 
    // Apply all additions:      
    di.applyOperation(op);
This will cast 10 random lines on the current active layer of the current document.
For a specific layer that needs some adaptions.
When the current layer is not available to draw on, the standard exception warning is issued.
As is, on an empty document one gets 10 Null-length entities.

One can Copy/Paste this as text in the Script Shell (GE).
Or one can store it as a script (e.g. RandomLines.js) and run it with Run Script (XC).
fdixon wrote:
Sun Jan 02, 2022 1:07 am
I hope I posted this in the right place.
I will ask to move the topic to:
QCAD 'Script Add-On & Plug-in challenge' - Work in Progress

Regards,
CVH
Last edited by CVH on Fri Jan 07, 2022 2:36 pm, edited 1 time in total.

Panchdara
Premier Member
Posts: 185
Joined: Wed Nov 25, 2015 4:15 pm

Re: I want to make some random lines or

Post by Panchdara » Fri Jan 07, 2022 10:18 am

I tried this.

To draw a BoundingBox -> Misc/Draw/Draw Bounding Box. The Misc menu is not described in the Reference, however the Misc menu is mentioned https://www.qcad.org/rsforum/viewtopic. ... enu#p36005

However, nothing seems to happen when trying this option.

I guess the default BoundingBox is the rectangular area surrounding all entities on all layers - Misc/Draw/Draw Bounding Box seems to do nothing and is undocumented.

And for this to work, there must be an entity (size > 0) in the drawing for this to work as stated "(Requires a not empty document)".

Looking further - there is a bbox.bat file

Code: Select all

@echo off
qcadcmd.com -no-gui -allow-multiple-instances -autostart scripts\Pro\Tools\BoundingBox\BoundingBox.js %0 %*
that references BoundingBox, but there is no Pro subdirectory 🤔

very confusing, I opened up my own li'l can 'o worms. 👍

BTW, thanks CVH, this is interesting.

QCAD 3.27.1/Win 10
Windows 10

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

Re: I want to make some random lines or

Post by CVH » Fri Jan 07, 2022 2:20 pm

Panchdara wrote:
Fri Jan 07, 2022 10:18 am
To draw a BoundingBox -> Misc/Draw/Draw Bounding Box. The Misc menu is not described in the Reference, however the Misc menu is mentioned https://www.qcad.org/rsforum/viewtopic. ... enu#p36005
However, nothing seems to happen when trying this option.
Menu Misc .. Draw .. Draw Bounding Box requires a selection upfront.
- That would be visual with a grayed out icon but it hasn't any declared.
- That would be noted in the tooltip on a toolbar but it only occurs in a menu.

Drawing a bounding box around a selection has nothing to do with this topic or with the given script.

The working area 'option A' retrieves a bounding box around whole of the visible document:
https://qcad.org/doc/qcad/3.0/developer ... 22c0a301ed
'Option B' is ruled out by '//' at the beginning of the line.
Here one could specify the working area with 2 opposite corners.

In both cases the variable called bbox holds a certain RBox:
https://qcad.org/doc/qcad/3.0/developer ... r_box.html

'Option A' on an empty document returns an RBox equal to ((0,0),(0,0)).
All random RVectors would then reduce to (0,0).
All random lines would then be zero long and located at (0,0).

Panchdara wrote:
Fri Jan 07, 2022 10:18 am
The Misc menu is not described in the Reference
The reason why is explained by Andrew in the link that you provide.
Panchdara wrote:
Fri Jan 07, 2022 10:18 am
but there is no Pro subdirectory
Still, if you have a pro licence or if you use the trial version, 'under the hood' there is.
PRO-prietary and again not readily available as open source.
Pro scripts/resources/Command Line Tools are included in compiled form.


I certainly can agree that I haven't included any guarding against:
- No document (Excludes most of the tools including the Misc menu or GE or XC)
- No valid RBox
- Missing/invalid end points
- No valid or Null-length entities
- No valid layer to draw on
- ...

Not as an addon, no icon/button, no options, no interactions, ...
Just kept it as simple as I could without crashing QCAD.

Regards,
CVH

Post Reply

Return to “QCAD 'Script Add-On & Plug-in challenge' - Work in Progress”