Author Topic: How to get negative values with GETDIST?  (Read 1059 times)

0 Members and 1 Guest are viewing this topic.

Woabow

  • Newt
  • Posts: 56
How to get negative values with GETDIST?
« on: September 26, 2012, 10:38:34 AM »
This code snippet is from a program that moves selections in only X direction after asking the distance. The user can enter both positive and negative values, no problem so far.

Code: [Select]
(setq ss1 (ssget))
(if (/= nil ss1)
    (progn
        (setq ss ss1)
    (setq
    af (getdist "\nDistance: ")
    pt1 (list 0.0 0.0 0.0)
    pt2 (list af 0.0 0.0)
    )
        (command ".move" ss "" "_none" pt1 "_none" pt2)
    )


But I would like to enter the distance by picking two points too. This only returns positive values (as a distance is always positive).

Can somebody help me with a way to have both input modes (direct input and two points) possible, but returning a negative value when the first X value of the point is higher as the second one? Are the two points entered stored somewhere maybe?

ribarm

  • Water Moccasin
  • Posts: 2189
  • Marko Ribar, architect
Re: How to get negative values with GETDIST?
« Reply #1 on: September 26, 2012, 11:39:43 AM »
Code: [Select]
(setq ss (ssget))
(if ss
  (progn
    (setq p1 (getpoint "\nPick or enter first point : "))
    (setq p2 (getpoint p1 "\nPick or enter second point - distance : "))
    (if (> (car p1) (car p2))
      (command "_.move" ss "" "_non" p1 "_non" (mapcar '+ p1 (list (distance p1 p2) 0.0 0.0)))
      (command "_.move" ss "" "_non" p1 "_non" (mapcar '- p1 (list (distance p1 p2) 0.0 0.0)))
    )
  )
)

Yes, all variables that are setq-ed and not localized by defined function, after execution of function are stored in memory of ACAD and are called global variables... You can view them among other defined functions in a list that is called with :
Code: [Select]
(atoms-family 0)
or if you want to use a list with them only as strings as elements with :
Code: [Select]
(atoms-family 1)

If you want to view values of them, you can use :
Code: [Select]
(mapcar 'eval (atoms-family 0))

But with this you'll get complete list of variables/values stored in CAD memory; if you want only setq-ed ones you have to before you do setq on them store all list in some variable like *list*
Code: [Select]
(setq *list* (atoms-family 0))
then you do setq-es with starting function without localized variables you want to view, or just setq them...
then you view them by :
Code: [Select]
(vl-remove-if '(lambda (x) (member x *list*)) (atoms-family 0))
or view their values with :
Code: [Select]
(mapcar 'eval (vl-remove '*list* (vl-remove-if '(lambda (x) (member x *list*)) (atoms-family 0))))
« Last Edit: September 26, 2012, 02:28:31 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12292
  • London, England
Re: How to get negative values with GETDIST?
« Reply #2 on: September 26, 2012, 02:13:50 PM »
Since you cannot obtain a negative distance using getdist with two points picked from the drawing, you would need to use getpoint with a base point (or use the origin as the base point with a single getpoint call).

So perhaps:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:moveit ( / p1 p2 ss )
  2.     (if (and
  3.             (setq ss (ssget "_:L"))
  4.             (setq p1 (getpoint "\n1st Point: "))
  5.             (setq p2 (getpoint "\n2nd Point: " p1))
  6.         )
  7.         (command "_.move" ss "" "_non" '(0.0 0.0) "_non" (list (- (car p2) (car p1)) 0.0))
  8.     )
  9.     (princ)
  10. )

owenwengerd

  • Bull Frog
  • Posts: 439
Re: How to get negative values with GETDIST?
« Reply #3 on: September 26, 2012, 02:45:47 PM »
Can somebody help me with a way to have both input modes (direct input and two points) possible, but returning a negative value when the first X value of the point is higher as the second one? Are the two points entered stored somewhere maybe?

As mentioned, you can use (getpoint) to obtain the points, but you would need to use (initget 128) to allow arbitrary input for the first (getpoint), then convert any arbitrary input to a real number with (atof) if the first (getpoint) returns a string instead of a point.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10369
Re: How to get negative values with GETDIST?
« Reply #4 on: September 26, 2012, 03:11:17 PM »
Maybe this:
Code: [Select]
(defun c:test (/ ss p1 p2 x)
  (defun txt2num (txt) ; CAB 10/2005
    (cond
      ((distof txt 5))
      ((distof txt 2))
      ((distof txt 1))
      ((distof txt 4))
      ((distof txt 3))
    )
  )
  (setq ss (ssget))
  (initget 128)
  (setq p1 (getpoint "\nDistance: "))
  (if (and ss p1)
    (progn
      (if (listp p1) (setq p2 (getpoint p1 "\nSpecify second point.")))
      (if (and p1 p2)
         (setq x (distance p1 p2)
               x (if (< (car p2) (car p1)) (- x) x))
         (setq x (txt2num p1))
      )
      (if x (command ".move" ss "" "_none" '(0 0 0) "_none" (list x 0 0)))
    )
  )
  (princ)
)
« Last Edit: September 26, 2012, 03:22:37 PM by CAB »
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: 12292
  • London, England
Re: How to get negative values with GETDIST?
« Reply #5 on: September 26, 2012, 03:20:41 PM »
Nice idea Owen to use arbitrary input, I hadn't thought of that  :-)

Perhaps CAB's code could be condensed to:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / di p1 p2 ss )
  2.     (if (and
  3.             (setq ss (ssget "_:L"))
  4.             (progn
  5.                 (initget 128)
  6.                 (setq p1 (getpoint "\nDistance: "))
  7.             )
  8.             (or (and
  9.                     (listp p1)
  10.                     (setq p2 (getpoint "\nSpecify second point: " p1))
  11.                     (setq di (distance p1 p2)
  12.                           di (if (< (car p1) (car p2)) di (- di))
  13.                     )
  14.                 )
  15.                 (setq di (distof p1))
  16.             )
  17.         )
  18.         (command "_.move" ss "" "_non" '(0 0 0) "_non" (list di 0 0))
  19.     )
  20.     (princ)
  21. )

I figured the user would specify a distance in the same units as the drawing units, hence omitted the format argument from distof.

Cheers guys :)

Woabow

  • Newt
  • Posts: 56
Re: How to get negative values with GETDIST?
« Reply #6 on: September 26, 2012, 03:31:40 PM »
The (initget 128) is what I was looking for indeed. When just asking two points the user cannot enter a distance with the keyboard.

Thanks all.


owenwengerd

  • Bull Frog
  • Posts: 439
Re: How to get negative values with GETDIST?
« Reply #7 on: September 26, 2012, 03:42:39 PM »
                    (setq di (distance p1 p2)
                          di (if (< (car p1) (car p2)) di (- di))
                    )

I would use (setq di (- (car p2) (car p1))) and ignore the other coordinates completely.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10369
Re: How to get negative values with GETDIST?
« Reply #8 on: September 26, 2012, 03:42:47 PM »
Squeeze Mode ON.  8-)
Code: [Select]
(defun c:test (/ ss p1 p2 x)
  (defun txt2num (txt) ; CAB 10/2005
    (cond
      ((distof txt 5))
      ((distof txt 2))
      ((distof txt 1))
      ((distof txt 4))
      ((distof txt 3))
    )
  )
  (if (and (setq ss (ssget))
           (or (initget 128)(setq p1 (getpoint "\nDistance: ")))
           (or(and (listp p1) (setq p2 (getpoint p1 "\nSpecify second point."))
                   (setq x (distance p1 p2)
                         x (if (< (car p2) (car p1)) (- x) x)))
                   (setq x (txt2num p1))))
      (command ".move" ss "" "_none" '(0 0 0) "_none" (list x 0 0))
  )
  (princ)
)
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: 12292
  • London, England
Re: How to get negative values with GETDIST?
« Reply #9 on: September 26, 2012, 04:34:46 PM »
Code - Auto/Visual Lisp: [Select]
  1.                     (setq di (distance p1 p2)
  2.                           di (if (< (car p1) (car p2)) di (- di))
  3.                     )

I would use (setq di (- (car p2) (car p1))) and ignore the other coordinates completely.

Incidentally, I had indeed considered using that approach (as per my earlier code), however since the prompt is for a distance, I thought it may be counter-intuitive for the program to only use a component of the picked distance - but for such simple code, I suppose the point is moot.