Autolisp Programming
Autolisp Programming
Autolisp Programming
Segment 3
Date: January 22, 2007 Instructor: Mohmed Zuber Shaikh Level: Beginning Category: AutoLISP
Web: www.AUGI.com
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
Introduction
AutoLISP can handle files for Reading, Writing and Appending. This feature is useful for writing a program where input for program is stored in files. The Writing mechanism is useful for recording Design OR Bill of Material. The Program Users list etc. can be created by using the Appending feature of program. In short you can write an AutoLISP program capable of ---Reading data from file Creating Drawing File Creating Design file Creating Bill of Material
Files could be considered as streams of data. AutoLISP can read from external files and write to them. You may store drawing parameters in an ASCII file on your hard disk which can be imported in LISP programme to generate the drawing. There is a direct interface available with 'C' language and Visual Basic. AutoLISP allows files to be opened in only three modes. They are "r" for reading , "w" for writing and "a" for appending.
To open a file for reading. If the file does not exist a nil will be returned. To open a file for writing. If the file doesnt exist then file is created and opened for writing and if it exists the file is overwritten. To open a file to append. If the file exists then writing starts from where it last stopped. If the file doesn't exist then a file is created and opened.
"a" Append
6.2
OPEN
A file in AutoLISP is opened by using a function called open. The open function requests the operating system to allocate a slot to a file. Your program can open many files at a time; you need a separate file handler for each file. Your program can open fixed name file or the file name as specified by the user.
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
Syntax: (open <filename> <mode>) (open "filename.ext" "r") Read mode in lower-case only. Physical file on disk. AutoLISP function
The open function returns a file descriptor to be used by the other I/O functions. The file descriptor must be assigned to a symbol that uses the setq function.
(Setq nm (getstring "\n ENTER NAME OF DATA FILE : ")) (graphscr) (if (= (findfile nm) nil) (alert "File not found.") )
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
6.3
READ-LINE
The first call to read-line returned a string that was the first line in the file. Read-line takes one parameter and returns a string.
Example: ABC.DAT ASCII file ABC LTD. MY 001 FIRST 002 TRIAL 003 WITH 004 STREAM
ABC.LSP AutoLISP Programme (setq fh (open "ABC.DAT" "r")) (setq cnm (read-line fh)) (setq ln2 (read-line fh)) (setq ln3 (read-line fh))
Create the ASCII file ABC.DAT as shown above. Read the content using AutoLISP programme and display the same on Graphics screen.
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
6.4
CLOSE
The opened file must be closed. The function close does this job.
6.5
Write-line
Now, so far we were talking about "how to read now lets learn how to write".
Example:
Dont forget to close the file. AutoLISP writes the string in an area called the file buffer, and the contents of the buffer are flushed, only when 512 bytes go in to the buffer Or when the file is closed.
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
6.6
This sample program accepts site levels of ground and draws it as sequence of lines. The Road profile is created as single top-most formation line. The code of this program will pave a path for your lengthy program writing in the future. The sample site data are recorded in ASCII file CSr.txt as below. You can write the same data in Notepad using same sequence as shown below (one data per line). This file will be actually an input file for your main AutoLISP program CSRoad.lsp. CSr.txt
1200 7.50 16.0 10 0 12.5 2.5 13.56 3.5 13.2 5.5 14.5 2.25 12.78 4.0 13.05 5.5 11.80 DONT WRITE ANYTHING BEYOND 11.80. THE DATA 11.80 IS THE last line of file CSr.txt. Save it. THE MEANING OF EACH DATA IS EXPLORED AS BELOW. The First data 1200 is Chainage of Cross Section 7.50 is Formation Width of Road 16.0 is Formation Level of Road 10 is Datum Level for drawing CS 0 is CS center Chainage 12.5 is Ground Level at 0 chainage 2.5 is distance on right side for second ground level 13.56 Ground level at 2.5 meter distance 3.5 Distance Right Side 13.2 Ground Level at 3.5 distance 5.5 Distance Right Side 14.5 Ground Level at 5.5 distance 2.25 Distance Left Side 12.78 Ground Level at 2.25 mt 4.0 Distance Left Side 13.05 Ground Level at 4.0 mt
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
5.5 Distance Left Side 11.80 Ground Level at 5.5 mt WRITING OF ACTUAL PROGRAM You may write the following code in CSroad.lsp file using Notepad or Visual LISP editor. The figures appears beside the code are my attempt to link code with actual graphics result. This way you can clearly understand the code and how to write it for particular result.
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
(command "units" 2 2 "" "" "" "") (command "limits" '(0 0) '(40 30)) (command "zoom" "all")
(command "pline" '(0 0) "w" "0.1" "" '(40 0) '(40 30) '(0 30) "c")
(setq stpt (list 25 3.0)) (setq p2 (list 6 3.0)) (setq p3 (list 37 3.0))
(command "line" p2 p3 "") (command "array" "l" "" "r" 6 "" 1.5 )
(setq dp (list 6 15)) (setq dp1 (list (-(car dp) 1.5) (cadr dp))) (setq dp2 (list 37 15)) (setq dp3 (list 6 25)) (setq fw2 (/ tr 2.0)) (setq tp1 (list 1.5 3.6))
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
(setq cnt 1) (while (<= cnt 10) (command "_.color" cnt) (setq dp1 (list (car dp1) (+ (cadr dp1)1))) (setq datum (+ datum 1))
(command "line" gll0 gll1 gll2 gll3 "") (command "line" gll3 "@7<0" "") (command "line" gll0 gll4 gll5 gll6 "") (command "line" gll6 "@7<180" "")
(setq fll (- fl ddummy)) (setq fldp1 (list (car mpt) (+ (cadr dp) fll))) (setq fldp2 (list (car flrpt1) (cadr fldp1))) (setq fldp3 (list (car fllpt1) (cadr fldp1))) (command "_.color" 1)
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
10
My Dear participants, I hope this ATP has answered many questions cropping up in your mind and has generated some more questions for AutoLISP. Use the Course Forum for your remaining questions. Or as my friends in the ATP program like to say:
Remember that this material is only a portion of the class; support is always available online in the private course forum. I encourage you to visit the course forum and ask any questions that you may have
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
11
about this segment or simply join in the discussion. The ATP Mantra is: the only stupid question is the one you dont ask. Thanks again for attending this course!
Reuse of any or all material contained within this document for commercial purposes, without the express written consent of AUGI, Inc. or its authorized agents is expressly prohibited.
Copyright 2004 Autodesk User Group International, Inc. All rights reserved.
12