Author Topic: Text to Mtext AutoCAD 2015  (Read 15110 times)

0 Members and 1 Guest are viewing this topic.

Andrew

  • Guest
Text to Mtext AutoCAD 2015
« on: April 14, 2014, 02:43:45 AM »
I have a great text to mtext lisp that I downloaded years ago, I'm not sure where I found it so sorry for not giving credit.

It worked fine in autocad 2014, but in autocad 2015 it gives this error when I try and run it after selecting multiple texts to convert. Does anyone know what needs to be changed to make it work in 2015?

Quote
Select objects: Specify opposite corner: 54 found

Select objects:
VVC Internal Error

Code: [Select]
(defun c:t2mt (/ ss lst usercmd)
  ;;  CAB Text to Mtext conversion
  (defun T2M (code)
    ;; code =  dText Alignment code
    (cdr
      (assoc code
      '(( 0 ;|acAlignmentLeft        |; . acAttachmentPointBottomLeft)
               ( 1 ;|acAlignmentCenter      |; . acAttachmentPointBottomCenter)
               ( 2 ;|acAlignmentRight       |; . acAttachmentPointBottomRight)
               ( 3 ;|acAlignmentAligned     |; . acAttachmentPointBottomLeft)
               ( 4 ;|acAlignmentMiddle      |; . acAttachmentPointMiddleCenter)
               ( 5 ;|acAlignmentFit         |; . acAttachmentPointBottomLeft)
               ( 6 ;|acAlignmentTopLeft     |; . acAttachmentPointTopLeft)
               ( 7 ;|acAlignmentTopCenter   |; . acAttachmentPointTopCenter)
               ( 8 ;|acAlignmentTopRight    |; . acAttachmentPointTopRight)
               ( 9 ;|acAlignmentMiddleLeft  |; . acAttachmentPointMiddleLeft)
               (10 ;|acAlignmentMiddleCenter|; . acAttachmentPointMiddleCenter)
               (11 ;|acAlignmentMiddleRight |; . acAttachmentPointMiddleRight)
               (12 ;|acAlignmentBottomLeft  |; . acAttachmentPointBottomLeft)
               (13 ;|acAlignmentBottomCenter|; . acAttachmentPointBottomCenter)
               (14 ;|acAlignmentBottomRight |; . acAttachmentPointBottomRight))
     ))
  )
  (command "_Undo" "_end")
  (command "_Undo" "_begin")
  (setq ss (ssget '((0 . "TEXT")))) ; get only plain text
  (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (mapcar
    '(lambda (x)
       (setq obj (vlax-ename->vla-object x)
             ali (vla-get-alignment obj)
             ipt (vlax-get obj 'InsertionPoint)
             apt (vlax-get obj 'Textalignmentpoint)
       )
       (command "txt2mtxt" x "")
       (setq new (vlax-ename->vla-object(entlast)))
       (vla-put-AttachmentPoint new (eval (t2m ali)))
       (if (equal apt '(0.0 0.0 0.0))
         (vlax-put new 'InsertionPoint ipt)
         (vlax-put new 'InsertionPoint apt)
       )
       (vla-put-width new 3.5)
       
     )
    lst
  )
  (setvar "CMDECHO" usercmd)
  (command "_Undo" "_end")
  (princ)
)
(defun c:t2mtsetout (/ ss lst usercmd)
  ;;  CAB Text to Mtext conversion
  (defun T2M (code)
    ;; code =  dText Alignment code
    (cdr
      (assoc code
      '(( 0 ;|acAlignmentLeft        |; . acAttachmentPointBottomLeft)
               ( 1 ;|acAlignmentCenter      |; . acAttachmentPointBottomCenter)
               ( 2 ;|acAlignmentRight       |; . acAttachmentPointBottomRight)
               ( 3 ;|acAlignmentAligned     |; . acAttachmentPointBottomLeft)
               ( 4 ;|acAlignmentMiddle      |; . acAttachmentPointMiddleCenter)
               ( 5 ;|acAlignmentFit         |; . acAttachmentPointBottomLeft)
               ( 6 ;|acAlignmentTopLeft     |; . acAttachmentPointTopLeft)
               ( 7 ;|acAlignmentTopCenter   |; . acAttachmentPointTopCenter)
               ( 8 ;|acAlignmentTopRight    |; . acAttachmentPointTopRight)
               ( 9 ;|acAlignmentMiddleLeft  |; . acAttachmentPointMiddleLeft)
               (10 ;|acAlignmentMiddleCenter|; . acAttachmentPointMiddleCenter)
               (11 ;|acAlignmentMiddleRight |; . acAttachmentPointMiddleRight)
               (12 ;|acAlignmentBottomLeft  |; . acAttachmentPointBottomLeft)
               (13 ;|acAlignmentBottomCenter|; . acAttachmentPointBottomCenter)
               (14 ;|acAlignmentBottomRight |; . acAttachmentPointBottomRight))
     ))
  )
  (command "_Undo" "_end")
  (command "_Undo" "_begin")
  (setq ss (ssget '((0 . "TEXT")))) ; get only plain text
  (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
  (setq usercmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (mapcar
    '(lambda (x)
       (setq obj (vlax-ename->vla-object x)
             ali (vla-get-alignment obj)
             ipt (vlax-get obj 'InsertionPoint)
             apt (vlax-get obj 'Textalignmentpoint)
       )
       (command "txt2mtxt" x "")
       (setq new (vlax-ename->vla-object(entlast)))
       (vla-put-AttachmentPoint new (eval (t2m ali)))
       (if (equal apt '(0.0 0.0 0.0))
         (vlax-put new 'InsertionPoint ipt)
         (vlax-put new 'InsertionPoint apt)
       )
       (vla-put-width new 0)
       
     )
    lst
  )
  (setvar "CMDECHO" usercmd)
  (command "_Undo" "_end")
  (princ)
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Text to Mtext AutoCAD 2015
« Reply #1 on: April 14, 2014, 03:08:46 AM »
The code appears to have been written by CAB  http://www.theswamp.org/index.php?action=profile;u=30

He doesn't have AC2015 to test with.

Can you isolate the error to a specific line using the VLIDE Editor ?
« Last Edit: April 14, 2014, 06:50:15 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ChrisCarlson

  • Guest
Re: Text to Mtext AutoCAD 2015
« Reply #2 on: April 14, 2014, 07:55:46 AM »
I didn't dig too deep into this but what about using the inbuilt txt2mtxt command within AutoCAD?

Andrew

  • Guest
Re: Text to Mtext AutoCAD 2015
« Reply #3 on: April 14, 2014, 06:49:19 PM »
Thanks for the replies.

This lisp is a bit different to the inbuilt txt2mtxt command, it converts text to mtext individually and puts them in the same location whereas the inbuilt in one combines them. It's quite handy converting lots of individual txt to mtxt.

I tried using VLIDE but I'm not really sure what it's telling me.
If I run t2mt on one text only and do an error trace one of the items in VLIDE is this

Code: [Select]
(mapcar
    '(lambda (x)
       (setq obj (vlax-ename->vla-object x)
             ali (vla-get-alignment obj)
             ipt (vlax-get obj 'InsertionPoint)
             apt (vlax-get obj 'Textalignmentpoint)
       )
       (command "txt2mtxt" x "")
       (setq new (vlax-ename->vla-object(entlast)))
       (vla-put-AttachmentPoint new (eval (t2m ali)))
       (if (equal apt '(0.0 0.0 0.0))
         (vlax-put new 'InsertionPoint ipt)
         (vlax-put new 'InsertionPoint apt)
       )
       (vla-put-width new 3.5)
       
     )
    lst
  )
and it gives an error in the autocad text window
Code: [Select]
Select text objects, or [Options]<Options>: ; error: Exception occurred: 0xC0000005 (Access Violation)
; warning: unwind skipped on exception
; error: Exception occurred: 0xC0000005 (Access Violation)

If I use t2mt on multiple text items I can't seem to see where the error occurs in VLIDE. (This one gives the VVC Internal Error in the autocad text window)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Text to Mtext AutoCAD 2015
« Reply #4 on: April 27, 2014, 09:23:50 AM »
Hi,

Try replacing (command "txt2mtxt" x "") with (command-s "txt2mtxt" x "").
I had the same error (VVC: Internal Error) with A2015 in a LISP function which called vl-cmdf within a mapcar statement and replacing vl-cmdf with command-s solved it.
Speaking English as a French Frog

Andrew

  • Guest
Re: Text to Mtext AutoCAD 2015
« Reply #5 on: April 27, 2014, 08:20:16 PM »
Thanks gile. That got rid of the VVC: Internal error.

I've got another issue now. When a TEXT has alignment Left it moves the converted MTEXT to 0,0. It only seems to do this for TEXT with alignment left. If the alignment is anything else, right, centre, align etc it places it correctly in the same location.

I'm guessing it has something to do with this section. Any ideas?

Code: [Select]
      (assoc code
      '(( 0 ;|acAlignmentLeft        |; . acAttachmentPointBottomLeft)
               ( 1 ;|acAlignmentCenter      |; . acAttachmentPointBottomCenter)
               ( 2 ;|acAlignmentRight       |; . acAttachmentPointBottomRight)
               ( 3 ;|acAlignmentAligned     |; . acAttachmentPointBottomLeft)
               ( 4 ;|acAlignmentMiddle      |; . acAttachmentPointMiddleCenter)
               ( 5 ;|acAlignmentFit         |; . acAttachmentPointBottomLeft)
               ( 6 ;|acAlignmentTopLeft     |; . acAttachmentPointTopLeft)
               ( 7 ;|acAlignmentTopCenter   |; . acAttachmentPointTopCenter)
               ( 8 ;|acAlignmentTopRight    |; . acAttachmentPointTopRight)
               ( 9 ;|acAlignmentMiddleLeft  |; . acAttachmentPointMiddleLeft)
               (10 ;|acAlignmentMiddleCenter|; . acAttachmentPointMiddleCenter)
               (11 ;|acAlignmentMiddleRight |; . acAttachmentPointMiddleRight)
               (12 ;|acAlignmentBottomLeft  |; . acAttachmentPointBottomLeft)
               (13 ;|acAlignmentBottomCenter|; . acAttachmentPointBottomCenter)
               (14 ;|acAlignmentBottomRight |; . acAttachmentPointBottomRight))
     ))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Text to Mtext AutoCAD 2015
« Reply #6 on: April 27, 2014, 11:52:02 PM »
Hi,

Try replacing (command "txt2mtxt" x "") with (command-s "txt2mtxt" x "").
I had the same error (VVC: Internal Error) with A2015 in a LISP function which called vl-cmdf within a mapcar statement and replacing vl-cmdf with command-s solved it.

Thanks gile.
I was afraid of that with ac2015  :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

fixo

  • Guest
Re: Text to Mtext AutoCAD 2015
« Reply #7 on: April 28, 2014, 04:45:26 AM »
I'm guessing it has something to do with this section. Any ideas?
See the very last post by T.Willey in this thread :
https://groups.google.com/forum/#!topic/autodesk.autocad.customization/gGZBLTD3Qls

maicy

  • Mosquito
  • Posts: 12
Re: Text to Mtext AutoCAD 2015
« Reply #8 on: April 30, 2014, 03:07:58 AM »
in AutoCAD 2015,mapcar can't use command
(mapcar
    '(lambda (x)
      ......
       (command.....)
    ....
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Text to Mtext AutoCAD 2015
« Reply #9 on: May 02, 2014, 09:05:33 AM »
Hi,
Try replacing (command "txt2mtxt" x "") with (command-s "txt2mtxt" x "").
I had the same error (VVC: Internal Error) with A2015 in a LISP function which called vl-cmdf within a mapcar statement and replacing vl-cmdf with command-s solved it.

Thank you for reporting this type of problem and the solution, my question is on using "command" in this scenario:

vla-Open dwg file
(vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"LispFile\"\")\n\"")

Loading (and run inside itself) LispFile with sendcommand I can execute all I need (plot, export, etc.) and when I close the document the control go back to the starting document. I think it is a better method than scripts especially for errors check.

I thought I should abandon this method because it is very slow in 64-bit systems (due to the VBA 6/64), but in AutoCAD 2014 (with VBA 7) is back to being useful.

Now in 2015, with the elimination of "fibres", (I have read also your http://cadxp.com/topic/39448-compatibilite-autocad-2015/) I have several new problems:

Edit: see new thread: http://www.theswamp.org/index.php?topic=47005.0

andrew_nao

  • Guest
Re: Text to Mtext AutoCAD 2015
« Reply #10 on: May 05, 2014, 01:20:00 PM »

Thank you for reporting this type of problem and the solution, my question is on using "command" in this scenario:

vla-Open dwg file
(vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"LispFile\"\")\n\"")

...

Suggestions?

my suggestion is to drop that method and use this one by Lee Mac

Code: [Select]
;;  Author: Lee Mac,
(defun LM:Open ( target / shell result )
    (if
        (and
            (or
                (eq 'INT (type target))
                (setq target (findfile target))
            )
            (setq shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
        )
        (progn
            (setq result (vl-catch-all-apply 'vlax-invoke (list shell 'open target)))
            (vlax-release-object shell)
            (not (vl-catch-all-error-p result))
        )
    )
)
(vl-load-com)

Example to Open a File Selected by the User
(LM:Open (getfiled "Select File to Open" "" "" 16))

Example to Open a Drawing File at a Specified Location:
(LM:Open "C:\\My Folder\\File.dwg")

Example to Open a Directory:
(LM:Open "C:\\My Folder\\My SubFolder")

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Text to Mtext AutoCAD 2015
« Reply #11 on: May 05, 2014, 01:41:49 PM »
...
Example to Open a Drawing File at a Specified Location:
(LM:Open "C:\\My Folder\\File.dwg")
...
Ok but how can I execute commands in the openeded file and close it?

(progn
  (LM:Open "C:\\Temp\\pippo.dwg")
  (command "_line" "0,0" "10,10" "") 
  (command "_.CLOSE" "_Y")
)
...
Loading (and run inside itself) LispFile with sendcommand I can execute all I need (plot, export, etc.) and when I close the document the control go back to the starting document. I think it is a better method than scripts especially for errors check.
...

pedroantonio

  • Guest
Re: Text to Mtext AutoCAD 2015
« Reply #12 on: May 06, 2014, 06:54:38 PM »
I use this lisp.Try it !!

Code - Auto/Visual Lisp: [Select]
  1. ;;; AUTHOR
  2. ;;; Copyright© 2010 Ron Perez (ronperez@gmail.com)
  3. ;;; 11.02.2010 added grouping text by X values
  4. ;;; 11.05.2010 added grouping text by Y values if in same column
  5. ;;; 11.11.2010 added some conversions for underline and overline
  6. (defun c:t2mt (/          rjp-txtschtuff      rjp-ent2obj         rjp-getbbwdth       rjp-getbbtlc
  7.                 rjp-dxf   doc       fuz-p     fuz-x     fuz-y     hgt       i         obj
  8.                 otxt      pt        ss        tmp       txt       w         x         x-srt
  9.                 y         y-srt
  10.                )
  11.   ;;Added some conversion stuff per Joe's comment ... not gonna get too crazy about the formatting rabbit hole :P
  12.   (defun rjp-txtschtuff (txt)
  13.     ;;ExtraSpaces
  14.     (while (vl-string-search "  " txt) (setq txt (vl-string-subst " " "  " txt)))
  15.     ;;Underlines, Overlines
  16.     (foreach x '(("%%U" . "\\L") ("%%O" . "\\O"))
  17.       (cond ((vl-string-search (car x) txt)
  18.              (while (vl-string-search (car x) txt) (setq txt (vl-string-subst (cdr x) (car x) txt)))
  19.              (setq txt (strcat "{" txt "}"))
  20.             )
  21.       )
  22.     )
  23.     txt
  24.   )
  25.   (defun rjp-ent2obj (ent)
  26.     (if (= (type ent) 'ename)
  27.       (vlax-ename->vla-object ent)
  28.       ent
  29.     )
  30.   )
  31.   (defun rjp-dxf (code ent)
  32.     (if (and ent (= (type ent) 'ename))
  33.       (cdr (assoc code (entget ent)))
  34.     )
  35.   )
  36.   (defun rjp-getbb (ent / ll ur)
  37.     (vla-getboundingbox (rjp-ent2obj ent) 'll 'ur)
  38.     (mapcar 'vlax-safearray->list (list ll ur))
  39.   )
  40.   (defun rjp-getbbwdth (ent / out)
  41.     (setq out (mapcar 'car (rjp-getbb (rjp-ent2obj ent))))
  42.     (abs (- (car out) (cadr out)))
  43.   )
  44.   (defun rjp-getbbtlc (ent / out)
  45.     (setq out (rjp-getbb (rjp-ent2obj ent)))
  46.     (list (caar out) (cadr (last out)) 0.0)
  47.   )
  48.   ;;**Textheight multipliers**
  49.   ;;The max X grouping variance
  50.   (setq fuz-x 3)
  51.   ;;The max Y grouping variance
  52.   (setq fuz-y 4)
  53.   ;;Determine the max Y line spacing variance before making into paragraph
  54.   (setq fuz-p 2.5)
  55.   ;;**Textheight multipliers**
  56.   (if (and (setq ss (ssget ":L" (list '(0 . "text"))))
  57.            ;;list as X Y TEXT ENAME
  58.            (setq ss (mapcar '(lambda (x)
  59.                                (list (car (rjp-dxf 10 x))
  60.                                      (cadr (rjp-dxf 10 x))
  61.                                      (strcat (rjp-txtschtuff (rjp-dxf 1 x)) " ")
  62.                                      x
  63.                                )
  64.                              )
  65.                             (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  66.                     )
  67.            )
  68.       )
  69.     (progn ;;Sort top to bottom
  70.            (setq ss (vl-sort ss '(lambda (y1 y2) (> (cadr y1) (cadr y2)))))
  71.            ;;Group by x values using (*  fuz-x textheight)
  72.            (while (setq i (car ss))
  73.              (setq y (vl-remove-if-not
  74.                        '(lambda (x) (equal (car i) (car x) (* fuz-x (rjp-dxf 40 (last i)))))
  75.                        ss
  76.                      )
  77.              )
  78.              (mapcar '(lambda (x) (setq ss (vl-remove x ss))) y)
  79.              (setq x-srt (cons (mapcar 'cdr y) x-srt))
  80.            )
  81.            (foreach i x-srt
  82.              ;;Substract subsequent Y values starting at top
  83.              (setq y (mapcar '(lambda (x y) (cons (- (car x) (car y)) (cdr x)))
  84.                              i
  85.                              (append (cdr i) (list (last i)))
  86.                      )
  87.              )
  88.              ;;Group while Y distance is less than (* fuz-y toptextheight)
  89.              (while y
  90.                (setq tmp nil)
  91.                (setq tmp (cons (car y) tmp))
  92.                (setq hgt (rjp-dxf 40 (last (car y))))
  93.                (while (and (cdr y) (< (caar y) (* fuz-y hgt)))
  94.                  (setq y (cdr y))
  95.                  (setq tmp (cons (car y) tmp))
  96.                )
  97.                (setq y-srt (cons (reverse tmp) y-srt))
  98.                (setq y (cdr y))
  99.              )
  100.            )
  101.            (foreach item (reverse y-srt)
  102.              (setq txt  nil
  103.                    ;;Get widest piece of text to set mtext width
  104.                    w    (* 1.0125 (car (vl-sort (mapcar 'rjp-getbbwdth (mapcar 'caddr item)) '>)))
  105.                    hgt  (rjp-dxf 40 (last (car item)))
  106.                    pt   (rjp-getbbtlc (last (car item)))
  107.                    ;;Grab top text to pull properties from
  108.                    otxt (vlax-ename->vla-object (last (car item)))
  109.                    ;;Join the text into one string breaking into paragraphs if greater than (* fuz-p toptextheight)
  110.                    txt  (apply 'strcat
  111.                                (mapcar '(lambda (x)
  112.                                           (if (> (car x) (* fuz-p hgt))
  113.                                             (strcat (cadr x) "\\P\\P")
  114.                                             (cadr x)
  115.                                           )
  116.                                         )
  117.                                        item
  118.                                )
  119.                         )
  120.              )
  121.              ;;Insert mtext
  122.              (setq obj (vla-addmtext
  123.                          (if (= (getvar 'cvport) 1)
  124.                            (vla-get-paperspace doc)
  125.                            (vla-get-modelspace doc)
  126.                          )
  127.                          (vlax-3d-point pt)
  128.                          w
  129.                          txt
  130.                        )
  131.              )
  132.              ;;Match properties from top text object
  133.              (vla-put-height obj (vla-get-height otxt))
  134.              (vla-put-attachmentpoint obj actopleft)
  135.              (vlax-put obj 'insertionpoint pt)
  136.              (vla-put-rotation obj 0.0)
  137.              (vla-put-layer obj (vla-get-layer otxt))
  138.              (vla-put-stylename obj (vla-get-stylename otxt))
  139.              ;;(grdraw (car (rjp-getbb obj)) (cadr (rjp-getbb obj)) 1)
  140.              ;;Delete single line text
  141.              (mapcar 'entdel (mapcar 'last item))
  142.            )
  143.     )
  144.   )
  145.   (princ)
  146. )
  147.  

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Text to Mtext AutoCAD 2015
« Reply #13 on: May 06, 2014, 07:39:25 PM »
I use this lisp.Try it !!

I use this one as well (along with modified versions in some of my routines) and I can confirm that it works in 2015.

Alternatively, you could use Lee Mac's version:
http://www.lee-mac.com/text2mtext.html

It works well too, even in 2015.

Chris

  • Swamp Rat
  • Posts: 548
Re: Text to Mtext AutoCAD 2015
« Reply #14 on: August 20, 2014, 10:35:37 AM »
I've got a question on Ron's lisp, sorry, I haven't looked for the original post (not to mention it is quite old)
I would like to use Ron's program to combine text that is generated from GIS shape files into single pieces of text.  The program converts all the text to mtext, but not as a paragraph.  what I'm finding is that when listing the insertion points, it is rounding the y coord to 1 significant figure (and the x coord is in scientific notation).  I'm guessing this is because we are on state plane coordinates (huge numbers).  Is there anyway to force the insertion point properties to hold more than 1 decimal?  as it is running now, it thinks all the text is on top of itself.

Thanks,
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10