Hi. I tried to write this little lisp to print onto the screen the total distance of a path (as autocad does not have a distance function built into it for a path... only distance between two points).
My program doesn't seem to end the while loop when pressing enter? And then of course does not print the value of the variable d at the end... ?
Please can somebody help.
(defun c:lp ()
(setq c 0)
(setq d 0)
(setq p1 (getpoint "Choose point for text: "))
(while
(setq d (+ c d))
(setq a (getpoint "Click for point: "))
(setq b (getpoint "Click for point: "))
(setq c1 (- (car a) (car b)))
(setq c2 (- (car (cdr a)) (car (cdr b))))
(setq c (sqrt (+ (* c1 c1) (* c2 c2))))
)
(command "text" p1 0.2 0 d)
(princ)
)
Solved! Go to Solution.
Solved by marko_ribar. Go to Solution.
So what did you intend to use as the test expression and which part of your program is setting it to NIL ?
--
I thought that if one presses enter, any while loop automatically exits?
(defun c:lp ( / c d p1 a b d c1 c2 ) (setq c 0) (setq d 0) (setq p1 (getpoint "Choose point for text: "))
(setq a t)
(setq b t) (while (and (/= a nil) (/= b nil)) (setq d (+ c d)) (setq a (getpoint "Click for point: ")) (setq b (getpoint "Click for point: "))
(if (and a b)
(progn (setq c1 (- (car a) (car b))) (setq c2 (- (car (cdr a)) (car (cdr b)))) (setq c (sqrt (+ (* c1 c1) (* c2 c2))))
)
) ) (command "text" p1 0.2 0 d) (princ) )
Hi Miko
I'm afraid this did not work? The same error still occurs. If I press enter, the program simply moves onto the next "Choose point", and pressing enter twice results in the error "numberp nil"?
Kind regards
Derryck
Test it now, Derryck...
M.R.
@derryck wrote:Wow thanks Miko.
Cheers
Derryck
Yes it works now, but I would rather do it differently... Those remarks in color are all to make you understand why code wasn't functioning as it should...
Here is my approach to the same task... Works in any UCS...
(defun c:lp ( / c d p a b ) (setq c 0.0 d 0.0) (setq p (getpoint "\nClick or specify point for text : ")) (setq a t b t) (while (and (/= a nil) (/= b nil)) (setq a (getpoint "\nClick or specify point : ")) (if a (setq b (getpoint a "\nClick or specify point : "))) (if (and a b) (setq c (distance a b)) (setq c 0.0)) (setq d (+ c d)) ) (command "_.text" p 0.2 0 d) (princ) )
Regards, Marko Ribar, d.i.a.
@derryck wrote:
....
(while
(setq d (+ c d))
(setq a (getpoint "Click for point: "))
(setq b (getpoint "Click for point: "))
(setq c1 (- (car a) (car b)))
(setq c2 (- (car (cdr a)) (car (cdr b))))
(setq c (sqrt (+ (* c1 c1) (* c2 c2))))
)
...
There's no need to pick both ends of each segment of your path, which means you have to pick all intermediate locations twice. This routine from a few years back lets you pick a series of locations along the path, once each in sequence:
;; SumDistCont.lsp [command name: SDC]
;; for cumulative Sum of Distances - Continuous
;; (end-to-end; second point of each becomes first point of next)
;; Kent Cooper, March 2010
(defun C:SDC (/ P1 P2 sumdist)
(setq sumdist 0)
(setq P1 (getpoint "\nSelect first point: "))
(while (setq P2 (getpoint P1 "\nSelect next point, or press Enter to close: "))
(prompt
(strcat
"\nLatest distance = "
(rtos (distance P1 P2))
",\nCumulative distance = "
(rtos (setq sumdist (+ sumdist (distance P1 P2))))
); end strcat
); end prompt
(setq P1 P2)
); end while
(prompt (strcat "\nTotal sum of distances = " (rtos sumdist)))
(princ)
); end defun
(prompt "\nType SDC to Sum multiple Distances - Continuous.")
It only reports the total distance [as well as the distance of each segment along the way, which you can omit], but you can easily edit it to have it put that in as Text, as in your other routine.
Also, when I try your approach of using the number as a number for the Text content [rather than as a text string] in Acad2015, I always get a decimal number to 15 decimal places, even if I have Units set to Architectural, and even if a whole lot of the last decimal places are zeros. I would suggest using (rtos) on that, so you can specify the mode and precision, or without specifying them to express the distance in whatever your current mode and precision settings are, rather than always as a decimal number with all those digits.
@stevesfr wrote:
how to change accuracy to two decimal instead of 14 ??
So you've now found that, too....
(command "_.text" p 0.2 0 (rtos d 2 2))
The first 2 is for decimal mode, and the second for the precision [number of decimal places in that case]. Read about (rtos) for other mode options.