Remove line from all mtext

Remove line from all mtext

neam
Collaborator Collaborator
1,227 Views
12 Replies
Message 1 of 13

Remove line from all mtext

neam
Collaborator
Collaborator

Hi everyone

How can remove from all mtext, for example first line or second line or third line.... N line?

0 Likes
Accepted solutions (1)
1,228 Views
12 Replies
Replies (12)
Message 2 of 13

Kent1Cooper
Consultant
Consultant

Would the lines of content always and only be separated by Enters/returns [the \P that shows in the Contents in the Properties palette of the few I looked at], and never be the result of word-wrapping based on the Mtext's box width?

Kent Cooper, AIA
Message 3 of 13

Sea-Haven
Mentor
Mentor

Try this, no error checking for wrong objects etc.

 

 

; Thanks to lee-mac for function.
; 44 is comma 9 is tab 34 is space 58 is colon 126 is tilde

(defun c:remtext ( / x K _csv->lst126 obj str lst num)
; By AlanH Dec 2023

(defun _csv->lst126 ( str / pos )
	(if (setq pos (vl-string-position 126 str))
		(cons (substr str 1 pos) (_csv->lst126 (substr str (+ pos 2))))
		(list str)
    )
)

(setq num (- (getint "\nEnter line number to remove ") 1))

(while (setq ent (car  (entsel "Pick mtext ")))
  (setq obj (vlax-ename->vla-object ent))
  (setq str (vlax-get obj 'Textstring))

  (while (vl-string-search "\\P" str)
  (setq str (vl-string-subst "~" "\\P" str 1))
  )

  (setq lst (_csv->lst126 str))
  (setq K 0 newstr "")
  (If (> num (length lst))
   (alert "Number is larger than available ")
   (foreach val lst
   (if (= K num)
    (princ)
    (setq newstr (strcat newstr "\\P" val))
   )
   (setq k (1+ k))
   )
)

(vlax-put obj 'Textstring newstr)

)
(princ)
)

 

Message 4 of 13

neam
Collaborator
Collaborator

Thank you dear kent for reply.

Your answers is yes.
The points you raised are followed in all the mtext.

0 Likes
Message 5 of 13

neam
Collaborator
Collaborator

Hi dear sea-haven

thanks fo reply

Is it possible to use ssget else instead of entsel and delete line?

so that the text of three lines becomes two lines.
Currently, it only deletes the text and the line remains.

So that the text of three lines becomes two lines.
Currently, it only deletes the text and the line remains.
And in re-execution of the program, it does not work to delete another line of mtext.

0 Likes
Message 6 of 13

komondormrex
Mentor
Mentor
Accepted solution

hey there,

check this one

 

;*********************************************************************************************************************************

(defun string_to_list (input_string delimiter / delimiter_position output_list)
	(while (setq delimiter_position (vl-string-search delimiter input_string 0))
		(setq output_list (append output_list (list (substr input_string 1 delimiter_position)))
			  input_string (substr input_string (+ 3 delimiter_position))
		)
	)
	(append output_list (list input_string))
)

;*********************************************************************************************************************************

(defun c:delete_nth_line (/ mtext_sset mtext_list)
	(setq line_default_value 1
		  line (if (null (setq line_new (vl-catch-all-apply 'getint (list (strcat "\nEnter line number <" 
																				  (if line (itoa line) (itoa line_default_value)) 
																				  ">: "
																		  )
																	)
										)))
					  	 (if line line line_default_value)
						 (if (vl-catch-all-error-p line_new) nil (setq line_default_value line_new))
				   )
		  mtext_sset (ssget '((0 . "mtext")))
	)
	(if mtext_sset 
		(foreach mtext (vl-remove-if 'listp (mapcar 'cadr (ssnamex mtext_sset)))
			(setq mtext_list (string_to_list (cdr (assoc 1 (entget mtext))) "\\P"))
			(vla-put-textstring (vlax-ename->vla-object mtext)
								(apply 'strcat (cdr (apply 'append (mapcar '(lambda (text_line) (list "\\P" text_line))
																	   (vl-remove (nth (1- line) mtext_list) mtext_list)
															  )
													)
											   )
								)
			)
		)
	)
)

;*********************************************************************************************************************************

updated

 

Message 7 of 13

neam
Collaborator
Collaborator

Hi dear komondormrex

Is it possible to remove blank lines from the result?

neam_0-1703758845919.png

 

0 Likes
Message 8 of 13

neam
Collaborator
Collaborator

I want the first line and the third line to be deleted so that only the second line remains.

neam_0-1703758806826.png

 

0 Likes
Message 9 of 13

neam
Collaborator
Collaborator

Of course, this is an example.
In general, the mtext may have more lines and we want to delete some lines and keep the rest of the lines.

neam_0-1703759339195.png

 

0 Likes
Message 10 of 13

komondormrex
Mentor
Mentor

sure. check update.

Message 11 of 13

neam
Collaborator
Collaborator

it's greate

thank you very much komondormrex for your time to solve my problem.

🙏🙏

0 Likes
Message 12 of 13

komondormrex
Mentor
Mentor

your welcome

Message 13 of 13

smallƑish
Advocate
Advocate
Very useful!!