Jump to content

Put the text in the command string without copying and pasting


Nikon

Recommended Posts

Happy New Year to all
How can I put the text in the command string without copying and pasting,

but just specify the desired text and it will appear on the command string,

or perhaps by dragging the text to the command string?

 

Edited by Nikon
Link to comment
Share on other sites

Look at entsel and entget. Something like this, untested, no CAD today to check for typos

 

(setq MyEnt (entget (car (entsel "Select text:"))) ; Select an object

;; can do some checking here that the selected object is text

(setq MyText (cdr (assoc 1 (MyEnt))))              ; get the text string from the object

(princ MyText)

 

I assume you want to do more than just display the text in the command line, but these 3 lines above will do that, add to a LISP routine or make your own based around this maybe

 

 

Note that very long text strings (for example in mtext)(over 256 characters?) the text is stored in another variable in the entity description, something to be aware of

  • Like 1
Link to comment
Share on other sites

Posted (edited)

@Steven P Thank you and happy new year

 

I have little knowledge of programming,
I do not know what to do with these lines of code.

My task is as follows:
I have a list (text) with lisp names in the file dwg
(_autoload "11")
(_autoload "12")
. . . . . . . . . . . . .
(_autoload "16")
I point to the desired text and it is inserted into the command string,
I press "enter" and lisp loads and starts working...

 

The difficulty is that the text does not need to be highlighted, just point at it.

Edited by Nikon
Link to comment
Share on other sites

Time to start learning a little..... (this is a good resource, http://www.lee-mac.com/tutorials.html )

 

The snippets of code will do what you want but a learning point for you to make that into a LISP perhaps? Use the link above and other examples we've given you to see if you can make something up. Pretty much for that create the first row '(defun .....' and the last row ')', saved in a file.

 

For the next part you will need to select the text rather than pointing the mouse at the text (right clicks), for now though have a go at making the above into a LISP, post what you make up below and I am happy to guide you to the next steps of what you want to do.

 

However it sounds like what you want is a menu rather than selecting texts in the drawing? An Alternative is a bit more work and a pop-up dialog with the commands listed that you can select

 

 

  • Like 1
Link to comment
Share on other sites

Posted (edited)
On 01.01.2024 at 16:25, Steven P said:

However it sounds like what you want is a menu rather than selecting texts in the drawing? An Alternative is a bit more work and a pop-up dialog with the commands listed that you can select

No, it's just about pointing to the text from the dwg drawing.

 

Edited by Nikon
Link to comment
Share on other sites

Yes, that is possible using something called grread, but as the AutoCAD help suggests that is 'for advanced functions and generally "get" functions should be used' (something like that) - in other words AutoDesk are recommending to use another method.

 

It is generally faster to use one type of input, all mouse (point + click) or all keyboard (type in the LISP name) rather than hover, check you have text and the correct text highlighted, press enter.

 

If you want a list on the screen to select then I'd recommend point + click - which follows on from my answer above.

 

Maybe a description of your ultimate aims would help, part questions get part answers. What we might suggest as an output might not be compatible with what you want to do next.

Link to comment
Share on other sites

No it is very possible to do, just trickier and most will not, preferring the simpler ways to do things. Simpler ways have more example code and descriptions to learn from, and you will get more help using them.

 

Compare the 2 codes below. Simpler first:

 

;;Example, select text string traditionally. 6 lines of code

(defun c:GrabText ( / MyEnt MyText)
  (setq MyEnt (entget (car (entsel "Select text:")))) ; Select an object
  (setq MyText (cdr (assoc 1 MyEnt)))              ; get the text string from the object
  (princ MyText)
  (princ)
)

 

More complex.

 

;;Example, hover over text, press enter or space to select. 67 lines of code

(defun c:GrabText ( / MyValue MyEnt EndLoop grsel sela lw pt MyText LastText sel )
  (princ (setq Msg "Select text"))
  (setq MyValue "")
  (setq MyEnt (list))
  (setq EndLoop "No")
  (while (= EndLoop "No")
    (setq grsel (grread nil 9 2))
    (setq sela (car grsel))
    (if (= sela 5) ; Highlight on hover
      (progn
        (if lw (redraw lw 4) ) ;Remove any highlight
        (if
          (and
            (setq pt (cadr grsel) )
            (setq lw (ssget pt))
            (setq lw (ssname lw 0))
            (if (or (= LastText (cdr (assoc 1 (entget lw))))
                    (= (cdr (assoc 1 (entget lw))) nil)
                ) ; endor
              (redraw lw 3)
              (progn
                (setq MyText (cdr (assoc 1 (entget lw))))
                (setq LastText MyText)
                (princ "\n")
                (princ LastText)
          (redraw lw 3)
              ) ; end progn
            ) ; end if
          ) ; end and
          (redraw lw 3)
        ) ; end  if
      ) ; end progn
    ) ; end if

    (if (= sela 2) ; text entry only "Enter" or "Space"
      (progn
        (setq sel (nth 1 grsel))
        (if (= (type sel) 'LIST)
          (progn
            (setq MyEnt (nentselp sel))
            (if (= nil MyEnt)
              (Princ "\nMissed! Try again.\nSelect text: ")
              (setq EndLoop "Yes")
            ) ; end if
            (redraw lw 4) ; remember to (redraw xyz 3) in originating code to keep highlight
            (setq sel nil)
          ) ; end progn
          (progn
            (if (= sel 13) ; enter
              (progn
                (setq EndLoop "Yes")
              ) ; end progn
            ) ; end if

            (if (= sel 32) ; space
              (progn
                (setq EndLoop "Yes")
              ) ; end progn
            ) ; end if
          ) ; end progn
        ) ; end if list
    )) ; end progn ; end if text / mouse entry
  ) ; end while
  (princ "\nThanks. Selected text: ")
  (princ MyText)
  (princ)
)

 

 

I'll let you decide which one to you want to study and understand first.

 

  • Like 1
Link to comment
Share on other sites

To run after select.

 

(princ MyText)
replace with
(vl-cmdf mytext)

Why would you have your run lisp programs as text ? Just make a menu if you can use notepad you can make a menu.

  • Like 1
  • Agree 1
  • Thanks 1
Link to comment
Share on other sites

Some time ago on this forum, I used a context menu to insert predefined texts. On that occasion, I received the help of the talented @Lee Mac to develop the necessary code.

The code is basically a menu (list) that contains the specific texts that you want to insert. If you want to change the text, simply right click and choose the option with the desired text.

The height of the inserted text automatically adjusts to the current text style. In the event that the style does not have a defined height, the user will be prompted to enter a specific height.

You can find the original post where Lee Mac gave me help at this link.

 

Here you have the code adapted to your needs. I hope this is helpful to you.

(defun c:ITEXT ( / IR_*error* IR_dch IR_dcl IR_des IR_hgt IR_idx IR_ins IR_lst IR_sty )
    (defun IR_*error* ( msg )
        (if (< 0 IR_dch) (unload_dialog IR_dch))
        (if (= 'file (type IR_des)) (close IR_des))
        (if (and (= 'str (type IR_dcl)) (setq IR_dcl (findfile IR_dcl))) (vl-file-delete IR_dcl))
        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (setq IR_lst
       '(
            "_autoload \"11\""
            "_autoload \"12\""
            "_autoload \"13\""
            "_autoload \"14\""
            "_autoload \"15\""
            "_autoload \"16\""
        )
    )

    (setq IR_sty (getvar 'textstyle)
          IR_hgt (cdr (assoc 40 (tblsearch "style" IR_sty)))
    )
    (if (zerop IR_hgt)
        (progn
            (initget 6)
            (setq IR_hgt (cond ((getdist (strcat "\nAltura de texto <" (rtos (getvar 'textsize)) ">: "))) ((getvar 'textsize))))
        )
    )

    (if
        (and
            (setq IR_dcl (vl-filename-mktemp nil nil ".dcl"))
            (setq IR_des (open IR_dcl "w"))
            (foreach str
               '(
                    "txt : dialog"
                    "{"
                    "    key = \"dcl\"; spacer;"
                    "    : list_box"
                    "    {"
                    "        key = \"lst\";"
                    "        width = 40;"
                    "        height = 20;"
                    "        fixed_width = true;"
                    "        fixed_height = true;"
                    "        allow_accept = true;"
                    "    }"
                    "    ok_cancel;"
                    "}"
                )
                (write-line str IR_des)
            )
            (not (setq IR_des (close IR_des)))
            (< 0 (setq IR_dch (load_dialog IR_dcl)))
        )
        (while
            (not
                (cond
                    (   (not (new_dialog "txt" IR_dch))
                        (princ "\nDialog could not be loaded.")
                    )
                    (   (progn
                            (set_tile    "dcl" "Select Text")
                            (start_list "lst")
                            (foreach itm IR_lst (add_list itm))
                            (end_list)
                            (set_tile    "lst"  (cond (IR_idx) ((setq IR_idx "0"))))
                            (action_tile "lst" "(setq IR_idx $value)")
                            (zerop (start_dialog))
                        )
                    )
                    (   (while (setq IR_ins (getpoint "\rSpecify text insertion point <back>: "))
                            (entmake
                                (list
                                   '(000 . "MTEXT")
                                   '(100 . "AcDbEntity")
                                   '(100 . "AcDbMText")
                                   '(008 . "_TEXTOS")
                                    (cons 007 IR_sty)
                                    (cons 040 IR_hgt)
                                    (cons 010 (trans IR_ins 1 0))
                                    (cons 001 (nth (atoi IR_idx) IR_lst))
                                    (cons 011 (getvar 'ucsxdir))
                                    (cons 210 (trans '(0.0 0.0 1.0) 1 0 t))
                                )
                            )
                        )
                        (redraw)
                    )
                )
            )
        )
        (princ "\nUnable to write & load DCL file.")
    )
    (IR_*error* nil) (princ)
)

 

  • Thanks 1
Link to comment
Share on other sites

10 hours ago, Steven P said:
;;Example, select text string traditionally. 6 lines of code

(defun c:GrabText ( / MyEnt MyText)
 (setq MyEnt (entget (car (entsel "Select text:")))) ; Select an object
 (setq MyText (cdr (assoc 1 MyEnt))) ; get the text string from the object
 (princ MyText)
 (princ)
)

This is close to my task, the text appears on the command string, but the lisp does not load.
Why is that?

1 hour ago, Romero said:
(defun c:ITEXT ( / IR_*error* IR_dch IR_dcl IR_des IR_hgt IR_idx IR_ins IR_lst IR_sty )

This code inserts the selected text into the drawing, but I already have my list in dwg.

Link to comment
Share on other sites

10 hours ago, Steven P said:
;;Example, hover over text, press enter or space to select. 67 lines of code

(defun c:GrabText ( / MyValue MyEnt EndLoop grsel sela lw pt MyText LastText sel )
 (princ (setq Msg "Select text"))

@Steven P  what should I add to the code for the command (selected text) to load?

Link to comment
Share on other sites

@nikon 

I apologize as it seems I misinterpreted your request. Now I understand that what you really need is to be able to select some text to be displayed on the command line, is that correct?

Here is a starting point that I hope will be useful to you.

 

(defun c:t2cl () 
  (setq IR_ent (car (entsel "\nSelect text: "))) 

  (if IR_ent
    (progn
      (setq IR_texto (cdr (assoc 1 (entget IR_ent)))) 
      (princ (strcat "\n" IR_texto)) 
    )
    (princ "\nno text selected.")
  )
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

Posted (edited)
30 minutes ago, Romero said:

 

(defun c:t2cl () 
 (setq IR_ent (car (entsel "\nSelect text: "))) 

@Romero  thanks
the command appears on the command string, but lisp does not load, as if the command is transparent 💨

or ghostly👻...

command: T2CL
Select text:
(_autoload "11")

Edited by Nikon
Link to comment
Share on other sites

22 minutes ago, Nikon said:

@Romero  gracias,
el comando aparece en la cadena de comando, pero lisp no se carga, como si el comando fuera transparente 💨

o fantasmal👻 ...

comando: T2CL
Seleccionar texto:
(_autoload "11")


 

In AutoLISP, there is no direct way to monitor or control the command line in real time to automatically detect and execute a command if it matches specific text.

AutoLISP runs sequentially and lacks the ability to interact with the AutoCAD command line in real time. The 'princ' function is used to print text to the command line, but it does not have the ability to detect or execute commands based on what is displayed on the command line.

Automatic detection of commands entered on the command line and their subsequent execution is not possible directly through AutoLISP.

To achieve similar functionality, it would be necessary to use another programming language or the API provided by AutoCAD, such as the .NET API or the use of event-triggered AutoLISP. These offer broader interaction capabilities and control over commands entered on the AutoCAD command line.

Unfortunately, I lack knowledge on how to achieve this. 🙁

Edited by Romero
  • Thanks 1
Link to comment
Share on other sites

Assuming I've understood what you're looking to achieve, you could potentially use the sendcommand method to accomplish this, i.e.:

 

(defun c:ctext ( / ent enx str )
    (while
        (not
            (progn
                (setvar 'errno 0)
                (setq ent (car (entsel "\nSelect command text: ")))
                (cond
                    (   (= 7 (getvar 'errno))
                        (prompt "\nMissed, try again.")
                    )
                    (   (null ent))
                    (   (not (wcmatch (cdr (assoc 0 (setq enx (entget ent)))) "*TEXT"))
                        (prompt "\nThe selected object is not text or mtext.")
                    )
                    (   (setq str (cdr (assoc 1 enx))))
                )
            )
        )
    )
    (if str (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) (strcat str "\n")))
    (princ)
)
(vl-load-com) (princ)

 

Edited by Lee Mac
  • Like 5
Link to comment
Share on other sites

38 minutes ago, Lee Mac said:

Assuming I've understood what you're looking to achieve, you could potentially use the sendcommand method to accomplish this, i.e.:

@Lee Mac Thank you very much!

Magic has happened! It works the way I wanted it to!
The text gets into the command string and the lisp is loaded. It's incredible!

Your work is very valuable. Good luck in the new year!

  • Like 1
Link to comment
Share on other sites

23 hours ago, BIGAL said:

To run after select.

 

(princ MyText)
replace with
(vl-cmdf mytext)

Why would you have your run lisp programs as text ? Just make a menu if you can use notepad you can make a menu.

 

That wasn't working and I think you'd have to use send command like Lee Macs code above or perhaps something including 'read' to make it run?

 

Link to comment
Share on other sites

Posted (edited)

I use the Lee "сtext" command to load Lisp of this type (_autoload "11"). 11 is the name of the command.

At the end of the code with the extension (.lsp), I add (c:11), and the list is loaded and the command is immediately active.

How can I write a string (_autoload "11") for files with the extension (.fas) and (.vlx), since these files cannot
be edited and. I cannot add them at the end of the code (C:11).

For simple loading (_autoload "dim text_bgmask") it works, but the command is not active, 
it needs to be additionally entered into the command string.

I'm trying to add

 

(_autoload "dimtext_bgmask" '("dimtext_bgmask"))

or

(_autoload "dimtext_bgmask" (c:dimtext_bgmask))

But it doesn't work, messages appear on the command string:
;error: too many arguments
;error: invalid function: "dim text_bgmask"

Please advise me how to write the string correctly (_autoload...

dimtext_bgmask.fas

Edited by Nikon
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...