Author Topic: SOLVED! About number of digits after the comma  (Read 2031 times)

0 Members and 1 Guest are viewing this topic.

scorpion76

  • Guest
SOLVED! About number of digits after the comma
« on: August 13, 2018, 11:18:31 AM »
Hi friends, i try to write a basic lisp code but result of formula is not seen as good as. How can i fix 3 digit after coma?
Thanks.

Code: [Select]
(defun c:nkt()
  (setq p1 (getpoint "\nGet Point :"))
  (setq r (getreal "\n Enter Radius:"))
  (setq p2 (polar p1 0 r))

  (setq area  (* pi  (* r r) ))

 ;(setq snc (strcat "Alan: " aln " m2"))

  (command "circle" p1 r "")
  (command "text" "j" "tl" p2 "1" "0" area)
)

Result is for example; 314.1592653589793 but i want to see like 314.159

Second question; i want to text Area:314.159 m²
« Last Edit: August 13, 2018, 11:56:25 AM by scorpion76 »

ChrisCarlson

  • Guest
Re: About number of digits after the comma
« Reply #1 on: August 13, 2018, 11:29:06 AM »
Look at the documentation for the
Code - Auto/Visual Lisp: [Select]
function

scorpion76

  • Guest
Re: About number of digits after the comma
« Reply #2 on: August 13, 2018, 11:44:45 AM »
Code: [Select]
(setq area (rtos (* pi  (* r r) ) 2 3)) it is ok thanks friend.

How can i fix second questions?

scorpion76

  • Guest
Re: About number of digits after the comma
« Reply #3 on: August 13, 2018, 11:48:06 AM »
Code: [Select]
(setq area (rtos (* pi  (* r r) ) 2 3)) it is ok thanks friend.

How can i fix second questions?

Solved! Thanks friend.

Code: [Select]
(defun c:nkt()
(setq p1 (getpoint "\nNokta Seç :"))
(setq r (getreal "\n Yarıçap Giriniz:"))

  (setq p2 (polar p1 0 r))

  (setq aln (rtos (* pi  (* r r) ) 2 3))
 (setq snc (strcat "Alan: " aln " m2"))

  (command "circle" p1 r "")
  (command "text" "j" "tl" p2 "1" "0" snc)

)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: SOLVED! About number of digits after the comma
« Reply #4 on: August 13, 2018, 01:10:45 PM »
Good job.
One caution on Osnaps, they might interfere with a COMMAND placement.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: About number of digits after the comma
« Reply #5 on: August 13, 2018, 06:15:26 PM »
Code: [Select]
(defun c:nkt()
(setq p1 (getpoint "\nNokta Seç :"))
(setq r (getreal "\n Yarıçap Giriniz:"))

  (setq p2 (polar p1 0 r))

  (setq aln (rtos (* pi  (* r r) ) 2 3))
 (setq snc (strcat "Alan: " aln " m2"))

  (command "circle" p1 r "")
  (command "text" "j" "tl" p2 "1" "0" snc)

)

Here are a few pointers which may steer you in the right direction -



Since r looks to be a radius, the getdist function with p1 as the basepoint argument may be more applicable:
Code - Auto/Visual Lisp: [Select]
  1. (setq r (getdist p1 "\nYarıçap Giriniz:"))



You can account for null user input using a simply if statement with an and expression to test the validity of the value returned by each prompt, e.g.:
Code - Auto/Visual Lisp: [Select]
  1.     (if
  2.         (and
  3.             (setq p1 (getpoint   "\nNokta Seç :"))
  4.             (setq r  (getdist p1 "\nYariçap Giriniz:"))
  5.         )
  6.         (progn
  7.             ;; ... continue ...
  8.         )
  9.     )

Here, the progn function is used to evaluate multiple expressions as part of the 'then' argument, but to supply the if function with a single expression for the 'then' argument.



Always declare local variables within the variable list of the defun expression, i.e.:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:nkt ( / aln p1 p2 r snc )
This ensures that the symbols are initialised to nil within the scope of the c:nkt function, and then return to their original values outside of this scope. Without declaring variables as local to the function, the become global variables and retain their value after the function has completed evaluation (which can lead to undesirable outcomes when building lists) - for more on this, you may wish to read my tutorial here.



When evaluating commands using the command function in AutoLISP, it is good practice to prefix the command name with an underscore (_) and period (.). The underscore indicates that the command name that follows is the English command name, else it is interpreted as a localised command name in non-English versions of AutoCAD. The period ensures that the original definition of the command is executed, in case the command has been redefined in the current environment.

For example:
Code - Auto/Visual Lisp: [Select]
  1. (command "_.circle" ...



When supplying point arguments to a command in AutoLISP, the points will be affected by any Object Snap modes active when the command is evaluated. This can result in points snapping to nearby objects outside of the user's control.

To avoid this, either disable all Object Snap modes prior to executing the command by modifying the value of the OSMODE system variable, or precede the point input with the "_non" or "_none" Object Snap modifier, indicating that no Object Snap modes should be used for the following input.

For example:
Code - Auto/Visual Lisp: [Select]
  1. (command "_.circle" "_non" p1 r)



Be careful when executing the TEXT or -TEXT command from AutoLISP, as the number of prompts will depend upon whether or not the active Text Style has a non-zero text height. If the text height is zero, the user will receive an additional prompt for text height.

You can account for this by checking the text height associated with the active Text Style, or alternatively, you can use entmake or entmakex to create the text object.



When using rtos to format the circle area to three decimal places, be aware that leading and trailing zeroes will be controlled by the value of the DIMZIN system variable.

scorpion76

  • Guest
Re: SOLVED! About number of digits after the comma
« Reply #6 on: August 14, 2018, 05:21:59 AM »
Thanks for explanation friend.