Author Topic: Select object or enter text?  (Read 2883 times)

0 Members and 1 Guest are viewing this topic.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Select object or enter text?
« on: August 22, 2013, 08:10:42 AM »
Can someone show me the trees through the forest. I can't for the life of me remember how to code this.

I want to prompt the user to select an object or enter text at the command prompt.  I am expanding a routine that currently prompts the user to enter a number at the command prompt.  However the drawing may have a block that has the number in it already so I want the option to select to the block which I can grab the number from.

I have done this in the past but for the life of me I can't seem to find the code. (and I haven't coded anything in well over 8 months).

Any help would be much appreciated.

Thanks
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
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.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Select object or enter text?
« Reply #2 on: August 22, 2013, 08:41:43 AM »
Thanks CAB,  I'll check those.

It drives me nuts when I know I've done this before but for the life of me I can't remember how.  As soon as I get this done, I will run across the code I have.  :ugly:
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Select object or enter text?
« Reply #3 on: August 22, 2013, 08:48:40 AM »
I do the same thing, I can now blame  "Old Age", it's about the only benefit.  :)

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.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Select object or enter text?
« Reply #4 on: August 22, 2013, 08:50:22 AM »
Well I was hoping it wasn't that, I just rounded the corner into the 40's, if it's old age I have a h3ll of a long way to go  :lmao:
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Select object or enter text?
« Reply #5 on: August 22, 2013, 09:13:26 AM »
Lee Mac Version 2011
http://www.theswamp.org/index.php?topic=39042.msg442350#msg442350

Many thanks for the recommendation CAB, here is the updated version from my library:

Code: [Select]
;; Selection or Text  -  Lee Mac
;; Prompts the user to select an object or enter an arbitrary string.
;; msg - [str] [Optional] Prompt string
;; ftr - [lst] [Optional] ssget filter list
;; Returns: [ent/str] Entity name of selected entity or entered string; "" if enter is pressed.

(defun LM:select-or-text ( msg ftr / gr1 gr2 rtn sel )
    (setq msg (princ (cond (msg) ("\nSelect object: ")))
          rtn ""
    )
    (while
        (progn
            (setq gr1 (grread nil 14 2)
                  gr2 (cadr gr1)
                  gr1 (car  gr1)
            )
            (cond
                (   (= 3 gr1)
                    (if (ssget gr2) ;; nentselp is slow for xrefs
                        (if (setq sel (ssget gr2 ftr))
                            (progn (setq rtn (ssname sel 0)) nil)
                            (princ (strcat "\nInvalid object selected." msg))
                        )
                        (princ (strcat "\nMissed, try again." msg))
                    )
                )
                (   (= 2 gr1)
                    (cond
                        (   (< 31 gr2 127)
                            (setq rtn (strcat rtn (princ (chr gr2))))
                        )
                        (   (= 13 gr2)
                            nil
                        )
                        (   (and (= 8 gr2) (< 0 (strlen rtn)))
                            (setq rtn (substr rtn 1 (1- (strlen rtn))))
                            (princ "\010 \010")
                        )
                        (   t   )
                    )
                )
                (   (= 25 gr1)
                    nil
                )
                (   t   )
            )
        )
    )
    rtn
)

Code: [Select]
(LM:select-or-text "\nSelect a Line: " '((0 . "LINE")))

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Select object or enter text?
« Reply #6 on: August 22, 2013, 09:33:14 AM »
Thanks for the update LEE.
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Select object or enter text?
« Reply #7 on: August 22, 2013, 09:41:42 AM »
Thanks for the update LEE.

You're welcome Tim  :-)

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Select object or enter text?
« Reply #8 on: August 22, 2013, 12:58:26 PM »
Found it!  I knew if that once I started to incorporate the code into the program that I would run across it.  It is slightly different than what was provided, needs an enter to type text.

Code - Lisp: [Select]
  1. (defun foo (/ Test)
  2.         (princ "\nSelect object OR <ENTER> to type: \n")
  3.         (if (null (setq Test (ssget)))
  4.                 (setq Test (getstring "\nEnter text: "))
  5.         )
  6.         Test
  7. )
  8.  

The calling function will have to bear the load of checking the type and acting accordingly.


Thanks again guys.
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Select object or enter text?
« Reply #9 on: August 27, 2013, 06:14:18 AM »
@ Lee:
Why do you use:
Code: [Select]
(princ "\010 \010")Instead of:
Code: [Select]
(princ "\010")?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Select object or enter text?
« Reply #10 on: August 27, 2013, 06:24:16 AM »
@ Lee:
Arrow keys... :-D

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Select object or enter text?
« Reply #11 on: August 27, 2013, 08:01:48 AM »
@ Lee:
Why do you use:
Code: [Select]
(princ "\010 \010")Instead of:
Code: [Select]
(princ "\010")?

IIRC, in earlier versions of AutoCAD, printing a backspace character to the command-line would only move the cursor back one position, without actually erasing the last character, hence you would need to print a space to 'overwrite' the last character and then move the cursor back again.

ribarm

  • Gator
  • Posts: 3309
  • Marko Ribar, architect
Re: Select object or enter text?
« Reply #12 on: August 27, 2013, 09:55:49 AM »
@ Lee:
Why do you use:
Code: [Select]
(princ "\010 \010")Instead of:
Code: [Select]
(princ "\010")?

IIRC, in earlier versions of AutoCAD, printing a backspace character to the command-line would only move the cursor back one position, without actually erasing the last character, hence you would need to print a space to 'overwrite' the last character and then move the cursor back again.

You are genius Lee, how clever...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: Select object or enter text?
« Reply #13 on: August 31, 2013, 10:45:22 AM »
I want to prompt the user to select an object or enter text at the command prompt.  I am expanding a routine that currently prompts the user to enter a number at the command prompt.  However the drawing may have a block that has the number in it already so I want the option to select to the block which I can grab the number from.

hmm, may be you asking something like this
Code - Auto/Visual Lisp: [Select]
  1. (defun swmp_get_string (/ ask_str ask_ent ret_val)
  2.  
  3.   (initget 0 "Select")
  4.   (setq ask_str (getstring T "\nEnter text string [Select] <Exit>: "))
  5.  
  6.   (if (= "S" ask_str)
  7.     (if (progn
  8.           (initget 0)
  9.           (setq ask_ent (car (entsel "\nSelect text object <Exit>: ")))
  10.         )
  11.      
  12.         (or
  13.           (setq ret_val (cdr (assoc 1 (entget ask_ent))))
  14.           (prompt "\nWrong object selected!")
  15.         )
  16.      
  17.       (prompt "\nNothing selected!")
  18.     )
  19.     (setq ret_val (if (/= ask_str "") ask_str ))
  20.   )
  21.  
  22.   ret_val
  23. )