Jump to content

Auto lisp for AutoCAD LT 2026 - to convert numerical values in text or Mtext in MM to Meters


Recommended Posts

Posted

Name: - xpapillon

I am a CAD & Auto lisp user but don't  know lisp programming.

India Standard Time

Time zone (GMT+5:30)

Saturday, 19 April 2025, 10:41 am

 

I was in need of a lisp program & tried to get it from Ai chatbots like Gemini etc.

My description of required lisp was as shown below:-

 

I need an Auto lisp program with following requirements: -

1.       It should run in AutoCAD LT 2026

2.       It should select & work on multiple text strings at the same time; both simple text & Mtext.

3.       It should convert all text in digits (without decimal mark in it) to Meters (up to two decimal points with trailing zeros).

For Example:

4059 +45899 * 0.452 828MM 25.27 M

3.06  +45.90 * 0.45 0.83 M 25.27 M

4.       It should keep all spaces & characters except numerical.

5.       It must not contain VB code, as AutoCAD LT does not run VB.

 

I observed that Ai chatbot can solve the problem at 50 to 60%. But it can never resolve the problem 100% with all the required parameters.

Thanks in advance...

 

convertMMtoM.lsp

Posted

These don't make any sense to me ?

 

4059 +45899 * 0.452 828MM 25.27 M

3.06  +45.90 * 0.45 0.83 M 25.27 M

 

Explain more what it is you want do 1 line per step.

 

  • Like 1
Posted

Thanks for reply.

Problem details:

We receive plans from Architects with room dimensions written in MM.

Although after our request; they are sending in Meters. But often we receive in MM.

When we give these plans for presentation in Corel Draw; we need to convert these text in Meters manually.

It is a tedious task. I am attaching a plan with this problem. The drawing units are Millimeters.

 

You can see why the following particular requirement arises from this drawing....

  1. All text in MM (room size) to be converted in Meters.
  2. Any signs, text other than numerical text to be kept as is.
  3. Text "MM" to be converted to "M". (Sometimes passages etc. are written 1200MM wide)
  4. Any text or Mtext in MM to be converted to M with digits up to two numbers after decimal point.
  5. If a figure in MM is already converted to Meters (with two numbers after decimal) & gets selected for conversion (by mistake), it should not get divided by 1000 again!!

Thank you!

PLAN.dwg

Posted

Give this a try. Please note your sample dwg has some problems, the text is all contained in 4 blocks I have made no attempt to fix that you have to explode 1st so all text & mtext. I stripped the mtext coding out not sure if that is a problem.

 

; https://www.cadtutor.net/forum/topic/97507-auto-lisp-for-autocad-lt-2026-to-convert-numerical-values-in-text-or-mtext-in-mm-to-meters/
; By AlanH April 2025

(defun c:m2mm ( / LM:UnFormat domm doxy csv->lst obj txt)

;;-------------------=={ 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
            )
        )
    )
)

(defun csv->lst (str ans / pos )
(if (setq pos (vl-string-position ans str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)) ans))
    (list str)
    )
)

(defun domm ( / vals newtxt)
  (setq vals (csv->lst txt 32))
  (setq newtxt (strcat
   (rtos (/(atof (car vals)) 1000.) 2 2)
   "mm "
   (caddr vals)
   )
  )
  (vlax-put obj 'textstring newtxt)
)

(defun doxy ( / vals newtxt)
  (setq vals (csv->lst txt 88))
  (setq newtxt (strcat
   (rtos (/(atof (car vals)) 1000.) 2 2)
   " X "
   (rtos (/(atof (cadr vals)) 1000.) 2 2)
   )
  )
  (vlax-put obj 'textstring newtxt)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;; starts here ;;;;;;;;;;;;;;;;;

(vl-load-com)

(setq ss (ssget '((0 . "*text"))))

(if (= ss nil)(progn (Alert "no suitable objects chosen will exit now ")(exit)))

(repeat (setq x (sslength ss))
  (setq obj (vlax-ename->vla-object (ssname ss (setq x (1- x)))))
  (setq txt (strcase (LM:UnFormat (vlax-get obj 'textstring) T)))
  (cond
  ((wcmatch txt "*'*")(princ "feet"))
  ((wcmatch txt "* X *")(doxy))
  ((wcmatch txt "*MM*")(domm))
  (princ (strcat "\nskipped " txt))
  )
)

(princ)
)

 

 

Posted
3 hours ago, BIGAL said:

Give this a try. Please note your sample dwg has some problems, the text is all contained in 4 blocks I have made no attempt to fix that you have to explode 1st so all text & mtext. I stripped the mtext coding out not sure if that is a problem.

 

; https://www.cadtutor.net/forum/topic/97507-auto-lisp-for-autocad-lt-2026-to-convert-numerical-values-in-text-or-mtext-in-mm-to-meters/
; By AlanH April 2025

(defun c:m2mm ( / LM:UnFormat domm doxy csv->lst obj txt)

;;-------------------=={ 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
            )
        )
    )
)

(defun csv->lst (str ans / pos )
(if (setq pos (vl-string-position ans str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)) ans))
    (list str)
    )
)

(defun domm ( / vals newtxt)
  (setq vals (csv->lst txt 32))
  (setq newtxt (strcat
   (rtos (/(atof (car vals)) 1000.) 2 2)
   "mm "
   (caddr vals)
   )
  )
  (vlax-put obj 'textstring newtxt)
)

(defun doxy ( / vals newtxt)
  (setq vals (csv->lst txt 88))
  (setq newtxt (strcat
   (rtos (/(atof (car vals)) 1000.) 2 2)
   " X "
   (rtos (/(atof (cadr vals)) 1000.) 2 2)
   )
  )
  (vlax-put obj 'textstring newtxt)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;; starts here ;;;;;;;;;;;;;;;;;

(vl-load-com)

(setq ss (ssget '((0 . "*text"))))

(if (= ss nil)(progn (Alert "no suitable objects chosen will exit now ")(exit)))

(repeat (setq x (sslength ss))
  (setq obj (vlax-ename->vla-object (ssname ss (setq x (1- x)))))
  (setq txt (strcase (LM:UnFormat (vlax-get obj 'textstring) T)))
  (cond
  ((wcmatch txt "*'*")(princ "feet"))
  ((wcmatch txt "* X *")(doxy))
  ((wcmatch txt "*MM*")(domm))
  (princ (strcat "\nskipped " txt))
  )
)

(princ)
)

 

 

 

That may not run in AutoCAD LT

Posted

Thank you!!

I tried the lisp...

I t is giving this error...

Command: M2MM

Select objects: 1 found

Select objects:  ; error: bad argument type: stringp nil

 

Note: I am using AutoCAD LT 2026......
 

Text in MM.dwg

Posted

Maybe it's because of this line of code 

(setq txt (strcase (LM:UnFormat (vlax-get obj 'textstring) T)))

;;;;it will only work if MTEXT type of entity is, it will not work if TEXT type of entity is because at the end you have "T"

 

  • 3 weeks later...
Posted
On 4/26/2025 at 9:06 AM, BIGAL said:

As it’s @lee-mack code he may want to comment.

Did you want to mention Mr. Lee Mac?

Mr. lee-mack seems to be a new member??

I am a big fan of Mr. Lee Mac's lisps....

LengthAreaFieldV1-4.lsp is my favorite & many more I had been using since last so many years...

any comments on editing Meters to MM lisp?

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