Author Topic: handle numbers  (Read 3929 times)

0 Members and 1 Guest are viewing this topic.

Robert98

  • Guest
handle numbers
« on: October 13, 2010, 01:07:44 PM »
I'm beginner in autolisp programming and at a peace of my codes an error occurs , please tell me what is my mistake ? :realmad:
code :
Quote
(defun mta (/ x y z)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq p (* x y))
  (cond
    ((z nil) (princ (+ p 0)))      
    ((numberp z) (princ (+ p z)))

  )               ;end cond
)

ronjonp

  • Needs a day job
  • Posts: 7533
Re: handle numbers
« Reply #1 on: October 13, 2010, 01:19:56 PM »
If I'm understanding your question ... see if this makes any sense to you:

Code: [Select]
(setq pt (getpoint))

(setq X*Y (* (car pt) (cadr pt)))

(if (and (setq z (caddr pt)) (numberp z))
  (alert (rtos (+ X*Y z)))
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

HasanCAD

  • Swamp Rat
  • Posts: 1423
Re: handle numbers
« Reply #2 on: October 13, 2010, 01:36:14 PM »
An answer from a beginner

In case of using this code as a subroutine

Code: [Select]
(defun mta (x y z /)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq p (* x y))
  (cond
    ((=z nil) (princ (+ p 0)))     
    ((numberp z) (princ (+ p z)))

  );end cond
)

In case of using this code as a separate lisp
* PS command line is MTA

Code: [Select]
(defun c:mta (/ pnt1 pnt2 x y z)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq pnt1 (getpoint "\n pick Lower left corner"))
  (setq pnt2 (getcorner pnt1 "\n pick Upper right corner")
x (- (car pnt2) (car pnt1))
y (- (cadr pnt2) (cadr pnt1))
z (- (caddr pnt2) (caddr pnt1))
)
  (setq p (* x y))
  (cond
    ((=z nil) (princ (+ p 0)))     
    ((numberp z) (princ (+ p z)))

  );end cond
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: handle numbers
« Reply #3 on: October 13, 2010, 01:43:27 PM »
My simple answer. 8-)
Code: [Select]
(defun mta (x ; variable value supplied by calling routine
            y
            z
            / p)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq p (* (x y)) ; note that if both are integer's the result will be an integer
        ;;  if one is a real number the result will be a real number
        (princ "\n") ; line feed
  (cond
    ((null z) (princ (+ p 0)))      
    ((numberp z) (princ (+ p z)))
    (t (princ (+ p 0))(princ " Z is not a number or nil"))
  )               ;end cond
)


(defun c:test()
  (mta 10 2 nil)
  (princ)
)
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.

HasanCAD

  • Swamp Rat
  • Posts: 1423
Re: handle numbers
« Reply #4 on: October 13, 2010, 01:46:18 PM »
Another option with alert message
Code: [Select]
(defun c:mta (/ x y z)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq pnt1 (getpoint "\n pick Lower left corner"))
  (setq pnt2 (getcorner pnt1 "\n pick Upper right corner")
x (- (car pnt2) (car pnt1))
y (- (cadr pnt2) (cadr pnt1))
z (- (caddr pnt2) (caddr pnt1))
)
  (setq p (* x y))
  (cond
    ((= z 0.0) (alert (strcat "Area = "(rtos (+ p 0) 2 2))))     
    ((numberp z) (alert (strcat "volume= "(rtos (+ p z) 2 2))))

  );end cond
)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: handle numbers
« Reply #5 on: October 13, 2010, 01:55:33 PM »
Felt left out.  Here is my entry.

Code: [Select]
(defun mta ( x y z ) (+ (if (numberp z) z 0) (* x y)))
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ronjonp

  • Needs a day job
  • Posts: 7533
Re: handle numbers
« Reply #6 on: October 13, 2010, 01:57:00 PM »
Felt left out.  Here is my entry.

Code: [Select]
(defun mta ( x y z ) (+ (if (numberp z) z 0) (* x y)))

You beat me  :lol:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: handle numbers
« Reply #7 on: October 13, 2010, 01:58:20 PM »
Felt left out.  Here is my entry.

Code: [Select]
(defun mta ( x y z ) (+ (if (numberp z) z 0) (* x y)))

Without a more lucid description of the op's wants I think this is the answer, good job Tim.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: handle numbers
« Reply #8 on: October 13, 2010, 01:58:54 PM »
Felt left out.  Here is my entry.

Code: [Select]
(defun mta ( x y z ) (+ (if (numberp z) z 0) (* x y)))

You beat me  :lol:

Figured all other ways were posted, so decide to post one that I would use, even though it might not be easy to read for someone beginning.   :-D

Felt left out.  Here is my entry.

Code: [Select]
(defun mta ( x y z ) (+ (if (numberp z) z 0) (* x y)))

Without a more lucid description of the op's wants I think this is the answer, good job Tim.

Thanks Michael.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Robert98

  • Guest
Re: handle numbers
« Reply #9 on: October 13, 2010, 02:45:47 PM »
If I'm understanding your question ... see if this makes any sense to you:

Code: [Select]
(setq pt (getpoint))

(setq X*Y (* (car pt) (cadr pt)))

(if (and (setq z (caddr pt)) (numberp z))
  (alert (rtos (+ X*Y z)))
)

An answer from a beginner

In case of using this code as a subroutine

Code: [Select]
(defun mta (x y z /)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq p (* x y))
  (cond
    ((=z nil) (princ (+ p 0)))     
    ((numberp z) (princ (+ p z)))

  );end cond
)

In case of using this code as a separate lisp
* PS command line is MTA

Code: [Select]
(defun c:mta (/ pnt1 pnt2 x y z)
  ;multiply x and y and if z is not nil and is any number so add it to x*y
  (setq pnt1 (getpoint "\n pick Lower left corner"))
  (setq pnt2 (getcorner pnt1 "\n pick Upper right corner")
x (- (car pnt2) (car pnt1))
y (- (cadr pnt2) (cadr pnt1))
z (- (caddr pnt2) (caddr pnt1))
)
  (setq p (* x y))
  (cond
    ((=z nil) (princ (+ p 0)))     
    ((numberp z) (princ (+ p z)))

  );end cond
)


thanks for your tips , but I don't have a single point with x , y , z coordinates.my x , y , z are any positive number for example distances for area and volume computations . I don't pick any point and my "mta" function means is : multiply,then , if maybe add z value :-(

ronjonp

  • Needs a day job
  • Posts: 7533
Re: handle numbers
« Reply #10 on: October 13, 2010, 02:50:51 PM »
Perhaps a bit more of what you're trying to accomplish  ... to me a "Z" value would be related to a point? Either way ... IMO Tim's solution is the most straight forward answer. Your X and Y are always numbers correct?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: handle numbers
« Reply #11 on: October 13, 2010, 03:29:33 PM »
If they are points, wouldn't it be easier to use something like one of the following?
Code: [Select]
(defun mta (lst)
  (+ (* (car lst) (cadr lst))
     (if (numberp (caddr lst))
       (caddr lst)
       0.
     )
  )
)

OR

(defun mta (lst)
  (+ (* (car lst) (cadr lst))
     (cond ((caddr lst))
           (0.)
     )
  )
)

That way you could also apply it to a list of point lists with mapcar.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: handle numbers
« Reply #12 on: October 13, 2010, 11:42:56 PM »
Robert you will find the Lispers usually use variable names "x" "y" and "z" for processing
point data. You should use something line "n1" "n2" and "n3" for numbers.
Just a suggestion. 8-)
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.

Robert98

  • Guest
Re: handle numbers
« Reply #13 on: October 14, 2010, 07:29:34 AM »
Robert you will find the Lispers usually use variable names "x" "y" and "z" for processing
point data. You should use something line "n1" "n2" and "n3" for numbers.
Just a suggestion. 8-)

Dear CAB, that's a good idea and I'll use it at my future codes.
special thanks to all members that helped me.