Author Topic: Newbie to world of (Lost in Stupid Parentheses) errr..(List)  (Read 103771 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #240 on: November 13, 2003, 06:07:48 PM »
Andre, what I just showed is a very bad way to do it. When you get a bit more experienced you will want to verify values before use. Nesting functions that may produce invalid data is not good (if the user hits Enter to any of the GETxxx functions above they will return nil, which will crash POLAR).  I merely showed it as an example to underline that we don't really care about what the radius or angle is. Those are the users choices and all we need to do is to calculate a start point for the user.

If you are determined to solve 2.5 then try out the ARC command in AutoCAD, see in what direction is goes when you specify center point first ("CEnter") and then see how your own center point and start point - aquired via GETPOINT and POLAR - could fit in.

SMadsen

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #241 on: November 13, 2003, 07:04:05 PM »
I'm gonna give the strongest hints I can on this exercise and it may even turn out to be a solution, so whether you want to read it or not is up to you. I recommend reading it, though!


You already know that you need a center point, and you have also shown that you know how to get it:

(setq pnt1 (getpoint "\nSelect Center Point: "))

Great, we are assuming that the user picked a point so now you have a point saved in the variable pnt1. We don't want to know what a point is at this point (no pun intended) - it's just a point that AutoLISP knows how to handle.

The exercise states: "Create a new command that asks for a center point, a radius, a starting angle and an ending angle.", so next step is to get a radius from the user. You know now that it can be done with GETDIST - a function that either accepts a number that is typed directly at the command line or it accepts that the user picks two points on screen from which it calculates the distance automatically. Whatever way it gets the distance, it returns it as a number.

However, if the user decides to pick a distance on the screen, we don't want him to actually pick two points. Why? Because we already know the center point and it would be very natural that the user wants to see the center point when he specifies the radius. So, looking up GETDIST in the help reference, we notice that it can take two arguments - both optional:

(getdist [pt] [msg])

The msg argument, you already know what is - it's the prompt written to the screen. The other argument, pt, is a point that serves as a starting point for the distance being dragged on screen. So, try to insert the center point, pnt1, and see what happens. Try this sequence at the command line in AutoCAD:

Command: (setq pnt1 (getpoint "\nSelect Center Point: "))
Select Center Point: [pick a point]
(9.58282 4.83715 0.0)

Command: (setq rad (getdist pnt1 "\nEnter Radius of arc: "))
Enter Radius of arc: [notice the anchor point, the rubber band and go pick a point]
7.02746

Did you notice that GETDIST anchors the first point at the center point that you just specified? Also, it drew a rubber band between the anchor point and your crosshair cursor? This is exactly what e.g. the LINE command uses to draw a line segment; after giving it the first point and the second point, you don't need to specify a first point again if you stay in the command and decides to draw one more line segment.

Now we have code to get the center point and the radius. The latter even with a nice interface that holds on to the center point and draws a rubberband from it:

(setq pnt1 (getpoint "\nSelect Center Point: "))
(setq rad (getdist pnt1 "\nEnter Radius of arc: "))

Next we want a starting angle. GETANGLE works exactly like GETDIST: it either accepts a number typed at the command line or it accepts two points picked on screen. The only difference between GETDIST and GETANGLE is that GETANGLE returns the angle between the points (alternatively the angle typed at the command line) instead of the distance. GETANGLE can also anchor the first point by giving it a point as an argument. The syntax is no different than GETDIST:

(getangle [pt] [msg])

So, we will again make the assumption that the user would want to drag the angle from the center point - that would be a natural way to specify the angle for an arc. We simply supply the center point again and, of course, your nice prompt:

(setq ang (getangle pnt1 "\nSelect Angle of arc: "))

Oh wait, you had already supplied the point in your code?! Good job! :)

Now we have a center point, a radius and an angle from the center point to the start point. However, we do not know where the start point is. When the user dragged the radius, it didn't need to be the start point of the arc that he specified. Nor is it for certain that he specified the start point when dragging the angle.
If either really was the intended start point, then we have no way of intercepting the point a which he clicked because we used GETDIST and GETANGLE and they do not return a point. But we don't care. Why? Because we have data enough to calculate it!

POLAR takes a point, an angle and a distance. When you have those data, it is geometrically possible to calculate a unique point, and that is what POLAR does. It calculates the point that lies at x angle and in y units from the point. So giving POLAR our center point, our angle and our distance, it spits out the start point of the arc:

(setq pnt2 (polar pnt1 ang rad))

All we have to do now is to start the ARC command through AutoLISP, invoke the right choices in the command and feed it the data. This is where you should run ARC in AutoCAD to make sure that you get the sequence right.

Let's try it and let us use the exclamation mark to call out those variables that we setq'ed previously. First we would want to specify the center point, so in ARC we call the subcommand "CE" to make it accept the center point.
Then we give it the center point and notice that it will want a start point. Oh my, we do have start point! Cool. Give it the start point and then .. umm, then what? Then nothing. Let the user finish the command by letting him specify an end point.

Command: arc
Specify start point of arc or
: CE
Specify center point of arc: !pnt1
(9.58282 4.83715 0.0)
Specify start point of arc: !pnt2
(14.592 9.76599 0.0)
Specify end point of arc or [Angle/chord Length]: [pick an end point on screen]

And there you have a nice arc drawn with the specifications that we asked of the user. Only thing that remains is to write this with the help of the COMMAND function in AutoLISP. Oh, and to arrange all the expressions that we wrote into a DEFUN.

Will you do that, Andre?

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #242 on: November 13, 2003, 09:09:08 PM »
You bet...
Good morning all...
Well after Smadsen explanation made things a lot more sense to me..

Here's is what i came up with after reading his previous post.

Code: [Select]

(defun c:arctest (/ pnt1 rad ang pnt2 cmd)
  (setq pnt1 (getpoint "\nSelect Center Point: "))
  (setq rad (getdist pnt1 "\nEnter Radius of arc: "))
  (setq ang (getangle pnt1 "\nSelect Angle: "))
  (setq pnt2 (polar pnt1 ang rad))
  (setq cmd (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (command "arc" "c" pnt1 pnt2 pause)
  (setvar "cmdecho" cmd)
  (princ)
)


I was getting stuck on the ( Start and Ending of Angle ), i know it sounds dumb but that part was confusing me like crazy.

And another thing.. I'm very happy that Smadsen made somethin look very interesting to me.. The last part of his post, running through the "ARC" command with the variables as opposed to just doin the command without them.. It made a lot more sense when i saw it that way..

Thanks,

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #243 on: November 14, 2003, 10:29:00 AM »
Any other exercises for me to work on would be great?

SMadsen

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #244 on: November 14, 2003, 10:51:44 AM »
Very well done!! Glad you finally did it, Andre.

I know angles can be hard to get a grip on at this stage, but do you see now that the finished code doesn't really care about the angles? Well, as an argument to POLAR, yes, but it doesn't use angles for anything else than that. They could be measured in Newton or in nautical miles for all we care. All the code cares about is to set up communication with the user and prepare data in order to feed a frequently used AutoCAD command.

Anyway, excellent solution. Everything is in right order and local variable declarations are remembered. Even saving and restoring CMDECHO is there. Good job.

SMadsen

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #245 on: November 14, 2003, 10:57:55 AM »
Well, you could write a function that makes a rectangle from two diagonally picked points. That exercise is a classic. It would keep you in the current line of thinking and still let you explore something new, like lists!

The description would be:
Ask the user for a lower left corner and an upper right corner of a rectangle. Write code that calculates the two missing corners and draws a closed polyline using all four corners to form the rectangle.

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #246 on: November 14, 2003, 11:03:16 AM »
Thanks Smadsen,

I'll go over Lists in my book and see how it works and i'll post any questions or comments after i'm done..

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #247 on: November 14, 2003, 11:52:21 AM »
Smadsen here's what i got..

Code: [Select]

(defun c:REC (/ pnt1 pnt2 pnt3 pnt4 cmd)
  (prompt "\nThis will create a Rectangular Pline ")
  (setq pnt1 (getpoint "\nSelect Starting Point: "))
  (setq pnt2 (getcorner pnt1 "\nSelect Opposite Corner: "))
  (setq pnt3 (list (car pnt2) (cadr pnt1)))
  (setq pnt4 (list (car pnt1) (cadr pnt2)))
  (setq cmd (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (command "pline" pnt1 pnt4 pnt2 pnt3 "c")
  (setvar "cmdecho" cmd)
  (princ)
)


What do you think?
Pretty impressive i must say... i surprised myself by actually doin this.

daron

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #248 on: November 14, 2003, 12:06:40 PM »
That's good. Can you make a rectangle at any angle using two points for width and a third point or user input for length?

SMadsen

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #249 on: November 14, 2003, 12:30:09 PM »
Quote from: amgirard2003
What do you think?
Pretty impressive i must say... i surprised myself by actually doin this.

Pretty darn impressive! Ok, be honest, where did you get that code? :D

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #250 on: November 14, 2003, 01:11:23 PM »
Quote from: Daron
That's good. Can you make a rectangle at any angle using two points for width and a third point or user input for length?


O.k Need help with this one..getting lost...
Can't figure out how to do this one..

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #251 on: November 14, 2003, 01:12:41 PM »
Quote from: SMadsen
Quote from: amgirard2003
What do you think?
Pretty impressive i must say... i surprised myself by actually doin this.

Pretty darn impressive! Ok, be honest, where did you get that code? :D


Out of my brain baby!!!  :lol:
I read about lists last night so it was still fresh in my head..

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #252 on: November 14, 2003, 01:31:04 PM »
O.k I started with the following

Code: [Select]
(defun c:rec ()
 (Prompt "\Pline Rectangle" )
 (setq len (getdist "\nEnter Length: "))
 (setq wid (getdist "\nEnter Width: "))
 (setq pnt1 (getpoint "\nEnter Starting Point: "))
 (setq ang (getangle pnt1 "\nEnter Angle: "))

 

That's how far i got so far..
don't know where and how to use the width and length  variables..

daron

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #253 on: November 14, 2003, 04:24:23 PM »
Let's see if I can spell out what to do.
-user selects ponit for start of rectangle - pnt1
-user needs length of rectangle.
   method one - user could type the length
   method two - user could use second from pnt1 as length
-user needs width or height of rectangle.
   same as above
-program needs to calculate the angle between pnt1 and pnt2 to get the angle from pnt1 and pnt2 to pnt4 and pnt3, respectively.
Does that help?

amgirard2003

  • Guest
Newbie to world of (Lost in Stupid Parentheses) errr..(List)
« Reply #254 on: November 17, 2003, 08:37:32 AM »
Mornin, How's everyone after a fine weekend..

I'm working on the following:

Quote
Can you make a rectangle at any angle using two points for width and a third point or user input for length?


Code: [Select]
(defun c:REC (/ pnt1 pnt2 pnt3 pnt4 cmd)
  (prompt "\nThis will create a Rectangular Pline ")
  (setq pnt1 (getpoint "\nSelect Starting Point: "))
  (setq wid (getdist pnt1 "\nEnter Width: ")) (terpri)
  (setq len (getdist pnt1 "\Enter Length: ")) (terpri)
  (setq ang (getangle pnt1 "\nSelect Angle: "))
  (setq pnt2 (polar pnt1 ang len)
  (setq pnt3 (polar pnt2 (+(angle pnt1 pnt2) (/ pi 2)) wid))
  (setq pnt4 (polar pnt3 (+(angle pnt1 pnt2) (/ pi 2)) wid))
  (setq cmd (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (command "pline" (pnt1 pnt2 pnt3 pnt4 "c"))
  (setvar "cmdecho" cmd)
  (princ)
)


This is what i've come up with but it's not working..
Entering of the Length and Width is confusing me.. i can't figure out how to apply this to the polar function..

Thanks for your help