Author Topic: What am I doing wrong with GRREAD?  (Read 3975 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
What am I doing wrong with GRREAD?
« on: March 19, 2009, 09:02:15 AM »
I am trying to use grread to track the mouse and keyboard input, but can't seem to keep it from throwing an error when I hit the escape key.  As I understand the help topic, bit 3 (8) should control display of an error message when the escape key is pressed.  It doesn't seem to do anything with the code below:
Code: [Select]
(defun c:test()
  (while (setq input (grread T [color=red](+ 1 2 8)[/color] 0))
    (cond
      ((= 5 (car input))
       (if (setq ent (nentselp (cadr input)))
(progn
   (setq obj (vlax-ename->vla-object (car ent)))
   (princ (strcat "\nFound a " (vla-get-objectname obj) " at "(vl-princ-to-string (cadr ent))))
   )
)
       )
      ((= 2 (car input))
       (cond
((= 27 (cadr input))
  (princ "\xPressed escape ")
  (exit)
  )
(T (princ (vl-prin1-to-string input)))
)
       )
      ((= 3 (car input))
       (if (setq ent (nentselp (cadr input)))
(progn
   (setq obj (vlax-ename->vla-object (car ent)))
   (princ (strcat "\nFound a " (vla-get-objectname obj) " at the point selected: "(vl-princ-to-string (cadr ent))))
   )
)
       )
      )
    )
  )
What am I missing?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: What am I doing wrong with GRREAD?
« Reply #1 on: March 19, 2009, 10:00:09 AM »
Using bit code 8 suppresses this message
Application ERROR: Console break

but does not trap the error. You must use your error routine.

Something to add to the routine which will stop the display when the mouse is not moving:
Code: [Select]
      ((= 5 (car input)) ; pointing device
       (cond
         ((and LastPT (< (distance (cadr input) LastPT) 0.0001))) ; no update if same point
         ((setq ent (nentselp (setq LastPT (cadr input))))
   (setq obj (vlax-ename->vla-object (car ent)))
   (princ (strcat "\nFound a " (vla-get-objectname obj) " at "(vl-princ-to-string (cadr ent))))
)
       )
      )
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.

gskelly

  • Newt
  • Posts: 185
Re: What am I doing wrong with GRREAD?
« Reply #2 on: March 19, 2009, 12:29:14 PM »
Using bit code 8 suppresses this message
Application ERROR: Console break

but does not trap the error. You must use your error routine.

Ha! That put a light bulb on for me... Thanks.
Bricscad v12

mkweaver

  • Bull Frog
  • Posts: 352
Re: What am I doing wrong with GRREAD?
« Reply #3 on: March 19, 2009, 03:56:51 PM »
Thanks, CAB.  Not quite what I was hoping for, but knowing what needs to be done is 3/4 of the battle.

Mike

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: What am I doing wrong with GRREAD?
« Reply #4 on: March 19, 2009, 05:39:12 PM »
Mike,
Don't forget that you can trap the error/ESCape with
Code: [Select]
  (vl-catch-all-apply
    '(lambda ()
        (do your stuff)
     )
  )

Does that help? If not I'll show you how.
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: What am I doing wrong with GRREAD?
« Reply #5 on: March 19, 2009, 06:20:30 PM »
OK, couldn't hold back. Just love to code.  8-)
Code: [Select]
;;  grread with trap for ESCape by CAB
(defun c:test()
  (vl-load-com)
  (while ; stay in loop until one of the COND statements return nil
    (progn
      (setq input (vl-catch-all-apply 'grread (list T (+ 1 2 8) 0)))
    (cond
      ((vl-catch-all-error-p input)
  (princ "\nPressed escape ")
  nil ; exit
      )
      ((= 5 (car input)) ; pointing device
       (cond
         ((and LastPT (< (distance (cadr input) LastPT) 0.0001))) ; no update if same point
         ((setq ent (nentselp (setq LastPT (cadr input))))
   (setq obj (vlax-ename->vla-object (car ent)))
   (princ (strcat "\nFound a " (vla-get-objectname obj) " at "(vl-princ-to-string (cadr ent))))
)
       )
       t ; stay in loop
      )
      ((= 2 (car input)) ; Keyboard input
       (princ (vl-prin1-to-string input))
       t ; stay in loop
       )
      ((= 3 (car input))  ; Selected 3d point
       (if (setq ent (nentselp (cadr input)))
(progn
   (setq obj (vlax-ename->vla-object (car ent)))
   (princ (strcat "\nFound a " (vla-get-objectname obj) " at the point selected: "(vl-princ-to-string (cadr ent))))
   )
)
       t ; stay in loop
       )
      )
     )
    ) ; while
  (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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: What am I doing wrong with GRREAD?
« Reply #6 on: March 19, 2009, 07:27:18 PM »
Don't know if this will help (offered as "food for thought") but this is a function in my library that stays in a loop until the user presses a key and then returns the character corresponding to the key pressed, including the escape key (ASCII code 27 = "\e"), kinda like C's getchar function.

Code: [Select]
(defun _GetChar ( / data key result done )

    [color=green];;  return the character for the key pressed[/color]
   
    (while (not done)
        [color=green];;  stay in the loop until the user presses a key[/color]
        (vl-catch-all-apply
           '(lambda ( )
                (setq
                    data nil
                    data (grread nil 14 1)
                )
            )
        )
        (setq
            key    (car data)
            result (cadr data)
        )
        (cond
            [color=green];;  user pressed <esc>[/color]
            ((null data) (setq done t result 27))
            [color=green];;  user pressed a key[/color]
            ((eq 2 key) (setq done t))
            [color=green];;  user hit right mouse button, consider same as enter[/color]
            ((eq 25 key)(setq done t result 13))
        )
    )

    (chr result)

)
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

gskelly

  • Newt
  • Posts: 185
Re: What am I doing wrong with GRREAD?
« Reply #7 on: March 20, 2009, 12:01:11 AM »
Cool... I'm starting to get this in my head now. This may be just noise to mkweaver but hopefully not. This place is great!

Code: [Select]
(defun my_esc_test ( / *error* te en input)
;;;=============================================================================
;;; Trying to trap ESC key and process mouse position
;;;=============================================================================

    ;;;=========================================================================
    ;; Define the local error handler
    (defun *error* (msg)
        (princ "\nnss:slide_text2: bummer I am in the local *error*...")
    )
    ;;;=========================================================================

    ;; Get user to select a text or mtext
    (while (not te)
        (if (setq en (entsel "Select text to slide: "))
            (cond
                ((wcmatch (cdr (assoc 0 (setq te (entget (car en))))) "*TEXT")
                    te)
                (T
                    (setq te NIL)))))

    ;; Ok, try out the grread...
    (setq loop_again T)
    (while loop_again
        (cond
            ((not (setq input (ax:trap 'grread '(T 13 0))))
                (princ "\nI think I caught an ESC... do cleanup stuff")
                (setq loop_again NIL)
            )
            ((= 5 (car input)) ;; mouse move
                (nss:move_text te (cadr input))
            )
        )
    )
   
    ;; Exit quietly
    (prin1)

)
;;;=============================================================================


(defun c:test ()
    (my_esc_test)
)


(defun nss:text_alignment_group ( te )
;;;=============================================================================
    (if (and
            (zerop (cdr (assoc 72 te)))
            (zerop (cdr (assoc 73 te)))
        )
        10 ;; use first alignment point iff groups 72 & 73 are zero
        11 ;; otherwise use the second alignment point
    )
)
;;;=============================================================================


(defun nss:entmod ( ent grp val / a )
;;;=============================================================================
;;; Function to modify and entity.
;;;
;;; Input: ent --> the entity association list
;;;        grp --> the dxf group code of interest
;;;        val --> the value to put in "ent" for "grp"
;;;
;;; Return: the association list of the modified entity.
;;;=============================================================================
    (if (setq a (assoc grp ent))
        (and
            (setq ent (subst (cons grp val) a ent))
            (entmod ent)
        )
        (and
            (setq ent (append (ent (cons grp val))))
            (entmod ent)
        )
    )
)
;;;=============================================================================


(defun nss:move_text ( te pt / tag )
;;;=============================================================================
;;; Function to move a text entity.
;;;
;;; Input: te --> text entity and entity name or association list
;;;        pt --> pt ot move the text to as a list (x y z)
;;;
;;; Return:
;;;=============================================================================
    ;(princ "\nnss:move_text: entered...")

    ;; Get association list if we have entity name
    (if (= (type te) 'ENAME)
        (setq te (entget te))
    )
   
    (cond
        ((= (cdr(assoc 0 te)) "TEXT")
            ;; Move a text entity
            (and
                (setq tag (nss:text_alignment_group te))
                (setq te (nss:entmod te tag pt))
            )
        )
        ((= (cdr (assoc 0 te)) "MTEXT")
            ;; Move a multiline text entity
            (setq te (nss:entmod te 10 pt))
        )
        (T
            (princ
                (strcat
                    "\nnss:move_text: unexpected entity type = "
                    (cdr (assoc 0 te))))
        )
    )
    ;(princ "\nnss:move_text: exited...")
)
;;;=============================================================================


(defun ax:trap (func arglist / val )
;;=============================================================================
;; Apply a function func with arguments arglist and catch errors.
;;
;; Return: NIL if error caught (message is output)
;;         T   if func returned NIL but no error was caught.
;;         val if func returned val
;; Based on ideas I've read in several places.
;;=============================================================================
    (cond
        ((and
            func
            ;(princ "\nfunc: ")(princ (type (eval func)))
            arglist
            ;(princ "\narglist: ")(princ (type arglist))
            (= (type (eval func)) 'SUBR)
            ;(princ "\nfunc is a SUBR...")
            (= (type arglist) 'LIST)
            ;(princ "\narglist is a LIST...")
         )
            (setq val (vl-catch-all-apply func arglist))
            (cond
                ((vl-catch-all-error-p val)
                    ;(princ (strcat "\n" (vl-catch-all-error-message val)))
                    NIL
                )
                (val
                    ;; if we get here there has been no error and
                    ;; func returned a value in val... pass it on...
                )
                (T
                    ;; if we get here there has been no error but
                    ;; func returned NIL... return T
                )
            )
        )
        (T
            (princ "\nERR: ax:trap - invalid input parameters...")
            NIL
        )
    )
)
;;=============================================================================

Bricscad v12

mkweaver

  • Bull Frog
  • Posts: 352
Re: What am I doing wrong with GRREAD?
« Reply #8 on: March 20, 2009, 09:03:33 AM »
I appreciate all the code and suggestions.  The catch-all-apply is the trick I needed, and the subsequent code makes my job so much easier.

Thanks,
Mike

mkweaver

  • Bull Frog
  • Posts: 352
Re: What am I doing wrong with GRREAD?
« Reply #9 on: March 20, 2009, 09:42:15 AM »
This is where I was headed with the original code - this aligns selected hatch objects with whatever object is under the cursor.  Currently only supports lines, text objects (attributes, text, mtext...) and hatch objects:
Code: [Select]
(defun c:AlignHatch ()
  (vl-load-com)
  (setq
    ss1 (ssget '((0 . "HATCH")))
    objHatch (vlax-ename->vla-object (ssname ss1 0))
    )
  (while ; stay in loop until one of the COND statements return nil
    (progn
      (setq input (vl-catch-all-apply 'grread (list T (+ 1 2 8) 0)))
      (cond
;;catch the escape key
((vl-catch-all-error-p input)
nil ; exit
)
;;drag point captured
((= 5 (car input)) ; pointing device
(cond
   ;;the mouse never moved enough to worry about
   ((and LastPT (< (distance (cadr input) LastPT) 0.001)))
   ;;if there is anything under the cursor
   ((setq ent (nentselp (setq LastPT (cadr input))))
    (setq obj (vlax-ename->vla-object (car ent)))
    (cond
      ((vlax-property-available-p obj 'ANGLE)
       (vla-put-patternangle objHatch (vla-get-angle obj))
       )
      ((vlax-property-available-p obj 'ROTATION)
       (vla-put-patternangle objHatch (vla-get-rotation obj))
       )
      ((vlax-property-available-p obj 'PATTERNANGLE)
       (vla-put-patternangle objHatch (vla-get-patternangle obj))
       )
      )
    t ;stay in loop
   )
)
t ; stay in loop
)
;; Keyboard input
((= 2 (car input))
(princ (vl-prin1-to-string input))
t ; stay in loop
)
;;Selected 3d point
((= 3 (car input))
(if (setq ent (nentselp (cadr input)))
   ;;object found at the selected point
   (progn
     (setq obj (vlax-ename->vla-object (car ent)))
     (princ (strcat "\nFound a "
    (vla-get-objectname obj)
    " at the point selected: "
    (vl-princ-to-string (cadr ent))
    )
     )
   )
)
t ; stay in loop
)
      )
    )
  ) ; while
  (princ)
)