Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why isn't this program working?

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
derryck
444 Views, 11 Replies

Why isn't this program working?

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)
  )

11 REPLIES 11
Message 2 of 12
martti.halminen
in reply to: derryck

So what did you intend to use as the test expression and which part of your program is setting it to NIL ?

 

-- 

 

Message 3 of 12
derryck
in reply to: martti.halminen

I thought that if one presses enter, any while loop automatically exits?

Message 4 of 12
marko_ribar
in reply to: derryck

(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) )

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 5 of 12
derryck
in reply to: marko_ribar

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

 

 

Message 6 of 12
marko_ribar
in reply to: derryck

Test it now, Derryck...

 

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 7 of 12
derryck
in reply to: marko_ribar

Wow thanks Miko.

 

Cheers

 

Derryck

Message 8 of 12
marko_ribar
in reply to: derryck


@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.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 9 of 12
stevesfr
in reply to: marko_ribar

how to change accuracy to two decimal instead of 14 ??
Message 10 of 12
Kent1Cooper
in reply to: derryck


@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.

Kent Cooper, AIA
Message 11 of 12
Kent1Cooper
in reply to: stevesfr


@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.

Kent Cooper, AIA
Message 12 of 12
stevesfr
in reply to: Kent1Cooper

@Kent......thanks

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost