Author Topic: K, having a bit of trouble here  (Read 5511 times)

0 Members and 1 Guest are viewing this topic.

collegeCAD

  • Guest
K, having a bit of trouble here
« on: November 09, 2003, 05:30:30 PM »
Here's the deal:

Have to make a multiplication chart. Very simple. Here are the requirements:

  • Ask user for top left hand corner of chart
  • Ask user for number of multipliers
  • Make column headers green
  • Make row titles red
  • Make products cyan
  • Put a border around the chart
  • [/list:u]

    Now, I have a simple outline already made out from a previous project seen here:

Code: [Select]

(defun c:mt ()
  (setvar "cmdecho" 0)
  (command "layer" "m" "rowtitles" "C" "red" "")
  (command "layer" "m" "columnheaders" "C" "green" "")
  (command "layer" "m" "products" "C" "cyan" "")
  (command "style" "standard" "" "0.25" "0.75" "0" "n" "n" "n")
  (initget 7)
  (setq rows (getint "\nHow many rows would you like: "))
  (initget 7)
  (setq columns (getint "\nHow many columns would you like: "))
  (initget 7)
  (setq spt (getpoint "\nEnter the start point: "))

  (setq index1 1)
  (setq pt (polar spt 0 0.5))
  (repeat rows
    (command "text" "m" pt "0" (itoa index1))
    (setq index1 (+ index1 1))
    (setq pt (polar pt 0 0.5))
  )
   
  (setq index2 1)
  (setq pt (polar spt 4.71239 0.5))
  (repeat columns
    (command "text" "m" pt "0" (itoa index2))
    (setq index2 (+ index2 1))
    (setq pt (polar pt 4.71239 0.5))
  )
  (command "zoom" "e")
  (princ)
  (setvar "cmdecho" 1)
)


Now, we did another project that I think we will need to use somewhat hand in hand w/ this, minus the alert box. Here is that code:

Code: [Select]

(defun c:tt ()
  (setq mult (getint "\nEnter multiplier: "))
  (setq multiple (getint "\nEnter multiple: "))

  (setq index 1)
  (repeat mult
    (setq loop 1)

    (repeat multiple
      (alert (itoa (* index loop)))
      (setq loop (1 + loop))
      );repeat

    (setq index (1+ index))
   
  );repeat

  );defun c:tt


What am I not seeing? How do I get the products to show up? All I can muster up on the screen are the column headers and row titles thus far.

Blah. I guess I've hit a brick wall as to what to do. I just picked up a second job week before last. Working 2 jobs full time and being a full time student has killed my brain in the last 2 weeks.

Thanks for any help.

SMadsen

  • Guest
K, having a bit of trouble here
« Reply #1 on: November 10, 2003, 06:23:04 AM »
Try this for size (add error handling - e.g. regarding layers and style).
It basically starts out where the routine C:MT left by plotting in the headers (just added some layer changes and lines). It then resets index1/index2 and pt and starts over by filling in the products.

Code: [Select]
(defun c:mt (/ cmd)
  (setq cmd (getvar "CMDECHO"))
  (setvar "cmdecho" 0)
  (command "layer" "m" "rowtitles" "C" "red" "" "")
  (command "layer" "m" "columnheaders" "C" "green" "" "")
  (command "layer" "m" "products" "C" "cyan" "" "")
  (command "style" "standard" "" "0.25" "0.75" "0" "n" "n" "n")
  (initget 6)
  (if (setq rows (getint "\nHow many rows would you like: "))
    (progn (initget 6)
           (setq columns (getint "\nHow many columns would you like: "))
    )
  )
  (cond ((and rows columns (setq spt (getpoint "\nEnter the start point: ")))
         ;; Make columns headings
         (setvar "CLAYER" "columnheaders")
         (setq index1 1
               pt     (polar spt 0 0.5)
         )
         (repeat columns
           (command "text" "m" pt "0" (itoa index1))
           (setq index1 (+ index1 1))
           (setq pt (polar pt 0 0.5))
         )
         (command "LINE"
                  (list (- (car spt) 0.25)
                        (- (cadr spt) 0.25)
                        0.0
                  )
                  (list (- (car pt) 0.25)
                        (- (cadr pt) 0.25)
                        0.0
                  )
                  ""
         )
         ;; Make row headings
         (setvar "CLAYER" "rowtitles")
         (setq index2 1
               pt     (polar spt 4.71239 0.5)
         )
         (repeat rows
           (command "text" "m" pt "0" (itoa index2))
           (setq index2 (+ index2 1))
           (setq pt (polar pt 4.71239 0.5))
         )
         (command "LINE"
                  (list (+ (car spt) 0.25)
                        (+ (cadr spt) 0.25)
                        0.0
                  )
                  (list (+ (car pt) 0.25)
                        (+ (cadr pt) 0.25)
                        0.0
                  )
                  ""
         )
         ;; Fill in products
         (setvar "CLAYER" "products")
         (setq index1 1
               index2 1
               x      (+ (car spt) 0.5)
               y      (- (cadr spt) 0.5)
         )
         (repeat columns
           (setq pt (list x y 0.0))
           (repeat rows
             (command "text" "m" pt "0" (rtos (* index1 index2) 2 0))
             (setq index2 (1+ index2)
                   pt     (list x (setq y (- y 0.5)) 0.0)
             )
           )
           (setq index2 1
                 index1 (1+ index1)
                 x      (+ x 0.5)
                 y      (- (cadr spt) 0.5)
           )
         )
         (command "zoom" "e")
        )
  )
  (setvar "cmdecho" cmd)
  (princ)
)

SMadsen

  • Guest
K, having a bit of trouble here
« Reply #2 on: November 10, 2003, 06:40:45 AM »
Btw, turn off OSNAP when running it.

collegeCAD

  • Guest
K, having a bit of trouble here
« Reply #3 on: November 10, 2003, 09:07:46 AM »
That's excellent, but I have a question (you didn't think you'd get away that easy from a college student did ya?).

Is it necessary to have the cmd as an argument in the defun command? Or can I leave that blank and then just set cmdecho to 0 at the begining of the script and then reset it at the end?

I guess I don't really see the necessity of it... but then again, I', not a lisp God like the rest of you lol.

Thanks so much. I see what I was doing wrong now. I was on the right track, but at the rate I was going, it would have taken me a year to get it all down correctly.  :wink:

SMadsen

  • Guest
K, having a bit of trouble here
« Reply #4 on: November 10, 2003, 09:28:04 AM »
Excellent question.
cmd is not an argument (you can't call a C:-function with arguments as a command on the command line). It is declared as a local variable to the function, which means that it will only be used within the routine and will disappear after the routine has finished. The most prominent reasons for using local variables are 1. not to hog memory and 2. not to interfer with other code loaded into the environment.

It's true that arguments appear in the same list as local variables and it takes a while to spot the difference. What you should be looking for is a slash that separates arguments and local vars. Arguments go to the left of the slash and locals go to the right. The rules are:
- No arguments and no local vars: slash is optional
- No arguments but local vars: slash definately needed
- Arguments but no local vars: slash is optional
- Arguments and local vars: slash definately needed

Many more of the variables in the routine - actually ALL of them - should be declared as local variables, i.e. added to the list on the right side of the slash. However, in order to give you a chance to inspect the variables while testing the routine, you can leave them as global. Afterwards, when the routine is free from errors, they should all be declared local.

collegeCAD

  • Guest
K, having a bit of trouble here
« Reply #5 on: November 10, 2003, 09:40:00 AM »
Quote from: SMadsen
Excellent question.
cmd is not an argument (you can't call a C:-function with arguments as a command on the command line). It is declared as a local variable to the function, which means that it will only be used within the routine and will disappear after the routine has finished. The most prominent reasons for using local variables are 1. not to hog memory and 2. not to interfer with other code loaded into the environment.

It's true that arguments appear in the same list as local variables and it takes a while to spot the difference. What you should be looking for is a slash that separates arguments and local vars. Arguments go to the left of the slash and locals go to the right. The rules are:
- No arguments and no local vars: slash is optional
- No arguments but local vars: slash definately needed
- Arguments but no local vars: slash is optional
- Arguments and local vars: slash definately needed

Many more of the variables in the routine - actually ALL of them - should be declared as local variables, i.e. added to the list on the right side of the slash. However, in order to give you a chance to inspect the variables while testing the routine, you can leave them as global. Afterwards, when the routine is free from errors, they should all be declared local.


when putting in the variables to the right of the slash, are they purged from memory after the routine has ended and reset to nil? we breiefly covered it all in class but didn't dwell into it that much because it wouldn't be needed to know for the class. yeah, my curiosity doesn't stop at the level the class reaches hehe. all we covered abotu that is those would be the only arguments passed in the routine.

SMadsen

  • Guest
K, having a bit of trouble here
« Reply #6 on: November 10, 2003, 10:22:45 AM »
Quote from: collegeCAD
when putting in the variables to the right of the slash, are they purged from memory after the routine has ended and reset to nil?

Correct