Author Topic: First try at lisp  (Read 5186 times)

0 Members and 1 Guest are viewing this topic.

jcoon

  • Newt
  • Posts: 157
First try at lisp
« on: February 10, 2015, 01:16:14 PM »
My first trying at lisp and needed a few samples to test.

I'm looking for a sample that starts like (drawbaseline) below but I want to be able to generate points (coordinates) along (p1) to (p2) and at 90 degrees left and right of the start point (p1) and direction (p2) see attached image.  my end sample will draw geometry connected to the points. what I've read so far, If I know the number of points that I want to create I need to create a variable for each point so that I can then assign the newly generated point to these variables. Also can you I set points in the negative direction to the baseline.

Hopefully with these sample I'll be able to test a lot of different ideas.

Thank you
John 

defun c:DRAWBASELINE ()
  (prompt "\nPick points to draw a line.")
  (if (and (setq p1 (getpoint "\nPick the first point."))
           (setq p2 (getpoint p1 "\nPick the second point For Direction."))
      )
    (command "._line" p1 p2 "")

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
Re: First try at lisp
« Reply #1 on: February 10, 2015, 01:39:08 PM »
Are B, C, E, & F the same?
Is A or D a constant?
You're going to need a few getdistance calls to define anything more than a line.
Localize those variables c:DRAWBASELINE (/ p1 p2)
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: First try at lisp
« Reply #2 on: February 10, 2015, 02:09:53 PM »
Below is a code tutorial to step you through the process of writing this program -
Please take some time to read the comments carefully and understand why each function is used at each step. If something is unclear, feel free to ask for clarification.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:myfirstprogram ( / a b c d e f ang 90d pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8 )
  2.     ;; Define function & declare local variables
  3.     ;; To understand why the declaration of local
  4.     ;; variables is an important practice, see http://bit.ly/15Qw104
  5.  
  6.     ;; Define some local variables for later use in the program
  7.     ;; These values are arbitrary and are just based on the image provided.
  8.     (setq a 10
  9.           b 10
  10.           c 10
  11.           d 30
  12.           e 10
  13.           f 10
  14.     ) ;; end SETQ
  15.    
  16.     (if
  17.         ;; If the following test expression returns a non-nil value
  18.         ;; Note that the term 'non-nil' is used as opposed to
  19.         ;; strictly 'T' since any value not equal to nil will
  20.         ;; validate the IF test expression.
  21.        
  22.         (and
  23.             ;; AND will continue to evaluate the enclosed
  24.             ;; expressions until either no expressions remain
  25.             ;; or an expression returns nil.
  26.             ;;
  27.             ;; In short, all of the enclosed expressions must
  28.             ;; return a non-nil value for AND to return T
  29.  
  30.             (setq pt1
  31.                 ;; Bound the value returned by the following
  32.                 ;; expression to the symbol 'pt1'
  33.  
  34.                 (getpoint "\nSpecify the 1st point: ")
  35.                 ;; Prompt the user to specify a point or pick a point on screen.
  36.                 ;; If the user fails to provide a point (e.g. by pressing
  37.                 ;; 'Enter' at the prompt), GETPOINT will return nil, causing
  38.                 ;; the SETQ expression to return nil, causing AND to return nil,
  39.                 ;; which invalidates the IF test expression, causing the 'else'
  40.                 ;; argument to be evaluated.
  41.  
  42.             ) ;; end SETQ
  43.  
  44.             (setq pt2
  45.                 ;; Bound the value returned by the following
  46.                 ;; expression to the symbol 'pt2'
  47.  
  48.                 (getpoint "\nSpecify the 2nd point: " pt1)
  49.                 ;; As above, however the second argument for GETPOINT
  50.                 ;; is provided to display a 'rubber band' to the first point
  51.                 ;; specified.
  52.                 ;;
  53.                 ;; We know that the local variable 'pt1' must contain a valid
  54.                 ;; point, as otherwise the first GETPOINT expression would
  55.                 ;; have returned nil, causing the AND function to cease
  56.                 ;; evaluation of the expressions passed to it
  57.                 ;; (this is the beauty of Special Forms in LISP).
  58.  
  59.             ) ;; end SETQ
  60.  
  61.         ) ;; end AND
  62.  
  63.         ;; End of test expression.
  64.         ;; The following is now the 'then' expression for the IF function
  65.  
  66.         (progn
  67.             ;; PROGN simply evaluates all supplied expressions and
  68.             ;; returns the value returned by the last evaluated expression.
  69.             ;; This may seem redundant, but it means that we can evaluate
  70.             ;; multiple expressions within the PROGN expression and pass
  71.             ;; the PROGN expression as a single argument to the IF function
  72.  
  73.             (setq
  74.  
  75.                 ang (angle pt1 pt2)
  76.             ;; Calculate the angle (in radians) between the x-axis
  77.             ;; and a line/vector spanning pt1 to pt2.
  78.  
  79.                 90d (/ pi 2.0)
  80.             ;; pi/2 radians is equal to 90 degrees
  81.             ;; I have defined this as a local variable as we will use
  82.             ;; this value repeatedly and I wanted it to be clearer as to how
  83.             ;; it is used in the code.
  84.  
  85.                 pt3 (polar pt1 ang a)
  86.             ;; Calculate the coordinates of 'pt3':
  87.             ;; This point lies a distance 'a' from 'pt1' at an
  88.             ;; angle parallel to the angle between 'pt1' & 'pt2'
  89.  
  90.                 pt4 (polar pt3 (- ang 90d) c)
  91.             ;; Calculate the coordinates of 'pt4':
  92.             ;; This point lies a distance 'c' from 'pt3' at an
  93.             ;; angle perpendicular to the angle between 'pt1' & 'pt2'
  94.             ;; Since POLAR measures angle in a counter-clockwise direction
  95.             ;; with zero radians at the x-axis, subtracting an angle will
  96.             ;; turn us in a clockwise direction about 'pt3'
  97.  
  98.                 pt5 (polar pt3 (+ ang 90d) b)
  99.             ;; Calculate the coordinates of 'pt5':
  100.             ;; As above, however, since we are now adding the angle,
  101.             ;; we are turning in a counter-clockwise direction about
  102.             ;; the base point argument 'pt3'
  103.  
  104.                 pt6 (polar pt3 ang d)
  105.             ;; Calculate the coordinates of 'pt6':
  106.             ;; Point 'pt6' is a distance 'd' from 'pt3' along the
  107.             ;; same angle as 'pt1' to 'pt2'.
  108.  
  109.                 pt7 (polar pt6 (- ang 90d) f)
  110.             ;; Calculate the coordinates of 'pt7':
  111.             ;; ( As 'pt4' )
  112.  
  113.                 pt8 (polar pt6 (+ ang 90d) e)
  114.             ;; Calculate the coordinates of 'pt8':
  115.             ;; ( As 'pt5' )
  116.  
  117.             ) ;; end SETQ
  118.  
  119.             ;; Now to construct some lines/polylines
  120.             ;; Here we could store the value of the CMDECHO system variable
  121.             ;; and set this variable to zero so that the following is
  122.             ;; not printed to the command-line, but as this is a beginner's
  123.             ;; program it might be worth seeing what is being issued to the
  124.             ;; command-line.
  125.  
  126.             ;; Issue the following statements at the AutoCAD command-line:
  127.             (command
  128.  
  129.                 "_.line"
  130.                 ;; Invoke the AutoCAD LINE command
  131.                 ;; To understand why "_." is used, see http://bit.ly/1d2jfB3
  132.  
  133.                 "_non"
  134.                 ;; Ignore any active Object Snap modes when issuing the next point
  135.                 ;; We could alternatively store the value of the OSMODE
  136.                 ;; system variable and temporarily set this system variable to
  137.                 ;; zero, however, we would then also need to include an error handler
  138.                 ;; to ensure the system variable is reset to its original value
  139.  
  140.                 pt1
  141.                 ;; Line start point (in UCS)
  142.  
  143.                 "_non"
  144.                 ;; As above
  145.  
  146.                 pt2
  147.                 ;; Line end point (in UCS)
  148.  
  149.                 ""
  150.                 ;; End the LINE command
  151.                 ;; ("" is equivalent to the user pressing ENTER)
  152.  
  153.                 "_.pline"
  154.                 ;; Invoke the AutoCAD PLINE command
  155.  
  156.                 "_non" pt4 "_non" pt7 "_non" pt8 "_non" pt5
  157.                 ;; Issue the calculated points in the appropriate order
  158.  
  159.                 "_C"
  160.                 ;; Close the polyline
  161.  
  162.             ) ;; end COMMAND
  163.  
  164.         ) ;; end PROGN
  165.  
  166.         ;; End of 'then' expression
  167.         ;; The following is where we would place the 'else' expression for the IF function
  168.         ;; however an else argument is not necessary in this case (unless perhaps the
  169.         ;; user should be notified that they have not specified two points)
  170.        
  171.     ) ;; end IF
  172.  
  173.     (princ)
  174.     ;; Suppress the return of the last evaluated expression
  175.  
  176. ) ;; end DEFUN
« Last Edit: February 11, 2015, 01:18:09 PM by Lee Mac »

jcoon

  • Newt
  • Posts: 157
Re: First try at lisp
« Reply #3 on: February 10, 2015, 04:15:49 PM »
Not sure what happen to my reply.


tombu, yes those values are the same but the detail. In the routine it could be anything. I was thinking more like a old survey setup on this point with the direction of p1 to p2 turn angle..
like that.

Lee Mac

W O W, Thank you....that looks like I got everything in one shot. I've printed it out the sample to see if I can follow what you have done. Does lisp have any debug functions or could I paste this into vlisp so I could view the variables as it loops thru the sample code.

again Thank you.

Have a great day
John 

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: First try at lisp
« Reply #4 on: February 10, 2015, 05:26:06 PM »
Lee Mac

W O W, Thank you....that looks like I got everything in one shot.

You're very welcome John  :-)

I've printed it out the sample to see if I can follow what you have done. Does lisp have any debug functions or could I paste this into vlisp so I could view the variables as it loops thru the sample code.

Excellent, I hope my comments are clear to follow & understand.

If you wish to follow the code during evaluation, I would suggest pasting the code into the Visual LISP IDE (VLIDE), add a few variables to the Watch Window and then use the animate functionality to view the values of each variable as the code steps through each expression (these steps are described in my tutorial here) - though, since the variables in the code hold constant values throughout the program (that is, no variable is redefined), there is little to gain from viewing the variable values other than to perhaps see the format of the data held by each variable.

I think more can be gained from understanding each expression: that is, knowing exactly what each argument corresponds to, and being able to predict what the function will return with the given arguments.

Lee

jcoon

  • Newt
  • Posts: 157
Re: First try at lisp
« Reply #5 on: February 10, 2015, 05:45:26 PM »
Lee Mac

I'll try to follow your comments and view sample in Visual LISP thanks to your link

have a great day

John

jcoon

  • Newt
  • Posts: 157
Re: First try at lisp
« Reply #6 on: February 11, 2015, 09:37:04 AM »
Lee Mac,

Went thru your comments.....those were great. I could follow the entire routine. I'll try one based on that during lunch.
Your comment & code sample looks like an old survey setup program. create this point, set up or occupy point here, turn angle and dist. here.

Thank you. Have a great day,

John

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: First try at lisp
« Reply #7 on: February 11, 2015, 09:53:33 AM »
Below is a code tutorial to step you through the process of writing this program -
Please take some time to read the comments carefully and understand why each function is used at each step. If something is unclear, feel free to ask for clarification.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:myfirstprogram ( / a b c d e f ang 90d pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8 )
  2.     ;; Define function & declare local variables
  3. ...

Excellent post.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: First try at lisp
« Reply #8 on: February 11, 2015, 10:44:13 AM »
Below is a code tutorial to step you through the process of writing this program -
Please take some time to read the comments carefully and understand why each function is used at each step. If something is unclear, feel free to ask for clarification.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:myfirstprogram ( / a b c d e f ang 90d pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8 )
  2.     ;; Define function & declare local variables
  3. ...

Excellent post.
Indeed it is. Will save this for later!!
TheSwamp.org  (serving the CAD community since 2003)

Bethrine

  • Guest
Re: First try at lisp
« Reply #9 on: February 11, 2015, 11:10:13 AM »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: First try at lisp
« Reply #10 on: February 11, 2015, 01:24:42 PM »
Lee Mac,

Went thru your comments.....those were great. I could follow the entire routine. I'll try one based on that during lunch.
Your comment & code sample looks like an old survey setup program. create this point, set up or occupy point here, turn angle and dist. here.

Thank you. Have a great day,

John

Excellent - I'm delighted that my comments were clear and that you were able to follow the process - I look forward to seeing you develop your own programs!

Of course, the use of the polar function to calculate the coordinates of each point is just one way to tackle the problem (the easiest way perhaps); another way might be to define a UCS based on the rotation of the initial two points and then calculate the positions of the remaining points by altering the X & Y values of each point, but I think the use of polar is easier to follow when learning.

Below is a code tutorial to step you through the process of writing this program -
Please take some time to read the comments carefully and understand why each function is used at each step. If something is unclear, feel free to ask for clarification.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:myfirstprogram ( / a b c d e f ang 90d pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8 )
  2.     ;; Define function & declare local variables
  3. ...
Excellent post.
Excellent post.
Indeed it is. Will save this for later!!
+1 ...er...2?

Many thanks all!  :-)

jcoon

  • Newt
  • Posts: 157
Re: First try at lisp
« Reply #11 on: March 01, 2015, 12:26:10 PM »
Help with syntax:

When setting a variable like dist1,with (setq dist1 (getdist "\nEnter Total distance Inner Width:")) do you always have to include the variable in the
(defun c:myfirstprogram ( / a b c d e f dist1 ang 90d pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8 ) or can they be insert on the fly. Lee's sample to set pt4 from variable
 c, I'd like to prompt for dist1 var.

currently I get ; error: syntax error when I add the(setq dist1 (getdist "\nEnter Total distance Inner Width:")) section, but If I add
pt3 (polar pt1 ang (getdist "\n Pick points from pt1 to pt3 or enter distance: ")) it creates pt4 with the variable from the prompt. I guess overwriting the
setq value set in the  Define some local variables for later use in the program section. I've read my online links but I have not found one that shows or describes
 



;; Get prompt value to set dist from pt3 to pt4 with command line return:
               ;;Lee Mac sample    pt4 (polar pt3 (- ang 90d) c)
               (setq dist1 (getdist "\nEnter Total distance Inner Width:")) ;; I'd like to use this prompt to set pts 4 & 5 from Lee's sample
               pt4 (polar pt3 (- ang 90d) dist1)

Thanks for any links or comments
John

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: First try at lisp
« Reply #12 on: March 01, 2015, 01:26:21 PM »
Hi John,

The syntax error is because you are inserting a setq expression within the existing setq expression.

For example, your code:
Code: [Select]
pt4 (polar pt3 (- ang 90d) c)
(setq dist1 (getdist "\nEnter Total distance Inner Width:")) ;; I'd like to use this prompt to set pts 4 & 5 from Lee's sample
pt4 (polar pt3 (- ang 90d) dist1)

Is equivalent to doing this:
Code: [Select]
(setq var1 1.0 (setq var2 2.0) var3 3.0)
To avoid the error, simply remove the nested setq expression:
Code: [Select]
pt4 (polar pt3 (- ang 90d) c)
dist1 (getdist "\nEnter Total distance Inner Width:") ;; I'd like to use this prompt to set pts 4 & 5 from Lee's sample
pt4 (polar pt3 (- ang 90d) dist1)

However, since you are prompting the user for the value using the getdist function, there is the possibility that the user may respond to the prompt by pressing ENTER without entering or picking a valid numerical value, hence, you may want to insert the setq expression within the AND expression which forms the test expression for the IF statement - this way, the test expression will not be validated if the user fails to provide a valid value, and the program will consequently exit without error under such circumstances.

You should still include the dist1 symbol within the list of local variables following the forward slash in the defun expression, otherwise this symbol will become a global variable and will retain its value after the program has completed evaluation, which can lead to unintended consequences - for more information, see my tutorial here.
« Last Edit: March 01, 2015, 01:33:18 PM by Lee Mac »

jcoon

  • Newt
  • Posts: 157
Re: First try at lisp
« Reply #13 on: March 02, 2015, 12:06:44 PM »
Lee Mac,

Thank you again for the description of what I was doing wrong. I looked for samples declaring or setting variable with the setq function so that's why I was trying to add it in my sample.
This should take me a couple more days before I get in trouble again. I'll read you  attachment.

Thank you very much for your help.

John

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: First try at lisp
« Reply #14 on: March 02, 2015, 12:21:35 PM »
You're very welcome John - if anything is unclear, feel free to ask.

Lee