In continuation of part-2 of my article on same topic we will discuss some more autolisp functions and there use here.
Reading and appending files: See the following example:
(setq f(open “aaa.txt” “r”))
This statement will open the file called aaa.txt for reading
(read-line f )
As we already written the first line as “I love you”, this statement will return that line.
This will close the file.
Appending is very similar to writing except the fact that for appending the file need to be exist already and for writing if the file does not exist already autolisp will create it at its installation directory (most of the cases at “C:\Program Files\ACAD2000”)
(setq f(open “aaa.txt” “a”))
This statement will open the file called aaa.txt for appending.
(write-line ”addition of line” f )
As we already written the first line as “I love you”, this statement add the “addition of line “to the file.
This will close the file.
Now in the above example if we could have opened it for writing instead of appending, then the existing text would have been overwritten by the newly added text.
Let us consider the following example:
(setq f (open “aaa.txt” “w”))
(write-line “I love u” f)
If you have a file named aaa.txt at any location (say at “C:\MAIN DATA\aaa.txt” )other than your AutoCAD installation location (say, “C:\program files”), then the above program will not take the file which is have at “C:\MAIN DATA” instead it will create a file named “aaa.txt” at , “C:\program files” and start writing in it. To avoid this we can modify the above code as below:
(setq file (findfile "aaa.txt"))
(setq f (open file "a"))
(write-line "I love u" file)
In this case it will first find out where in computer the file named “aaa.txt” is already there and then start operation on it.
Functions used for getting user inputs (get functions): These functions are very important in terms of getting user inputs for your autolisp programs. Program execution will pause for user inputs for these functions.
Syntax: (getangle [point] [message])
Point: If specified in program it will be considered as first point of the two points for specifying angle. User has only to specify the second point to specify angle. If this field has not been specified then user has to give both the two points. Please note that angle will be measured with respect to “x” axis and on the current working plane.
Message: the statement at this field will be prompted to user.
(setq a (getangle))
In this case user has to type the value of angle in degree and autolisp will take the value in radian.
(setq a (getangle ‘(2 3)))
Here autolisp will take the point (2,3) as the first point of the line which will define the angle with respect to “X” axis of present working plane. User has to provide the second point in order to define the line.
(setq a (getangle "Which way? "))
In this case autolisp will prompt showing the line “which way”, then after user specify the first point it will ask for second point to define the angle.
(setq a (getangle '(2 3) "Which way? "))
In this case autolisp will consider first point as (2,3) and ask user for the second point.
Syntax: (getpoint [pt] [msg])
(setq pnt (getpoint))
AutoCAD will stop for user’s curser click to locate point.
(setq pnt (getpoint "locate? "))
AutoCAD will show a prompt as “locate?” to the user for getting mouse click.
(setq pnt (getpoint '(10.5 20.0) "Second point is "))
Here AutoCAD will take the specified point as base point and calculate the location of the second point from the mouse click.
Please note that whenever you will specify a co-ordinate of a point you have to use ‘ before the point co-ordinate.
Nowadys, many 3D CAD and FEA packages are available for helping out the mechanical design engineers but still, AutoCAD is used extensively in the industries. By means of AutoCAD customization using autolisp you will be able to automate the drawing creation process. Thus you can reduce the design time.
This post is part of the series: Autocad customization
- AutoCAD Customization using Autolisp – Part 1
- AutoCAD Customization using autolisp (part 2)
- AutoCAD Customization using autolisp (part 3)