Help with making a script file

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
John Hyslop
Premier Member
Posts: 473
Joined: Mon Sep 30, 2019 6:21 am
Location: Melbourne - Australia

Help with making a script file

Post by John Hyslop » Wed Feb 03, 2021 1:56 am

Hi All

Years ago I used to make very basic lisp files to do very very basic tasks, can anyone help me with this :|
Attached is the lisp routine :- and before anyone says yes there is probably a better way to make the lsp file
and also return osnaps to previous settings :lol:

All i want to do is select and Arc or even better window select arcs and turn them into circles.
There is a problem when sometimes exploding blocks circles become 1 arc with a start point 0
and end point 360, Qcad displays these arcs and one can assume they are circles, but when loading
the dxf into CNC software it displays as a Dot, likewise in AutoCad a dot / zero length arc e_surprised

Thanks for any help in advance
Cheers
John
PS I didn't know how to insert it as code :cry: it kept saying Spam Detected , here is a screenshot
if you don't want to download the zip file..
A2C..png
A2C..png (29.68 KiB) Viewed 3859 times
A2C.LSP.zip
(427 Bytes) Downloaded 298 times
Last edited by John Hyslop on Wed Feb 03, 2021 8:23 am, edited 2 times in total.
IF IT IS TO BE IT IS UP TO ME [ Ten most powerful 2 letter words ]

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

Re: Help with making a script file

Post by CVH » Wed Feb 03, 2021 5:41 am

Hi John,
Already a while QCAD doesn't explode circles to 1 full Arc but in two 2 semi full Arcs.
A Polyline that is.
Recently, explosion of circles is made optional in App.Prefs.

Below you'll find a script for full Arcs to Circles:
Regards.
CVH

Code: Select all

// Full Arcs to Circles script by CVH
var doc = getDocument();
var di = getDocumentInterface();
var arcsCollected = [];

// Retrieve the selection:
var ids = doc.querySelectedEntities();

// Terminate without selection:
if (ids.length === 0) {
    EAction.handleUserWarning("Nothing Selected! Script terminated.");
    return;
}

// Filter full Arcs from selection:
for (var i=0; i<ids.length; i++) {
    // Get entity from the document:
    var entity = doc.queryEntity(ids[i]);

    // Collect when exist and is full Arc:
    if (!isNull(entity)){
        if (isArcEntity(entity)) {
            var arcSweep = Math.abs(entity.getSweep());
            if (RMath.fuzzyAngleCompare(arcSweep, Math.PI*2.0)) {
                arcsCollected.push(ids[i]);
            }
        }
    }
}

// Terminate without full Arcs in selection:
if (arcsCollected.length === 0) {
    EAction.handleUserWarning("No full Arc entities selected! Script terminated.");
    return;
}

// Setup an operation:
var operation = new RModifyObjectsOperation();
operation.setText("Full Arcs to circles");

// Process collected Arcs
for (var i=0; i<arcsCollected.length; i++) {    //Cycle collected Arcs
    // Get full Arc from the document:
    var fullArc = doc.queryEntity(arcsCollected[i]);

    // Create a new Circle entity:
    var newCircle = new RCircleEntity(
                    doc,
                    new RCircleData(
                        fullArc.getCenter(),
                        fullArc.getRadius()
                    )
    );

    // Add the new Circle to the operation:
    operation.addObject(newCircle, false, true);    // NOTuseCurrentAttributes, DOforceNew

    // Mark the full Arc to delete:
    operation.deleteObject(fullArc);
} // Loop Arcs

// Apply all modifications:
di.applyOperation(operation);

// Finished:
var msg = "Created " + arcsCollected.length + " Circle entity/ies."
EAction.handleUserInfo(msg);
EAction.handleUserMessage("Script ended.");
return;

Last edited by CVH on Wed Feb 03, 2021 9:26 am, edited 3 times in total.

John Hyslop
Premier Member
Posts: 473
Joined: Mon Sep 30, 2019 6:21 am
Location: Melbourne - Australia

Re: Help with making a script file

Post by John Hyslop » Wed Feb 03, 2021 7:45 am

Thanks CVH :)

I'll try later, Yes i just tested explodes to 2 arcs that's fine for cnc, I must of seen this in a previous version of Qcad...

Cheers
John
IF IT IS TO BE IT IS UP TO ME [ Ten most powerful 2 letter words ]

John Hyslop
Premier Member
Posts: 473
Joined: Mon Sep 30, 2019 6:21 am
Location: Melbourne - Australia

Re: [ Solved ] Help with making a script file

Post by John Hyslop » Wed Feb 03, 2021 8:21 am

Hi CVH

As you know I don't know much about Java scripting :cry:
I copied your text and saved it in a file a2c.js
made a folder under Scripts/Misc/c2a
and placed it there but Qcad won't run unless I remove it :oops:

Could you explain how and where i put this file so I can use it in the Qcad UI ? is it a Java script file?
I don't know e_confused

Cheers
John
IF IT IS TO BE IT IS UP TO ME [ Ten most powerful 2 letter words ]

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

Re: Help with making a script file

Post by CVH » Wed Feb 03, 2021 9:16 am

John Hyslop wrote:
Wed Feb 03, 2021 8:21 am
Could you explain how
Sure,
It is not a GUI action ...
... with a button.
(If you want that ... :wink: )

Make selection.
Open Script Shell (GE)
Copy all.
Paste.

Saved as a *.js file
Make selection.
Run Script (XC) ...

Regards,
CVH

PS: :P Herrinbore seed for BHT ready :wink:

John Hyslop
Premier Member
Posts: 473
Joined: Mon Sep 30, 2019 6:21 am
Location: Melbourne - Australia

Re: Help with making a script file

Post by John Hyslop » Wed Feb 03, 2021 9:33 am

e_surprised Herringbone.. I bet I won't understand the equation and not be able to add :cry:
Looking forward to the maths, I bet it's a beauty..

Cheers
John
IF IT IS TO BE IT IS UP TO ME [ Ten most powerful 2 letter words ]

Post Reply

Return to “QCAD 'How Do I' Questions”