A few questions and general direction needed.

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
linktogunner
Newbie Member
Posts: 3
Joined: Sun Jul 31, 2022 8:23 pm

A few questions and general direction needed.

Post by linktogunner » Sun Jul 31, 2022 8:57 pm

OS: Arch linux
QCAD Professional 3.27.6.0

So here is my idea - Customer sends me a pdf or dwg file of a part they want me to fabricate. I want to have a script that scrapes that dwg or pdf for the dimensions I need (in this example its always a standard flange for a pipe so only dimensions I need are OD, BC, ID, and hole size) and putting that data in the relevant JavaScript code to make it a script for QCAD, open QCAD and run the QCAD portion of the script. I want the javascript portion for QCAD to draw the part, and save the part as "part1234.dxf" to a specific folder. I plan on writing this mainly in Python except for the portion where it needs to be js for QCAD scripting.

I started on drawing a mock part with a script already. I figured I wanted to get the qcad script down first before I start on the rest. Here is that code. I have literally never used JavaScript in the past so be gentle. Please point out if there is a better way to do what I am trying to do with this drawing. Basically in the past when I draw these parts up manually in QCAD I make my OD circle, BC circle, ID circle, and hole circle. Then I move the hole to a center reference point on the BC circle, go to modify > move and rotate > and make however many copies I need (usually 8) and set the relevant angle. The end result is a flange with 8 (usually) evenly spaced holes around the BC. Thats what I want to accomplish with the script. I couldn't figure out how to script the move/rotate portion of the process so in the code you will see my work around. Again, totally hoping someone has some suggestions on how to do it better. Also I cant figure out how to save the drawing as a dxf to a specific directory. Hoping someone can help me on that as well.
Code:
include("scripts/EAction.js");
include("scripts/File/SaveAs/SaveAs.js")


addLayer("OD")
addLayer("BC")
addLayer("ID")
addLayer("HOLES")
circle_od = addCircle(0, 0, 3)
circle_id = addCircle(0, 0, 1.75)
circle_hole = addCircle(1.75, 0, .25)
rotate(circle_hole, angle = 45)
circle_hole2 = addCircle(1.75, 0, .25)
rotate(circle_hole2, angle = 90)
circle_hole3 = addCircle(1.75, 0, .25)
rotate(circle_hole3, angle = 135)
SaveAs('flngscripttest.dxf')

Again, complete novice here. What I have there ^ was me totally going in blind just trying to get a drawing similar to what I need on the screen, which that code accomplishes that. I am stuck on finding the legit way to move/rotate and copy my holes and how to save the drawing as a relevant .dxf file in the directory I need. Also if there is any guidance on how I can get Python to automatically read the customers dwg and pdf and spit out that code ^ into a .js file and open QCAD and run the script (all automatically from the python script) that would be great. If I have to use 100% javascript to do it all please let me know.

I am writing the code in a text editor and then opening the script from QCAD Misc > Development > Run Script

Thanks for reading.

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4935
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: A few questions and general direction needed.

Post by Husky » Sun Jul 31, 2022 10:07 pm

Hi,

first things first - I've zero knowledge of programming. Ask me how to script a horizontal red line from point 1 xy to point 2 xy and I'll leave the room ...

This said ...
linktogunner wrote:
Sun Jul 31, 2022 8:57 pm
OS: Arch linux
QCAD Professional 3.27.6.0

So here is my idea - Customer sends me a pdf or dwg file of a part they want me to fabricate. I want to have a script that scrapes that dwg or pdf for the dimensions I need (in this example its always a standard flange for a pipe so only dimensions I need are OD, BC, ID, and hole size) and putting that data in the relevant JavaScript code to make it a script for QCAD, open QCAD and run the QCAD portion of the script. I want the javascript portion for QCAD to draw the part, and save the part as "part1234.dxf" to a specific folder. I plan on writing this mainly in Python except for the portion where it needs to be js for QCAD scripting.
... so far I don't understand why do you need a script to duplicate an existing drawing and save it as a dxf in folder xyz. QCAD can read dwg's and vector pdf's. It can also export them as dxf in what ever folder. You could use the GUI for a handful of drawings or the Command Line Tool's for a bunch of drawings.
Where is the need coming from to have a new script for something what is already covered by QCAD ...
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

linktogunner
Newbie Member
Posts: 3
Joined: Sun Jul 31, 2022 8:23 pm

Re: A few questions and general direction needed.

Post by linktogunner » Mon Aug 01, 2022 4:37 am

Husky wrote:
Sun Jul 31, 2022 10:07 pm

... so far I don't understand why do you need a script to duplicate an existing drawing and save it as a dxf in folder xyz. QCAD can read dwg's and vector pdf's. It can also export them as dxf in what ever folder. You could use the GUI for a handful of drawings or the Command Line Tool's for a bunch of drawings.
Where is the need coming from to have a new script for something what is already covered by QCAD ...
That's a fair question, and you're totally right, I can do everything I need to do from the QCAD gui manually. There's a few reasons for me wanting to do it the way I described, though.

1) The customers drawings all have more info than just the 2d drawing of their part. There is weld finish specs, customer info, etc as a part of the drawing that the part itself needs to be isolated from for me to make a "cut ready" dxf for my cnc table.

2)The drawings need to be modified. For example, the customers print of their part says that the bolt hole needs to be .875. I am cutting their part on a plasma table, so I need the hole size to be about 1/32 over sized (.906) for the best result.

3)The main reason for all of this is automation. Sure, I can load the drawings up in QCAD, quickly delete everything I dont need, select all of the holes and add 1/32 to their diameter, save the file as a dxf and call it a party. And that's how I do it usually. But if I could write a couple scripts that would do all of that for me automatically that would save time and money. As the email from the customer comes in, the needed drawings get downloaded, relevant data extracted and put into a script for QCAD and then executed and the drawings are made automatically and saved. This eliminates a process that can sometimes be time consuming when you have hundreds of parts to make. And to be perfectly honest, if I can get this to work how I would like, it will be a nice project to put into my portfolio for software dev jobs.

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

Re: A few questions and general direction needed.

Post by CVH » Mon Aug 01, 2022 6:53 am

Hi,

I think you should forget about Phyton to collect data from a document with QCAD.
For that you need Phyton resources that handle the DXF, DWG or PDF content.

'Scraping' a document for relevant data is already more complex than you might think.
OD, BC, ID, hole size and number of holes are nothing that is stored readily available.
The first four are merely diameters, any Circle or Arc entity in a drawing has that property.
Circular shapes can be made up of arc segments, a full arc, a polyline or even an ellipse or approximated by line segments.

So, the source should be well organized to be able to retrieve data.

Taken it from the point that you have sets of valid data ...
... And skipping the heuristic AI to read arbitrary customers files :wink:

Scripting under QCAD has several levels.

Code: Select all

addLayer("OD");
addCircle(0, 0, 3);
Are instructions of the simple API (Hint: I placed them in a code display :wink: )
The complete signatures can be found here:
https://qcad.org/doc/qcad/3.0/developer ... imple.html

Best practice is to include the base class at the top:

Code: Select all

include("scripts/simple.js");
While the simple API is versatile and kept simple, there are a few to more limitations.
The next level would not use the simple API, a little harder but you have full control over what is done and how.

For example:

Code: Select all

addLayer("OD");
addLayer("BC");
addLayer("ID");
addLayer("HOLES");
addCircle(0, 0, 3);
This will create 4 white layers, continuous lines, weight 0 and the last created will be the current active layer.
The circle will then be created on the layer 'HOLES', @(0, 0), with a radius of 3 drawing units.

Without the simple API we create a fundamental shape and turn that into a document entity and are able to set any relevant property.

The function 'addCircle' returns a reference to the created entity.
We could store that in a variable, best practice is to declare your variables with var ... :wink:

Code: Select all

var circle_od = addCircle(0, 0, 3);
To rotate an entity we also need a point to rotate around ...
https://qcad.org/doc/qcad/3.0/developer ... 9dcf40c280
Remind that the simple API uses degrees while for the rest, radians is the common angle notation.

Is that really necessary? One could simply calculate the positions on a center circle ... :wink:
... All depending on the number of holes.

The simple API has no method to save a drawing as far as I know.
I can only point you to something very simplistic:
https://github.com/qcad/qcad/blob/bc881 ... drawing.js
Remark that this example acts on an off-screen document without the use of the GUI or file dialog.
And that it is ran from the OS command line.


A complete other route might be DrawFromCSV.
https://qcad.org/rsforum/viewtopic.php?t=8135
That would require the basic data in a spreadsheet, a list of some sort and a transformation matrix to drawing instructions for each part.
I use it to automate boring repetitive tasks like the generation of several hundreds of tag-plates in different size, with individual text.
The only downside is that I didn't include methods to open/start or save a file ... Bummer.
It was not intended to run without human interaction ... :lol:

Regards,
CVH
Last edited by CVH on Mon Aug 01, 2022 8:14 am, edited 3 times in total.

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

Re: A few questions and general direction needed.

Post by CVH » Mon Aug 01, 2022 7:11 am

linktogunner wrote:
Mon Aug 01, 2022 4:37 am
2)The drawings need to be modified. For example, the customers print of their part says that the bolt hole needs to be .875. I am cutting their part on a plasma table, so I need the hole size to be about 1/32 over sized (.906) for the best result.
Please investigate the CAM offset functionality or tool compensation in general.
A plasma or laser beam is simply a cutting tool. :wink:

Regards,
CVH

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

Re: A few questions and general direction needed.

Post by CVH » Mon Aug 01, 2022 7:54 am

Husky wrote:
Sun Jul 31, 2022 10:07 pm
first things first - I've zero knowledge of programming. Ask me how to script a horizontal red line from point 1 xy to point 2 xy and I'll leave the room ...
Given that point1 @(0,0) and point2 @(100, 0) ...

Code: Select all

include("scripts/simple.js");
setCurrentColor(new RColor("red"));
addLine(0, 0, 100, 0);
It can be as easy as that. :wink:
Only, it is on the current active layer and the current pen color is now red.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”