Jump to content

Set variable from text


carmelo

Recommended Posts

Hi all, 

how is it possible to set a variable by selecting a text?

I explain:

a and b are 2 variables;

a is a text who i select;

b=a/8.

At the end, i print b.

 

Thank you

Link to comment
Share on other sites

3 hours ago, Tharwat said:

Welcome to the forum. :)

Can you post your codes ( if you already wrote any ) regarding what you are trying to achieve?

Thanks ;) 

i'm sorry but i not have any code (i'm not able..) 🤨

Anyway, have I been clear with the explanation in the first post?

Link to comment
Share on other sites

Alright then, something like this?

(defun c:foo (/ a e)
  (if (and (setq a (car (entsel "\nSelect text : ")))
           (or (= (cdr (assoc 0 (setq e (entget a)))) "TEXT")
               (alert "Invlaid object. Try again")
           )
           (or (numberp (setq a (distof (cdr (assoc 1 e)))))
               (alert "Invalid contents of text <!>")
           )
      )
    (princ (/ a 8.0))
  )
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

Just expanding a bit more in case any more than / 8. Can just type on command line (foo number).

 

(defun foo ( div / a e)
  (if (and (setq a (car (entsel "\nSelect text : ")))
           (or (= (cdr (assoc 0 (setq e (entget a)))) "TEXT")
               (alert "Invlaid object. Try again")
           )
           (or (numberp (setq a (distof (cdr (assoc 1 e)))))
               (alert "Invalid contents of text <!>")
           )
      )
    (princ (/ a div))
  )
  (princ)
)
(defun c:foo8 ()
  (foo 8.0)
  (princ)
)
(defun c:foo4 ()
  (foo 4.0)
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

8 hours ago, BIGAL said:

Just expanding a bit more in case any more than / 8. Can just type on command line (foo number).

 

That expansion is redundant and not required but having one expression to get the value from the user with getreal getint getdist would be more than enough I believe.

Link to comment
Share on other sites

You're welcome anytime.

 

Yes it is possible unless you have your Mtext contents formatted then you would be in need of unformatting the contents in prior of retrieving the value.

But anyway, just replace the following expression:

(= (cdr (assoc 0 (setq e (entget a)))) "TEXT")

With this expression.

(wcmatch (cdr (assoc 0 (setq e (entget a)))) "TEXT,MTEXT")

 

  • Thanks 1
Link to comment
Share on other sites

5 minutes ago, carmelo said:

No, does not work with mtext.

Run the following codes on the Mtext that you tried the previous routine with and post a snapshot of the return message if you can.

(and (setq s (ssget "_+.:S:E" '((0 . "MTEXT"))))
     (alert (vl-princ-to-string (cdr (assoc 1 (entget (ssname s 0))))))
     )

 

Link to comment
Share on other sites

this is the code and below the message

(defun c:divvv (/ a e)
  (if (and (setq a (car (entsel "\nSelect text : ")))
           (or (wcmatch (cdr (assoc 0 (setq e (entget a)))) "TEXT,MTEXT")
		   
		   (and (setq s (ssget "_+.:S:E" '((0 . "MTEXT"))))
     (alert (vl-princ-to-string (cdr (assoc 1 (entget (ssname s 0))))))
     )

               (alert "Invalid object. Try again")
           )
           (or (numberp (setq a (distof (cdr (assoc 1 e)))))
               (alert "Invalid contents of text <!>")
           )
      )
    (princ (/ a 8.0))
  )
  (princ)
)

 

image.png.3d476a5a01ea8d1a854e8bd6e6926e5c.png

Link to comment
Share on other sites

6 minutes ago, Tharwat said:

Run the following codes on the Mtext that you tried the previous routine with and post a snapshot of the return message if you can.


(and (setq s (ssget "_+.:S:E" '((0 . "MTEXT"))))
     (alert (vl-princ-to-string (cdr (assoc 1 (entget (ssname s 0))))))
     )

 

 

Run the above codes separately without adding them to the previous routine.

Link to comment
Share on other sites

Sorry Tharwat, it work, and without your second code!

But, if i use an existing mtext, it does not work, and, if i use a new mtext, it work...

Link to comment
Share on other sites

The second codes are just for testing and to get sure of the return value of that Mtext.

Do you know what does formatted Mtext mean ?

Link to comment
Share on other sites

2 minutes ago, Tharwat said:

Do you know what does formatted Mtext mean ?

the same of text formatting in Word?
i.e., bold, justify...?!?

🤔

Link to comment
Share on other sites

Can you upload a sample of that drawing that you tested the routine on including the same Mtext that the codes failed with as you previously demonstrated?

Please save the drawing to AutoCAD 2016 or earlier for me to be able to open the drawing.

  • Like 1
Link to comment
Share on other sites

Thanks Ron. 

@carmelo Please try the following expanded codes to get the job done the way it should be.

(defun c:foo (/ a e)
  (if
    (and (setq a (car (entsel "\nSelect text : ")))
         (or (wcmatch (cdr (assoc 0 (setq e (entget a)))) "TEXT,MTEXT")
             (alert "Invlaid object. Try again")
         )
         (or (numberp
               (setq a (distof (Clear_Mtext_String (cdr (assoc 1 e)))))
             )
             (alert "Invalid contents of text <!>")
         )
    )
     (princ (/ a 8.0))
  )
  (princ)
)
;;				;;
(defun Clear_Mtext_String (String / Text Str)
  (setq Text "")
  (while (/= String "")
    (cond ((wcmatch (strcase (substr String 1 6)) "\\PXQC;")
           (setq String (substr String 7))
          )
          ((wcmatch (strcase (setq Str (substr String 1 2)))
                    "\\[\\{}`~]"
           )
           (setq String (substr String 3)
                 Text   (strcat Text Str)
           )
          )
          ((wcmatch (substr String 1 1) "[{}]")
           (setq String (substr String 2))
          )

          ((and (wcmatch (strcase (substr String 1 2)) "\\P")
                (/= (substr String 3 1) " ")
           )
           (setq String (substr String 3)
                 Text   (strcat Text " ")
           )
          )
          ((wcmatch (strcase (substr String 1 2)) "\\[LOP]")
           (setq String (substr String 3))
          )
          ((wcmatch (strcase (substr String 1 2)) "\\[ACFHQTW]")
           (setq String (substr String (+ 2 (vl-string-search ";" String))))
          )
          ((wcmatch (strcase (substr String 1 2)) "\\S")
           (setq Str    (substr String 3 (- (vl-string-search ";" String) 2))
                 Text   (strcat Text (vl-string-translate "#^\\" " " Str))
                 String (substr String (+ 4 (strlen Str)))
           )
           (print Str)
          )
          (T
           (setq Text   (strcat Text (substr String 1 1))
                 String (substr String 2)
           )
          )
    )
  )
  Text
)
(vl-load-com)

 

  • Thanks 1
Link to comment
Share on other sites

3 minutes ago, Tharwat said:

Thanks Ron. 

@carmelo Please try the following expanded codes to get the job done the way it should be.


(defun c:foo (/ a e)
  (if
    (and (setq a (car (entsel "\nSelect text : ")))
         (or (wcmatch (cdr (assoc 0 (setq e (entget a)))) "TEXT,MTEXT")
             (alert "Invlaid object. Try again")
         )
         (or (numberp
               (setq a (distof (Clear_Mtext_String (cdr (assoc 1 e)))))
             )
             (alert "Invalid contents of text <!>")
         )
    )
     (princ (/ a 8.0))
  )
  (princ)
)
;;				;;
(defun Clear_Mtext_String (String / Text Str)
  (setq Text "")
  (while (/= String "")
    (cond ((wcmatch (strcase (substr String 1 6)) "\\PXQC;")
           (setq String (substr String 7))
          )
          ((wcmatch (strcase (setq Str (substr String 1 2)))
                    "\\[\\{}`~]"
           )
           (setq String (substr String 3)
                 Text   (strcat Text Str)
           )
          )
          ((wcmatch (substr String 1 1) "[{}]")
           (setq String (substr String 2))
          )

          ((and (wcmatch (strcase (substr String 1 2)) "\\P")
                (/= (substr String 3 1) " ")
           )
           (setq String (substr String 3)
                 Text   (strcat Text " ")
           )
          )
          ((wcmatch (strcase (substr String 1 2)) "\\[LOP]")
           (setq String (substr String 3))
          )
          ((wcmatch (strcase (substr String 1 2)) "\\[ACFHQTW]")
           (setq String (substr String (+ 2 (vl-string-search ";" String))))
          )
          ((wcmatch (strcase (substr String 1 2)) "\\S")
           (setq Str    (substr String 3 (- (vl-string-search ";" String) 2))
                 Text   (strcat Text (vl-string-translate "#^\\" " " Str))
                 String (substr String (+ 4 (strlen Str)))
           )
           (print Str)
          )
          (T
           (setq Text   (strcat Text (substr String 1 1))
                 String (substr String 2)
           )
          )
    )
  )
  Text
)
(vl-load-com)

 

Now is perfect!

wow...you didn't stop until you totally solved the problem! You are a great men 🏆

Thank you very much 

  • Thanks 1
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...