X & Y Differences

X & Y Differences

Anonymous
Not applicable
512 Views
6 Replies
Message 1 of 7

X & Y Differences

Anonymous
Not applicable

Hello,

 

I know this is a little rude, but I am rusty on autolisp.  I dug out my old book, but it still might take me some time to do what I want.

 

I'm sure somebody could whip this out in no time, and it would be greatly appreciated:

 

I'd like to pick two points

have it print to the command line e.g.

   the difference between the X's (rounded to the 2nd Place e.g. -0.02)

   the difference between the Y's (rounded to the 2nd Place e.g. -0.02)

Then prompt for two texts and change the 

  content of the first text to the absolute value of the X difference e.g. 0.02

  content of the second text to the absolute value of the Y difference e.g. 0.02

 

Pretty simple for someone who knows autolisp, heck I can't even remember the difference between CAR and CDR,

Thank you!

 

0 Likes
513 Views
6 Replies
Replies (6)
Message 2 of 7

hmsilva
Mentor
Mentor

Hello cadger,

 

as a starting point

 

(defun c:demo ( / dimz ent1 ent2 pt1 pt2 sel1 sel2 xs ys)
  (if (and (setq pt1 (getpoint "\nEnter the first point: "))
           (setq pt2 (getpoint "\nEnter the second point: "))
      )
    (progn
      (setq dimz (getvar 'DIMZIN))
      (setvar 'DIMZIN 0)
      (princ (strcat "\nThe difference between the X's = " (setq xs (rtos (- (car pt1) (car pt2)) 2 2))))
      (princ (strcat "\nThe difference between the Y's = " (setq ys (rtos (- (cadr pt1) (cadr pt2)) 2 2)))
      )
      (setvar 'DIMZIN dimz)
      (if (and (setq sel1 (entsel "\nSelect the difference between the X's Text: "))
               (= (cdr (assoc 0 (setq ent1 (entget (car sel1))))) "TEXT")
               (setq sel2 (entsel "\nSelect the difference between the Y's Text: "))
               (= (cdr (assoc 0 (setq ent2 (entget (car sel2))))) "TEXT")
               (not (eq ent1 ent2))
          )
        (progn
          (entmod (subst (cons 1 xs) (assoc 1 ent1) ent1))
          (entmod (subst (cons 1 ys) (assoc 1 ent2) ent2))
        )
        (princ "\nYou didn't select valid texts... ")
      )
    )
    (princ "\nYou didn't enter valid points... ")
  )
  (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 3 of 7

Kent1Cooper
Consultant
Consultant

@hmsilva wrote:

.... 

...
          (entmod (subst (cons 1 xs) (assoc 1 ent1) ent1))
          (entmod (subst (cons 1 ys) (assoc 1 ent2) ent2))
...

....


Because of the absolute-value component in the last part of the request, I at first thought of saving each difference as a number rather than as a string right off the bat, so that (abs) could be applied to it.  But then you'd need to apply (rtos) twice, once to report to the Command: line [not absolute value] and once for the Text content [with (rtos (abs ...))].  Perhaps a simpler way would be to just strip the minus sign off the string if it starts with one, by replacing the above with:

    (entmod (subst (cons 1 (vl-string-trim "-" xs)) (assoc 1 ent1) ent1))
    (entmod (subst (cons 1 (vl-string-trim "-" ys)) (assoc 1 ent2) ent2))

Kent Cooper, AIA
0 Likes
Message 4 of 7

hmsilva
Mentor
Mentor

@Kent1Cooper wrote:

@hmsilva wrote:

.... 

...
          (entmod (subst (cons 1 xs) (assoc 1 ent1) ent1))
          (entmod (subst (cons 1 ys) (assoc 1 ent2) ent2))
...

....


Because of the absolute-value component in the last part of the request, I at first thought of saving each difference as a number rather than as a string right off the bat, so that (abs) could be applied to it.  But then you'd need to apply (rtos) twice, once to report to the Command: line [not absolute value] and once for the Text content [with (rtos (abs ...))].  Perhaps a simpler way would be to just strip the minus sign off the string if it starts with one, by replacing the above with:

    (entmod (subst (cons 1 (vl-string-trim "-" xs)) (assoc 1 ent1) ent1))
    (entmod (subst (cons 1 (vl-string-trim "-" ys)) (assoc 1 ent2) ent2))


My bad,

have not read the:

 

'Then prompt for two texts and change the 

  content of the first text to the absolute value of the X difference e.g. 0.02

  content of the second text to the absolute value of the Y difference e.g. 0.02'

 

Thank you!

Henrique

EESignature

0 Likes
Message 5 of 7

Anonymous
Not applicable
thank you!

i finally went with this for now:

(defun c:get (/ PT1 PT2 SEL1 SEL2 XS YS)

(SETQ PT1 (GETPOINT "\n PICK SURVEY POINT: "))
(SETQ PT2 (GETPOINT "\n PICK PLAN POINT: "))
(princ (strcat "\nThe difference between the X's = " (setq xs (rtos (- (car pt1) (car pt2)) 2 2))))
(princ (strcat "\nThe difference between the Y's = " (setq ys (rtos (- (cadr pt1) (cadr pt2)) 2 2))))
;(setq xs (rtos (ABS ((- (car pt1) (car pt2)) 2 2))))
;(setq ys (rtos (ABS ((- (cadr pt1) (cadr pt2)) 2 2))))

(if (and (setq sel1 (entsel "\nSelect the difference between the X's Text: "))
(= (cdr (assoc 0 (setq ent1 (entget (car sel1))))) "TEXT")
(setq sel2 (entsel "\nSelect the difference between the Y's Text: "))
(= (cdr (assoc 0 (setq ent2 (entget (car sel2))))) "TEXT")
(not (eq ent1 ent2))
)

(progn
(entmod (subst (cons 1 xs) (assoc 1 ent1) ent1))
(entmod (subst (cons 1 ys) (assoc 1 ent2) ent2))
)
)
(PRINC)
)


your help got me flying right off the bat.. now i can spend some time relearning all the functions... for some reason the top lisp post gave me an error (does't tell me what line) so i had to reduce it to the above, but thanks for the great start!

regards,
cadger
0 Likes
Message 6 of 7

hmsilva
Mentor
Mentor

@Anonymous wrote:
thank you!

i finally went with this for now:
...
your help got me flying right off the bat.. now i can spend some time relearning all the functions... for some reason the top lisp post gave me an error (does't tell me what line) so i had to reduce it to the above, but thanks for the great start!
...

You're welcome, cadger!
The top lisp post should not give an error...

 

'It is always a good principle to validate all data, before using it as an argument.'

I did made some modifications in your code, to to prevent a message like this

; error: bad argument type: numberp: nil

if the user press enter key, insted enter a valid point...

Henrique

EESignature

0 Likes
Message 7 of 7

Anonymous
Not applicable

This maybe works!

 

(defun C:do-it(/ p1 p2 dist x-dt y-dt)
  (setq  p1 (getpoint "\nPlease pick the first point:\n")
  p2 (getpoint "please pick the second point:\n")
         dist (distance p1 p2)
         x-dt (abs (- (car p1) (car p2)))
        y-dt (abs (- (cadr p1) (cadr p2)))
  )
  (prompt (strcat "\nDistance of the Points " (rtos dist) "mm\n"
                          "Δx:  " (rtos x-dt) "mm\n"
                          "Δy:  " (rtos y-dt) "mm\n"
                 )
  )
)

0 Likes