Jump to content

UnFormat.lsp does not work in Autocad 2021


Recommended Posts

Posted

This code does not work in Autocad 2021 (localized version), the selected MText  is being deleted. 
How can this be fixed?
 

; UnFormat MText, MLeader, Table - strip formatting contol codes from texts
;
; based on Lee Mac's UnFormat string - www.lee-mac.com/unformatstring.html
; CAD Studio, 2018, www.cadstudio.cz  www.cadforum.cz
;
;
(vl-load-com)

;;-------------------=={ 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 doUnformatTable (table / rowCounter colCounter)
 (setq rowCounter (vla-Get-Rows table))
 (repeat rowCounter
 (setq rowCounter (1- rowCounter))
 (setq colCounter (vla-Get-Columns table))
 (repeat colCounter
  (setq colCounter (1- colCounter))
  (setq cellType (vla-GetCellType table rowCounter colCounter))
  (if (= cellType acTextCell)(progn
         (setq cellText (vla-GetText table rowCounter colCounter))
         (if (/= cellText "")
          (vla-SetText table rowCounter colCounter (LM:UnFormat cellText T)))
  ))
 ) ; rep
 ) ; rep
)

(defun c:UNFORMAT (/ ss ssl cnt en xxobj otyp txr ntx)
 (princ "\nSelect MTEXTs/DIMENSIONs/MLEADERs/TABLEs: ")
 (setq ss (ssget '((0 . "MTEXT,DIMENSION,MULTILEADER,ACAD_TABLE"))))
 (if (and ss (> (setq ssl (sslength ss)) 0))(progn
  (setq cnt 0)
  (repeat ssl
    (setq en (ssname ss cnt))
	(setq obj (vlax-ename->vla-object en))
	(setq otyp (cdr (assoc 0 (entget en)))) ; (vla-get-objectname obj))
	(cond
	 ((= otyp "MTEXT")(vla-put-TextString obj (LM:UnFormat (vla-get-TextString obj) T))) ; AcDbMText
	 ((= otyp "DIMENSION")(vla-put-TextOverride obj (LM:UnFormat (vla-get-TextOverride obj) T))) ; AcDbRotatedDimension, AcDbAlignedDimension
	 ((= otyp "MULTILEADER")(vla-put-TextString obj(LM:UnFormat (vla-get-TextString obj) T))) ; AcDbMLeader
	 ((= otyp "ACAD_TABLE")(doUnformatTable obj)) ; AcDbTable
	)
	(setq cnt (1+ cnt))
  ) ;rep
 )
 (princ "\nNothing selected!")
 ) ;if
 (princ)
)

(princ "\nUNFORMAT loaded.")
(princ)

 

Posted (edited)

 

9 hours ago, ronjonp said:

You should post a sample drawing.

In Autocad 2015, the code worked correctly, in Autocad 2021, 
texts containing control codes are deleted after selection...

 

Unformat not work in Autocad 2021.dwg

Edited by Nikon
Posted (edited)

It was working OK for me in your sample drawing - I am using 2022 though but would expect any changes in 2021 to be carried through to that.

 

 

Looking at the LISP, there is nothing in there to delete any texts, try loading it last in case there is another LISP loaded with the same name as some of the sub routines and try again maybe

Edited by Steven P
  • Thanks 1
Posted

Works in AutoCAD 2026.

 

Try restarting AutoCAD, then a reset to defaults if that doesn't work.

  • Thanks 1
Posted

If you haven't solved it yet, there's another possibility:
Replace '(setq en (ssname ss cnt))' with '(setq en (ssname ss cnt)) ex en)'
Load the code and run it again.
Then, type '(entget ex)' on the command line.
If it returns the entity list, see if '(60 . 1)' appears anywhere.

  • Thanks 1
Posted
2 hours ago, GLAVCVS said:

If you haven't solved it yet, there's another possibility:
Replace '(setq en (ssname ss cnt))' with '(setq en (ssname ss cnt)) ex en)'
Load the code and run it again.
Then, type '(entget ex)' on the command line.
If it returns the entity list, see if '(60 . 1)'.

texts containing control codes are deleted after selection...

Reaction to the Command: (entget ex)
; error: invalid argument type: lentityp nil

Posted

I agree with StevenP.

I've tested Lee Mac's code in the 2021 version, and it works fine in your drawing.

It's probably some interference between your loaded Lisp code.

  • Thanks 1
Posted (edited)
55 minutes ago, GLAVCVS said:

I agree with StevenP.

I've tested Lee Mac's code in the 2021 version, and it works fine in your drawing.

It's probably some interference between your loaded Lisp code.

Is there a way to find this intervention?

I have two more codes with this function that don't work...

Edited by Nikon
Posted (edited)

Try this as an experiment:

 

(Defun C:LMUnF ( / )
  (Load " -NIKON LISP FILEPATH- \\LM-Unformat.lsp") (c:UNFORMAT)(princ)
)

 

Where -Nikon Lisp Filepath is where the file is saved, LM-Unformat.lsp is the file name.

 

It should load this file when you run 'LMUnf' - and since it is the latest loaded will take precedence over anything loaded previously, 

 

 

 

Second option is to modify the text using entmod perhaps

 

Edited by Steven P
  • Thanks 1
Posted (edited)
1 hour ago, Steven P said:

It should load this file when you run 'LMUnf' - and since it is the latest loaded will take precedence over anything loaded previously, 

Thank you all for your advice. 
I've tried all the options. It doesn't work…

There is no file UNFORMAT in the startup, I open a new file, download lisp UNFORMAT and nothing changes...

Mysticism?!?

UNFORMAT.gif

Edited by Nikon
Posted
36 minutes ago, Nikon said:

There is no file UNFORMAT in the startup,

 

 

You'll need to apply some thought there, use the filename and location of where you have saved the LSP file containing the unformat lisps you posted above.

Posted
4 hours ago, Nikon said:

Is there a way to find this intervention?

 

Yes

This, for Eixample

;*******************  p o r d e s í a r g o  ********************
;************************ G L A V C V S *************************
;************************** F E C I T ***************************
(defun c:buscaDefunsRepets (/ lstLsps arch  nmarch linea lst as nbref separa<->palabras lstDefuns lstRepets tit dale path escrutArch)
  
  (defun separa<->palabras (tx lstCtrs / c p l)
    (foreach c (vl-string->list tx)
      (if (member (setq c (chr c)) lstCtrs)
        (if p (setq l (cons p l) p nil))
        (setq p (if p (strcat p c) c))
      )
    )
    (reverse (if p (cons p l) l))
  )

  (defun escrutArch (nmarch / arch linea lst as nbref)
    (if (and (setq nmarch (findfile nmarch)) (setq arch (open nmarch "r")))
      (while (setq linea (read-line arch))
	(cond
	  ((wcmatch linea "*(defun *")
	      (setq lst (separa<->palabras linea ;'(" " "("))
		    '(" " "(" "\""))
		    as (assoc (setq nbref (cadr lst)) lstDefuns)
	      )
	      (if lstDefuns
	        (if (not (vl-some ; comprobamos cada defun acumulada
			   '(lambda (v)
			      (if (and (= (car v) nbref) (/= nbref "defun")); si la defun recien leída coincide con alguna de las encontradas anteriormente
			        (if (setq lr (assoc nbref lstRepets))
				  (setq lstRepets (subst (append lr (list nmarch)) lr lstRepets))
				  (setq lstRepets (append lstRepets (list (append v (list nmarch)))))
			        )
		  	      )
			    )
			   lstDefuns
	       	         )
		    )
		  (setq lstDefuns (append lstDefuns (list (list nbref nmarch))))
	        )
	        (setq lstDefuns (append lstDefuns (list (list nbref nmarch))))
	      )
	    )
	)
      )
    )
    (if arch (close arch))
  )
  
  (setq lstLsps (list "acad2021.lsp" "acad2021doc.lsp") extens '("lsp"))
  (foreach lsp lstLsps
    (if (and (findfile lsp) (setq arch (open (setq nmarch (findfile lsp)) "r")))
      (progn
	(while (setq linea (read-line arch))
	  (cond
	    ((wcmatch linea "*(defun *")
	      (setq lst (separa<->palabras linea '(" " "("))
		    as (assoc (setq nbref (cadr lst)) lstDefuns)
	      )
	      (if lstDefuns
	        (if (not (vl-some ; comprobamos cada defun acumulada
			   '(lambda (v)
			      (if (and (= (car v) nbref) (/= nbref "defun")); si la defun recien leída coincide con alguna de las encontradas anteriormente
			        (if (setq lr (assoc nbref lstRepets))
				  (setq lstRepets (subst (append lr (list nmarch)) lr lstRepets))
				  (setq lstRepets (append lstRepets (list (append v (list nmarch)))))
			        )
		  	      )
			    )
			   lstDefuns
	       	         )
		    )
		  (setq lstDefuns (append lstDefuns (list (list nbref nmarch))))
	        )
	        (setq lstDefuns (append lstDefuns (list (list nbref nmarch))))
	      )
	    )
	    ((wcmatch linea "*(load *\")*")
	     (setq lst (separa<->palabras linea '(" " "(" "\""));'(" " "("))
		   as (assoc (setq nbref (cadr lst)) lstDefuns)
	     )
	     (foreach v lst
	       (if dale
		 (setq dale
			(if (and
			      (not (member (strcase v T) (list "acad2021.lsp" "acad2021doc.lsp")))
			      (or 
			        (= (length (setq lst1 (separa<->palabras v '(".")))) 1)
				(member (strcase (cadr lst1) T) extens)
			      )
			    )
			  (escrutArch v)
			)
		       dale nil
		 )
	         (if (= (strcase v T) "load")
		   (setq dale T)
		 )
	       )
	       (if nmArch1 (escrutArch nmArch1))
	     )
	    )
	  )
	)
      )
    )
  )
	    
  (if arch (close arch))
		      
  (if lstRepets
    (if (setq arch (open (setq nmarch (strcat (VL-REGISTRY-READ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" "Personal") "\\informe.txt")) "w"))
      (foreach lr lstRepets
	(princ (strcat (if (not tit) (setq tit "FUNCTIONS DEFINED MULTIPLE TIMES:\n") "") "\n    Function NAME \'" (car lr) "\' in:\n") arch)
	(foreach path (cdr lr)
	  (princ (strcat "\t" path "\n") arch)
	)
      )
    )
  )
  (if arch (progn (close arch) (startapp "notepad" nmarch)))
  (princ)
)

 

Try it

Posted (edited)

PS:

The command will analyze the code in the files in the 'lstLsps' list and any files loaded from them. However, it will not analyze any files loaded from the latter. That is, it will not analyze code loaded beyond the second level of nesting.

But I think that should be enough.

Edited by GLAVCVS
Posted (edited)

@GLAVCVS thank you very much!  

I run the command buscaDefunsRepets  and I get this message:

 

FUNCTIONS DEFINED MULTIPLE TIMES:

    Function NAME '*merr*' in:
    C:\Users\Nikon\AppData\Roaming\Autodesk\AutoCAD 2021\R24.0\rus\support\acad2021doc.lsp
    C:\Users\Nikon\AppData\Roaming\Autodesk\AutoCAD 2021\R24.0\rus\support\acad2021doc.lsp

    Function NAME '*merrmsg*' in:
    C:\Users\Nikon\AppData\Roaming\Autodesk\AutoCAD 2021\R24.0\rus\support\acad2021doc.lsp
    C:\Users\artem\Nikon\AppData\Roaming\Autodesk\AutoCAD 2021\R24.0\rus\support\acad2021doc.lsp

 

What do I need to do next?

 

acad2021doc.lsp

...........

(defun *merr* (msg)
  (setq *error* m:err m:err nil)
  (princ)
)

(defun *merrmsg* (msg)
  (princ msg)
  (setq *error* m:err m:err nil)
  (princ)
)

...................

Edited by Nikon

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