Subtract selected two text/mtext and show results in command line

Subtract selected two text/mtext and show results in command line

izzatiadrina
Explorer Explorer
776 Views
8 Replies
Message 1 of 9

Subtract selected two text/mtext and show results in command line

izzatiadrina
Explorer
Explorer

Hi guys, I did a bit of searching and chat GPTting. But still cant find the solution for this problem of mine. All I want is, I selected two text/mtext (inside contains few word the numbers) example of mtext >(BOP EL. +45702), then subtract/minus it, results in command line.

Thanks in advance guys. 

0 Likes
Accepted solutions (2)
777 Views
8 Replies
Replies (8)
Message 2 of 9

Sea-Haven
Mentor
Mentor

Try this, note mtext may give unpredictable results. 

 

(defun c:t1t2 ( / t1 t1num t2 t2num)
;;  Parses a list of numerical values from a supplied string. ;;
;;-------------------=={ Parse Numbers }==--------------------;;
;;                                                            ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  s - String to process                                     ;;
;;------------------------------------------------------------;;
;;  Returns:  List of numerical values found in string.       ;;
;;------------------------------------------------------------;;

(defun LM:ParseNumbers ( s )
  (
    (lambda ( l )
      (read
        (strcat "("
          (vl-list->string
            (mapcar
              (function
                (lambda ( a b c )
                  (if
                    (or
                      (< 47 b 58)
                      (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
                      (and (= 46 b) (< 47 a 58) (< 47 c 58))
                    )
                    b 32
                  )
                )
              )
              (cons nil l) l (append (cdr l) (list nil))
            )
          )
          ")"
        )
      )
    )
    (vl-string->list s)
  )
)

;;-------------------=={ UnFormat String }==------------------;;
;;                                                            ;;
;;  Returns a string with all MText formatting codes removed. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  str - String to Process                                   ;;
;;  mtx - MText Flag (T if string is for use in MText)        ;;
;;------------------------------------------------------------;;
;;  Returns:  String with formatting codes removed            ;;
;;------------------------------------------------------------;;

(defun LM:UnFormat ( str mtx / _replace rx )
    (defun _replace ( new old str )
        (vlax-put-property rx 'pattern old)
        (vlax-invoke rx 'replace str new)
    )
    (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
        (progn
            (setq str
                (vl-catch-all-apply
                    (function
                        (lambda ( )
                            (vlax-put-property rx 'global     actrue)
                            (vlax-put-property rx 'multiline  actrue)
                            (vlax-put-property rx 'ignorecase acfalse) 
                            (foreach pair
                               '(
                                    ("\032"    . "\\\\\\\\")
                                    (" "       . "\\\\P|\\n|\\t")
                                    ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
                                    ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
                                    ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
                                    ("$1"      . "[\\\\]({)|{")
                                )
                                (setq str (_replace (car pair) (cdr pair) str))
                            )
                            (if mtx
                                (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
                                (_replace "\\"   "\032" str)
                            )
                        )
                    )
                )
            )
            (vlax-release-object rx)
            (if (null (vl-catch-all-error-p str))
                str
            )
        )
    )
)

(setq t1 (cdr (assoc 1 (entget (car (entsel "\nPick text 1 "))))))
(setq t2 (cdr (assoc 1 (entget (car (entsel "\nPick text 2 "))))))

(setq t1a (LM:UnFormat t1 nil))
(setq t2a (LM:UnFormat t2 nil))
(setq t1num (car (LM:ParseNumbers t1a)))
(setq t2num (car (LM:ParseNumbers t2a)))
(alert (strcat t1 " - " t2 " = " (rtos (- t1num t2num) 2 3)))
(princ)
)
(c:t1t2)

 

0 Likes
Message 3 of 9

izzatiadrina
Explorer
Explorer

izzatiadrina_1-1685774814013.png

Thanks for the reply. But it shows 0.000. I think it is because the text inside it? Can filter the text out? 

0 Likes
Message 4 of 9

Sea-Haven
Mentor
Mentor

I did say will not work maybe with mtext, you need an extra step converting the string to plain text with no extra Mtext control in the text. 

 

Your lucky had some time code updated, strip mtext added.

Message 5 of 9

Kent1Cooper
Consultant
Consultant

@izzatiadrina wrote:

.... example of mtext >(BOP EL. +45702).... 


Do they always have the + just before the number?  Or maybe - instead?  If so, it should not be difficult to get the part you want out of them, regardless of interior formatting code stuff.

Kent Cooper, AIA
0 Likes
Message 6 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

I've discovered a truly-remarkably-convenient factoid for this situation:

   (getpropertyvalue MtextEntityName "text")

returns the plain text string content of Mtext minus any and all internal formatting!  No need to go through StripMtext or other sophisticated routine!

 

If it is valid to assume that the numerical part you want from your Text/Mtext always has a + sign just before it and there is no other + sign before that, and is always the last part with the exception of a possible concluding ), and is always an integer value, then this [in simplest terms] works for me, with plain Text and/or Mtext, with or without concluding ), and if the Mtext has internal formatting including inside the numerical part:

(defun C:INTDELTA (/ tstr +num T1 T2)
  (defun tstr (te); text string from text/mtext entity [unformatted if Mtext]
    (getpropertyvalue te (if (= (cdr (assoc 0 (entget te))) "TEXT") "textstring" "text"))
  )
  (defun +num (str); number part following +
    (atoi (vl-string-right-trim ")" (substr str (+ 2 (vl-string-position 43 str)))))
  )
  (setq
    T1 (car (entsel "\nSelect Text/Mtext item 1: "))
    T2 (car (entsel "\nSelect Text/Mtext item 2: "))
    delta (- (+num (tstr T1)) (+num (tstr T2)))
  ); setq
)

It can be expanded to account for a possible - instead of the +, or [a little more complicated] neither.  It could take the absolute value if the result is negative, if you want the raw difference and it doesn't matter in which order you pick.  It could be made to allow for non-integer values.

 

It leaves the difference in the 'delta' variable to do something else with if you want.  It just returns the difference as a number at the command line, but could put that into a prompt or an alert instead.

 

It could get the usual enhancements, such as verifying that you selected the right kinds of objects, etc.  But see whether, under my assumptions and in this basic form, it reports the difference correctly under your actual conditions.

Kent Cooper, AIA
Message 7 of 9

calderg1000
Mentor
Mentor
Accepted solution

Regards @izzatiadrina 

Try this simple code. Previously you must remove the format of the Mtext.
Watch video...

(defun c:Rtx( / sx1 sx2 tx1 tx2 np nt1 nt2)
  (setq sx1 (entget(car(entsel "\nSelect Mtext1: ")))
        sx2 (entget(car(entsel "\nSelect Mtext1: ")))
        tx1(cdr(assoc 1 sx1))
        tx2(cdr(assoc 1 sx2))
        )
  (setq np (vl-string-search "+" tx1)
        nt1 (substr tx1 (+ 2 np))
        nt2 (substr tx2 (+ 2 np))
        )
  (print (-(atof nt1)(atof nt2)))
  (princ)
  )

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 8 of 9

izzatiadrina
Explorer
Explorer

Always show + sign. Only numbers that change.

0 Likes
Message 9 of 9

komondormrex
Mentor
Mentor

@Kent1Cooper wrote:

returns the plain text string content of Mtext minus any and all internal formatting!  No need to go through StripMtext or other sophisticated routine!


not all. subscript or superscript for instance leaves a preceding or following slash.

0 Likes