Jump to content

Recommended Posts

Posted

Hello, I am new to LISP and programming in general and stuck with an issue. I am to create a program that will calculate the area and volume ofa 3D box.

 

Input:

Enter width of 3D Box:

Enter depth of 3D Box:

Enter height of 3D Box:

 

Output:

-------------------------------------------------

Box Statistics

-------------------------------------------------

Box Width: XX.XXXX

Box Depth: XX.XXXX

Box Height: XX.XXX

Box Surface Area: XX.XXXX square inches

Box Volume: XX.XXXX cubic inches

-------------------------------------------------

 

Algorithms: V=w*d*h

SA= 2(hd + dw + hw)

 

Now here is what I have as far as drawing the 3D box, I understand mostly how to figure the W/D/H of both and such, but I'm having trouble figuring out best way to make these all print how they should and look correct. Any help is appreciated.

 

 

(defun C:3DBOX ( / )

 

(setq SP (getpoint "\nPick Starting Point of Box: "))

(setq L (getreal "\What is the Length of the box? "))

(setq W (getreal "\What is the Width of the box? "))

(setq H (getreal "\What is the Height of the box?

 

(command ".box" SP "L" L W H )

 

);eop

Posted

Please look for PRINT, STRCAT and RTOS functions.

Posted

Acadstudent,

 

Are you familiar with reverse polish notation ?

 

You seem to be stuck there. This is among the first thing you will need to get familiar with

 

(setq v (* w d h)                             ;  V = w * d * h           
(setq sa (*  2  (+ (* h d) (* d w) (* h w)))  ;  SA = 2(hd + dw + hw)

 

ymg

Posted

Okay so I should set up something like:

 

(setq BW "L" BD "W" BH "H" BSA "(* ( + (* BH BD) (* BD BW) (* BH BW) ) 2)" BV "( * BW BD BH)" )

 

(strcat "Box Width: " BW)

(strcat "Box Depth: " BD)

(strcat "Box Height: " BH)

(strcat "Box Surface Area: " BSA)

(strcat "Box Volume: " BV)

 

Does this look like the right idea?

Posted

As already told you, you should look for RTOS to convert a number into a string. By using your way you set those variables to strings; to understand just test this into console/command prompt:

(progn
(setq aa 1.2345)
(print "aa")
(print (rtos aa 2 4))
)

 

Presuming that L, W and H were the ones defined into your previous post, then:

(setq BSA (* (+ (* H W) (* W L) (* H [color=red]L[/color])) 2)
     BV  (* L W H))
(print (strcat "Box Width: " (rtos L 2 3)))

Please edit your post and add the code tags!

Posted

I have taken you advice and changed my code as mentioned. I'm still not getting the correct returns, but I appreciate all the help in getting there. Here is what I have thus far:

 


(defun C:3DBOX ( / )


 (setq SP  (getpoint "\nPick Starting Point of Box: ")) ;Starting 
Point of box
 (setq W  (getreal "\What is the Width of the 
box? "));Box Length           

 (setq D  (getreal "\What is the Depth of the box? ")) ;Box 
Width            

 (setq H  (getreal "\What is the Height of the box? "));Box 
Height           


 (command ".box" SP "L" L W H "" )


 (setq BW W BD D BH H BSA (* (+ (* H W) (* W L) (* H L)) 2) BV ( * D W 
H) )


(print
(strcat "Box Width: " (rtos W 2 3))
(strcat 
"Box Depth: " (rtos D 2 3))
(strcat "Box Height: " (rtos H 2 
3))
(strcat "Box Surface Area: " (rtos BSA 2 3))
(strcat "Box 
Volume: " (rtos BV2 3))
)
 


 );eop

 

 

thanks again!

Student

Posted

Just a bit simpler to see whats going on

 

 

(setq BW W BD D BH H BSA (* (+ (* H W) (* W L) (* H L)) 2) BV ( * D W 
H) )


(setq BW W 
BD D 
BH H 
BSA (* (+ (* H W) (* W L) (* H L)) 2) 
BV ( * D W H) 
)

Posted

Seems that you changed the L variable to D, but didn't adjusted the code accordingly. I have did some corrections and commented them:

 

(defun C:3DBOX( / [color=red]SP W D H BSA BV[/color] )   [color=blue];;get used to localize variable - to avoid conflicts with other functions[/color]
[color=blue] ;;ensure user input[/color]
(if [color=red](and [/color]
     (setq SP (getpoint "\nPick Starting Point of Box: "))   ;Starting Point of box
     (setq W  (getreal "\[color=red]n[/color]What is the Width of the box? "))  ;Box Length
     (setq D  (getreal "\[color=red]n[/color]What is the Depth of the box? "))  ;Box Width
     (setq H  (getreal "\[color=red]n[/color]What is the Height of the box? ")) ;Box Height
    [color=red])[/color]
[color=red] (progn[/color]
  [color=blue];;get used to take care of localized AutoCAD[/color]
  (command "[color=red]_[/color].box" SP "L" W [color=red][b]D[/b][/color] H)   [color=red];;order of edge dimensions ?!?[/color]

  [color=blue];;don't need to change variables name, it just make debug harder!!! [/color]
  (setq BSA (* (+ (* H W)
                  (* W D)
                  (* H D)) 
               2)
        BV  (* D W H))

  [color=blue];;switch to text window for report[/color]
[color=red]  (textscr)[/color]
  [color=blue];;PRINT/PROMPT accept only one argument!!![/color]
  (prompt (strcat "[color=red]\n[/color]Box Width:        " (rtos W 2 3))[color=red])[/color]   [color=blue];;print each to new line[/color]
  [color=red](prompt [/color](strcat "[color=red]\n[/color]Box Depth:        " (rtos D 2 3))[color=red])[/color]
  [color=red](prompt [/color](strcat "[color=red]\n[/color]Box Height:       " (rtos H 2 3))[color=red])[/color]
  [color=red](prompt [/color](strcat "[color=red]\n[/color]Box Surface Area: " (rtos BSA 2 3))[color=red])[/color]
  [color=red](prompt [/color](strcat "[color=red]\n[/color]Box Volume:       " (rtos [color=red]BV 2[/color] 3)))   [color=blue];;missed a space[/color]
[color=red]  )
)[/color]

[color=red] (princ) [/color]  [color=blue];;exit silent[/color]
);eop

You may want also to look for GETDIST instead of GETREAL to allow for interactive input.

Posted

MSasu,

 

Thank you, that makes sense, and I appreciate how you marked up where I made some mistakes, that helps me to realize and see where the issues were. This program works beautifully now! I added in the "Cubic Inches" and "Square Inches" text after the formulas in the rtos functions to complete it. I am curious though if there is a way to return a leading "0" in front of any single digit returns. Example being: if the user inserts 5 as the Width of the box, when that it returned it is returned as:

 

5.000

 

I thought that the 2 3 following the W/D/H/BSA/BV would take care of this, however it does not seem to. Any thoughts? Again I appreciate all of your help and will be posting many more questions as I realize this forum is extremely helpful to learning quickly!

 

Thank you again!

Acadstudent

Posted (edited)

You're entirely welcome!

 

For printing those zeros, you will need to play with DIMZIN system variable and RTOS arguments. It should be set to 0 to get the trailing zeros:

(rtos 2.5 2 4) ;this will return "2.5000" for DIMZIN = 0

A good programming practice is to restore user's environment after affecting it:

(defun MyFunction( / oldDimZin )
...
(setq oldDimZin (getvar "DIMZIN"))   ;retain current state of the system variable
(setvar "DIMZIN" 0)                  ;force display of trailing zeros
...
do your prints
...
(setvar "DIMZIN" oldDimZin)          ;restore the system variable

(princ)
)

Edited by MSasu

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...