Need help understanding some errors in my code in AutoLisp

Need help understanding some errors in my code in AutoLisp

Anonymous
Not applicable
917 Views
2 Replies
Message 1 of 3

Need help understanding some errors in my code in AutoLisp

Anonymous
Not applicable

Hello all,

 

I'm currently learning AutoLISP and as such I'm working on a few practice assignments. I've got code that needs to plot a rectangle given user inputs and then rotate that triangle based on a rotation angle specified by the user. However, the dimensions cannot exceed 15 x 10 inches. This is where I run into my problem is with my conditionals.

 

With the following code, I've got the "barebones" up and running. Meaning that the program can draw the rectangle and rotate it; so that part doesn't need help. 

 

I'll attach my code for those who would like to take a look at it.

0 Likes
Accepted solutions (1)
918 Views
2 Replies
Replies (2)
Message 2 of 3

Kent1Cooper
Consultant
Consultant
Accepted solution

A few things I notice....

 

Your checks on the dimensions where they are first entered are really not needed, since their value is checked again later to decide whether to draw and Rotate the rectangle.  [And I would avoid using the word "plot" for the drawing of it, since that has a specific and different meaning in AutoCAD.]  But even if you have reason to check them initially, the checks don't do anything differently if the tests are satisfied than if they are not -- the W1 and H1 values remain whatever was entered, regardless.  You could structure them differently, e.g.:

(setq W1 (if (<= W1 15) W1))

 

That will wipe W1 out [set it to nil] if it's too big, because the (if) function will return nil.  Then, to verify valid values later, it would need to check merely whether both values exist at all, rather than whether they're within range:

 

(if (and W1 H1)
  (progn
    (command .....

 

But I would suggest a different approach:  check whether the value is within range, and if not, ask the User again to enter a valid value, and keep asking until they do:

 

(while

  (not

    (and

      (setq W1 (getdist "\n Enter the width of the rectangle: "))

      (<= W1 15)

    ); and

  ); not

  (prompt "\nValue must be no greater than 15.")

); while

 

Other possibilities:  you could make it forbid the specifying of 0 or a negative value [read about (initget)], but maybe you want the possibility of negative values to build the rectangle from upper right instead of from lower left.

 

For the angle of Rotation, I would suggest using (getangle) rather than (getreal), because the User has several additional options for specifying it that (getreal) won't accept.  They can pick two points on-screen, or they can type in something like 125d32'11", or (atan 0.5) [look into the (atan) function].  However, that will mean the value in A1 will be in radians, so that will need to be converted to degrees in the Rotate command.

 

For the drawing of the rectangle, you can use the RECTANGLE [or just RECTANG] command with two opposite corners, rather than calculating all four corners and using PLINE.  The result will be a Polyline anyway.  [It can be done with only the PT1 variable, not needing pt2/pt3/pt4, but I'll show it below using pt3, which can be calculated from PT1 without the intermediary of pt2, but another time....]  Also, you can combine more than one command into one (command) function.

 

And if you're not going to turn running Object Snap off, it's good to put None Osnap before given points.

 

So, for example [untested]:

 

(if (and W1 H1 A1)

  (command ; then

    "_.rectang" "_none" PT1 "_none" pt3

    "_,Rotate" "Last" "" "_none" PT1 (* (/ A1 pi) 180)

  ); command

); if

 

But in reality, if you force the User to give valid values, there should be no need to test whether they're there, because it won't get that far without them.

Kent Cooper, AIA
0 Likes
Message 3 of 3

Anonymous
Not applicable

Wow Kent, that was quite a bit of helpful information, thank you!

 

Although you hit the nail on the head and I suspected it was the culprit was me specifying the values early on the code. I deleted those lines and the code runs as the assignment specifies. However, I will definitely look into all the other information you gave. 

0 Likes