TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: daron on September 29, 2003, 02:15:18 PM

Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen 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  :)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: Mark on September 29, 2003, 03:13:58 PM
Quote
Sick arround

I see you STILL need that dictionary :D

http://www.dict.org
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK 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?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on September 29, 2003, 04:13:05 PM
I hope he comes over. CC, have you checked in yet?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: collegeACAD 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].**
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: collegeACAD 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron 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?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK 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.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron 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?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on September 30, 2003, 06:40:32 PM
Lousy spelling is one of Se7en's better traits.....we won't talk about the nastier ones. :lol:  :twisted:
I've been following this thread since its beginning (over "there") and you guys never cease to amaze me.  After running the "newbie" through all those hoops, 7  tells us he likes the first code better.......why am I not surprised?
Well....test in 15 min....gotta go.

T-
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on September 30, 2003, 07:04:23 PM
I know it is (7's spelling). I just couldn't let that one go. I could just imagine CC coming in here for help of a function that doesn't exist. I certainly don't want to offend, but 7, is your spelling really bad, or are you jaust a non-typist? I've seen others who could out-spell anybody, but when it comes to typing, they're so intent on staring at the keyboard trying to find all the keys and get out what they type so fast, that they appear to not be able to spell. I've also heard that drafters aren't good speller's. I can't figure that one out. Isn't that the job of a drafter, to be able to communicate well?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on September 30, 2003, 08:12:07 PM
No, no offence. I cant spell worth crap! (plus im typing on a laptop so the keys are a little to closs together for my taste.) But anyways...

That's fine. Put it in pseudo code. (You did teach him pseudo code didnt you.)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: collegeACAD on September 30, 2003, 09:40:00 PM
My teacher's name is Thomas Longnecker... very cool guy.

I heard mention of Plano... I live there (as I'm sure you know)... anyone else in the area?

Here's my final project. I plan on developing it further along the road... maybe setting up my limits, making my coffee in the morning, creating my title block, etc. Who knows where it will take me.:
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 have a modified version, w/out as many pop-ups... here's that code:

Code: [Select]

;;; "Layer Setup"
;;; layersetup2.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    
    (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: ")
 )
    )
    (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 "\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")


Here's the original, "fixed" of course. It's buggy as Daron originally mentioned. If you fart wrong around the program, it errors out:
Code: [Select]

(DEFUN c:LAYERSETUP()
  (COMMAND "LINETYPE" "LOAD" "*" "ACAD.LIN" "")
  (SETQ X (GETINT "\n How many layers would you like?  "))
  (REPEAT X
    (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: "))
    (COMMAND "LAYER" "MAKE" N "COLOR" C N "LTYPE" L N "")
  )  
  (PRINC)
)



If any admins read this, I was wondering if you could change my handle to collegeCAD since it got screwed up the first time I registered... I guess just post up in here and let me know that it's changed so I can change the username on the login page. Thanks. ;)

Take care for now, I'll check the thread later on tonight when I get home. Happy hour is calling me. Cheers!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: Mark on October 01, 2003, 02:42:10 AM
Quote from: collegeACAD

If any admins read this, I was wondering if you could change my handle to collegeCAD since it got screwed up the first time I registered... I guess just post up in here and let me know that it's changed so I can change the username on the login page. Thanks. ;)


Your first attempt failed because of a bad e-mail address.
Quote
<email@address removed>:
65.54.254.145 does not like recipient.
Remote host said: 550 Requested action not taken: mailbox unavailable
Giving up on 65.54.254.145.


I have reset the account for CollegeCAD with the original requested password.

Please use a valid e-mail address, thanks. I go out of my way to ensure that e-maill addresses on this forum are kept private. Unless you include it in your sig no one can see it but me!

Mark
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: collegeCAD on October 01, 2003, 05:29:47 AM
Quote from: Mark Thomas
Quote from: collegeACAD

If any admins read this, I was wondering if you could change my handle to collegeCAD since it got screwed up the first time I registered... I guess just post up in here and let me know that it's changed so I can change the username on the login page. Thanks. ;)


Your first attempt failed because of a bad e-mail address.
Quote
<email@address removed>:
65.54.254.145 does not like recipient.
Remote host said: 550 Requested action not taken: mailbox unavailable
Giving up on 65.54.254.145.


I have reset the account for CollegeCAD with the original requested password.

Please use a valid e-mail address, thanks. I go out of my way to ensure that e-maill addresses on this forum are kept private. Unless you include it in your sig no one can see it but me!

Mark


Much abliged, hats off to you ;)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 09:47:25 AM
Quote from: Daron
Stig, you doing a side job in Plano, Texas?
Well, I do run an email-based course in basic AutoLISP in Denmark (in Danish, of course). Doesn't attract alot of students, though, but it managed to put butter on the bread now and then.
Oh, let me know if there's a market for it in USA and I'll start translating it!! :)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 01, 2003, 09:58:43 AM
Stig, that would prolly be a cool idea. I dont know for sure, but im sure there might be some bites.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 01, 2003, 10:23:15 AM
Quote from: SMadsen
Well, I do run an email-based course in basic AutoLISP in Denmark (in Danish, of course). Doesn't attract alot of students, though, but it managed to put butter on the bread now and then.
Oh, let me know if there's a market for it in USA and I'll start translating it!! :)


What Se7en said, but why I asked was to get a jab in at se7en when he said "You can NEVER know it all."
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 12:10:41 PM
collegeCAD, good job! and congrats with your 'A'

I took the liberty of making a few suggestions to your code. It's still a schoolbook example (!), so I hope you have covered some of the stuff already. Maybe there are some things in it you can use, maybe not.

Code: [Select]
;;; "User Friendly Layer Setup"
;;; layersetup1.lsp
;;; By: Jon Nelson
;;; DFTG1352
;;; Thomas Longnecker

;;; Altered and commented by someone else

(defun c:layersetup ()
  (setq cmd (getvar "CMDECHO"))
  (setvar "cmdecho" 0)
 
  ;; Loading already loaded linetypes will result in
  ;; a prompt that will cause the routine to get stuck
  ;; in the LINETYPE command. This can be avoided by
  ;; setting the sysvar EXPERT to 3 before calling the
  ;; command.
  ;; Of course, with all changes in the users environment,
  ;; remember to reset it after you're done with it! The
  ;; same goes for CMDECHO above.
  (setq expert (getvar "EXPERT"))
  (setvar "EXPERT" 3)
  (command "linetype" "load" "*" "acad.lin" "")
  (setvar "EXPERT" expert)

  (vl-load-com)
  (setq n nil)
  (setq tn t)

  (while tn
    ;; For verifying symbol names in AutoLISP there is an
    ;; often overlooked function called SNVALID. Instead of
    ;; explaining here what it does, it'll be a good excercise
    ;; to look it up in the help reference. In short, it can be
    ;; used to verify layer names:
    (setq layName (getstring t "\nPlease enter a new layer name or [Enter] to continue: "))
    (if (snvalid layName)
      (setq n (append (list layName) n))
      (setq tn nil)
    )
  )
 
  (foreach item n
    ;; You seem to be well aware of the INITGET function, but
    ;; are you also aware that other GETxxx functions can accept
    ;; keywords? Because the LAYER command can accept integers, why
    ;; not use GETINT to retrieve the color?
    ;; However, because GETINT accepts integers between –32,768 and
    ;; 32,767 we need to verify the input before proceeding, so we'll
    ;; use tn as a flag once again to allow for multiple tries.
    (setq tn T)
    (while tn
      (initget 1 "Red Yellow Green Cyan Blue Magenta White")
      (setq layColor (getint (strcat "\nPlease enter a STANDARD COLOR NAME for "
                                     item
                                     "'s layer: "
                             )
                     )
      )
      (if (or (= (type layColor) 'STR) (< 1 layColor 255))
        (setq tn nil)
        (princ "\nInvalid color. Try again")
      )
    )

    ;; The above code should take care of input that any
    ;; AutoCAD user is familiar with so I don't think there
    ;; is a need to inform of valid inputs

    ;; If you want to inform about available linetypes then
    ;; it's possible to use the linetypes that are actually
    ;; available on the machine. I've known people who thought it
    ;; might save resources to delete some of these linetypes they
    ;; never use anyway from the acad.lin file.
    ;; Of course, getting the linetype pseudo-appearances are a
    ;; little harder so we'll save that for another time :)

    ;; The DRAWBACK of this method is that it's really hard to convert
    ;; the names and use them in a initget! But as a compensation for
    ;; our own codes sake we'll build a list to verify input.

    ;; By the way, I don't see why you are having problems with
    ;; complex linetypes?

    ;; First, reset some variables (no local vars, remember? :)
    (setq ltype nil ltypeInfo "" ltypeList nil)

    ;; Then run through the linetype table to pick up each name
    ;; of the actually available linetypes
    (while (setq ltype (tblnext "LTYPE" (not ltype)))
      (setq ltypeInfo (strcat ltypeInfo "\n" (cdr (assoc 2 ltype)))
            ltypeList (cons (cdr (assoc 2 ltype)) ltypeList)
      )
    )

    ;; We'll use tn again as a flag to control a loop until a
    ;; valid input is bagged. I don't think throwing an alert in
    ;; the users face is attractive. Instead we can provide an
    ;; alert with the info if the user desires. In order to do this
    ;; we can utilize bit 128 of INITGET that allows any arbitrary
    ;; input (= linetype name) and still have a chance to catch
    ;; the users request for help ("?")
    (setq tn T)
    (while tn
      (initget 128 "? Continuous")
      (setq ltype (getkword (strcat "\n\nPlease enter a linetype for "
                                    item
                                    "'s layer or [?/Continuous]: "
                            )
                  )
      )
      (if (= ltype "?")
        (alert (strcat "For linetypes, use one of the\nlisted lintetypes:\n" ltypeInfo))
        ;; Because linetypes are returned in uppercase by the
        ;; linetype table and the user could have used any
        ;; case he like, we'll convert his string to uppercase
        ;; before comparing it. Of course, if linetype is "Continuous"
        ;; then simply proceed
        ;; Also, because GETKWORD returns nil (and not "") on empty
        ;; response, we'll check for this.
        (if (and ltype (or (= ltype "Continuous") (member (strcase ltype) ltypeList)))
          (setq tn nil)
          (princ "\nInvalid linetype name. Try again")
        )
      )
    )

    (command "LAYER" "MAKE" item "COLOR" layColor item "LTYPE" ltype item "")
  )
  (setq
    current (getstring t "\nPlease enter which layer you would like to make current: ")
  )
  ;; Just making sure that the layer name input here is
  ;; a valid one by looking it up in the layer table before
  ;; trying to set it active
  (if (tblsearch "LAYER" current)
    (command "layer" "s" current "")
    (princ "\nNo such layer .. take a typing lesson!")
  )
  ;; Reset environment to what it was
  (setvar "cmdecho" cmd)
  (princ)
)

(prompt "Enter LAYERSETUP to start")
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 01, 2003, 12:37:48 PM
Beautiful Stig. I especially like the "...take a typing class.". CC, now do see why else I brought you over here? If your instructor gave you an A for what you turned in, something like this should break the bell curve and the grading system. Not to say that your's wasn't good, but man... like I said, if you can get what Stig's saying here, you might be able to teach your instructor a thing or two. Have you looked at this (http://theswamp.org/phpBB2/viewtopic.php?t=27) thread posted by Rugaroo? Stig gets into a really good discussion about lists and how to use them.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 01:02:11 PM
Quote from: Daron
If your instructor gave you an A for what you turned in, something like this should break the bell curve and the grading system.
"Hey, that's cheating!" ... "Nah, that's life"
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 01, 2003, 01:23:15 PM
Just to clarify, I would never tell someone to cheat or turn in something that someone else wrote. That's plagiarism. All I meant was that if CC were at the level of what you wrote, now, it would break the curve. In the near future, as long as CC keeps coming back here and asking questions, he WILL be at the level to write something of that magnitude and I'm sure his instructor will be grading him tougher and expect more out of him.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 01, 2003, 01:26:34 PM
Its getting alot better. however I would make one MINOR adjustment. I would use the "acad_colordlg" to offer a way to pick the color on the screen.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 01:51:46 PM
Se7en, the visible kind of guy, eh?  :shock:

I agree, that would be cool.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 01, 2003, 02:02:18 PM
lol.

Just for an example. Here is a function that i use everyday. It is in a file that is loaded upon startup cause i use it so much.
Code: [Select]
;Makes a new layer and asks for the color.
(defun C:MLL ()
   (setq name (strcase (getstring "\nEnter the new layer name: ")))
   (setq col (acad_colordlg 1 T))
    (command "Layer" "m" name "c" col name "")
  (princ)
)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 01, 2003, 02:35:30 PM
ok....now this interests me.  Here's the scenario...I have 30 parts that need to be dim'd on a cut sheet, each in its own vport.  The assy is xref'd into MS and I freeze all but part 01 in the first PS vport. After diming the part I want to be able to copy it to the right and freeze 01 and thaw 02.  so far, so good.  Here's the problem...for 30 parts, I need 30 dim layers...named "DIM01...DIM02...etc with color "blue".  I see the possibilities in CC's lisp but don't know how to tweak it to suit my needs.  I don't need the linetype section...all continuous. Somehow I need to have the routine create X number of layers, numbered sequentially, through a user input ("how many layers do you need?)...(30).  Does this make sense?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 03:23:11 PM
t-bear, hold on 30 seconds :)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 01, 2003, 03:23:57 PM
Will this work for you, Bear:
Code: [Select]
;;; "User Friendly Layer Setup"
;;; layersetup1.lsp
;;; By: Jon Nelson
;;; DFTG1352
;;; Thomas Longnecker

;;; Altered and commented by someone else
;;; Ripped apart for altered use by another

(defun c:layersetup ()
     (vl-load-com)
     (setq num 1
  name "DIM"
  reset (getvar 'clayer)
  cnt 1)
     (initget 7) ;I initially put 6. Edited to make it better.
     (setq rept (getint "\nHow many layers do you want?: "))
     (repeat rept
 (if (> cnt 9)
     (setq layName (strcat name (rtos cnt 2 0)))
     (setq layName (strcat name "0" (rtos cnt 2 0)))
 )
 (setq cnt (1+ cnt))
 (if (snvalid layName)
      (setq n (append (list layName) n))
      (setq tn nil)
 )
     )
     (foreach item n
     (command "Layer" "m" item "c" 5 "" "")
 )
     (setvar 'clayer reset)
     (princ)
)

(prompt "Enter LAYERSETUP to start")
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 03:59:23 PM
Darn, beaten by Daron again :)  Nice work

I was just sitting with something completely different and yet similar. Watch out for large numbers. It doesn't care if the user happened to type 500000 .. it'll make 500000 layers then (I think? Didn't test that particular case).

Code: [Select]
(defun C:ADDLAYERS ()
  ;; put whatever color number here
  (makeLayers 5)
)

(defun makeLayers (cnum / cnt ctrl layname laynum laypref zero zeroln)
  (setq ctrl 0)
  (initget 6)
  (cond ((and (numberp cnum) (setq laynum (getint "\nNumber of layers: ")))
         (initget 128 "Dim")
         (setq laypref (cond ((setq laypref (getkword "\nLayer prefix <Dim>: "))
                              (setq laypref (strcase laypref)))
                             ("DIM")))
         (setq cnt    1
               zeroln (strlen (strcat laypref (itoa laynum)))
               zero   ""
         )
         (repeat laynum
           (repeat (- zeroln (strlen (strcat laypref (itoa cnt))))
             (setq zero (strcat zero "0"))
           )
           (setq layname (strcat laypref zero (itoa cnt))
                 cnt     (1+ cnt)
                 zero    ""
           )
           (if (entmake (list '(0 . "LAYER")
                              '(100 . "AcDbSymbolTableRecord")
                              '(100 . "AcDbLayerTableRecord")
                              (cons 2 layname)
                              (cons 62 cnum)
                              '(70 . 0)))
             (setq ctrl (1+ ctrl))
           )
         )
        )
  )
  (princ (strcat "\n" (itoa ctrl) " layers created"))
  (princ)
)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 01, 2003, 04:03:31 PM
By the way, this is a good chance to notice the difference between the command interface and the direct access to entity creation/modification. Try creating 200 layers with each of these functions and check the time it takes!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 01, 2003, 04:14:02 PM
I'm glad you did yours different, Stig. I was just taking what you left CC with and modifying it to suit Bear's needs. I was thinking to use vla subs, but chose not to alter the original commands style. I think in the case of vla-properties and your entity creation/mod, your's is still the better way to go. So, I think with that, I just learned something new. Just to make sure I got this right, when adding objects to a collection, it's better to access the collection. Hm?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: Mark on October 01, 2003, 08:34:28 PM
My " two cents worth"  down-n-dirty ActiveX method.
Stig... got your stop watch handy? I'd like a race. <G>

Code: [Select]
(defun c:LayerCreator ( / lo num ll)

  (vl-load-com)

  (setq lo
        (vla-get-layers
          (vla-get-activedocument
            (vlax-get-acad-object)
            )
          )
        )

  (initget 7)
  (setq num (getint "\nHow many dim layers to create? (must be < 200) : "))

  (if
    (and num (< num 200))
    (while
      (> num 0)
      (cond
        ((< num 10)
         (setq ll (cons (strcat "DIM0"(itoa num)) ll)))
        (T
          (setq ll (cons (strcat "DIM"(itoa num)) ll))
          )
        )
      (setq num (1- num))
      ); while
    ; else
    (prompt (strcat "\nAre you kidding..."(itoa num)" layers...."))
    ); if

  (if ll
    (mapcar
      '(lambda (x / newlayer)
         (setq newlayer (vla-add lo x))
         (vlax-put-property newlayer  'Color 5)
         (vlax-release-object newlayer)
         )
      ll)
    ); if

  (if (not (vlax-object-released-p lo))
    (vlax-release-object lo)); if

  (princ)
  ); defun
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 01, 2003, 08:53:21 PM
Quote from: Mark Thomas
...
Stig... got your stop watch handy? I'd like a race. <G>


*Se7en sits back, hunkers down with some popcorn and gets ready for the show.

Fight, Fight! ...Kick his ass, kick - his - ass!

w00t!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 01, 2003, 08:56:37 PM
Quote from: Mark Thomas

Code: [Select]
... (initget 7)
  (setq num (getint "\nHow many dim layers to create? (must be < 200) : "))


Dude?! Dahell is that?! Stigs can do 50000 layers!? Yours can only do 199 ?!

...Wuss!

Edit: It appears that i missed a zero in Stig's functions capibilities.  I added it.

...Gee Mark, are you sure your not gonna trip on your dress when you run that progy?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 06:46:21 AM
And the result is ... !!!!

Two tests were done. One for each command in a fresh drawing each (called 15 times) and one in the same drawing where the commands were called in this sequence (called 10 times each):

AddLayer, LayerSetup, LayerCreator
LayerSetup, LayerCreator, AddLayer
LayerCreator, AddLayer, LayerSetup

Each command set up 100 layers and was followed by garbage collection, purge and garbage collection. Timer was started as soon as user inputs were done. The code below shows were timers were started and ended in each routine along with the timer and cleanup code used.

First, the results!! Unit is in seconds.

Code: [Select]
New drawings, only one command used in each drawing
LayerSetup   AddLayers    LayerCreator
 0,515989     0,046992     0,094025
 0,968975     0,061999     0,093985
 1,547004     0,046992     0,092979
 2,124994     0,046992     0,092979
 2,735008     0,061999     0,078978
 3,391009     0,063005     0,094025
 4,157008     0,063005     0,094025
 5,640001     0,063005     0,092978
 6,531005     0,047033     0,109997
 7,344036     0,061999     0,092978
 8,296998     0,063005     0,094025
 9,218015     0,061999     0,109032
10,436998     0,063005     0,108992
11,264996     0,063005     0,093019
12,202991     0,063005     0,108992

Averages:
 5,758335     0,058469    0,096734

Same drawing, called in sequence as mentioned
LayerSetup   AddLayers    LayerCreator
 0,484005     0,045986     0,092979
 1,078070     0,046992     0,078012
 1,922017     0,045986     0,078012
 2,688016     0,063005     0,109997
 3,592980     0,077972     0,108992
 4,922001     0,061999     0,108992
 6,077980     0,078012     0,108992
 7,280990     0,078978     0,108992
 9,188001     0,078012     0,108992
10,671034     0,078012     0,108992

Averages:
 4,790509     0,065495     0,101295


Notice anything in particular with the command pipeline in each test?

Here's the code:
Code: [Select]
(defun startTimer ()
  (setq time (getvar "DATE"))
)
(defun endTimer (func)
  (setq time    (- (getvar "DATE") time)
        seconds (* 86400.0 (- time (fix time)))
  )
  (gc)
  (outPut seconds func)
)
(defun outPut (secs def)
  (princ "\nPurging...")
  (command "PURGE" "Layers" "*" "N")
  (gc)
  (princ (strcat "\nTimed " def ": " (rtos secs 2 6)))
  (princ)
)

(defun C:ADDLAYERS ()
  ;; put whatever color number here
  (makeLayers 5)
)
(defun makeLayers (cnum / cnt ctrl layname laynum laypref zero zeroln)
  (setq ctrl 0)
  (initget 6)
  (cond ((and (numberp cnum) (setq laynum (getint "\nNumber of layers: ")))
         (initget 128 "Dim")
         (setq laypref (cond ((setq laypref (getkword "\nLayer prefix <Dim>: "))
                              (setq laypref (strcase laypref)))
                             ("DIM")))
         (startTimer)
         (setq cnt    1
               zeroln (strlen (strcat laypref (itoa laynum)))
               zero   "")
         (repeat laynum
           (repeat (- zeroln (strlen (strcat laypref (itoa cnt))))
             (setq zero (strcat zero "0")))
           (setq layname (strcat laypref zero (itoa cnt))
                 cnt     (1+ cnt)
                 zero    "")
           (if (entmake (list '(0 . "LAYER")
                              '(100 . "AcDbSymbolTableRecord")
                              '(100 . "AcDbLayerTableRecord")
                              (cons 2 layname)
                              (cons 62 cnum)
                              '(70 . 0)))
             (setq ctrl (1+ ctrl))))))
  (princ (strcat "\n" (itoa ctrl) " layers created"))
  (princ)
  (endTimer (vl-symbol-name 'makeLayers))
)


(defun c:layersetup ()
  (vl-load-com)
  (setq num   1 name  "DIM" reset (getvar 'clayer) cnt   1)
  (initget 7)       ;I initially put 6. Edited to make it better.
  (setq rept (getint "\nHow many layers do you want?: "))
  (startTimer)
  (repeat rept
    (if (> cnt 9)
      (setq layName (strcat name (rtos cnt 2 0)))
      (setq layName (strcat name "0" (rtos cnt 2 0))))
    (setq cnt (1+ cnt))
    (if (snvalid layName)
      (setq n (append (list layName) n))
      (setq tn nil)))
  (foreach item n
    (command "Layer" "m" item "c" 5 "" ""))
  (setvar 'clayer reset)
  (princ)
  (endTimer (vl-symbol-name 'c:layersetup))
)


(defun c:LayerCreator (/ lo num ll)
  (vl-load-com)
  (setq lo (vla-get-layers (vla-get-activedocument
             (vlax-get-acad-object))))
  (initget 7)
  (setq num (getint "\nHow many dim layers to create? (must be < 200) : "))
  (startTimer)
  (if (and num (< num 200))
     (while (> num 0)
        (cond ((< num 10)
               (setq ll (cons (strcat "DIM0" (itoa num)) ll)))
              (T (setq ll (cons (strcat "DIM" (itoa num)) ll))))
        (setq num (1- num)))              ; while
     (prompt (strcat "\nAre you kidding..." (itoa num) " layers....")))
  (if ll (mapcar
      '(lambda (x / newlayer)
         (setq newlayer (vla-add lo x))
         (vlax-put-property newlayer 'Color 5)
         (vlax-release-object newlayer)) ll))
  (if (not (vlax-object-released-p lo))
    (vlax-release-object lo))
  (princ)
  (endTimer (vl-symbol-name 'c:LayerCreator))
)
[/code]
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: Mark on October 02, 2003, 08:38:48 AM
Very interesting Stig, I am truly enjoying this. If you don't mind
I'm going to make a new thread and add the tested code to it.


I also tried creating 1000 layers with each.

1000 LAYERS
Timed MAKELAYERS: 0.172037      Stig's
Timed C:LAYERCREATOR: 0.297001  Mark's
Timed C:LAYERSETUP: 4.264994    Daron's

I'm going back to the pits for a tune up. <g>
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 08:46:20 AM
:)
Will be interesting to see if you can tune activex to match lisp in performance.

Just for the fun of it, I tried creating 12450 layers. AddLayers took 16 seconds, LayerSet took 10 minutes - not to create the layers, though, but to fold with a fatal error! Never got around to test LayerCreator after that  :)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 02, 2003, 08:47:23 AM
:?  huh!? That is cool!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: Mark on October 02, 2003, 08:53:08 AM
12450 layers.......

C:LAYERCREATOR: 15.125003
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 09:02:13 AM
Yeah yeah, AFTER you changed to ENTMAKE I'm sure!  heh

Seriously, would you mind run AddLayers on your machine with 12450 layers?  I hate to admit it, but this is an OLD 500 MHz (don't laugh .. ok, laugh .. I do!) PIII thingy.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 02, 2003, 02:50:52 PM
Man, this is AWSOME!  We loaded all three routines here and tried 'em all out....Stig, yours won.  The guys like the prefix ability....Mark, yours is great too. Daron...hate to say this, but I ran yours with 30 layers then, after a purge, with 10.  got 30 .... seems to have locked into that # for some reason.?
Now for part 2)  
My drafter would like something similar, but with the ability to cycle through, oh say, 8 pre-determined colours.
01 = blue
02 = red
03 = magenta.....
get the picture?
He would like to be able to set the colors in the code...that way if he needed to he could open the file and edit there.....
Any ideas?

Oh, Sig....understand that I'm real stupid, OK?  Yours sets a default prefix to DIM...how do i get it to make the layers without a prefix?....01..02..03..etc...?
Am going to play with these for a bit.

Fellas...you are GODS among lesser-mortals.....Thank you VERY MUCH!!!!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 02, 2003, 03:07:26 PM
Quote from: t-bear
Daron...hate to say this, but I ran yours with 30 layers then, after a purge, with 10.  got 30 .... seems to have locked into that # for some reason.?
Not a problem T. As I stated before, I took what was there and modified it to your request. I guess it depends on the machine, because I was able to put up 30 layers no problem. Now, you'll see the race shows that mine folded on 12450 layer or less. That's the problem with not using the database the way Stig or Mark did. If I wanted to create anew, mine would've resembled Mark's more or less. I'm glad I did what I did. It is a great tool to show the progamming community that there is a right way and a wrong way to do things, even though all cases can work to an extent.

Quote from: t-bear
I ran yours with 30 layers then, after a purge, with 10. got 30 .... seems to have locked into that # for some reason.?

At a quick glance, I can't see why, but you don't want to use mine anyway. Use Stig's, it's proven.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 02, 2003, 04:46:37 PM
Daron....
Thanks for the lessons...it's been an eye-opener.  I'll pro'lly never learn to write code but...see below:
Stig...I "tinkered" a bit...took all reference to "DIM" from your code and got the default to <>.  Now the user can get just 01..02..03 or, by placing a prefix, get DIM01..DIM02..DIM03....ONE SWEEEET ROUTINE, SIR. ONE SWEEEET ROUTINE!
(I know where to look for some of this stuff....) :roll:
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 02, 2003, 04:58:04 PM
Below???
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 05:49:15 PM
Dang t-bear, you destroyed the "Bell Default Method" that I managed to sneak into that routine (anyone noticed that?)  :)

Seriously though, - good work changing it to suit your need.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 02, 2003, 05:57:57 PM
Okay, now I feel like the guy that was invited but sent as a joke to the wrong party. Where's the code you see, Stig? And, no, I didn't notice the BDM. Where is it?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 02, 2003, 05:58:31 PM
Quote from: SMadsen
Dang t-bear, you destroyed the "Bell Default Method" that I managed to sneak into that routine (anyone noticed that?)  :)

Seriously though, - good work changing it to suit your need.


Hey i never did get a chance to wrap my head arround that. I should go do that.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 06:09:43 PM
Daron, you don't see the code t-bear posted? Did you try whack your monitor?
I did, and I still can't see it.

The BDM is in the (setq laypref .. ) call. It's not as clean as it should be because there are some SETQ's in it (and one variable being tossed around, eww) but it's there.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 02, 2003, 06:17:05 PM
Quote
Originally posted (http://www.vbdesign.net/expresso/showthread.php?postid=80386#post80386) by R. Robert Bell
Has it ever bugged you trying to handle a default option with (getkword)?

The usual method I've seen is to get the input, and then handle the user's selection of the default, resetting the variable if need be. Here is an example of the "traditional" method (not really PHP, but I need those stupid brackets):

Code: [Select]
(initget 0 "Length Width Depth Time")
(setq Inp (getkword "\nSpecify a dimension [Length/Width/Depth/Time] <Time>: "))
(setq Inp (if Inp Inp "Time"))



Consider the alternative below however.

Code: [Select]
(initget 0 "Length Width Depth Time")
(setq Inp (cond ((getkword "\nSpecify a dimension [Length/Width/Depth/Time] <Time>: "))
                ("Time")))


By using (cond) to evaluate the return from (getkword), if it is nil (which happens if the user hits enter for the default), we can use the 2nd condition to return the default value. Otherwise, (getkword) will return something, so the first condition sets the value for the variable.

The clever programmer can even develop a slick function for general input. Why? Notice the repetitive elements in the example.

If there is enough interest, I will post mine. [/b]


Oh thats cool. Alright im off to look for a better way yet. :wink:
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 02, 2003, 06:20:33 PM
No Stig, I don't and I will not whack my computer. :(
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 06:23:45 PM
Heh, good luck Se7en. I'll bet a sixpack (of danish beer) that you can't make it shorter .. edited: oops, or BETTER .. (with all the functionality - don't forget the bracketed choices) unless you write an Assembler interface for AutoLISP :)
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: JohnK on October 02, 2003, 06:49:56 PM
yeah, that is a perty good way. I havent thought of a way yet. Im betting that, 'that' is the best way.

what dont you have one of those yet Stig?!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 02, 2003, 06:50:38 PM
Stig....
I didn't mean to rain on your parade.  I SAW it...just didn't realize how special it was.  To find that these guys missed it makes me feel all fuzzy inside ('course I was huntin' for sumpin suspific....)
Even a blind hog finds an acorn sometimes.
Now....did I do something wrong in the routine?  I deleted the word "DIM" in all its instances but left the parens () and quotes "" in place.  Are they still needed or can I get the same result a "cleaner" way?
Guess I'm asking some purty basic stuff here....hope you're not gittin' bored.....
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 02, 2003, 06:58:38 PM
Bear, will you show me where you posted this? I'd really like to see it.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: SMadsen on October 02, 2003, 06:59:42 PM
t-bear, I was just kidding around! Thought you might have just changed the default value. No problem at all .. on the contrary, you tweaked it to the better ('better' always being the users need).

Congrats with the grandson!
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 02, 2003, 10:06:44 PM
Daron...
Haven't posted it...thoughtless ol' coot that I am.  Will drop it in here (which one of these %*@#in' forums do I put it in?) in the morning.  Headin' for the house & a bite to eat....long day.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 03, 2003, 04:13:57 PM
OK Daron...Here's what Stig gave me with my mminor changes in bold...I don't want to post it in the "show Me....' section 'cause it ain't mine....
[code]like that, or pick the button when writing a post.[/code];edit by Daron
Code: [Select]
(defun C:ADDLAYERS ()
 ;;Created  10/2/03 by SMadsen on "The Swamp"...thanks Stig.
 ;;
  ;; put whatever color number here
  (makeLayers 5)
)

(defun makeLayers (cnum / cnt ctrl layname laynum laypref zero zeroln)
  (setq ctrl 0)
  (initget 6)
  (cond ((and (numberp cnum) (setq laynum (getint "\nNumber of layers: ")))
         (initget 128 "")
         (setq laypref (cond ((setq laypref (getkword "\nLayer [b]prefix <>: ")) [/b]
                              (setq laypref (strcase laypref)))
                             ("")))
         (setq cnt    1
               zeroln (strlen (strcat laypref (itoa laynum)))
               zero   [b]""[/b]
         )
         (repeat laynum
           (repeat (- zeroln (strlen (strcat laypref (itoa cnt))))
             (setq zero (strcat zero "0"))
           )
           (setq layname (strcat laypref zero (itoa cnt))
                 cnt     (1+ cnt)
                 zero    [b]""[/b]
           )
           (if (entmake (list '(0 . "LAYER")
                              '(100 . "AcDbSymbolTableRecord")
                              '(100 . "AcDbLayerTableRecord")
                              (cons 2 layname)
                              (cons 62 cnum)
                              '(70 . 0)))
             (setq ctrl (1+ ctrl))
           )
         )
        )
  )
  (princ (strcat "\n" (itoa ctrl) " layers created"))
  (princ)
)


BTW....how do you go about posting this code so it comes up like it does....sum muddle-rater Huh?
Oh yah....and quotes?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 03, 2003, 04:33:04 PM
Okay, thanks. I guess I just didn't see the RRB Default method in there when Stig posted it. I edited your post to look okay. The problem with it now is that you can't make anything bold in a code tag. I also commented on how to put it in a code tag. Quote is the same way, but I'll show you just to let you know. [quote] or [quote="T-Bear"]Place quote here[/quote]. It will look like:
[quote]
Quote
This is a quote with no author.
[/quote]
[quote="T-Bear"]
Quote from: T-Bear
This is a quote with the author.
[/quote]

How's that, you still confused?
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 04, 2003, 06:23:14 PM
Thanx Daron!!  I actually think I understand it.  So it's :


Quote from: DARON

paste quote here?


nope...that's not it....Hmmmmmm....Tol'ya I'm dain bramaged!
AHAH!  After three false starts (I forgot the closing /) I got it.  should have remembered that from the web page classes....
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: collegeCAD on October 05, 2003, 07:07:08 PM
Wow, I didn't realize this thread was still going hehe. I've been posting on my other board a lot more... guess I should pay attention to this one too  I might learn a thing or 50... as I already have.

As for the plagiarism comment a few pages back, I did explain to my teacher that I did get help. I showed him the original code and told him that's where it all started. I enlisted some help to figure out why it wasn't working since I'm a LISP n00b and all.

I then went on to explain that someone showed me a better way of coding it. I didn't understand half the things considering we have met in class only 5 or 6 times. So, to *further* educate myself, I took the liberty of picking apart each line of code until I understood it.

He then basically "quizzed" me on it all in front of the class and I explained, to the best of my ability, what each line meant, what each function did, etc. Hopefully I won't be labeled as "teacher's pet" form all this.

But, I do like the challenge of being expected to turn in more than just mediocre, elementary work. It'll push me harder to learn more than what will be covered in our class. And thanks in advance for being there w/ me through it all to help me along... I appreciate it much.

Anyway, test is tomorrow over all that we have covered so far. I feel fairly confident I'll do well in large part to you guys. I'll keep ya'll posted.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: Dent Cermak on October 05, 2003, 08:19:06 PM
It is truely a scary thought that T-Bear is helping to shape the mind of a college student!! I thought they usually had to be female and easily enticed with Snickers bars. (Or was it that they Snickered at him in bars? I forget.) :oops:
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: t-bear on October 05, 2003, 10:45:04 PM
Yep....#2....just so they don't point & laugh. You haven't been payin' attention....I had nothing to do with CC's "education" here...left that to the heavy-hitters.
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: collegeCAD on October 06, 2003, 03:10:04 PM
Quote from: Dent Cermak
It is truely a scary thought that T-Bear is helping to shape the mind of a college student!! I thought they usually had to be female and easily enticed with Snickers bars. (Or was it that they Snickered at him in bars? I forget.) :oops:


i'm your new resdent fat guy... i like snickers... but not if it entails what i think he wants out of it....
Title: AutoLISP routine help!!!! (by collegeCAD)
Post by: daron on October 06, 2003, 04:03:20 PM
Quote from: collegeCAD
Wow, I didn't realize this thread was still going hehe. I've been posting on my other board a lot more... guess I should pay attention to this one too  I might learn a thing or 50... as I already have.

Of course this thread is still going. It's what you'd call "thread jacking". It's a lot of fun. What you need to do now is set your home page up to this one. Heh, we have Stig here. Enough said.

Quote from: collegeCAD
But, I do like the challenge of being expected to turn in more than just mediocre, elementary work. It'll push me harder to learn more than what will be covered in our class. And thanks in advance for being there w/ me through it all to help me along... I appreciate it much.
You're welcome.
Quote from: collegeCAD
Anyway, test is tomorrow over all that we have covered so far. I feel fairly confident I'll do well in large part to you guys. I'll keep ya'll posted.

Personally, I find that I retain more by coming in here and giving to others what I've learned... so, come often, take what you need, ask what you like, and give back what you know, even if you think someone else might have a better or more accurate answer.