Author Topic: AutoLISP routine help!!!! (by collegeCAD)  (Read 21601 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« on: September 29, 2003, 02:15:18 PM »
I'm stealing me a new member, I think. So, this is from a guy over at Cadenceweb. So, I'm adding all of our correspondence to this page.
Membername: collegeCAD
Post 1:
Quote
I have an autolisp programming class that I am taking. Our first project is to create a lisp program that asks the user 4 questions and uses the response in the program. i wrote a lisp routine that will setup the layers and their linetypes, color, etc. however, the program doesn't repeat itself. it terminates after the first layer is set up. below is the code... can anyone tell me why it isn't working? i'm very new at this... it's kind of discouraging. thanks in advance.
Code: [Select]
(DEFUN c:LAYERSETUP()
(GRAPHSCR)
(COMMAND "LINETYPE" "LOAD" "*" "ACAD.LIN" "")
(SETQ X (GETINT "\n How many layers would you like? "))
(SETQ N (GETSTRING T "\n Please enter the new layer name: "))
(SETQ C (GETSTRING "\n Please enter the new layer's color: "))
(SETQ L (GETSTRING "\n Please enter the linetype for this layer: "))
(REPEAT X
(COMMAND "LAYER" "MAKE" N "COLOR" C N "LTYPE" L N "")
)
(PRINC)
)



My post.
Post 2
Quote
You must realize that layer names can never be the same, so, you need to enclose your question to the user within the repeat function. If you'd like to only ask that once, you could create a list of layer names, then instead of asking how many layers the user wants, you could use foreach instead of repeat. Here is an example, minus your loading of the linetypes, which seem like they would already be loaded:
Code: [Select]
(defun c:layersetup ()
     (graphscr)
     (vl-load-com)
     (setq n  nil
  tn t
     )
     (while tn
 (setq n   (append
n
(list (getstring
   "\nType the new layer name or [Enter]: "
      )
)
   )
 )
 (if (= (last n) "")
      (setq n (vl-remove "" n)
    tn nil
      )
 )
     )
     (foreach item n
 (setq c (getstring (strcat "\n Please enter layer "
    item
    "'s color: "
    )
 )
l (getstring (strcat "\n Please enter linetype for "
    item
    "'s layer: "
    )
 )
 )
 (command "LAYER" "MAKE" item "COLOR" c item "LTYPE" l item "")
     )
     (princ)
)

This code has bugs.
1) What if the user enters a color name wrong or hits escape?
2) What if the user enters a linetype that does not exist or hits escape?
You might want to consider getting the names of all the colors and linetypes that exist and puting them in an initget function and using getkword instead of getstring.

One thing I've learned recently is that the user of your code, unless it is the programmer, is not to be blamed for not using it correctly. As a programmer, you need to make it "idiot proof".

Post 3 and 4 by
collegeCAD
Quote
Thanks for the help... I have a few more questions, if you don't mind, that I was hoping you could answer. I know these are going to be rudimentary questions and answers, but they will definitely help me get a more solid footing in grasping all these concepts.
I was hoping you could explain the vl- functions to me... I guess my books are a little harder to understand than I thought and we haven't covered them yet in class. I'm trying to teach myself from books and AutoCAD's help files but they don't go into ANY depth at all.

In line 4 you set "n" to nil... is this just to clear out anything that "n" may have been set to?

Also, if I understand this correctly, the argument "t" allows the user to put spaces in their inputs. Am I understanding this correctly? In line 5 of the code, you setq tn to t... what does this do?

And finally, in lines 9 - 11, is this just the code for when the user is finished entering all the new layers they would like and they just press enter to move on?

Thanks so much for the help so far. You've relly been a big help!

On edit: I just saw what you wrote beneath the code... definitely something I noticed too. I will be incorporating the getkword function into it as well... that is once I can teach myself how to use it  Thanks again for the responses.

Quote
Here's the finished lisp routine... don't know if you see any bugs in it:
Code: [Select]

;;; "User Friendly Layer Setup"
;;; layersetup1.lsp
;;; By: Jon Nelson
;;; DFTG1352
;;; Thomas Longnecker
(defun c:layersetup ()
     (setvar "cmdecho" 0)
     (command "linetype" "load" "*" "acad.lin" "")
     (vl-load-com)
     (setq n nil)
     (setq tn t)
     (alert "The following linetypes WILL NOT be used w/ this AutoLISP routine:
 - ISO 128 (ISO/DIS 12011) linetypes
 - HOT_WATER_SUPPLY
 - GAS_LINE
 Setting these linetypes to a certain layer can only be done
 manually."
     )
     (while tn
 (setq n   (append
n
(list
     (getstring
  t
  "\nPlease enter a new layer name or [Enter] to continue: "
     )
)
   )
 )
 (if (= (last n) "")
      (setq n (vl-remove "" n)
    tn nil
      )
 )
     )
     (foreach item n
 (alert
      "For layer color, use one of
 the\nfollowing standard color names:\n\n- Red\n- Yellow\n- Green-
 Cyan\n- Blue\n- Magenta\n- White\n- 8 (dark gray)\n- 9 (light gray)"
 )
 (initget 1 "Red Yellow Green Cyan Blue Magenta White 8 9")
 (setq c   (getkword
(strcat
     "\nPlease enter a STANDARD COLOR NAME
 for "      item
     "'s layer: "
)
   )
 )
 (alert
      "For
 linetypes, use one of the\nlisted lintetypes:\n- Batting
 SSSSSSSSSSSSSSS- Border __ __ . __ __ .- BORDER2 __.__.__.__.__.-
 BORDERX2 ____  ____  .  - BYBlock _______________- BYLayer
 _______________- CEnter ____ _ ____ _ _- CENTER2 ___ _ ___ _ ___-
 CENTERX2 ________  __  _- COntinuous _______________- DASHDOT __ . __
 . __ . - DASHDOTX2 ____  .  ____  - DASHED __ __ __ __ __ - DASHED2
 _ _ _ _ _ _ _ _- DASHEDX2 ____  ____  ___- DIvide ____ . . ____ .-
 DIVIDE2 __..__..__..__.- DIVIDEX2 ________  .  . - DOT . . . . . .
 . .- DOT2 ...............- DOTX2 .  .  .  .  .  - FENCELINE1
 ----0-----0----- FENCELINE2 ----[]-----[]--- Hidden __ __ __ __ __ -
 HIDDEN2 _ _ _ _ _ _ _ _- HIDDENX2 ____ ____ ____ - Phantom ______  __
 __ - PHANTOM2 ___ _ _ ___ _ _- PHANTOMX2 ____________   - Tracks
 -|-|-|-|-|-|-|-- Zigzag VVVVVVVVVVVVV\nYes, there is a reason that
 some of the letters are capitalized."
 )
 (initget
      1
      "BAtting Border
 BORDER2 BORDERX2 BYBlock BYLayer CEnter CENTER2 CENTERX2
 COntinuous DASHDOT DASHDOTX2 DASHED DASHED2 DASHEDX2 DIvide DIVIDE2
 DIVIDEX2                DOT DOT2 DOTX2 FENCELINE1 FENCELINE2 Hidden
 HIDDEN2 HIDDENX2 Phantom PHANTOM2                PHANTOMX2 Tracks
 Zigzag"  )
 (setq l   (getkword (strcat "\n\nPlease enter a linetype
 for "      item
     "'s layer: "
     )
   )
 )
 (command
      "LAYER" "MAKE" item "COLOR" c item "LTYPE" l item ""
     )
     )
     (setq
 current
     (getstring
  t
  "\nPlease enter which layer you would like to
 make current: "
     )
     )
     (command "layer" "s" current "")
     (setvar
 "cmdecho"
 1
     )
     (princ)
)
(prompt "Enter LAYERSETUP to start")

I'm still wondering what exactly the purpose of the lines (setq n nil) & (setq tn t) are for.... call me naive or just flat out retarded but I've become brain fried from learning a lot of AutoLISP and VLisp in the last few days that we won't even be dwelling into throughout the duration my course. Everything else I've learned along the way through MUCH reading and research has turned my brain into pudding. So I can't even think of the simplest things at this point.

But yeah, thanks so much for suggesting the initget and getkword functions... took me a while to master it as there is apprently a limit on how many arguments you are allowed to have w/ the initget function (don't even ask how long it took me to figure this one out). How many that is, I don't know... all I know is I kept getting the error "too many arguments." So, I reduced the allowed inputs from the user to only the standard color names and showed them what was acceptable through the second alert box.

As for the linetypes, it tooke me about 4 hours of reading to realize that you couldn't use underscore's and almost anything else when defining what was allowed to be keyed in. Thus, I set the first alert box that pops up saying it won't use the ISO lines, Hot_Water_Supply, and Gas_Line linetypes in this lisp routine... the only ones that had underscores and coincidently the ones that wouldn't work.  I tell the yuser they would have to be set manually. And, to make it easier for the user, I added a 3rd alert box showing them which linetypes were acceptable.

Thanks again for everything... you helped in more ways than one. First by giving me a rough outline of what was needed (which was very far from what I had initially) and second by using visual lisp commands, thus making me look further into the lisp programming language and forcing me to learn new finctions, etc.

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #1 on: September 29, 2003, 02:47:13 PM »
Quote
In line 4 you set "n" to nil... is this just to clear out anything that "n" may have been set to?

I like to do that, just to make sure the variable is clear. The place you should be clearing the local variables is in () after defun, like this:
(defun c:layersetup (/ tn x c l n)
That will clear all local variables after the command is run. Like I said, I like to make sure it's clear before using the append command. If another program set n to a value and didn't clear that space, all the lists you put into it will be placed along with what was currently in there. I purposely left out the (/ tn...). So, the answer to your question is, Yes!
Quote
Also, if I understand this correctly, the argument "t" allows the user to put spaces in their inputs. Am I understanding this correctly? In line 5 of the code, you setq tn to t... what does this do?
Simply put, it allows you to get out of the while loop. Test this. Take out the if statement contained in the while loop. No matter how much you hit enter, you'll never get out. Escape is the only way and that would crash the routine. Now, t means true. So, While true, perform the next action. If the last piece of list "n" equals "" or enter, being one in the same, set tn nil. That is how you will get out of the while loop. You need to make while not true. Also, yes it is true that with inputs and string case and other functions, t allows spaces or changes the case or whatever the function performs. T, is much more than that as you should now be able to see. I believe this answers the next question, too, so I'll move on.
Quote
call me naive or just flat out retarded but I've become brain fried from learning a lot of AutoLISP and VLisp in the last few days that we won't even be dwelling into throughout the duration my course. Everything else I've learned along the way through MUCH reading and research has turned my brain into pudding. So I can't even think of the simplest things at this point.

Wouldn't think of it. I'm sure everybody who has ever taken the time to learn this stuff has felt this way. We all know what you're going through. As far as what you will most likely learn in school, when I was at ITT, the most I got out of the lisp portion of a class was that lisp means LISt Processor and how to use it to make cad draw it's own rectangle. We also had to do the garden path tutorial. It wasn't until I got started in the work force that I was able to get someone to teach me how to really use it. That was before Vlisp came to be. Se7en, later instructed me in the ways of ActiveX VL back in April. I hope you find more than you ever expected here. Have fun. We are a small group, because this site is new, but we've got some great minds here in Mark, Se7en and SMadsen. You'll learn a ton from these guys if you stick around.

SMadsen

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #2 on: September 29, 2003, 02:55:02 PM »
Well, better supply an answer so that we can welcome a new member when he has to pop over here to read it :)

Or sort of a short answer, anyway (gotta catch a train).

(setq tn T) is used as a flag to keep the WHILE loop running until a certain condition is met. It is set to nil when the user doesn't wanna play anymore and hits enter at the GETSTRING statement. That stops the loop.

(setq n nil) is used to initialize a list. Technically, the value nil is an empty list, so adding items to nil with one of the list constructors will create a list - in this case it's adding items with APPEND. However, it appears in the code either because the author doesn't know about local variables, or because your class hasn't covered local variables yet (in which case you shouldn't let it confuse you yet) :)

edited: Oh, beaten by Daron  :)

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #3 on: September 29, 2003, 02:59:43 PM »
Quote
Technically, the value nil is an empty list, so adding items to nil with one of the list constructors will create a list - in this case it's adding items with APPEND. However, it appears in the code either because the author doesn't know about local variables


Nil is an empty list. I knew I forgot to mention something.
One way to test this is to inspect the append function with and without it.

I'm guessing they haven't gone over local variables yet.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9236
AutoLISP routine help!!!! (by collegeCAD)
« Reply #4 on: September 29, 2003, 03:08:07 PM »
Welcome aboard.  Sick arround and we would be glad to help you learn the "in's" and "outs" of the language.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Mark

  • Custom Title
  • Seagull
  • Posts: 28682
AutoLISP routine help!!!! (by collegeCAD)
« Reply #5 on: September 29, 2003, 03:13:58 PM »
Quote
Sick arround

I see you STILL need that dictionary :D

http://www.dict.org
TheSwamp.org  (serving the CAD community since 2003)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9236
AutoLISP routine help!!!! (by collegeCAD)
« Reply #6 on: September 29, 2003, 04:09:52 PM »
*Sigh*

Marc, I thunk im going 2 kikc your but ...err i mean A55 ...no, i mean butt.

collegeCAD, C what I have to put up with?
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #7 on: September 29, 2003, 04:13:05 PM »
I hope he comes over. CC, have you checked in yet?

collegeACAD

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #8 on: September 30, 2003, 05:11:16 PM »
Quote from: Daron
I hope he comes over. CC, have you checked in yet?


Alas! I am here... I created the account for collegeCAD but never got an e-mail to activate my account... it's quite possible that I typed my e-mail addy wrong and that's why. So, if any of you have admin powers, you can go ahead and delete that user.

Anywho, I turned in my program last night... teacher loved it. Most people's programs consisted of about 10 lines... one person drew a star w/ theirs. So, he was impressed that the code I turned in went way above and beyond what he was expecting.

Thanks Daron for the help... you posted up the code, I learned about each and every command, modified it here, changed it a bit there, and voila!

Last question I have about the routine is the variable tn. Could this have been set to anything (if I am correct in assuming so, yes) such as lmnop or chocolate or is it tn for a specific reason?

Now, a few side notes:

- No, the class hasn't covered local variable, but I have now over the course of the weekend.

- Se7en, thanks for the welcome... I'm liking this class so far and I'm sure there will be many questions that I will be asking throughout the semester... I appreciate you guys helping a newbie out ;) And yes, I am beginign to see what you have to put up w/ here... but (see next side note)

- I prefer www.dictionary.com to dict.org. Besides, it isn't too P.C. to yell out "Hey! Go to dict.com and look this up!" I think you know what I mean....

- It's a little off topic, but Se7en might enjoy it. I got it in an e-mail. A lot have seen it already so it may be a repost:

Subject: Now this will mess with your mind!

Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae.

The rset can be a total mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.

**This concludes my first of many posts. [Aaahnold] I'll be back [/Aaahnold].**

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #9 on: September 30, 2003, 05:25:55 PM »
Welcome CC.
Quote from: CC
Last question I have about the routine is the variable tn. Could this have been set to anything (if I am correct in assuming so, yes) such as lmnop or chocolate or is it tn for a specific reason?

You are correct, mostly. I like to use tn just to remind me that it's a true/nil value I want to see here. The only thing you can't name it is 1 or 2 or any number, so it's a good idea to try and leave numbers out. Also, you don't want to do something like this: (setq t "Doh! Why did I do that?"). Don't change the value of constants: t, nil, gc. Hope you get a good grade. Also, if you stick with us, by the time your done with class, you'll probably be teaching your teacher a thing or two about lisp. It wouldn't be unheard of.

collegeACAD

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #10 on: September 30, 2003, 06:11:08 PM »
Quote from: Daron
Welcome CC.
Quote from: CC
Last question I have about the routine is the variable tn. Could this have been set to anything (if I am correct in assuming so, yes) such as lmnop or chocolate or is it tn for a specific reason?

You are correct, mostly. I like to use tn just to remind me that it's a true/nil value I want to see here. The only thing you can't name it is 1 or 2 or any number, so it's a good idea to try and leave numbers out. Also, you don't want to do something like this: (setq t "Doh! Why did I do that?"). Don't change the value of constants: t, nil, gc. Hope you get a good grade. Also, if you stick with us, by the time your done with class, you'll probably be teaching your teacher a thing or two about lisp. It wouldn't be unheard of.


haha, don't know about teaching my teacher anything. He's a lisp guruand does nothing BUT AutoLISP and VLisp as a job... teaching is his "hobby" so to speak. But, nonetheless, he was impressed and did give me an A on the project.

Thanks for clarifying the tn bit. I knew about the constants t and nil, but not gc. I'll make a note not to change that one.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9236
AutoLISP routine help!!!! (by collegeCAD)
« Reply #11 on: September 30, 2003, 06:14:54 PM »
Whats his name?

...You'll teach him a thing or two, i gaurantee it.  You can NEVER know it all.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #12 on: September 30, 2003, 06:18:37 PM »
Well, I'd like to see your final project? I'm hoping you modified it further. There is so much more you can do with it.

Quote from: 7
Whats his name?

...You'll teach him a thing or two, i gaurantee it. You can NEVER know it all.
Stig, you doing a side job in Plano, Texas?

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9236
AutoLISP routine help!!!! (by collegeCAD)
« Reply #13 on: September 30, 2003, 06:23:59 PM »
Personaly, I like the first code he posted. (it has to be fixed of course) but its clean and simple. Add a bit of "flash" like accessing the acad_colordlg and you got yourself a cool little program.

edited by Daron to get the spelling correct. You missed the r in color.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #14 on: September 30, 2003, 06:34:54 PM »
Anyway, I was hoping we could dive further into lists and get Stig-ified. That's why I left it open. I can't imagine you'd want to ask about layers in a while loop and not ask about what linetype or color at the same time. Couldn't we construct it in an association list and process each layer, color and ltype all at the same time?