+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,736

    Default Writing LISP Data to an External Program (such as Excel/Notepad...etc)

    Registered forum members do not see this ad.

    This thread is solely in response to an original thread posted by Gazzalp, which needed the extraction of various data to an excel spreadsheet.

    But I am intrigued, as I too do not know the first thing about writing data to external programs such as Excel.

    If, for instance, one has variables: var1, var2, var3; can one write the values of these variables to various cells in an Excel Sheet? or lines in a Txt doc? And if so, how?

    Help & advice is always appreciated.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  2. #2
    Super Moderator fuccaro's Avatar
    Using
    AutoCAD 2006
    Join Date
    Nov 2002
    Location
    Romania, Marosvasarhely
    Posts
    3,540

    Default

    The simplest way is to write in text files. First define and open a file for writing:
    (setq file (open "my_file.txt" "w"))
    Now you can write into it, just convert the data to be written into string:
    (write-line (rtos var1) file)
    when finished, close the file
    (close file)
    Better see in AutoLISP help file the commands Open, Close, Write-char, Write-line, print, ...
    Also Getfiled worth a look

    Sometimes I write in the same way but in files with extension CSV instead of TXT. Those files can be opened in Excel. Just be carefull to write a comma in the file to separate two neighbor data.
    It's nice to be nice, but sometimes is nicer to be evil!.
    Tip: Please do not PM or email me with CAD questions - use the forums, you'll get an answer sooner.

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,736

    Default

    I have created a test txt file in a known directory, and have used your example to write the line to the text file.

    but, after closing the file using (close "file"), and manually opening the file myself, nothing seems to be written...
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,736

    Default

    Don't mind me, I just didn't include the filename in the (write-line parenthesis...

    As usual, more coffee needed...
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  5. #5
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,736

    Default

    Thanks for all your help Fuccaro, the LISP is working perfectly - the csv file is a good option, and I see what you mean about commas!
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  6. #6
    Super Moderator fuccaro's Avatar
    Using
    AutoCAD 2006
    Join Date
    Nov 2002
    Location
    Romania, Marosvasarhely
    Posts
    3,540

    Default

    Glad to see an other happy man!
    It's nice to be nice, but sometimes is nicer to be evil!.
    Tip: Please do not PM or email me with CAD questions - use the forums, you'll get an answer sooner.

  7. #7
    Senior Member
    Computer Details
    VVA's Computer Details
    Operating System:
    Windows 7
    CPU:
    Intel Core i5-2400
    RAM:
    8 Gb
    Graphics:
    Nvidia Quadro 600
    Primary Storage:
    Seagate 500 GB + WD 750 GB
    Monitor:
    Philips 27"
    Using
    AutoCAD 2013
    Join Date
    Dec 2006
    Location
    Minsk, Belarus
    Posts
    401

    Default

    For record in Excel look functions XLS and XLSF

  8. #8
    Super Member David Bethel's Avatar
    Discipline
    Multi-disciplinary
    David Bethel's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Commercial Food Service
    Using
    AutoCAD pre 2000
    Join Date
    Dec 2003
    Location
    Newport News, Virginia
    Posts
    1,926

    Default

    Lee Mac,

    You may also want to think about exporting in an AutoLisp list format
    Code:
      (setq x 45 y 50 z 55)
    
      (setq wf (open "mydata.dat" "w"))
      (write-line "(setq mydata '(" wf)
      (foreach v '(x y z)
         (princ "(" wf)
         (prin1 v wf)
         (princ (strcat " . " (rtos (eval v) 2 8) ")") wf)
         (write-line "" wf))
      (write-line "))" wf)
      (close wf)
    Then you access it later by using the (load) function

    Code:
    Command: (load "mydata.dat")
    
    Command: !mydata
    ((X . 45.0) (Y . 50.0) (Z . 55.0))
    
      (assoc 'x mydata)
      (cdr (assoc 'x mydata))
    This is very similar to a XML file but in AutoLISPs list format. -David
    R12 (Dos) - A2K

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,736

    Default

    Registered forum members do not see this ad.

    Thanks David, thats very clever.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

Similar Threads

  1. good program for writing lisp
    By chrisdarmanin in forum AutoLISP, Visual LISP & DCL
    Replies: 8
    Last Post: 9th May 2008, 08:57 am
  2. How can I change attributes with a external program like Access?
    By Nightcellar in forum AutoLISP, Visual LISP & DCL
    Replies: 27
    Last Post: 26th Oct 2007, 10:28 pm
  3. Save the work then Close Autocad2007 with an external program
    By henry.wuguangyu in forum AutoLISP, Visual LISP & DCL
    Replies: 21
    Last Post: 12th Jul 2007, 03:20 pm
  4. Run external program
    By Wex in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 9th Aug 2006, 04:34 pm
  5. importing external data into autocad or inventor??
    By dave_b in forum AutoCAD General
    Replies: 0
    Last Post: 18th May 2006, 09:46 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts