Author Topic: Hey guys, long time no talk! Need some help if you can spare  (Read 7787 times)

0 Members and 1 Guest are viewing this topic.

collegeCAD

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« on: October 21, 2003, 05:39:55 AM »
... the time to do so.

First off... made a simple routine in class today. Nothing fancy, but thought I'd share nonetheless.

It's a quasi spreadhseet maker in AutoCAD that may prove to be useful (that is, if I could ever find a use for it). We were just experimenting w/ the CHR, ATOF, ATOI, ITOA, and RTOS functions and we came up w/ this:

Code: [Select]


(defun c:nr ()
  (setvar "cmdecho" 0)
  (command "style" "standard" "" "0.25" "0.75" "0" "n" "n" "n")
  (initget 7)
  (setq num1 (getint "\nHow many rows would you like: "))
  (initget 7)
  (setq num2 (getint "\nHow many columns would you like: "))
  (initget 7)
  (setq Spt (getpoint "\nEnter the start point: "))

  (setq index1 65)
  (setq pt (polar Spt 0 0.5))
  (repeat num1
    (command "text" "m" pt "0" (chr index1))
    (setq index1 (+ index1 1))
    (setq pt (polar pt 0 0.5))
  )
   
  (setq index2 1)
  (setq pt (polar Spt 4.71239 0.5))
  (repeat num2
    (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)
)


like I said, nothing fancy and pretty damn simple.

Now I have a project for you guys, if you're up for it. I don't really have the time or resources available to create it but thought you might be able to in your spare time.

A buddy of mine would like a lisp routine that converts currency between the US dollar, Canadian Dollar, and Mexican Peso. Exchange rates are as follows:

1.00 USD = 1.32158 CAD

1.00 CAD = 0.756669 USD  

1.00 USD = 11.1436 MXN

1.00 MXN = 0.0897377 USD

1.00 MXN = 0.118591 CAD

1.00 CAD = 8.43232 MXN

Nothing too hard but I'm completely stacked here at work and don't have the time to script it out for him. Hell, knowing you guys, you'll prob have 20 different codes up before I wake up in the morning hehe.

Another question, is there a way to make an alert type box in which the user can imput data through that or must it all be done through the command line?

Thanks,
Jon

hendie

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #1 on: October 21, 2003, 07:04:12 AM »
easiest and simplest route is to use VBA.
the only problem I see with your request is that the exchange rate is always going to fluctuate.
You could create a VBA app that calculates different amounts based on exchange rates.
you would need 3 textboxes for current exchange rate, 1 each for USD, CAD & MXN with a checkbox against each (to select which currency to evaluate). Checkboxes in a group so only 1 option can be true.
1 textbox for inputing sum to be calculated and 1 textbox for output
then it's just somethin like:
Code: [Select]

If
checkbox1.value=T then
SelectedCurrency=USD.value
else if
checkbox2.value=T then
SelectedCurrency=CAD.value
else if
checkbox3.value=T then
SelectedCurrency=MXN.value
end if


OutputSum.value= InputSum.value * SelectedCurrency

you could also add a couple of labels and change the caption based on the currency selected.



to be honest, a quicker way would be to log on to a currency converter and use that ~ you would need to find the latest exchange rates anyway  :lol:

now if you were converting between different units of measurement.. that would be a more useful project because the values are always static

nivuahc

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #2 on: October 21, 2003, 09:44:27 AM »
Spiffy little routine collegeCAD.

And, since you're in the learning process, would you like some constructive criticism? Keep in mind that most of what I say is just my opinion so don't take offense to anything I say.

First of all let's talk about variable names. There's no reason not to make your variable names descriptive. And that goes for LISP as well as VBA or any other laguage you're programming in. So setting a variable and calling it num1 which is actually a letter (A,B,C, etc...) is a bad habit to get into. In fact, it would make more sense to name it comething like column_count since that's what it actually is. Same goes for your other variables.

I say that for two reasons:

1. When someone else looks at your code it will make more sense if the variable names are indicative of what they stand for.
2. A year from now when you go back and look at your code you'll have to study it in order to remember what you were doing. Setting the variable names properly now will save you headaches in the future.

Next, let's talk about comments. I realize that this was a quick throw-together and that's great. But you should always put comments in your code explaining what is going on. That helps out with both of the points I mentioned above. And you'd be surprised... some of the most powerful LISP routines I have were quick 'throw-together' type routines that I needed right then and there. Some time later I would need that routine again and, not commenting those little routines, well that just makes it difficult to find what you're looking for. Visual LISP has some great 'Search' features and commenting little snippets like this could save you tons of time in the future.

Now to your actual code. COLUMNS are the labels going across the top (A, B, C, D, etc...) and ROWS are the numbered labels on the left so the wording in your prompts needs to change to reflect that.

Secondly, should a person want more than 26 COLUMNS they end up getting ...X Y Z [ \ ] ^ - etc...

It shouldn't bee too difficult to modify your routine so that, should the value of num1 be greater than 26 then the character returned is double lettered as in AA, BB, CC, DD etc... or AA, AB, AC, AD etc...

The second option would be easier, in my opinion, and it would conform to actual spreadsheet standards (open up EXCEL and take a scroll to the left and you'll see what I mean).

Anyway, nice job! And I hope I didn't come across too harsh because I sincerely wasn't trying to be. If you're in the learning stages and you pick up a couple of bad habits they'll bite you in the butt some time in the future. And they'll be much harder to change later on.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Hey guys, long time no talk! Need some help if you can spare
« Reply #3 on: October 21, 2003, 09:54:52 AM »
Quote
A buddy of mine would like a lisp routine that converts currency between the US dollar, Canadian Dollar, and Mexican Peso. Exchange rates are as follows:

Here's my feeble attempt, could use more error checking.
Code: [Select]
;;; calculate the exchange rate
;;;1.00 USD = 1.32158 CAD 1.00 CAD = 0.756669 USD
;;;1.00 USD = 11.1436 MXN 1.00 MXN = 0.0897377 USD
;;;1.00 MXN = 0.118591 CAD 1.00 CAD = 8.43232 MXN
(defun exchg (from to amt / ex)
  (cond ((and (= from "US") (= to "CA")) (setq ex (* amt 1.32158)))
        ((and (= from "US") (= to "MX")) (setq ex (* amt 11.1436)))
        ((and (= from "CA") (= to "US")) (setq ex (* amt 0.756669)))
        ((and (= from "CA") (= to "MX")) (setq ex (* amt 8.43232)))
        ((and (= from "MX") (= to "US")) (setq ex (* amt 0.0897377)))
        ((and (= from "MX") (= to "CA")) (setq ex (* amt 0.118591)))
        (T (princ))
        )
  ex
  )

;;; string parse into list i.e. US56 becomes ("US" . 56.00)
(defun strc (str / cd)
  (setq cd
         (cons (substr str 1 2)(atof (substr str 3 (strlen str))))
        )
  )
 
;;; main function
(defun c:cvf (/ input src cnv output)
  (textscr)
 
  (prompt
    "\nEnter currency type & ammount in the form of CurrencyDollorAmount..."
    )
  (prompt "\nExample: US40.00 - 40.00 US dollars..")
  (prompt "\nUS = US Dollar")
  (prompt "\nCA = Canadian Dollar")
  (prompt "\nMX = Mexican Peso")
  (setq input
         (strcase
           (getstring
             "\nEnter currency type & ammount to convert from (US CA MX) : "
             )
           )
        ) ;setq
  ;; check input for accuracy
  (if input
    (cond ((= (substr input 1 2) "US") (setq src (strc input)))
          ((= (substr input 1 2) "CA") (setq src (strc input)))
          ((= (substr input 1 2) "MX") (setq src (strc input)))
          (T
           (alert "Un-known input type...")
           (setq input nil)
           (princ)
           )
          )
    ) ;if
 
  (if src
    (progn
      (initget "Us Ca Mx")
      (setq cnv (strcase (getkword "\nConvert to (Us Ca Mx)? : ")))
      )
    ) ;if
 
  (if cnv
    (setq
      output
      (strcat " "
              (rtos (cdr src) 2 2)
              " "
              (car src)
              " = "
              (rtos (exchg (car src) cnv (cdr src)) 2 2)
              " "
              cnv
              ); strcat
      )
    ); if
  );defun
TheSwamp.org  (serving the CAD community since 2003)

collegeCAD

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #4 on: October 21, 2003, 03:24:30 PM »
Quote from: hendie
easiest and simplest route is to use VBA.
the only problem I see with your request is that the exchange rate is always going to fluctuate.
You could create a VBA app that calculates different amounts based on exchange rates.
you would need 3 textboxes for current exchange rate, 1 each for USD, CAD & MXN with a checkbox against each (to select which currency to evaluate). Checkboxes in a group so only 1 option can be true.
1 textbox for inputing sum to be calculated and 1 textbox for output
then it's just somethin like:
Code: [Select]

If
checkbox1.value=T then
SelectedCurrency=USD.value
else if
checkbox2.value=T then
SelectedCurrency=CAD.value
else if
checkbox3.value=T then
SelectedCurrency=MXN.value
end if


OutputSum.value= InputSum.value * SelectedCurrency

you could also add a couple of labels and change the caption based on the currency selected.



to be honest, a quicker way would be to log on to a currency converter and use that ~ you would need to find the latest exchange rates anyway  :lol:

now if you were converting between different units of measurement.. that would be a more useful project because the values are always static


heh, he originally wanted it in VB but I unfortunately know NADA about programming in it... so I said I'm sure I or someone else could probably put something together in LISP... I'll pass this along w/ the other codes on to him and see if it helps him. Thanks!

collegeCAD

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #5 on: October 21, 2003, 03:29:23 PM »
Quote from: nivuahc
Spiffy little routine collegeCAD.

And, since you're in the learning process, would you like some constructive criticism? Keep in mind that most of what I say is just my opinion so don't take offense to anything I say.

First of all let's talk about variable names. There's no reason not to make your variable names descriptive. And that goes for LISP as well as VBA or any other laguage you're programming in. So setting a variable and calling it num1 which is actually a letter (A,B,C, etc...) is a bad habit to get into. In fact, it would make more sense to name it comething like column_count since that's what it actually is. Same goes for your other variables.

I say that for two reasons:

1. When someone else looks at your code it will make more sense if the variable names are indicative of what they stand for.
2. A year from now when you go back and look at your code you'll have to study it in order to remember what you were doing. Setting the variable names properly now will save you headaches in the future.

Next, let's talk about comments. I realize that this was a quick throw-together and that's great. But you should always put comments in your code explaining what is going on. That helps out with both of the points I mentioned above. And you'd be surprised... some of the most powerful LISP routines I have were quick 'throw-together' type routines that I needed right then and there. Some time later I would need that routine again and, not commenting those little routines, well that just makes it difficult to find what you're looking for. Visual LISP has some great 'Search' features and commenting little snippets like this could save you tons of time in the future.

Now to your actual code. COLUMNS are the labels going across the top (A, B, C, D, etc...) and ROWS are the numbered labels on the left so the wording in your prompts needs to change to reflect that.

Secondly, should a person want more than 26 COLUMNS they end up getting ...X Y Z [ \ ] ^ - etc...

It shouldn't bee too difficult to modify your routine so that, should the value of num1 be greater than 26 then the character returned is double lettered as in AA, BB, CC, DD etc... or AA, AB, AC, AD etc...

The second option would be easier, in my opinion, and it would conform to actual spreadsheet standards (open up EXCEL and take a scroll to the left and you'll see what I mean).

Anyway, nice job! And I hope I didn't come across too harsh because I sincerely wasn't trying to be. If you're in the learning stages and you pick up a couple of bad habits they'll bite you in the butt some time in the future. And they'll be much harder to change later on.


no, no, no. Constructive criticisim is good and much appreciated! Thanks!

I agree about the comments and variable names completely. But, like both you and I said, we threw this together in a matter of minutes so it wasn't real high on my list of priorities. I'll probably go in later today and change it up so it is easier to pick apart and look at for future references, no doubt.

Now a quick question, using the code I have, how would I go about changing it to AA, AB, etc.? I noticed the same problem you described. Is there a way to make it go infinitely? I know just messing around w/ it, when I put in something like 6,867 for the number of rows, AutoCAD "crashes." I'm guessing it has somethingn to do w/ a lack of characters after the ascii numbers have all run out.

Enlighten me, Obi Wan  :D

Bill::Meat4Brains

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #6 on: October 21, 2003, 04:51:30 PM »
does this help?
Code: [Select]

(defun chr_index (index / n1 n2 n3 tag)
   (if (and (> index 0) (< index 18279))
      (progn
         (setq n1 0 n2 0 n3 0)
         (repeat index
            (setq n1 (1+ n1))
            (if (> n1 26) (setq n1 1 n2 (1+ n2)))
            (if (> n2 26) (setq n2 1 n3 (1+ n3)))
         )
         (setq tag (strcat
            (if (= n3 0) "" (chr (+ 64 n3)))
            (if (= n2 0) "" (chr (+ 64 n2)))
            (if (= n1 0) "" (chr (+ 64 n1)))
         ))
      )
      (progn
         (alert (strcat "CHR_INDEX OUT OF RANGE (1 TO 18278):  " (rtos index 2 0) "."))
         (quit)
      )
   )
)

nivuahc

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #7 on: October 21, 2003, 04:58:57 PM »
Well Bill beat me to it!  :lol:

Nicely done, BTW.

collegeCAD

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #8 on: October 22, 2003, 02:05:57 AM »
Quote from: Bill::Meat4Brains
does this help?
Code: [Select]

(defun chr_index (index / n1 n2 n3 tag)
   (if (and (> index 0) (< index 18279))
      (progn
         (setq n1 0 n2 0 n3 0)
         (repeat index
            (setq n1 (1+ n1))
            (if (> n1 26) (setq n1 1 n2 (1+ n2)))
            (if (> n2 26) (setq n2 1 n3 (1+ n3)))
         )
         (setq tag (strcat
            (if (= n3 0) "" (chr (+ 64 n3)))
            (if (= n2 0) "" (chr (+ 64 n2)))
            (if (= n1 0) "" (chr (+ 64 n1)))
         ))
      )
      (progn
         (alert (strcat "CHR_INDEX OUT OF RANGE (1 TO 18278):  " (rtos index 2 0) "."))
         (quit)
      )
   )
)


how would I incorporate that into the existing routine I already have?

And btw, how do you create more than one function in a lisp file? do you simply drop downa few spaces after the first one ends and begin a new one w/ defun, etc.?

daron

  • Guest
Hey guys, long time no talk! Need some help if you can spare
« Reply #9 on: October 22, 2003, 12:14:32 PM »
Quote
how would I incorporate that into the existing routine I already have?

And btw, how do you create more than one function in a lisp file? do you simply drop downa few spaces after the first one ends and begin a new one w/ defun, etc.?


1) In your routine at the place you'd be needing the function type (chr_index index) <-supply the information that index is asking for.
2) Exactly.
Also, to load one function from another lisp file, type: (load "NameOfFileToLoad.lsp")<- you might need to write the whole path if it is in a different folder.