Jump to content

To few arguments--help


KESHAR

Recommended Posts

I am new to both this forum and creating lisp routines. I do have some experience modifying existing routines though.

 

 

I was not able to find a routine to do what I need, so I am creating one.

 

 

I am trying to export mtext to a csv file for use in excel.

 

 

I have it to a point but am getting an error of "too few arguments". Any help would be greatly appreciated. I have attached the code below:

 

 

(defun c:MTO (/ elist en fn fname i ss mtxt)

(if (setq ss (ssget (list (cons 0 "MTEXT")))))

(progn

(setq fname (getfiled "Save Text File As:" "" "csv" 1))

(setq fn (open fname "w"))

(progn

(setq a (ssget))

(if (eq (get object 0) "MTEXT"))

(write-line txt fn)

))

(princ

(strcat "\n* Text file " fname " \n has been created *")

)

(setq fn fname)

(startapp (strcat "Excel " (chr 34) fn (chr 34)))

(princ)

)

)

Link to comment
Share on other sites

(defun c:MTO (/ elist en fn fname i ss mtxt)
 (if (setq ss (ssget (list (cons 0 "MTEXT"))))[color=red] one to many closing braces[/color]
    (progn[color=seagreen];if true do this[/color]
        (setq fname (getfiled "Save Text File As:" "" "csv" 1))
        (setq fn (open fname "w"))
    [color=red]);closing brace for progn missing[/color]
    (progn[color=seagreen];if false do this[/color]
        (setq a (ssget))
        (if (eq (get object 0) "MTEXT")[color=red] one to many closing braces (get object 0) doesn't work[/color]
           (write-line txt fn)[color=seagreen];if true do this[/color]         
        )[color=red];closing brace for if[/color]
    )
 )
 (princ
     (strcat "\n* Text file " fname " \n has been created *")
 )
 (setq fn fname)
 (startapp (strcat "Excel " (chr 34) fn (chr 34)))
 (princ)
)
[color=red] one to many closing braces[/color]

see red items above. This is my take on the logic, I could be wrong.

 

 

Green items are for clarity. If statements require one or two things to happen The first is what happens when the if is true, the second optional what happens when it is false. Since you were closing the if before supplying any options it was erroring. You made the same mistake with the following

 

(if (eq (get object 0) "MTEXT")). The (get object 0) here is nonsensical so this will again error.

Link to comment
Share on other sites

So am I going about this this wrong way? Is the initial "if" statement necessary if what I am wanting to do is select the text?

Link to comment
Share on other sites

So I have modified the code. Now I am getting "malformed list on input".

 

 

(defun c:MTO (/ elist en fn fname i ss mtxt)

(if (setq ss (ssget (list (cons 0 "MTEXT")))

(progn

(setq fname (getfiled "Save Text File As:" "" "csv" 1))

(setq fn (open fname "w"))

(progn

(setq a (ssget))

(if (eq (get object ALL) "MTEXT")

(write-line txt fn)

))

(princ

(strcat "\n* Text file " fname " \n has been created *")

)

(setq fn fname)

(startapp (strcat "Excel " (chr 34) fn (chr 34)))

(princ)

)

Link to comment
Share on other sites

A 'malformed list on input' error means you are missing one or more closing parentheses - in your case:

(setq ss (ssget (list (cons 0 "MTEXT")))[color="red"])[/color]

You may find this reference useful when troubleshooting.

 

Aside, what editor are you using when writing your code? I ask as any basic code editor (e.g. Notepad++) will have parenthesis matching.

Personally, I use the Visual LISP IDE provided with AutoCAD when writing AutoLISP and Notepad++ for everything else.

 

PS: Please edit your post and enclose your code with code tags:

 

[highlight][noparse]

[/noparse][/highlight]Your code here[highlight][noparse]

[/noparse][/highlight]

Link to comment
Share on other sites

I have edited the code as requested. I am trying to learn to use the Visual LISP within AutoCAD. I am new to this and would really like to learn this to add to my tool bag. Thanks for your help.

 

 

(defun c:MTO (/ elist en fn fname i ss mtxt)
(if (setq ss (ssget (list (cons 0 "MTEXT"))))
(progn
(setq fname (getfiled "Save Text File As:" "" "csv" 1))
(setq fn (open fname "w"))
(progn
(setq a (ssget))
(if (eq (get object ALL) "MTEXT")
(write-line txt fn)
))
(princ
(strcat "\n* Text file " fname " \n has been created *")
)
(setq fn fname)
(startapp (strcat "Excel " (chr 34) fn (chr 34)))
(princ)
)

Link to comment
Share on other sites

I have been going through this and have made some progress. I now have it partially working. The issue that I am having now is that I am being told that there are too many arguments. it is getting hung up at the (ssget object all) portion of the below code. How do you go about fixing that?

(defun c:MTO (/ elist en fn fname i ss mtxt)
 (progn
(setq fname (getfiled "Save Text File As:" "" "csv" 1)))
(setq fn (open fname "w"))
(progn
(setq a (ssget)))
(if (eq (ssget object all) "MTEXT")
(write-line txt fn)
)
(princ
(strcat "\n* Text file " fname " \n has been created *")
)
(setq fn fname)
(startapp (strcat "Excel " (chr 34) fn (chr 34)))
(princ)
)

Link to comment
Share on other sites

I have been going through this and have made some progress. I now have it partially working. The issue that I am having now is that I am being told that there are too many arguments. it is getting hung up at the (ssget object all) portion of the below code. How do you go about fixing that?

(defun c:MTO (/ elist en fn fname i ss mtxt)
(progn
(setq fname (getfiled "Save Text File As:" "" "csv" 1)))
(setq fn (open fname "w"))
(progn
(setq a (ssget)))
(if (eq (ssget object all) "MTEXT")
(write-line txt fn)
)
(princ
(strcat "\n* Text file " fname " \n has been created *")
)
(setq fn fname)
(startapp (strcat "Excel " (chr 34) fn (chr 34)))
(princ)
)

Please explain what you are trying to do. Are you trying to get just a single mtext entity or all mtext entities. Just getting the mtext entity doesn't get you the text it contains, all you have is an entity name that may be different the next time you load the drawing.

Link to comment
Share on other sites

I am trying to get all of the mtext (text contained within the intities) within a pick window into a csv file. So it will be different for each dwg that this is used on.

Link to comment
Share on other sites

Try this

 

 

(defun c:MTO (/ elist en fn fname i ss mtxt)
(setq fname (getfiled "Save Text File As:" "" "csv" 1))
(setq fn (open fname "w"))
(setq a (ssget (list (cons 0 "*text")))) ; filters text and mtext
(repeat (setq x (sslength a))
(setq txt (vla-get-textstring (vlax-ename->vla-object(ssname a (setq x (- x 1))))))
(write-line (vl-string-translate "\\" "," txt) fn) ; need a replace \\ with , here
)
(close fn)
(alert (strcat "\n* Text file " fname " \n has been created *"))
(startapp (strcat "Excel " (chr 34) fname (chr 34)))
(princ)
)

Link to comment
Share on other sites

the following code is the solution (although it is inverted in its output).

 

 

;----------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Created by Karl E. Sharp
; Date: 6/27/2018
;
; This Program was designed to copy mtext from a dwg
; to csv file for use in excel
;
;   Code Modified by: Chris Wade - 06/28/2018
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:MTO (/ elist en fn fname i ss mtxt Filter Ct Obj txt)
   ;Supporting Functions
   ;;-------------------=={ UnFormat String }==------------------;;
;;                                                            ;;
;;  Returns a string with all MText formatting codes removed. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;
;;------------------------------------------------------------;;
;;  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 "[url="file://\\$1$2$3"]\\$1$2$3[/url]" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
                               (_replace "\\"   "\032" str)
                           )
                       )
                   )
               )
           )
           (vlax-release-object rx)
           (if (null (vl-catch-all-error-p str))
               str
           )
       )
   )
)
   ;;--------------------=={ Get TextString }==------------------;;
   ;;                                                            ;;
   ;;  Returns the TexString associated with an object,          ;;
   ;;  retaining all special symbols.                            ;;
   ;;------------------------------------------------------------;;
   ;;  Author: Lee Mac, Copyright © 2010 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;
   ;;------------------------------------------------------------;;
   ;;  Arguments:                                                ;;
   ;;  object - VLA-Object/ename for which to return TextString  ;;
   ;;------------------------------------------------------------;;
   ;;  Returns:  TextString associated with object, else nil     ;;
   ;;------------------------------------------------------------;;
(defun LM:GetTextString ( object )
 ;; © Lee Mac 2010
 (
   (lambda ( entity / _type elist )    
     (cond
       (
         (wcmatch
           (setq _type
             (cdr
               (assoc 0
                 (setq elist
                   (entget entity)
                 )
               )
             )
           )
           "TEXT,*DIMENSION"
         )
         (cdr (assoc 1 elist))
       )
       (
         (eq "MULTILEADER" _type)
         (cdr (assoc 304 elist))
       )
       (
         (wcmatch _type "ATTRIB,MTEXT")
         (
           (lambda ( string )
             (mapcar
               (function
                 (lambda ( pair )
                   (if (member (car pair) '(1 3))
                     (setq string (strcat string (cdr pair)))
                   )
                 )
               )
               elist
             )
             string
           )
           ""
         )
       )
     )
   )
   (if (eq 'VLA-OBJECT (type object))
     (vlax-vla-object->ename object)
     object
   )
 )
)
;End of Supporting Functions
   (princ "\nSelect text to export:")
   (setq Filter '((-4 . "<OR") (0 . "TEXT") (0 . "MTEXT") (-4 . "OR>")))
   (if (setq ss (ssget Filter))
       (progn
           (setq fname (getfiled "Save Text File As:" "" "csv" 1))
           (setq fn (open fname "w"))
           (if fn
               (progn
                   (setq Ct 0)
                   (while (< ct (sslength ss))
                       (setq Obj (vlax-ename->vla-object (ssname ss Ct)))
                       (if Obj
                           (progn
                               (setq txt (LM:Unformat (LM:GetTextString Obj) T))
                               (if txt
                                   (write-line txt fn)
                               )
                           )
                       )
                       (setq ct (+ ct 1))
                   )
                   (princ (strcat "\n* Text file " fname " \n has been created *"))
                   (close fn)
                   (startapp (strcat "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.exe" " " (chr 34) fname (chr 34)));Make sure you update the path to match where your installation of Excel is or write a routine to find the location of Excel.
               )
               (princ "\n* Text file was not created.")
           )
       )
       (princ "\n* No text was found to export.")
   )
   (princ)
)

Link to comment
Share on other sites

it strips the formatting and exports the text all in one code. Thanks to all that helped both on this forum and the swamp!

Link to comment
Share on other sites

This is the fix for the inverted export (kudos to cmwade77 for the swamp for fixing my mess and thanks to lee-mac for the use of some of his coding).

;----------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Created by Karl E. Sharp
; Date: 6/27/2018
;
; This Program was designed to copy mtext from a dwg
; to csv file for use in excel
;
;   Code Modified by: Chris Wade - 06/28/2018
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:MTO (/ elist en fn fname i ss mtxt Filter Ct Obj txt)
   ;Supporting Functions
   ;;-------------------=={ UnFormat String }==------------------;;
;;                                                            ;;
;;  Returns a string with all MText formatting codes removed. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;
;;------------------------------------------------------------;;
;;  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 "[url="file://\\$1$2$3"]\\$1$2$3[/url]" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
                               (_replace "\\"   "\032" str)
                           )
                       )
                   )
               )
           )
           (vlax-release-object rx)
           (if (null (vl-catch-all-error-p str))
               str
           )
       )
   )
)
   ;;--------------------=={ Get TextString }==------------------;;
   ;;                                                            ;;
   ;;  Returns the TexString associated with an object,          ;;
   ;;  retaining all special symbols.                            ;;
   ;;------------------------------------------------------------;;
   ;;  Author: Lee Mac, Copyright © 2010 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;
   ;;------------------------------------------------------------;;
   ;;  Arguments:                                                ;;
   ;;  object - VLA-Object/ename for which to return TextString  ;;
   ;;------------------------------------------------------------;;
   ;;  Returns:  TextString associated with object, else nil     ;;
   ;;------------------------------------------------------------;;
(defun LM:GetTextString ( object )
 ;; © Lee Mac 2010
 (
   (lambda ( entity / _type elist )    
     (cond
       (
         (wcmatch
           (setq _type
             (cdr
               (assoc 0
                 (setq elist
                   (entget entity)
                 )
               )
             )
           )
           "TEXT,*DIMENSION"
         )
         (cdr (assoc 1 elist))
       )
       (
         (eq "MULTILEADER" _type)
         (cdr (assoc 304 elist))
       )
       (
         (wcmatch _type "ATTRIB,MTEXT")
         (
           (lambda ( string )
             (mapcar
               (function
                 (lambda ( pair )
                   (if (member (car pair) '(1 3))
                     (setq string (strcat string (cdr pair)))
                   )
                 )
               )
               elist
             )
             string
           )
           ""
         )
       )
     )
   )
   (if (eq 'VLA-OBJECT (type object))
     (vlax-vla-object->ename object)
     object
   )
 )
)
;End of Supporting Functions
   (princ "\nSelect text to export:")
   (setq Filter '((-4 . "<OR") (0 . "TEXT") (0 . "MTEXT") (-4 . "OR>")))
   (if (setq ss (ssget Filter))
       (progn
           (setq fname (getfiled "Save Text File As:" "" "csv" 1))
           (setq fn (open fname "w"))
           (if fn
               (progn
(setq ct (sslength ss)); Code to process in reverse order came from: David Bethel - [url]https://www.theswamp.org/index.php?topic=3162.msg39448#msg39448[/url]
(while (not (minusp (setq ct (1- ct))))
                       (setq Obj (vlax-ename->vla-object (ssname ss Ct)))
                       (if Obj
                           (progn
                               (setq txt (LM:Unformat (LM:GetTextString Obj) T))
                               (if txt
                                   (write-line txt fn)
                               )
                           )
                       )
                   )
                   (princ (strcat "\n* Text file " fname " \n has been created *"))
                   (close fn)
                   (startapp (strcat "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.exe" " " (chr 34) fname (chr 34)));Make sure you update the path to match where your installation of Excel is or write a routine to find the location of Excel.
               )
               (princ "\n* Text file was not created.")
           )
       )
       (princ "\n* No text was found to export.")
   )
   (princ)
)

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