lisp convert solid volume in liter and gallons

lisp convert solid volume in liter and gallons

Anonymous
Not applicable
1,256 Views
9 Replies
Message 1 of 10

lisp convert solid volume in liter and gallons

Anonymous
Not applicable

Hi,

 

i need a way to convert volume(cube in)  in liter and gallons 

 

i got this to fing the volume,

 

(defun c:3DVOL (/ eName 3dObj)
(vl-load-com)
(if (and (setq eName (car (entsel "\n >> Select 3D Object for Current Volume: ")))
(= "3DSOLID" (cdr (assoc 0 (entget eName))))
(vlax-property-available-p
(setq 3dObj (vlax-ename->vla-object eName))
'volume))
(princ
(strcat
"\n <!> 3D Volume = "
(rtos (vla-get-volume 3dObj) 2 2)
" <!> ")))
(princ))

 

 

( it's a copy past from another topic i dont have any credit on it lol ) 

 

 

for gallons is x / 231

for liter is  x / 61.02374

 

 

thank you 

0 Likes
Accepted solutions (2)
1,257 Views
9 Replies
Replies (9)
Message 2 of 10

Kent1Cooper
Consultant
Consultant
Accepted solution

....

  (strcat
    "\n<!> 3D Volume = "
    (rtos (vla-get-volume 3dObj) 2 2) " cubic inches, or "

    (rtos (/ (vla-get-volume 3dObj) 231) 2 2) " gallons, or "

    (rtos (/ (vla-get-volume 3dObj) 61.02374) 2 2) " liters. <!>")

  ); strcat

  )); [the others on the end of your current line, completing (princ) and (if) functions

....

 

Change those last arguments in the (rtos) functions from 2 to whatever is appropriate to your needs for each type of volume [there's not much point, after all, in dividing the volume by a number to 5 decimal places for liters if you're only going to show the number of liters to 2 decimal places].

Kent Cooper, AIA
Message 3 of 10

Anonymous
Not applicable

here's my new code 

 

(defun c:3DVOL (/ eName 3dObj)
(vl-load-com)
(if (and (setq eName (car (entsel "\n >> Select 3D Object for Current Volume: ")))
(= "3DSOLID" (cdr (assoc 0 (entget eName))))
(vlax-property-available-p
(setq 3dObj (vlax-ename->vla-object eName))
'volume))
(princ
(strcat
"\n<!> 3D Volume = "
(rtos (vla-get-volume 3dObj) 2 2) " cubic inches, or "
(rtos (/ (vla-get-volume 3dObj) 231) 2 2) " gallons, or "
(rtos (/ (vla-get-volume 3dObj) 61.02374) 2 2) " liters. <!>")
); strcat
));

 

 

 

is that normal that in result it's said 2 times ? 

 

exemple:

 

<!> 3D Volume = 1451526.96 cubic inches, or 6283.67 gallons, or 23786.27 liters. <!>"\n<!> 3D Volume = 1451526.96 cubic inches, or 6283.67 gallons, or 23786.27 liters. <!>"

 

 

thank you ?

 

0 Likes
Message 4 of 10

stevor
Collaborator
Collaborator
Accepted solution

Probably is if that string is the last value in that function.

To prevent that, insert:

  ); strcat
) (princ) );

Where it says:

); strcat
));

S
Message 5 of 10

Anonymous
Not applicable
thank you it works !
0 Likes
Message 6 of 10

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

is that normal that in result it's said 2 times ? 

.... 


To elaborate a little more....

 

AutoLisp functions always return the result of the last expression in them at the end.  So your (princ (strcat...)) put that text out there, and since that was the last function in the routine, its result was also returned by the overall routine -- that's the reason for the repetition.

 

The (princ) with no arguments at the end [which you did have in your original code in Post 1] is the typical "quiet exit" that you find in many routines, since it returns nothing, and therefore as the last function in the routine, causes the overall routine to return nothing at the end, leaving the result of the (princ (strcat...)) part within as the last thing put out to the Command: line.  This "exit quietly" function is specifically mentioned in Help for at least one of those printing functions -- (princ) or (print) or (prin1) -- any of which will do the same.

 

[Personally, I would use the (prompt) function for the reporting part, because that's what it's doing.  The (prin...) functions are built to also have the option to do other things such as send the result out to a file, so while they "work" for the purpose, their capabilities are wasted on mere prompting.]

Kent Cooper, AIA
Message 7 of 10

Anonymous
Not applicable

I replace princ for prompt and it works too,

 

if i want to save the values as a insert field that ia create for it like US gal for the gallons and LITER

 

is that possible ?

0 Likes
Message 8 of 10

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... if i want to save the values as a insert field....


... you will need someone else to help you.  [I haven't had the use of new-enough versions long enough yet to become accustomed to Fields.]

Kent Cooper, AIA
0 Likes
Message 9 of 10

david_wilson8B8GE
Explorer
Explorer

Is there a way to be able to select multiple solids in one go without having to union?

0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

Another way to display the answer. NOTE 231.0 not 231 to make sure get a real answer not an integer answer.

 

 

 

(alert
(strcat
"\n<!> 3D Volume = \n"
(rtos (vla-get-volume 3dObj) 2 2) " cubic inches, or \n"
(rtos (/ (vla-get-volume 3dObj) 231.0) 2 2) " gallons, or \n"
(rtos (/ (vla-get-volume 3dObj) 61.02374) 2 2) " liters. <!>"
)
)

 

Just a side note

SeaHaven_0-1685430617244.png

 

 

0 Likes