Saturday, November 30, 2013

AutoLISP And Finding A Direction 2

Introduction


In the previous post I promised you that here you'll get my AutoLISP program. Well. Here it is. With an explanation and how it works.


The AutoLISP program


(defun c:fnddr (/ dr e1 e2 e3 e4 e5 e6 ls p1 p2
p3
)
(start)
(setq p1 (getpoint "\nP1: ")
p2 (getpoint "\nP2: " p1)
)
(command "line" p1 p2 "")
(while (setq p3 (getpoint "\nP3: " p2))
(command "line" p2 p3 "")
(setq dr (fnddr p1 p2 p3))
(princ "\nDirection: ")
(princ dr)
(setq p1 p2
p2 p3
)
)
(endpr)
)


(defun start (/ ss)
(setvar "cmdecho" 0)
(textscr)
(command "limits" (list 0 0)
(list 420 297)
)
(command "snap" 10)
(command "grid" 10)
(setq ss (ssget "c" (list 0 0)
(list 420 297)
)
)
(if ss
(command "erase" "all" "")
)
(command "zoom" "all")
(command "zoom" "0.8x")
)


(defun fnddr (p1 p2 p3 / a1 a2 an dr)
(setq a1 (angle p1 p2)
a2 (angle p2 p3)
)
(if (> a1 pi)
(setq a2 (+ a2 (* pi 2)))
)
(princ "\nA1: ")
(princ (antdg a1))
(princ "\nA1 + pi: ")
(princ (antdg (+ a1 pi)))
(princ "\nA2: ")
(princ (antdg a2))
(if (and (< a1 a2)
(> (+ a1 pi) a2)
)
(setq dr "left")
(setq dr "right")
)
dr
)


(defun antdg (an)
(* (/ an pi) 180)
)


(defun endpr ()
(setvar "cmdecho" 1)
(princ)
)


(c:fnddr)


What The Program Does


Copy the program to a text file, save it under the name FNDDR.LSP, and run the program in AutoCAD or any other suitable CAD program.


The program starts right away. There is asked to pick a point in the screen. That point is P1. Next there is asked to pick point P2.


A line is drawn from P1 to P2. And then there is asked to pick point P3. Again a line is drawn. This time from P2 to P3.


The angles of the line from P1 to P2 and the angle of the line from P2 to P3 is printed in the screen. Also the direction is given.


Again there is asked for point P3. If you want to stop with the program, then press the right mouse button or the ENTER key.


An Example Of The Program


See the picture. It gives an example of how the program works. Eight lines have been drawn. The angles and direction are given.


How The Program Works


The AutoLISP program start with the START function. You already know how that function works. No explanation.


There is asked for the points P1 and P2. And a line is drawn between these two points. And then a WHILE loop starts.



The WHILE loop continues as long as the point P3 is picked. In the WHILE loop a line is drawn and the direction is found.


The line is drawn from point P2 to point P3. And the direction is found in the function FNDDR. I talk about it later.


The direction is printed in the screen. After that the value of the points P1 and P2 are changed. P1 becomes P2 and P2 becomes P3.


The FNDDR Function


Here is the FNDDR function.


(defun fnddr (p1 p2 p3 / a1 a2 an dr)
(setq a1 (angle p1 p2)
a2 (angle p2 p3)
)
(if (> a1 pi)
(setq a2 (+ a2 (* pi 2)))
)
(princ "\nA1: ")
(princ (antdg a1))
(princ "\nA1 + pi: ")
(princ (antdg (+ a1 pi)))
(princ "\nA2: ")
(princ (antdg a2))
(if (and (< a1 a2)
(> (+ a1 pi) a2)
)
(setq dr "left")
(setq dr "right")
)
dr
)


(defun antdg (an)
(* (/ an pi) 180)
)


The angle between the points P1 and P2 is found and the angle between the points P2 and P3. Those angels are stored in the variables A1 and A2.


If the angle A1 is more than 180 degrees, then the value is increased with 360 degrees. That is done with the IF function.


The angles are written down and the direction of the second line is found. The area of the second angle is checked.


We also have the ANTDG function. That function is used to turn angels in radians into angels in degrees.


The Complete AutoLISP Program


Very good. Now you have got the complete AutoLISP program. With an explanation of it and an explanation of how it works.




Telling Others


Do you like what you have read? Do you know other people, that could be interested? Could you tell them about this blog?




Comment


In the mean time. If you have any questions. Or you want to tell me something. Feel free to add a comment to this blog.


I would love to hear from you. And when you come to me with a comment. I will give a reply to your question or remark.




Warning


Don't spend a lot of money on AutoCAD Light. It is only for creating 2D drawings and it doesn't support AutoLISP.


There is a very similar CAD program. And that is completely free. The name of the program? DraftSight.


How to get it? Do a search on Google for “download DraftSight”. And you will find where you can download the program.






Only Create Drawings


When your CAD operators don't create the borders of your drawings, a lot of time is saved. You get your drawings much faster.


I have created a program that does exactly that. An AutoCAD drawing has been created and the program draws a border around it.


If you want to see how the program works, go to YouTube. You'll find the working of the program here:


http://www.youtu.be/O8Zy6n9zS8Q


Now the AutoLISP program gives you the choice to select the size of your border. You can select A4, A3, A2, or A1.


Maybe you already know the size of the border of your drawing. Maybe all your drawings have the size A1. If so. Let me know.


That is important. The program finds the proper scale of your drawing. Or do you already know what the scale always is?


I can write the AutoLISP program that you want. The price? I will only charge you RM 600.-. That is less than US $ 200.-.






You are welcome to publish my article provided you include the resource box with links intact.




Resource Box


Jos van Doorn is an AutoLISP programmer and blogger. He is originally from Holland and now he lives in Malaysia.



He is the founder of the Make AutoCAD Fast business. Make AutoCAD Fast is writing AutoLISP programs for AutoCAD users.



Make AutoCAD Fast created an AutoLISP program for drawing a border around AutoCAD drawings. The program can be found on YouTube.
http://www.youtu.be/O8Zy6n9zS8Q



Jos is writing a blog. In it are AutoLISP programs with information and it is about Make AutoCAD Fast. You can find his blog here:


http://www.makeautocadfast.blogspot.com


Maybe you have a question. Or want to tell me something. You can contact me on this e-mail address:


makeautocadfast@ymail.com


This article may be freely reprinted or distributed in its entirety in any Ezine, newsletter, blog, or website. As long as the following resource box is added:


-------------------------------------------------
This article is written by Jos van Doorn. He is an AutoLISP programmer who has helped engineering firms and architects to get their AutoCAD drawings fast, without waiting for hours.


You can find more valuable information about his
business and AutoLISP at his blog. It is here:


http://www.makeautocadfast.blogspot.com


If you want to contact him. Go to his blog and leave a comment.
-------------------------------------------------


No comments:

Post a Comment