Jump to content

getstring default input highlight and cursor setting


ryydman

Recommended Posts

Hi all!

 

I am new to AutoLISP programming. I wanted to try and create this program that would allow me to more easily edit text in AutoCAD. 

The program is working but I would like that the default input given to user wouldn't be highlighted or selected. That is a problem because if I then press enter it doesn't input anything. Also having the cursor on the right for easy editing would also be better. Is there a way to achieve this?
My code: 

(defun c:TRE (/ ss i text textList filter)
	;; Define the selection set filter to select only text objects
	(setq filter '((0 . "TEXT,MTEXT")))
	;; Select the text objects 
	(setq ss (ssget filter))
	(if ss
		(progn
			;; Create list of entities from selection set
			(setq textList nil)
			(repeat (setq i (sslength ss))
				(setq text (ssname ss (setq i (1- i))))
				(setq textList (append textList (list text)))
			)
			;; Tolerance to y-coordinate sorting 
			(setq tolerance 5.0)
			;; Sorting text list from top to bottom left to right
			(setq textList
				(vl-sort textList 
					(function 
						(lambda (e1 e2)
                        				;; check if on same y-coordinate (+-tolerance) then sort left to right otherwise sort top to bottom
							(if (< (abs (- (cadr (cdr (assoc 10 (entget e1)))) (cadr (cdr (assoc 10 (entget e2)))))) tolerance)
								(< (car (cdr (assoc 10 (entget e1)))) (car (cdr (assoc 10 (entget e2)))))
								(> (cadr (cdr (assoc 10 (entget e1)))) (cadr (cdr (assoc 10 (entget e2)))))
							)
						)
					)
				)
			)
			;; Going through each text and letting user modify
			(foreach text textList 
				(entmod (subst (cons 1 (getstring T (strcat "Edit text: <" (cdr (assoc 1 (entget text))) "> -> ")))
							   (assoc 1 (entget text))
							   (entget text)))
							   
				(entupd text)
			)
		(princ "\nText objects edited successfully.")
		) ; end of progn
	(princ "\nNo objects found in the specified area.")
	) ; end of if ss
(princ)
) ; end of program

All help is appreciated!
Best regards

Link to comment
Share on other sites

Give this a try:

(defun c:TRE (/ ss i text textList ntxt otxt filter)
	;; Define the selection set filter to select only text objects
	(setq filter '((0 . "TEXT,MTEXT")))
	;; Select the text objects 
	(setq ss (ssget filter))
	(if ss
		(progn
			;; Create list of entities from selection set
			(setq textList nil)
			(repeat (setq i (sslength ss))
				(setq text (ssname ss (setq i (1- i))))
				(setq textList (append textList (list text)))
			)
			;; Tolerance to y-coordinate sorting 
			(setq tolerance 5.0)
			;; Sorting text list from top to bottom left to right
			(setq textList
				(vl-sort textList 
					(function 
						(lambda (e1 e2)
                        				;; check if on same y-coordinate (+-tolerance) then sort left to right otherwise sort top to bottom
							(if (< (abs (- (cadr (cdr (assoc 10 (entget e1)))) (cadr (cdr (assoc 10 (entget e2)))))) tolerance)
								(< (car (cdr (assoc 10 (entget e1)))) (car (cdr (assoc 10 (entget e2)))))
								(> (cadr (cdr (assoc 10 (entget e1)))) (cadr (cdr (assoc 10 (entget e2)))))
							)
						)
					)
				)
			)
			;; Going through each text and letting user modify
			(foreach text textList
            (setq otxt (cdr (assoc 1 (entget text)))
                  ntxt (getstring T (strcat "Edit text: <" otxt "> -> "))
            )
            (if (= ntxt "")(setq ntxt otxt))
            (entmod (subst (cons 1 ntxt)
                    (assoc 1 (entget text))
                    (entget text)))
							   
				(entupd text)
			)
		(princ "\nText objects edited successfully.")
		) ; end of progn
	(princ "\nNo objects found in the specified area.")
	) ; end of if ss
(princ)
) ; end of program

 

  • Thanks 1
Link to comment
Share on other sites

4 minutes ago, pkenewell said:

Give this a try:

(defun c:TRE (/ ss i text textList ntxt otxt filter)
	;; Define the selection set filter to select only text objects
	(setq filter '((0 . "TEXT,MTEXT")))
	;; Select the text objects 
	(setq ss (ssget filter))
	(if ss
		(progn
			;; Create list of entities from selection set
			(setq textList nil)
			(repeat (setq i (sslength ss))
				(setq text (ssname ss (setq i (1- i))))
				(setq textList (append textList (list text)))
			)
			;; Tolerance to y-coordinate sorting 
			(setq tolerance 5.0)
			;; Sorting text list from top to bottom left to right
			(setq textList
				(vl-sort textList 
					(function 
						(lambda (e1 e2)
                        				;; check if on same y-coordinate (+-tolerance) then sort left to right otherwise sort top to bottom
							(if (< (abs (- (cadr (cdr (assoc 10 (entget e1)))) (cadr (cdr (assoc 10 (entget e2)))))) tolerance)
								(< (car (cdr (assoc 10 (entget e1)))) (car (cdr (assoc 10 (entget e2)))))
								(> (cadr (cdr (assoc 10 (entget e1)))) (cadr (cdr (assoc 10 (entget e2)))))
							)
						)
					)
				)
			)
			;; Going through each text and letting user modify
			(foreach text textList
            (setq otxt (cdr (assoc 1 (entget text)))
                  ntxt (getstring T (strcat "Edit text: <" otxt "> -> "))
            )
            (if (= ntxt "")(setq ntxt otxt))
            (entmod (subst (cons 1 ntxt)
                    (assoc 1 (entget text))
                    (entget text)))
							   
				(entupd text)
			)
		(princ "\nText objects edited successfully.")
		) ; end of progn
	(princ "\nNo objects found in the specified area.")
	) ; end of if ss
(princ)
) ; end of program

 

Thank you! 

This solves the issue of having no text input but I'm still wondering if there's some way to have the cursor on the right automatically without having to move it there. I know it's a small thing but the whole point of this code was to make editing multiple text as easy as possible 😅

Anyways thanks a lot for helping!

Link to comment
Share on other sites

34 minutes ago, ryydman said:

Thank you! 

This solves the issue of having no text input but I'm still wondering if there's some way to have the cursor on the right automatically without having to move it there. I know it's a small thing but the whole point of this code was to make editing multiple text as easy as possible 😅

Anyways thanks a lot for helping!

 I'm not sure what you mean by "way to have the cursor on the right automatically". If you mean move the cursor to where the text is, then the answer is no. Alternatively, you could zoom in to where the text is perhaps?

  • Like 1
Link to comment
Share on other sites

Are you saying you want to add a Suffix to each text ? Like this "Pt" = "Pt 123" . Every Pt is numbered. 

 

Please explain more the desired result.

  • Like 1
Link to comment
Share on other sites

18 hours ago, pkenewell said:

 I'm not sure what you mean by "way to have the cursor on the right automatically". If you mean move the cursor to where the text is, then the answer is no. Alternatively, you could zoom in to where the text is perhaps?

7 hours ago, BIGAL said:

Are you saying you want to add a Suffix to each text ? Like this "Pt" = "Pt 123" . Every Pt is numbered. 

 

Please explain more the desired result.

Sorry for my bad explanation I attached a gif that hopefully clears things up. There you can see when you get the default text it is automatically highlighted and you have to move the cursor to the right every time when usually what you want to change is on the right. So I understand I could create another program to work better with just adding the suffix but what I wanted this program to do was just generally make all text editing, when theres a lot of it, easier with no mouse movements required. Seems like there isn't a way to do that but this already helps nicely.

Thanks again for the help! 

 

spacer.png

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