Use command line like a calculator

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
riverbuoy
Senior Member
Posts: 121
Joined: Thu Oct 03, 2013 5:37 pm

Use command line like a calculator

Post by riverbuoy » Tue Jun 23, 2015 1:55 pm

Hi

I have made a small change to use the command line as a calculator. Although you can enter expressions in the command line, it doesn't display the result. Also sometimes you just want to do a quick calculation without actually sending the result to a command.
If the first character in the command line is an equals character ("="), then the rest of the line is treated as a maths expression. If the expression results in a valid number then it is displayed in the command line, preceded by an equals character. For example:
=2+2
=4
After entering the expression, press the 'return' or 'enter' key to display the result. As the result is preceded by an equals character you can continue to enter another expression.
If the result is invalid then an error message is displayed in the line above, and the command line is left unchanged.
If you don't want the equals character displayed then add a space character to the end of the expression. The result will then be displayed without the equals character. For example:
=2+2
4
There is a space character after the final 2.
If you want a relative character displayed, end the expression with the relative character. I use the default 'at' symbol (@).
=2+2@
@4
If you have changed the symbol, use that symbol instead. For example:
=2+2%
%4
This can be used at any time in, any command.
I have attached the file CommandLine.js, which goes into the 'scripts/Widgets/CommandLine' directory.
Hope this is useful.

riverbuoy
Attachments
CommandLine.js
(13.78 KiB) Downloaded 3654 times

User avatar
Clive
Moderator
Posts: 1329
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Use command line like a calculator

Post by Clive » Mon May 23, 2016 9:52 am

Hi riverbuoy

I would like to thank you for this contribution, an 'on-board' calculator is something that all/most good CAD programs have as standard and your tool has already stirred quite a bit of discussion. I would like to ask if it would be possible that the Calculator could have it's own dialog window, so that, if need be one could place or dock it at a convenient position somewhere?

Thanks again

Regards

Clive

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

Re: Use command line like a calculator

Post by andrew » Mon May 23, 2016 10:10 am

Clive: if you wish to use a separate window for calculations, you might want to use the script shell instead which can be used as a calculator among other things:
Misc > Development > Script Shell

Some examples and their results:

Simple calculations:

Code: Select all

2+3
5

sin(12.5)
0.21643961393810288

pow(2,3)
8
Advanced calculations using functions:

Code: Select all

var f = function(n) { var a = 0, b = 1, f = 1; for(var i = 2; i <= n; i++) { f = a + b; a = b; b = f; } return f; };
(defines a function f to compute fibonacci numbers)

f(50)
12586269025
Adding entities:

Code: Select all

addLine(10,10,50,10)
(adds a line from 10,10 to 50,10)

for (a=0; a<360-1; a++) addLine(a,sin(a)*100,a+1,sin(a+1)*100)
(plots a sine curve)

User avatar
Clive
Moderator
Posts: 1329
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Use command line like a calculator

Post by Clive » Mon May 23, 2016 3:46 pm

Andrew, thanks that'll work for me :)

That's why your the developer and I'm not, the only thing I'm good at developing at the moment is a cold :cry:

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

Re: Use command line like a calculator

Post by andrew » Mon May 23, 2016 4:23 pm

Clive wrote:Andrew, thanks that'll work for me :)
OK, great!
Clive wrote:That's why your the developer and I'm not, the only thing I'm good at developing at the moment is a cold :cry:
Get well soon! Must be the weather..

riverbuoy
Senior Member
Posts: 121
Joined: Thu Oct 03, 2013 5:37 pm

Re: Use command line like a calculator

Post by riverbuoy » Mon May 23, 2016 9:09 pm

Hi Clive,
Clive wrote:I would like to ask if it would be possible that the Calculator could have it's own dialog window
I think Andrew's suggestion is the best solution. Of course, there are stand alone calculators available for each operating system. You could create a quick link to use one of these, especially if you want to do a lot of calculations.I actually use a separate, physical calculator, as well as the command line.

Regards

riverbuoy

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

Re: Use command line like a calculator

Post by andrew » Wed May 25, 2016 4:34 pm

riverbuoy, the latest QCAD version 3.15.1 has an updated version of RMath::eval which allows for some interesting stuff with your command line calculator:
command_line_calculator.gif
command_line_calculator.gif (57.05 KiB) Viewed 83964 times

User avatar
Clive
Moderator
Posts: 1329
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Use command line like a calculator

Post by Clive » Thu May 26, 2016 9:34 am

Nice work, thank you both :)

Personally I still think a dedicated dialog would be better :wink:

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

Re: Use command line like a calculator

Post by Husky » Thu May 26, 2016 8:41 pm

Clive wrote:Hi riverbuoy

... I would like to ask if it would be possible that the Calculator could have it's own dialog window, so that, if need be one could place or dock it at a convenient position somewhere?

Clive
Clive wrote:Personally I still think a dedicated dialog would be better :wink:
Hi Clive,

I work with fx-Calc on Windows - the nice thing about this stand alone calculator is that it has a customizable "Function Database" and it works with up to five Variables.
And .... oh yes ... it is free .... :D
Husky-2016.05.26-01.png
Husky-2016.05.26-01.png (71.85 KiB) Viewed 83892 times
I start this calculator program with a keyboard shortcut (Alt Gr + c) and I'm able to keep it on top (Alt Gr + t) displayed over all other running Programs like QCAD. Yes, you are still not able to dock it to QCAD but you can place it at a convenient position and it is always visible. Hey .... that is very close to docking ... isn't it. :shock:

Shortcut and "always on top" can be controlled on different ways - I use "AutoHotkey" because in the case of a move to an other Computer I have everything what I need just in one file .... lazy me ;-)
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..."

User avatar
Clive
Moderator
Posts: 1329
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Use command line like a calculator

Post by Clive » Fri May 27, 2016 9:00 am

Hi Husky

Thanks, will this work on Linux?

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

Re: Use command line like a calculator

Post by andrew » Fri May 27, 2016 9:17 am

Clive, there are certainly similar options available for Linux (I haven't tried these though and installation might or might not be a nightmare):
http://extcalc-linux.sourceforge.net/
http://qalculate.sourceforge.net/screenshots.html

User avatar
Clive
Moderator
Posts: 1329
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Use command line like a calculator

Post by Clive » Fri May 27, 2016 9:32 am

Andrew, thanks I'll take a look.

Mostly I calculate pitch, angle, roof and slope for roofing. There are many online roof pitch calculators out there but I always prefer to calculate myself, keeps my brain active you know :roll:

Post Reply

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