Author Topic: Arbitrary Input with Entsel  (Read 2145 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Arbitrary Input with Entsel
« on: June 10, 2009, 09:19:03 AM »
Ok, this is quite a "noobish" question, but I rarely use entsel in this way, and wondered if the following was possible:

I would like the user to be able to have the option of either selecting an entity on-screen, or providing a block name to use.

Currently, I am using (initget 128) to provide arbitrary input, but, not only does this not allow spaces, but the entsel returns:

Quote
*Invalid selection*
Expects a point or Last

To whatever I input.

Any help on this matter would be great  :wink:

Thanks,

Lee

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Arbitrary Input with Entsel
« Reply #1 on: June 10, 2009, 10:41:44 AM »
You will need to 'Roll Your Own' if you need the space capability.

Try mine. 8-)
Code: [Select]
(defun c:test(/ user ent)

  (setq user (getpoint_or_text 2 "\nSelect or enter name. "))
  (cond
    ((null user) (prompt "\nUser Quit."))
    ((= (type user) 'STR) (prompt (strcase "\nUser entered " user)))
    ((listp user)
     (if (setq ent (nentselp user))
       (prompt "\nObject selected.")
       (prompt "\nNothing selected.")
     )
    )
    (t (prompt "\nInvalid selection."))
  )
  (princ)
)

;;  http://www.theswamp.org/index.php?topic=11342.msg143630#msg143630

;;;=======================[ getpoint_or_text.lsp ]=======================
;;; Author: Copyright© 2005 Charles Alan Butler
;;; Version:  1.0 Dec. 12, 2005
;;; Purpose: To get user entered text or picked point
;;; Sub_Routines: -None
;;; Requirements: -ctype is the cursor type
;;;                      0  Display the normal crosshairs.
;;;                      1  Do not display a cursor (no crosshairs).
;;;                      2  Display the object-selection "target" cursor
;;;               -prmpt is the user prompt, start it with \n
;;; Returns: - picked point or
;;;            the user entered text or
;;;            ""  for Enter Key
;;;            nil for Escape Key
;;;==============================================================
(defun getpoint_or_text (ctype prmpt / char code data result flag p str)
  (vl-load-com)
  (vl-catch-all-apply
    '(lambda ()
       (setq flag t
             str ""
       )
       (princ prmpt)
       (while flag
         (setq p    (grread t 15 ctype)
               code (car p)
               data (cadr p)
         )
         (cond
           ((= code 3) ; clicked point
            (setq result data
                  flag nil
            )
           )
           ((= code 2) ; keyboard
            (setq char data)
            (cond
              ((<= 32 char 126)
               (princ (chr char))
               (setq str (strcat str (chr char)))
              )
              ((= char 8)
               ;; backspace was hit .. go chop off a character
               (and (> (strlen str) 0)
                    (princ (strcat (chr 8) " " (chr 8)))
                    (setq str (substr str 1 (1- (strlen str))))
               )
              )
              ((= char 13)
               (setq result str
                     flag nil
               )
              )
            )
           )
         )
       ) ;_ while
     )
  )
  result
)
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: 12914
  • London, England
Re: Arbitrary Input with Entsel
« Reply #2 on: June 10, 2009, 10:49:22 AM »
Fantastic CAB, great work :)

I do have one question though: I see that you use (vl-catch-all-apply) to encompass your lambda function in the getpoint_or_text function, I can see that this would prevent errors, but how are you able to use this function without supplying a list to apply the function to? I thought that a list was necessary for vl-catch-all-apply...


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Arbitrary Input with Entsel
« Reply #3 on: June 10, 2009, 11:49:49 AM »
Boy you have lots of questions!

And I like it. 8-)

See attached two of my files on error traps.
I normally don't post these as I am a poor house keeper and in the early years I did not save the links back to the origins.
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: 12914
  • London, England
Re: Arbitrary Input with Entsel
« Reply #4 on: June 10, 2009, 12:16:05 PM »
Wow, that is a lot of information right there - thanks Alan, I'll check it out  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Arbitrary Input with Entsel
« Reply #5 on: June 10, 2009, 01:49:58 PM »
Alan,

Sorry for this bombardment of questions... but I am really trying to fill the gaping holes in my knowledge...  :-P

I notice in the examples you posted in your previous post, they supply an example of using vl-catch-all-apply to a situation using a command call:

Code: [Select]
;;  method to use a command
  (vl-catch-all-apply
    '(lambda ()
        (apply 'command (list "line" "0,0" "2,2" ""))
     )
  )
 
  (setq err (vl-catch-all-apply
               'vl-cmdf (list ".arc" ptpick pause pause)))

My question is:

Why do you have to use:

Code: [Select]
'(lambda ()
        (apply 'command (list "line" "0,0" "2,2" ""))
     )

When using command, but not when using vl-cmdf?

I thought that vl-catch-all-apply performed in the same way as the apply function, but would not crash on errors, so why do you need to use another apply function within the lambda expression?

Perhaps I'm having a bad day on this one and am missing someone basic, but I am desperately trying to get my head around this vl-catch-all-apply function...   :ugly:

Cheers,

Lee


Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Arbitrary Input with Entsel
« Reply #6 on: June 11, 2009, 07:05:38 AM »
Perhaps I'm having a bad day on this one and am missing someone basic, but I am desperately trying to get my head around this vl-catch-all-apply function...   :ugly:

Perhaps some of these posts will help.
http://www.theswamp.org/index.php?topic=3385.0
TheSwamp.org  (serving the CAD community since 2003)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Arbitrary Input with Entsel
« Reply #7 on: June 11, 2009, 08:37:17 AM »
Cheers Mark - some great information.. that'll keep me busy for a while  :-P

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Arbitrary Input with Entsel
« Reply #8 on: June 11, 2009, 09:39:46 AM »
Lee,
The code in those files are collections from the internet community, some are mine but most are
collected from here & there. As I said I did a poor job of documenting the source.

The code in your question is not mine but it uses (lambda()  to wrap the code intended to trap.

It could be written this way:
Code: [Select]
 (vl-catch-all-apply
    '(lambda ()
        (command "line" "0,0" "2,2" "")
     )
  )
 
or this way

Code: [Select]
 (vl-catch-all-apply
    'command  (list "line" "0,0" "2,2" "")
  )
Usually the code I put in my files are things that I think I may not remember or examples done in a way I usually
would not do or unusual conditions.  Stuff like that.
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: 12914
  • London, England
Re: Arbitrary Input with Entsel
« Reply #9 on: June 11, 2009, 09:56:14 AM »
Thanks Alan for the explanation.

I also keep what I call "Useful Goodies" tucked away in a folder for reference if I need them :)