Jump to content

replace + symbol to change line (enter) in mtext


Recommended Posts

Posted

Hi I have some calculations in mtext I want to sepetate them in the same mtext like this

 

This is Mtext

1.61 x 1.35 + 1.11 x 4.28 + 2.89 x 2.63

 

This is Mtext

1.61 x 1.35
1.11 x 4.28
2.89 x 2.63

 

I dont know if it possible to replace a chatacter  with enter (change line). In my case  i want to replace  " + "    <-- space + space

 

 

Thanks

Posted (edited)

Command RSTNL for Replace Symbol To New Line

 

;; http://www.lee-mac.com/stringsubst.html
;;--------------------=={ String Subst }==--------------------;;
;;                                                            ;;
;;  Substitutes a string for all occurrences of another       ;;
;;  string within a string.                                   ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  new - string to be substituted for 'old'                  ;;
;;  old - string to be replaced                               ;;
;;  str - the string to be searched                           ;;
;;------------------------------------------------------------;;
;;  Returns:  String with 'old' replaced with 'new'           ;;
;;------------------------------------------------------------;;
(defun LM:StringSubst ( new old str / inc len )
    (setq len (strlen new)
          inc 0
    )
    (while (setq inc (vl-string-search old str inc))
        (setq str (vl-string-subst new old str inc)
              inc (+ inc len)
        )
    )
    str
)


;; RSTNL for Replace Symbol To New Line
(defun c:RSTNL	( / mtext symbl oldtext newtext)
	;; symbol to be used as new line delimiter.
	;; you could also let the user pick it like this:
	;;	(setq symbl (getstring "\nEnter Symbol: " T))  ;; The T allows the user to add space characters
	(setq symbl " + ")
	(princ "\nNow pick the MText elements: ")
	(while	(setq mtext (car (entsel "\nSelect Mtext: ")))
		(setq oldtext (cdr (assoc 1 (entget mtext))))
		;; substitute the symbol to "\\P".   "\\P" means new line for MTexts
		(setq newtext (LM:StringSubst  "\\P" symbl oldtext))
		;; entmod (modify entity) by substitute of the assoc/cont 1 group 
		(entmod (subst (cons 1 newtext) 	(assoc 1 (entget mtext)) 	(entget mtext) ))  	;; substitute text contents
	)
)

 

Edited by Emmanuel Delay
typo
  • Like 1
Posted
(vl-string-translate " x " " + " "1.61 x 1.35 + 1.11 x 4.28 + 2.89 x 2.63")
;; "1.61 + 1.35 + 1.11 + 4.28 + 2.89 + 2.63"

 

  • Like 1
Posted

hI ronjonp. Your code convert  x to + .  Emmanuel Delay  code convert + to change line.

 

Thanks for your time !!!

 

 

Posted (edited)

I think he wants the + to be a new line Ronjonp

 

the replace I inputted was " + " or you wouldn't line up if it was just "+"

 

(defun C:NEWLINE (/ replace txt text)
  (setq replace (getstring "\nReplace Text: " T))
  (setq txt (vlax-ename->vla-object (car (entsel "\n Select Text: "))))
  (while (vl-string-search replace (setq text (vla-get-textstring txt))) ;search string for replace value
    (vla-put-textstring obj (vl-string-subst "\\P" replace (vla-get-textstring txt)))
  )
)

 

Edited by mhupp
  • Like 1
Posted

Hi mhupp. Nice code. I want to ask something else. I have this

 

1.35 x 2.94 + 6.16 x 4.19 + 2.94 x 1.25 + 1/2 x (1.84 + 2.08) x 3.74

 

When i run the code i get this results

 

1.35 x 2.94
6.16 x 4.19
2.94 x 1.25
1/2 x (1.84
2.08) x 3.74

 

The correct is

 

1.35 x 2.94
6.16 x 4.19
2.94 x 1.25
1/2 x (1.84 + 2.08) x 3.74

 

Is any way to search if the  + is between  (number space + space number)  and don't replace it ?

 

(defun C:N2 (/ replace txt obj text)
  (setq replace " + ")
  (setq txt (car (entsel "\n Select Text: ")))
  (setq obj (vlax-ename->vla-object txt))
  (while (vl-string-search replace (setq text (vla-get-textstring obj))) ;search string for replace value
    (vla-put-textstring obj (vl-string-subst "\\P" replace (vla-get-textstring obj)))
  )
)

 

Posted (edited)

I don't know about that one. Maybe ask if you want to Continue?  But you would have to hit enter or right click each time.

so maybe NLAdv ?

 

NLADV
Replace Text:  +
 Select Text:
Continue? [<Yes>/No]:
Continue? [<Yes>/No]:
Continue? [<Yes>/No]: n

 

(defun C:NLAdv (/ replace txt obj text)
  (setq replace (getstring "\nReplace Text: " T))
  (setq txt (vlax-ename->vla-object (car (entsel "\n Select Text: "))))
  (setq rep "Yes")
  (while (eq rep "Yes")
    (vla-put-textstring txt (vl-string-subst "\\P" replace (vla-get-textstring txt)))
    (if (vl-string-search replace (setq text (vla-get-textstring txt)))
      (progn
        (initget "Yes No")
        (setq rep
          (cond
            ((getkword "\nContinue? <Yes>/No: ")) ( "Yes")
          )
        )
      )
      (setq rep "No")
    )
  )
    (princ)
)

 

--EDIT

Prob just easier to make an undo mark after each text string update. Then you can undo the last change.

 

(defun C:NL (/ replace txt text)
  	(vl-load-com)
  (setq Drawing (vla-get-activedocument (vlax-get-acad-object)))
  (setq replace (getstring "\nReplace Text: " T))
  (setq txt (vlax-ename->vla-object (car (entsel "\n Select Text: "))))
  (while (vl-string-search replace (setq text (vla-get-textstring txt))) ;search string for replace value
    (vla-startundomark Drawing)
    (vla-put-textstring txt (vl-string-subst "\\P" replace (vla-get-textstring txt)))
    (vla-endundomark Drawing)    
  )
)

 

NL

 

1.35 x 2.94
6.16 x 4.19
2.94 x 1.25
1/2 x (1.84 
2.08) x 3.74

 

undo or ctrl Z

 

1.35 x 2.94
6.16 x 4.19
2.94 x 1.25
1/2 x (1.84 + 2.08) x 3.74

 

Edited by mhupp
  • Like 1
Posted (edited)

Just off the top of my head, no proper thinking done yet.

 

In the case of 1/2 x (1.84 + 2.88) x 3.74.

 

I have used Lee Macs string to list before doing a similar text replace, the deliminator in this case being the '+', then recombining it with a strcat and whatever I want to put in between the list items.

 

Would give a list ( ("2.94 x 1.25") ("1/2 x (1.84") ("2.08 ) x 3.74")

 

Then do an if and wcmatch, to decide if the + should be replaced by a new line or left as a +

 

 

Just thinking out loud

Edited by Steven P
  • Like 1
Posted

Maybe this ... it produces a tighter result without the spaces:

(apply
  'strcat
  (mapcar '(lambda (x)
	     (cond ((and (= 43 x) (null f)) "\\P")
		   ((= 40 x) (setq f t) (chr x))
		   ((= 41 x) (setq f nil) (chr x))
		   ((= 32 x) "")
		   ((chr x))
	     )
	   )
	  (vl-string->list "1.35 x 2.94 + 6.16 x 4.19 + 2.94 x 1.25 + 1/2 x (1.84 + 2.08) x 3.74")
  )
)

image.png.bd27ed4ccbeea9e4bb9b2c231f68a959.png

  • Like 2
Posted (edited)

So this should work on the examples above:

 

(defun c:newline ( / MyEnt oldtext textlist acount newtext temptext)
  (defun LM:str->lst ( str del / pos ) ;;http://lee-mac.com/stringtolist.html
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
      (list str)
    )
  )

  (setq MyEnt (ssname (ssget "_+.:E:S" '((0 . "MTEXT"))) 0)) ;; Select only single mtext
  (setq oldtext (cdr (assoc 1 (entget MyEnt)))) ;; get text string (up to 256 characters)
  (setq textlist (LM:str->lst oldtext "+"))     ;; make list, remove all '+'
  (setq acount 0)   ;; just a counter
  (setq newtext "") ;; blank text string
  (while (< acount (length textlist)) ;; while loop, looping through list of texts
    (setq temptext (vl-string-left-trim " " (vl-string-right-trim " " (nth acount textlist)) )) ;; trim spaces
    (setq newtext (strcat newtext temptext)) ;; create new text
    (setq acount (+ acount 1))
    (if (wcmatch (nth (- acount 1) textlist) "*(*" ) ;; if bracket, return a + else a new line
      (setq newtext (strcat newtext " + "))
      (setq newtext (strcat newtext "\\P"))
    ) ; end if
  ) ; end while

  (entmod (subst (cons 1 newtext) (assoc 1 (entget MyEnt)) (entget MyEnt) )) ;; Uodate text
  (princ)
)

 

Edited by Steven P
  • Like 1

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