Author Topic: (challenge) read from file [newbies]  (Read 6541 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file [newbies]
« on: January 22, 2004, 12:28:49 PM »
This challenge is for the AutoLisp newbies out there.

Using the sub-routine below write a program that will open a file, read it's contents and write each line to the ACAD Text Window.

Code: [Select]

;;; this function will prompt the user with a dialog box
;;; to select an existing file to open
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq fo (open_file))
(defun open_file (/ file_select file_open)
  (if (setq file_select (getfiled "Select File to Read" "" "txt" 8))
    (setq file_open (open file_select "r"))
    )
  )


And don't forget to close the open file handle

Edited.......
TheSwamp.org  (serving the CAD community since 2003)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) read from file [newbies]
« Reply #1 on: January 22, 2004, 01:28:15 PM »
Dangit .... this is for newbies only ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
(challenge) read from file [newbies]
« Reply #2 on: January 22, 2004, 03:19:49 PM »
I think he was intending it to be for newbies because its not realy that hard of a procedure or concept. BUT thats not saying that you --or anyone for that matter-- can not participate.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file [newbies]
« Reply #3 on: January 22, 2004, 03:27:28 PM »
You can participate, just wait a day or so. Don't want to give out the answer just yet. :D
TheSwamp.org  (serving the CAD community since 2003)

hyposmurf

  • Guest
(challenge) read from file [newbies]
« Reply #4 on: January 22, 2004, 06:08:13 PM »
[code]
Code: [Select]
;;; this function will prompt the user with a dialog box
;;; to select an existing file to open
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq open_file (open_file))
(defun C:FO (/ file_select open_file)
  (if (setq file_select (getfiled "Select File to Read" "" "txt" 8))
    (setq open_file (open file_select "r"))
    ) (princ)
  )
[/code]
Got that far  :( ,I'm still coming to terms with it all :shock: .So heres my miserable effort :) .Saved the file to my CDrive as a lisp file and apploaded it.Changed around the FO so that it follows the defun,also added a C: (so that the function acts as a command when I enter it into the command line).The mods I've done now bring up the open file dialog box.Have'nt got time to finish the rest of it,might not even know how to :) .Think the princ goes at the end to finish it off and exit cleanlyIt would be really cool if when you reveal a solution to your question you could run through it and explain why you altered each part.

daron

  • Guest
(challenge) read from file [newbies]
« Reply #5 on: January 22, 2004, 06:13:27 PM »
Good effort. You may have notice the 8) 8) when you posted. I added code tags for you to format it and show you how it's done by making non-functioning code tags to wrap it. So, as you can see Hypo, your code is not smiling, is now formatted and shows you how to make it all happen.

hyposmurf

  • Guest
(challenge) read from file [newbies]
« Reply #6 on: January 22, 2004, 06:19:28 PM »
Thanks.I've only really sat down in the last days and started reading "The ABC's of AutoLISP".I've always had a problem when it comes to figures ,I loose interest very quickly get bored,but I can see the possibilties arising if I do manage to suss AutoLISP.Seeing what you lot manage come up with is pretty cool.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file [newbies]
« Reply #7 on: January 23, 2004, 07:04:25 AM »
The point of this challenge was to call the sub-routine from your main program. i.e.
Code: [Select]

(defun c:main_program (/ fo)
  (setq fo (open_file)); calles the subr 'open_file'
  ..........
); defun


Quote
It would be really cool if when you reveal a solution to your question you could run through it and explain why you altered each part.

I will after a few more days.
TheSwamp.org  (serving the CAD community since 2003)

rude dog

  • Guest
(challenge) read from file [newbies]
« Reply #8 on: January 23, 2004, 09:38:05 AM »
I'm hesitant posting this (I know there is a ton of things wrong)
but ill not learn if you dont pick it apart.....

Code: [Select]
(defun OF (/ FN OF)
(if (setq FN (getfiled "Select file to read" "" "txt" 8))
(setq OF (open FN "r"))
))

(defun c: FO ()
(setq FLO (OF))
(while (setq RF (read-line FLO))
(princ RF)
)
(close FLO)
)
(princ)
)
ATTACK!

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
(challenge) read from file [newbies]
« Reply #9 on: January 23, 2004, 09:47:41 AM »
Here, i fixed some of the syntax problems. but other then that ...it works, i wouldnt be assamed of it.

Code: [Select]
(defun open_file (/ file_select file_open)
  (if (setq file_select (getfiled "Select File to Read" "" "txt" 8))
    (setq file_open (open file_select "r"))
    )
  )
(defun OF (/ FN OF)
  (if (setq FN (getfiled "Select file to read" "" "txt" 8))
    (setq OF (open FN "r"))
    )
  )
(defun c:FO ()
  (setq FLO (OF))
  (while
    (setq RF (read-line FLO))
    (princ RF)
    )
  (close FLO)
 (princ)
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
(challenge) read from file [newbies]
« Reply #10 on: January 23, 2004, 09:52:15 AM »
Rude dog, this is kind of a hookey way to do it, but take a look at your last procedure. You are putting one line after another with out telling the intriperter to enter a new line after it prints one off.  So take a look at this replacement and see if it works better.

Code: [Select]
(defun c:FO ()
  (setq FLO (OF))
  (while
    (setq RF (read-line FLO))
    (princ "\n")
    (princ RF)
    )
  (close FLO)
 (princ)
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Anonymous

  • Guest
(challenge) read from file [newbies]
« Reply #11 on: January 23, 2004, 10:09:57 AM »
:wink: gottcha dude...thanks
any good tuts or books on using the lisp editor that comes with 2002
i think this console could help me if i knew how to use it

daron

  • Guest
(challenge) read from file [newbies]
« Reply #12 on: January 23, 2004, 10:10:45 AM »
You could also do a strcat so you don't have too many princs in there. Plus, it's a good way to get to know more functions of lisp. Here:
Code: [Select]
...(setq rf (read-line flo))
(princ (strcat "\n" rf))...

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file [newbies]
« Reply #13 on: January 23, 2004, 10:16:55 AM »
Quote from: Anonymous
any good tuts or books on using the lisp editor that comes with 2002 i think this console could help me if i knew how to use it

Look for a file called acad_dev.chm then look for a topic called Using the Visual LISP Environment
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file [newbies]
« Reply #14 on: January 26, 2004, 09:15:18 AM »
-   Answer  -
First of all let's examine our sub-routine 'open_file'. Notice in the header it states "returns the open file handle or nil", 'nil' is what you would get if the user hits the "Cancel" button or clicks on the "x" in the dialog. So to make sure we get an open file handle in our main program we need to test what we get from 'open_file' and make sure it's not 'nil'.


Code: [Select]

;;; this function will prompt the user with a dialog box
;;; to select an existing file to open
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq fo (open_file))
(defun open_file (/ file_select open_file)
  (if (setq file_select (getfiled "Select File to Read" "" "txt" 8))
    (setq open_file (open file_select "r"))
    )
  )
;;; main function
;;; prints the contents of a .txt file to the AutoCAD Text window.
(defun c:pr2scr (/ fo each_line)
 (if (setq fo (open_file));                 [1]
  (progn;                                   [2]
   (textscr);                               [3]
   (while (setq each_line (read-line fo));  [4]
    (prompt (strcat "\n" each_line));       [5]
   ); while
   (close fo);                              [6]
  )
 )
 (princ);                                   [7]
)


[1] Call the subr 'open_file', set the variable 'fo' to the returned open file handle.Since 'open_file' returns an open file handle _or_ nil we use IF to test that 'open_file' at least returned something besides 'nil'.

[2] We are going to do more than just one thing if our test is true so we use a 'progn'

[3] Open the the AutoCAD Text window.

[4] Begin a 'while' loop. Set the variable 'each_line' to the string returned by 'read-line'. When 'read_line' gets to end of the file it returns nil and the while loop will exit.

[5] Print the contents of 'each_line' to the text window.

[6] Close the file.

[7] Clean exit.
TheSwamp.org  (serving the CAD community since 2003)