Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Import the text from a .txt file via a lisp

34 REPLIES 34
SOLVED
Reply
Message 1 of 35
jleslieGLECD
5150 Views, 34 Replies

Import the text from a .txt file via a lisp

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.

34 REPLIES 34
Message 2 of 35
pbejse
in reply to: jleslieGLECD


@jleslieGLECD wrote:

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.


Easy.

 

Supply the filename  (getfiled....)

open the file (open ...)

read the file  (read-line....)

save the file to a list (cons..)

close the file (close ....)

 

I can write a short code for you, what are you wanting to do with the content anyway?

 

 

Message 3 of 35
jleslieGLECD
in reply to: pbejse

That would be great...

i would just like it to post as a list in the drawing..

eventual i'd like to have a dialog box open to allow selection of which text to insert based on headers.  Here is an example of the txt

 

WSNO=>173307
[<EASEMENT>]
#NotSet#
[<PAD MOUNT>]
CONTRACTOR
TO EXCAVATE 8' X 10' X 4' LEVEL BOTTOM PIT
INSTALL 4'-8" X 7' X 4' CONCRETE VAULT TRANSFORMER FOUNDATION AND GROUND MAT
INSTALL 4'-8" X 7' CONCRETE VAULT COVER
INSTALL
225 KVA - 3 PH - 7.2/12.5KV - 120/208V STAINLESS STEEL
PAD MOUNT TRANSFORMER [SP7518].
LABEL TRANSFORMER LOCATION NO. 2780

[<PRIMARY UG>]
#NotSet#
[<SERVICE>]
INSTALL 4 INCH PVC SCHEDULE 40 CONDUIT [SP7316] FROM
LOCATION NO. #### TO LOCATION NO. ####.

IN 24 IN. DEEP TRENCH DUG AND BACKFILLED BY CUSTOMER
INSTALL 180 FT.
350-4/0-350 AL TRIPLEX & (1) 350 KCM AL UG SERVICE CABLES.
TAG CABLES '80-A-84', 80-B-84', '80-C-84' AND
'80-N-84' ON BOTH ENDS.
LEAVE UG CABLE WARNING TAPE FOR CUSTOMER TO INSTALL WHEN BACKFILLING

[<TAKE-OFF POLE>]
INSTALL THREE PHASE [7.2/12] KV L-G OH TO UG
TRANSITION [SP7220].

INSTALL POLE GROUND [SP3800].

INSTALL THREE 27 KV CUTOUTS AND BRACKET FOR UNDERGROUND
[SP3579].
FUSE 25 AMPS

INSTALL 4" PVC PRIMARY RISER & STANDOFFS [SP7328].

IN 36 IN. DEEP TRENCH DUG AND BACKFILLED BY CUSTOMER
INSTALL 231 FT.
OF 3 RUNS
NO. 2 15KV UG PRIMARY CABLES
LEAVE UG CABLE WARNING TAPE FOR CUSTOMER TO INSTALL WHEN BACKFILLING

INSTALL 4 INCH PVC SCHEDULE 40 CONDUIT [SP7316] FROM
LOCATION NO. #### TO LOCATION NO. ####.

Message 4 of 35
john.uhden
in reply to: jleslieGLECD

I am probably an older dog than any of whom you speak.  @pbejse (aka PBGV) gave you the basic idea.  The question is in what format you want to import the file, as mtext or as separate text objects (one for each line).  What about layer, color, style, and justification?  I am presuming top left and that the point you pick for its location is the top left.  Do you Dview;Twist your drawings yet want the text to come in looking horizontal?

Getting more involved, do you want every line feed in the file to be a paragraph in mtext, or would you like to create a new paragraph only if a line ends in a period?  The latter gives you the flexibility to grip-drag the width and let AutoCAD adjust the lines accordingly.  Maybe even make columns.

John F. Uhden

Message 5 of 35
pbejse
in reply to: jleslieGLECD


@jleslieGLECD wrote:

That would be great...

i would just like it to post as a list in the drawing..

eventual i'd like to have a dialog box open to allow selection of which text to insert based on headers.  Here is an example of the txt

 

 


Yup it can be done.

 

Silly questions but here it is anyway.

 

How large is the source TEXT file? [ we can write a code to read the contents in one-go depending on the files size is why I'm asking ]

 

And what he said up there 🙂

 

 

Message 6 of 35
john.uhden
in reply to: pbejse

I suspect one-go is the way.  If it is too big to fit on one sheet, he can always copy it to another sheet and cut each where needed. "(continued next sheet)"

John F. Uhden

Message 7 of 35
jleslieGLECD
in reply to: john.uhden

I'd like it to import as mtext, Layer TEXT, Color 140, Style Standard, Justification TopLeft, We do not Twist, Also at the end of each period would be great!

Message 8 of 35
jleslieGLECD
in reply to: jleslieGLECD

one-go makes sense

 

Message 9 of 35
john.uhden
in reply to: jleslieGLECD

Try this out (MTFF.lsp attached).

 

Right now you must browse for the TXT file to import, but you can hardwire the file name if you want (look for getfiled).

 

I also ended up with mixed feelings about creating a new paragraph only if a line ends in a period, but that's the way this is written.  I hope that's more good than bad.

John F. Uhden

Message 10 of 35
jleslieGLECD
in reply to: john.uhden

Thank you so much. I may tweek it a little later but this is excellent!

Message 11 of 35
jleslieGLECD
in reply to: john.uhden

What if i want a new paragraph after each "%".  How would i change this?

Message 12 of 35
jleslieGLECD
in reply to: jleslieGLECD

Also the file to import will always be "c:/acadcfg/MOSNOTES.txt".  Would I turn filedia off to achieve this?

Message 13 of 35
john.uhden
in reply to: jleslieGLECD

Thanks for bringing us good news.

I understand the % thing,

Just change the line of code that reads:

 

(if (wcmatch line "*`.,")

to

(if (wcmatch line "*`.,*%,")

That's not really after every %, but only a line ending in a %. It would be just a little more code to force a new paragraph after every %. Notice that I decided to treat an empty line as a new paragraph, so as to force a gap between paragraphs, just as the text file probably looks.

John F. Uhden

Message 14 of 35
roland.r71
in reply to: jleslieGLECD

might i suggest reading the file into a layered list, grouped by section?

 

(setq text_list nil
      header ""
      file (open "c:/temp/txt2list.txt" "r"))
(while (setq txtline (read-line file))
   (if (= (substr txtline 1 2) "[<")
     (setq text_list (append text_list (list (list header section_list)))
           header txtline
           section_list nil
      )
      (setq section_list (append section_list (list txtline)))
   )
)
(if section_list
   (setq text_list (append text_list (list (list header section_list)))
         section_list nil
   )
)
(close file)

This will turn a file format like your example:

first
[<SECTION 1>]
this is the
second part
[<SECTION 2>]
this is the
last part

into:

(("" ("first")) ("[<SECTION 1>]" ("this is the" "second part")) ("[<SECTION 2>]" ("this is the" "last part")))

 

So each item on the list is a list with 2 items. The first being the section header, the second is a list of all text lines that follow.

The first section having an empty header, unless a header is the first line in your file.

 

This would allow you to work with different sections of text. To do with as you please.

 

edit:

Forgot to list that finall section there 🙂

Message 15 of 35
john.uhden
in reply to: jleslieGLECD

Now that we've gone through this exercise I have to wonder.

If it's the same file, and the file never changes, why not just make a block of it that you can insert, even maybe put it in a tool palette or whatever menu thingies people use these days?

Don't read me wrong.  The exercise was fun, and myself and any lurkers might get some use out of it.  As my son always says, "It's all good."

 

Filedia has nothing to do with this.  When you make up your mind about %s, we can make all the fixes.

John F. Uhden

Message 16 of 35
roland.r71
in reply to: john.uhden


@john.uhden wrote:

Now that we've gone through this exercise I have to wonder.

If it's the same file, and the file never changes, why not just make a block of it that you can insert, even maybe put it in a tool palette or whatever menu thingies people use these days?

Don't read me wrong.  The exercise was fun, and myself and any lurkers might get some use out of it.  As my son always says, "It's all good."

 

Filedia has nothing to do with this.  When you make up your mind about %s, we can make all the fixes.


He never said the file's content would never change. (in fact, he mentioned it being generated)

Just the name & location.

 

Message 17 of 35
john.uhden
in reply to: roland.r71

@roland.r71 wrote "He never said the file's content would never change. (in fact, he mentioned it being generated)"

 

I was actually hoping that was the case.  Luckily (sorta) my boss was too busy to give me enough work today, so I played a little.  It was fun.

Actually, I tweaked my own EDITNUM.lsp and did about 6 hours work in 5 minutes.

John F. Uhden

Message 18 of 35
jleslieGLECD
in reply to: roland.r71

That is exactly right... a secondary program creates a job specific txt that always goes to the same place on the users pc.... constantly overwriting itself each job

Message 19 of 35
john.uhden
in reply to: jleslieGLECD

Thank you for that information.  That makes this routine somewhat more worthwhile.

 

How about those %s?  Do you want a new paragraph after every one, or just if it's at the end of a line?

John F. Uhden

Message 20 of 35
roland.r71
in reply to: jleslieGLECD

ok, i worked my code into john's.

 

if you take john.uden's lisp, and replace the bottom part with the following:

  (setq cmdecho (getvar "cmdecho") new nil lines nil)
  (setvar "cmdecho" 0)
  (command "_.expert" (getvar "expert"))
;--- john part above, delete from here to end, and insert code below:
(setq text_list nil header "" file (open "c:/acadcfg/MOSNOTES.txt" "r")) (while (setq txtline (read-line file)) (if (= (substr txtline 1 2) "[<") (setq text_list (append text_list (list (list header section_list))) header txtline section_list nil ) (setq section_list (append section_list (list txtline))) ) ) (if section_list (setq text_list (append text_list (list (list header section_list))) section_list nil ) ) (close file) (foreach section text_list (setq header (nth 0 section) lines (nth 1 section) new nil ) (cond ((/= (nth 0 lines) "#NotSet#") (if (/= header "")(setq lines (cons header lines))) (setq Width (apply 'max (mapcar '@Textwidth lines))) (foreach line lines (setq new (append new (list (strcat line "\\P")))) ) (setq msg (strcat "\nTop Left insertion point for section " (substr header 3 (- (strlen header) 4)) ": ")) (setq IP (getpoint msg)) (@Mtext IP (apply 'strcat new) "TEXT" 140 "Standard" (getvar "textsize") width) ) ) ) (*error* nil) )

This will get you an mtext for each section. (and i made it a paragraph for each line)

Sections with #NotSet# are skipped.

 

Works nice with your example text. If i may say so 😉

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost