Author Topic: CAD Setup Routine  (Read 62770 times)

0 Members and 1 Guest are viewing this topic.

amgirard2003

  • Guest
CAD Setup Routine
« Reply #60 on: January 27, 2004, 11:22:36 AM »
Quote from: Daron
I have a challenge for you. Study, test and describe, the elements of each function; what makes them similar, what makes them different, and what in both cases, will make or allow them to perform actions on multiple items for one true statement. Example
Code: [Select]

(cond (t
          (do this)
          (do this)
          (do this)
         )
         (t
          (do this)
          (do this)
          (do this)
         )
)
(if (t)
    (what goes here
     (do this)
     (do this)
    )
    (and here, to make this work?
     (do this)
     (do this)
    )
)



Here's my explanations and correct me if i'm worng which i'm sure you will do..
First the Cond Function

The Cond function evaluates all the arguements in the list supplied to it in order until it finds one that is not equal to nil. Once it finds an arguement that is TRUE it locks itself to that arguement and does the following instructions that are attached to that arguement.
So in the following.. Cond is looking for either t = Name or t = Sport..
If the arguement doesn't equal either the cond will exit itself.

(cond (t = Name
          (ask the user his name)
          (ask the user his age)
          (ask the user his weight)
         )
         (t = Sport
          (ask the user his favorite sport)
          (ask the user his favorite team)
          (ask the user his favorite player)
         )
)

The IF function work a lot like the Cond but there is a difference.
The Cond function runs through all the arguements until it finds a match.. while the IF is more like "either it's there or it's not" IF it's there do this and this and that, IF it's not then do this and that and that...

So in the following i'll use Sport and Name again in this example the If is looking to see if t = name.. If it does it does the following instructions that are attached, IF t doesn not equal t then jump to the next set of instructions.


(if (t)
    (= name)
          (ask the user his name)
          (ask the user his age)
          (ask the user his weight)
  )
    (ask the user his favorite sport)
          (ask the user his favorite team)
          (ask the user his favorite player)
    )
)

How far off am i on this?  i got a little confused on the IF... but i think it's right..




[/code]

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
CAD Setup Routine
« Reply #61 on: January 27, 2004, 11:27:45 AM »
andre'

Here is a quickie.

Open VLIDE, I assume you have used it.
If not stop here & ask.

Click the NEW FILE icon.
Paste the code below in it.

Click the TOOLS pulldown
Click "Load Text in Editor"
 
Your routine is loaded in ACAD

Click the Watch window icon, One with screen in background & eyeglasses in for ground.
Move your cursor to each icon and pause until you see the label pop up to tell what each is.

At this point a "Watch Window" is on the screen.
You should see "*LAST VALUE* =" in the window.
If not click the eyeglass icon and pull down the Add Watch selections and select *LAST VALUE*

Now high lite var2 and click the eyeglass icon, var2 should be in the watch window.
Do the same for var1.

Put your cursor just before (setq var1 or high lite the parentheses & click the open hand icon.
This is Toggle Breakpoint icon. Which will pause the routine when processes get to that point.

Switch to acad and enter test

You will be returned to VLIDE at the break point.

Now is where the fun begins.....

Click the parentheses with arrow to center icon, this is the "Step Into" icon.
This will single step you through the routine.
After each click look at the LAST VALUE to see what is returned.
Some times nil is returned when there is no logical result from the action within the parentheses
like (prompt "text")

Step through the entire routine.

To the programers, I am still a beginner so if I misspoke please correct me.

Enjoy

CAB

Code: [Select]
(defun c:test()
  (setq var1 "Andre")
  (setq var2 "Alan")

  (if (= var1 "Andre")
      (prompt (strcat "\nI found " var1))
      (prompt (strcat "\nI did not find " var1))
  )

  (setq var1 "Daron")
  (if (/= var1 "Andre")
      (prompt "\nI did not find Andre")
      (prompt "\nI found Andre")
  )

 
  (if (= var1 "Andre")
      (prompt  "\nI found Andre")
      (prompt (strcat "\nI did not find Andre, \nbut found " var1))
  )

  (if (and (/= var1 "Andre") (= var2 "Alan"))
    (progn ; this is needed when you have more than one group of code
      (setq var1 "Andre")
      (prompt  "\nI have restored Andre")
    ); end progn
    (prompt  "\nI found Andre and Alan")
  )


  (if (and (/= var1 "Andre") (= var2 "Alan"))
    (progn ; this is needed when you have more than one group of code
      (setq var1 "Andre")
      (prompt  "\nI have restored Andre")
    ); end progn
    (prompt  "\nI found Andre and Alan")
  )

  (princ)
); end defun
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
CAD Setup Routine
« Reply #62 on: January 27, 2004, 12:30:25 PM »
Quote
(if (t)
(= name)
(ask the user his name)
(ask the user his age)
(ask the user his weight)
)
(ask the user his favorite sport)
(ask the user his favorite team)
(ask the user his favorite player)
)
)

How far off am i on this? i got a little confused on the IF... but i think it's right..


First of all, in this (t) is just there as a constant to always evaluate to true. (= name "Andre")  should be used in place of (t). If you look at CAB's post, you'll see a little subr in there called progn. Take note of that and read this. Note especially the last quote I put in it. That should help. The links I put in there have got to be the most mind blowing stuff about lisp and its author. Tons of reading there. It's my personal opinion that knowing how to program isn't enough. Know background of the language syntax and reasons it came about help form your understanding of the language and its intended uses.

amgirard2003

  • Guest
CAD Setup Routine
« Reply #63 on: January 27, 2004, 01:05:05 PM »
Thanks Daron,

I bookmarked the site and will read it along with all the other sites i have marked as well..

daron

  • Guest
CAD Setup Routine
« Reply #64 on: January 27, 2004, 01:26:27 PM »
Take it one step at a time. Don't get overwhelmed. Did you see what makes the if statement perform multiple tasks and that it is in cond, just not as obvious?

amgirard2003

  • Guest
CAD Setup Routine
« Reply #65 on: January 27, 2004, 01:44:16 PM »
Got me a little confused there...

But if i can see what you're asking me to look for :

IF: works in pairs , meaning (do this if true) (If not then do this)
Cond: works with how many arguements you want to pass to it..

That's the clearest difference i can see in my own eyes..

amgirard2003

  • Guest
CAD Setup Routine
« Reply #66 on: January 27, 2004, 01:55:30 PM »
I understand a lot of things conceptually but it's when i have to write it out in code (syntax) that i have the biggest problem... There are times i don't know where to begin, what i'm supposed to put in the code and how to put it together.. There are times where i've tried experimenting and put some code together and to me it looked correct but AC didn't like it... I spent an hour after tryin to figure what could be wrong but never find what was wrong..

That's the part that frustrated me the most... I have the whole plan in my head but can't seem to put it into code.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
CAD Setup Routine
« Reply #67 on: January 27, 2004, 03:08:15 PM »
andre'

I was at you level not too long ago.
Every bit of code was made with the manual in hand
and I spent hours chasing things that are obvious to me now.

Learning to work VLIDE & the WATCH window are critical to
your rapid improvement to the next level.

Don't hold back, post your code here and you will receive the
help you need, not criticisms.

They hold that back for later, just kidding. :)

My path was to take routines I have found, they are everywhere.
I wanted it to do something my way, so i had to find out how it
worked and then adjust it to do what I wanted.

Then I found more complicated routines and commented every line of
code I understood or thought I did. Then post it and the helpful
people here will correct your misunderstandings of the code and
fill in the blanks. Usually leading to instruction on the parts
that are not understood and a "How To" do it differently.

There are several recent threads that are of helping new coders.
Perhaps someone can link you to them. Good reading.

CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
CAD Setup Routine
« Reply #68 on: January 27, 2004, 03:20:46 PM »
It comes with time. What I'm trying to get at is this:
If, natively has three parts. A test of true or nil (false) and one or two things to do in either case. We've covered that.
Cond, as you've stated performs multiple tasks based on a test that results in true. If there are multiple test statements in cond and all result in a true value, only one will be processed. Regardless of that, in the if statement there are only two options, BUT they can be nested, i.e.
Code: [Select]

(if (= a b)
    (if (= a c)
        (if (= a d)
             (progn
                (if true in all three cases do this)
                (you could also put this in as something to do for true if you wrap it in a progn, as you see)
              );end progn
              (do this if a is not equal to d, but is equal to c and b)
         );end if
       (do this if a was not equal to c, but is equal to b)
     );end if
);end if

Note: there is something to do in case of nil in both nested if's. There is also a progn. The progn will make if work similar to cond in that it will perform more functions. That is what I want you to see. The quote from the John McCarthy link I sent you to earlier, that I quoted in my post, mentioned cond's progn statement. This is telling me that cond is similar to if in that it too needed a progn function to act like it does, but John McCarthy embedded that within the cond statement. Last thing: in the above example what would be the return value of IF, if a is not equal to b?

I don't want to confuse or frustrate you. I'm sorry if I do. As far as understanding things conceptually... I, and most likely everybody else, has the same problem. It is only through testing code that you will begin to really understand what works and what doesn't work in programming. From your writing, you seem to be native to a language other than English. When you started learning English or any other language for that matter, was it possible to read and understand concepts of the language and then jump out and speak fluently? No! I highly doubt it. You had to test the language and make many mistakes, before getting to the level with which you could speak or write fluently. Programming is the same way. You can do all the reading you want, but until you try it out, test it out and realize the return values of each function, nobody will ever learn it.

What CAB just said about finding code and testing it, look under the Show Your Stuff forum, here. Copy the code, even if you don't understand it. Test it, and ask questions in this forum about it. CAB has recently, since this board has been up, really started to branch out with greater understanding of lisp. I learned some new stuff back in April that pushed me beyond where I was before that.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
CAD Setup Routine
« Reply #69 on: January 27, 2004, 09:49:00 PM »
I have been lurking in the background keeping an eye on things and these guys would not steer you wrong. If something does not work like you expect, then ask and post code. If you don't understand allof the code, no worrys, you will learn in time. Pick a few basic commands to actually learn, add more as you become familiar, and by all means pick apart others code. I have went back and looked at code I wrote 4 or 5 years ago and asked myself why did I do something so silly when this command would work better...it is all a learning experience and soon enough you will be coding with the best of them, particularly if you apply yourself.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

amgirard2003

  • Guest
CAD Setup Routine
« Reply #70 on: January 28, 2004, 08:06:09 AM »
Quote from: Daron
BUT they can be nested, i.e.
Code: [Select]

(if (= a b)
    (if (= a c)
        (if (= a d)
             (progn
                (if true in all three cases do this)
                (you could also put this in as something to do for true if you wrap it in a progn, as you see)
              );end progn
              (do this if a is not equal to d, but is equal to c and b)
         );end if
       (do this if a was not equal to c, but is equal to b)
     );end if
);end if

in the above example what would be the return value of IF, if a is not equal to b?



In the above example IF the return value would be NIL (false)

I appreciate all the responses, I know with time that i will get a hang of all this... I'm pretty dedicated, I've got 3 books and many things i've downloaded off some websites and have been reading them non stop...not too sure if i'm understanding but i'll be re-reading the chapters to try and comprehend better.


Getting back to my project, let's say we work on little bits and pieces at a time.

How about we start with the layers part?
When the routine executes the following layers would be created

Here's the code i came up with, i would like your input.

Code: [Select]

;**************************************************************
;***********************Setting of Layers**********************
; Pseudo Code
; When the OK button is pressed on the Setup dialog box the following
; layers will be created.

(defun newlayers ( / cmd clay) ; defined the function as newlayers with local variables CMD CLAY
  (setq cmd (getvar "cmdecho")) ; set variable cmdecho to cmd for temporary status
  (setq clay (getvar "clayer" "0")) ; set variable clayer to clay for temporary status
  (setvar "cmdecho" 0) ; set variable cmdecho to 0, to disable commands being echoed to the command line
 
  (command ".layer" "m" "1"    "c"    "1"    ""    "" ; Start Layer command and create the following layers
  ".layer" "m" "2"    "c"    "2"    ""    "" ; ".layers" is the layer command, "m" stands for make
  ".layer" "m" "3"    "c"    "3"    ""    "" ; "1" is the name, "c" is the color, "1" is the color
  ".layer" "m" "4"    "c"    "4"    ""    ""
 ) ; End of the Layer command
  (setvar "clayer" "1") ; set current layer to 1
  (setvar "cmdecho" cmd) ; set cmdecho back to original state with variable cmd
  (setvar "clayer" clay) ; set clayer back to original state with variable clay
  (princ) ; silent exit
) ; end of defun function


These are a few layers, not all the layers i will be using but gives the idea behind what i want to do.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
CAD Setup Routine
« Reply #71 on: January 28, 2004, 08:25:40 AM »
you could also make your function more resuable by adding arguments
to the function.
Code: [Select]

; defined the function as newlayers with local variables CMD CLAY
; lay_name = layer name to create, must be a string
; color = color of created layer, must be an integer
(defun newlayers (lay_name color / cmd clay)
  (setq cmd (getvar "cmdecho"))
  (setq clay (getvar "clayer" "0"))
  (setvar "cmdecho" 0)
 
  (if (= (type lay_name) 'STR); if var is a string
    (if (= (type color) 'INT); if var is a integer then create the layer
      (command "._layer" "m" lay_name "c" color "" "")
      )
    )
 
  (setvar "clayer" lay_name)
  (setvar "cmdecho" cmd)
  (setvar "clayer" clay)
  (princ) ; silent exit
) ; end of defun function

you would use the function like so (newlayers "1" 100) to create
a layer named '1' with a color of '100'
TheSwamp.org  (serving the CAD community since 2003)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
CAD Setup Routine
« Reply #72 on: January 28, 2004, 08:40:56 AM »
I posted a pure lisp version to create layers in "Show Your Stuff" last night. It allows you to create a layer that is off/on thawed/frozen locked/unlocked and any color using the ACI. If the layer already exists it will update the status to the values passed.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

amgirard2003

  • Guest
CAD Setup Routine
« Reply #73 on: January 28, 2004, 09:10:28 AM »
Is my code for the layers a good starting point, am i on the right side of the street or am i driving into a head on crash :?:

daron

  • Guest
CAD Setup Routine
« Reply #74 on: January 28, 2004, 09:22:24 AM »
Andre' and Mark, why did I get this:
Code: [Select]
Command: (setq clay (getvar "clayer" "0"))
Error: too many arguments
Error: Function Cancelled.

in testing?

I'm sure Mark just overlooked it, but Andre', look at getvar and setvar. Also, don't forget to test each line of code.