Import the text from a .txt file via a lisp

Import the text from a .txt file via a lisp

Anonymous
Not applicable
7,634 Views
34 Replies
Message 1 of 35

Import the text from a .txt file via a lisp

Anonymous
Not applicable

I'm trying to mimic a tool that the "Old Dogs" are anchored to.  We lost the ability when we upgraded a secondary software.  I'm trying to import all the text from a .txt file generated my a secondary software via one lisp.  This file is always the same name and location.  any ideas?  dragging and dropping is to complex for these people, they want me to make a button.  Sounds easy, but i'm having a heck of a time.  Thanks.

0 Likes
Accepted solutions (4)
7,635 Views
34 Replies
Replies (34)
Message 21 of 35

john.uhden
Mentor
Mentor

Aah, we didn't need that @mtext function anyway.

Only kidding.  Very nice!

John F. Uhden

0 Likes
Message 22 of 35

pbejse
Mentor
Mentor

@Anonymous wrote:

one-go makes sense

 


one-go it is

 

(defun c:Readme  (/ _OneGo file ipt str)
;;; 			pBe Sep 2017			;;;
(defun _OneGo  (file2Read / fso opentextFile filecontent)
    (setq fso (vlax-create-object
                    "Scripting.FileSystemObject"))
    (setq opentextFile
               (vlax-invoke-method
                     fso
                     'OpenTextFile
                     file2Read
                     1
                     :vlax-false
                     -2))
    (setq filecontent (vlax-invoke opentextFile 'ReadAll))
    (vlax-release-object opentextFile)
    (vlax-release-object fso)
    filecontent
    )

      (if (and
                (setq file (findfile "C:\\acadcfg\\MOSNOTES.txt"))
                (setq ipt (getpoint "\nPick Mtext Location")))

            (progn
                  (setq str (_OneGo file))

                  (entmakex (append 
                        (list '(0 . "MTEXT")
                              '(100 . "AcDbEntity")
                              '(100 . "AcDbMText")
                              '(8 . "TEXT")
                              '(7 . "Standard")
                              '(62 . 140)
                              '(72 . 1))
                              (list (cons 40 (getvar "textsize"))
                                    (cons 10 ipt)(cons 1 str))))
                  )
          (princ
                (if (null file)
                      "\nFile Not Found"
                      "\nInput cancelled"))
            )
(princ)
      )

HTH

 

Message 23 of 35

john.uhden
Mentor
Mentor

'ReadAll.  Way cool.

 

Have you tried it on a not-tiny file?  When I tried to entmake a long string all I got was ""

John F. Uhden

0 Likes
Message 24 of 35

Anonymous
Not applicable

This is perfect......  Thank you guys so much!

0 Likes
Message 25 of 35

pbejse
Mentor
Mentor

@john.uhden wrote:

'ReadAll.  Way cool.

 

Have you tried it on a not-tiny file?  When I tried to entmake a long string all I got was ""


Glad you liked it.

I see what you mean by a not-tiny file Smiley Very Happy Should've known entmake Mtexts' restrictions with very looong strings.

 

FWIW, we can add vla-put-textstring like the one you had on your code.

 

 

(progn
                  (setq str (_OneGo file))
                  (setq CreatedMtext
			(entmakex (append 
                        (list '(0 . "MTEXT")
                              '(100 . "AcDbEntity")
                              '(100 . "AcDbMText")
                              '(8 . "TEXT")
                              '(7 . "Standard")
                              '(62 . 140)
                              '(72 . 1)
                              '(1 . "-"))
                              (list (cons 40 (getvar "textsize"))
                                    (cons 10 ipt))))
                        )
                  (vla-put-textstring (vlax-ename->vla-object CreatedMtext) str)
                  )

 

HTH

 

OR we can just use VL method

 

(setq obj (vla-addmtext
		        (vlax-get
		                 (vla-get-ActiveLayout
		                       (vla-get-activedocument
		                             (vlax-get-acad-object)))
		                 'Block)
		        (vlax-3D-point ipt) 0 str))

 

Thank you for the heads up John

 

0 Likes
Message 26 of 35

Anonymous
Not applicable

how would i change this so that there is no empty lines, no spaces between lines as it were?

0 Likes
Message 27 of 35

roland.r71
Collaborator
Collaborator
   (while (setq txtline (read-line file))
      (if (= (substr txtline 1 2) "[<")
         (if section_list 
(setq text_list (append text_list (list (list header section_list))))
) (setq header txtline section_list nil ) (if (/= txtline "")(setq section_list (append section_list (list txtline))))
) )

 

This should avoid empty sections too.

Note: a single space can still get you an empty line like this.

 

To avoid that too, use this instead:

 

(if (/= (vl-string-left-trim " " txtline) "")(setq section_list (append section_list (list txtline))))

 

edit:

i always manage to delete enters, while adding color...

0 Likes
Message 28 of 35

john.uhden
Mentor
Mentor

Definitely YES

and

probably YES.

 

I generally prefer using entmakex because I can slam dunk the layer and color and stuff, but if you have to vlax-put properties anyway, what's the difference?  And the DXF codes for justification always drive me nuts.  But I can cheat pretty well... (entget (car (entsel))) and copy-clip.

John F. Uhden

0 Likes
Message 29 of 35

roland.r71
Collaborator
Collaborator
Accepted solution

hmm...

I see i made a bad correction there too Smiley Sad

 

   (while (setq txtline (read-line file))
      (if (= (substr txtline 1 2) "[<")
(progn (if section_list (setq text_list (append text_list (list (list header section_list)))) ) (setq header txtline section_list nil) ) (if (/= txtline "")(setq section_list (append section_list (list txtline))))
) )

 

Message 30 of 35

lmillerK9S62
Explorer
Explorer

@john.uhden  I know this is an older post and I am new to coding, Your routine works perfectly, but I would like to hard code in a file location, how can I do this?

0 Likes
Message 31 of 35

john.uhden
Mentor
Mentor

@lmillerK9S62 

Good for you... digging back into history.

Just change:

(setq file (getfiled "Select Text File to Import" "" "TXT" 0))

to:

(setq file "full_path+name+ext")

No, not literally. 🙄  (and no +s)

But remember with AutoLisp you have to use two "\\" or one "/" as the delimiter.

John F. Uhden

0 Likes
Message 32 of 35

lmillerK9S62
Explorer
Explorer

Thank you so much, I was up half the night trying to figure this out, this was the path I never took 😂

0 Likes
Message 33 of 35

john.uhden
Mentor
Mentor

@lmillerK9S62 

Maybe you shoulda stayed up all night.  😞

John F. Uhden

0 Likes
Message 34 of 35

roland.r71
Collaborator
Collaborator

...or get some sleep and look at it with a 'fresh' mind. 🤔

Message 35 of 35

Sea-Haven
Mentor
Mentor

Watch out for spaces in directory or file names if you use "/" as the delimiter, may fail. "\\" very reliable.

0 Likes