Jump to content

lisp to insert texts with numbers directly by pressing the numbers from keyboard


shadi

Recommended Posts

hello everyone , hope u all doing so great 🙏... my problem here that i want easy way to insert numbers in autocad without trying to make texts or adjust them .. i wanna lisp make the number i press from keyboard be inserted into text or mtext and i just click the location of that text in cad ... hope somebody could help me in this ... thanks in advance

Link to comment
Share on other sites

Your wanting to add text on to existing text or all new text? You could modify one of my lisp if you need to add a number at the end.

 

;;----------------------------------------------------------------------------;;
;; ADD TEXT PREFIX OR SUFFIX TO EXISTING TEXT
(defun C:TXT (/ rep str *str ss txt sn ent e)
  (initget "Prefix Suffix")
  (setq rep
    (cond
      ((getkword "\nAdd Text to [Prefix/Suffix]<Suffix>:")) ("Suffix") ;defaults to suffix
    )
  )
  (or (setq *str (getenv "Insert-Text")) (setq *str "Here")) ;gets last string from registry
  (if (= "" (setq str (getstring (strcat "\nEnter " rep " Text \"" *str "\": "))))
    (setq str *str)
  )
  (setenv "Insert-Text" str) ;saves to registry
  (if (setq ss (ssget "_:L" '((0 . "*TEXT"))))
    (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq txt (vlax-ename->vla-object ent))
      (cond
        ((= rep "Prefix")
          (vla-put-textstring txt (strcat str (vla-get-textstring txt)))
        )
        ((= rep "Suffix")
          (vla-put-textstring txt (strcat (vla-get-textstring txt) str))
        )
      )
    )
  )
  (princ)
)

 

Also check this out for numbering things

 

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

20 hours ago, mhupp said:

Your wanting to add text on to existing text or all new text? You could modify one of my lisp if you need to add a number at the end.

 


;;----------------------------------------------------------------------------;;
;; ADD TEXT PREFIX OR SUFFIX TO EXISTING TEXT
(defun C:TXT (/ rep str *str ss txt sn ent e)
  (initget "Prefix Suffix")
  (setq rep
    (cond
      ((getkword "\nAdd Text to [Prefix/Suffix]<Suffix>:")) ("Suffix") ;defaults to suffix
    )
  )
  (or (setq *str (getenv "Insert-Text")) (setq *str "Here")) ;gets last string from registry
  (if (= "" (setq str (getstring (strcat "\nEnter " rep " Text \"" *str "\": "))))
    (setq str *str)
  )
  (setenv "Insert-Text" str) ;saves to registry
  (if (setq ss (ssget "_:L" '((0 . "*TEXT"))))
    (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq txt (vlax-ename->vla-object ent))
      (cond
        ((= rep "Prefix")
          (vla-put-textstring txt (strcat str (vla-get-textstring txt)))
        )
        ((= rep "Suffix")
          (vla-put-textstring txt (strcat (vla-get-textstring txt) str))
        )
      )
    )
  )
  (princ)
)

 

Also check this out for numbering things

 

 

thank u mhupp for ur answer 😇.. i meant new texts or mtexts to be fast way of inserting numbers ,  as an example i press 1 so text creating with 1 inside it after pressing space to end that , press 6 then 5 so another text with 6 number inside it creating after space pressed  ... u got it ?

Link to comment
Share on other sites

This will ask  the user for text and a point then create mtext at that location. will loop until new text isn't created

 

(defun C:foo (/ LastEntity ThisEntity txt pt)
  (setq LastEntity t)
  (while (not (equal LastEntity ThisEntity))
    (setq txt (getstring t "\nEnter Text: "))
    (if (/= txt "")
      (setq pt (getpoint "Select Insertion Point"))
      (setq txt nil)
    )
    (setq LastEntity (entlast))
    (if (and txt pt)
      (entmake (list '(0 . "MTEXT")
                     '(71 . 5)
                     '(100 . "AcDbEntity")
                     '(100 . "AcDbMText")
                     (cons 1 txt)
                     (cons 10 pt)  ;Insertion Point
                     (cons 40 (getvar "TextSize"))
               )
      )
    )
    (setq ThisEntity (entlast))
  )
  (princ)
)

 

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

2 hours ago, mhupp said:

This will ask  the user for text and a point then create mtext at that location. will loop until new text isn't created

 


(defun C:foo (/ LastEntity ThisEntity txt pt)
  (setq LastEntity t)
  (while (not (equal LastEntity ThisEntity))
    (setq txt (getstring t "\nEnter Text: "))
    (if (/= txt "")
      (setq pt (getpoint "Select Insertion Point"))
      (setq txt nil)
    )
    (setq LastEntity (entlast))
    (if (and txt pt)
      (entmake (list '(0 . "MTEXT")
                     '(71 . 5)
                     '(100 . "AcDbEntity")
                     '(100 . "AcDbMText")
                     (cons 1 txt)
                     (cons 10 pt)  ;Insertion Point
                     (cons 40 (getvar "TextSize"))
               )
      )
    )
    (setq ThisEntity (entlast))
  )
  (princ)
)

 

very nice 😍😍🤗 , this is so cool , i tried it , it working well , thanks much ... just if  u can remove pressing enter button after writing every time , as for example . i press 1 then 2 and locate the point of insertion 12 text without pressing enter 

Link to comment
Share on other sites

Doesn't work like that. You know what you want to input but how does the computer know when your done inputting and to move on to the next step? I guess a timer could be set . How long do you make it to time out? make it to fast and you might not input anything and have to repeat the command. make it to long and its not saving anytime. what happens if you input the wrong thing and try and change it to late times up moving on. Whats the big deal with hitting enter?  You can also right click with your mouse if setup properly.

 

Only other thing i can think of is to remove the t in getstring. This means you can't use spaces anymore so when you hit the space bar it acts like enter.

(setq txt (getstring t "\nEnter Text: "))
to
(setq txt (getstring "\nEnter Text: "))

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

7 minutes ago, mhupp said:

Doesn't work like that. You know what you want to input but how does the computer know when your done inputting and to move on to the next step? I guess a timer could be set . How long do you make it to time out? make it to fast and you might not input anything and have to repeat the command. make it to long and its not saving anytime. what happens if you input the wrong thing and try and change it to late times up moving on. Whats the big deal with hitting enter?  You can also right click with your mouse if setup properly.

 

Only other thing i can think of is to remove the t in getstring. This means you can use spaces anymore so when you hit the space bar it like enter.


(setq txt (getstring t "\nEnter Text: "))
to
(setq txt (getstring "\nEnter Text: "))

 

i am sorry , i thought there is a way to do texts without enter  , i appreciate ur work ... thank u a lot for ur help 😇

  • Like 1
Link to comment
Share on other sites

3 minutes ago, shadi said:

i am sorry , i thought there is a way to do texts without enter  , i appreciate ur work ... thank u a lot for ur help 😇

 

Maybe, I don't know everything.

if your going in sequential order you might have missed what i posted before.

http://www.lee-mac.com/numinc.html

Edited by mhupp
Link to comment
Share on other sites

It may be possible with .net where you look at the keyboard buffer not Acad. Way above my pay rate. A quick google suggest yes.

 

Keyboard Events

Keyboard events are generated when any key is pressed or released

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...