Jump to content

# Calculating weight of 2d shape & 3dsolid object by asigned material

## Recommended Posts

Hello,

I want share two lisp function from my collection,maybe find usefull for other.

First function(C:CalcWt) ,i get from internet by my request (thx to the autor),and second i modify for solid calculaton.In both i add materials that usualy i use,but material list is'n limited.

Maybe the both can be in one LSP,with option of choice,2d shape or solid or auto recognize the selected object.

Difference between this two function,exept the selected object(2d shape or solid,mean reading area or volume),is in material density measurements,(for example Al.for 2d shape 0.0027 and Al. for solid 0.0000027) and expression of sufix (kg/m or kg)

```
;;; Lisp function for calculating weight per metar lenght of section(profile) with choice to assign material.
::: Option to extend  materials list.
=====================================================================================
(defun C:CalcWt  (/ area en ent obj ospace prefix pt remd resp suffix)
;;local defun
(defun density  (mat)
(cond ((eq "Aluminium" mat) 0.0027)
((eq "Steel" mat) 0.00787)
((eq "SS316" mat) 0.008)
((eq "Glass" mat) 0.0026)
((eq "Rubber" mat) 0.0011)
((eq "Silicon" mat) 0.00233)
((eq "Plastic" mat) 0.0012)
((eq "Granit" mat) 0.0026)
((eq "Marble" mat) 0.0027)
;; add other materials similarly
(T nil)
)
)
;;main part
(setvar "dynmode" 3)
(setvar "dynprompt" 1)
(setq ospace (vla-get-block
(vla-get-activelayout
(vla-get-activedocument
(vlax-get-acad-object))))
)
(setq prefix "Weight="
suffix "kg/m"
)
(while (setq ent (entsel "\nSelect a shape (or press Enter to Exit) >>"))
(setq en (car ent))
(if (wcmatch (cdr (assoc 0 (entget en))) "REGION,*POLYLINE,CIRCLE,ELLIPSE")
(progn
(setq obj (vlax-ename->vla-object en))
(initget "Aluminium Steel SS316 Glass Rubber Silicon Plastic Granit Marble");<-- add other materials here
(setq resp (getkword
"\Choose material [ALuminium/STeel/SS316/GLass/RUbber/SIlicon/PLastic/GRanit/MArble] <AL>: "))
(setq area (strcat prefix
(rtos (* (vla-get-area obj)
(density resp))
2 ;<-- metric
2 ;<-- precision
)
suffix
)
)
(setq pt (getpoint "\nPick text point: "))
(vla-addtext
ospace
area
(vlax-3d-point pt)
3.0 ;;<-- change text height here
)
)
)
)
(princ)
)
(prompt "\n\t\t>>>\tType CALCWT or calcwt to calc mass\t<<<")
(prin1)
(vl-load-com)
```

```
;;; Lisp function for calculating weight of 3dsolid (extruded shape) in kilograms with choice to assign material.
::: Option to extend  materials list.
=====================================================================================
(defun C:Calc3DWt  (/ volume en ent obj ospace prefix pt remd resp suffix)
;;local defun
(defun density  (mat)
(cond ((eq "Aluminium" mat) 0.0000027)
((eq "Steel" mat) 0.00000787)
((eq "SS316" mat) 0.000008)
((eq "Glass" mat) 0.0000026)
((eq "Rubber" mat) 0.0000011)
((eq "Silicon" mat) 0.00000233)
((eq "Plastic" mat) 0.0000012)
((eq "Granit" mat) 0.0000026)
((eq "Marble" mat) 0.0000027)
;; add other materials similarly
(T nil)
)
)
;;main part
(setvar "dynmode" 3)
(setvar "dynprompt" 1)
(setq ospace (vla-get-block
(vla-get-activelayout
(vla-get-activedocument
(vlax-get-acad-object))))
)
(setq prefix "Weight="
suffix "kg"
)
(while (setq ent (entsel "\nSelect a Solid (or press Enter to Exit) >>"))
(setq en (car ent))
(if (wcmatch (cdr (assoc 0 (entget en))) "3DSOLID")
(progn
(setq obj (vlax-ename->vla-object en))
(initget "Aluminium Steel SS316 Glass Rubber Silicon Plastic Granit Marble");<-- add other materials here
(setq resp (getkword
"\Choose material [ALuminium/STeel/SS316/GLass/RUbber/SIlicon/PLastic/GRanit/MArble] <AL>: "))
(setq volume (strcat prefix
(rtos (* (vla-get-volume obj)
(density resp))
2 ;<-- metric
2 ;<-- precision
)
suffix
)
)
(setq pt (getpoint "\nPick text point: "))
(vla-addtext
ospace
volume
(vlax-3d-point pt)
3.0 ;;<-- change text height here
)
)
)
)
(princ)
)
(prompt "\n\t\t>>>\tType CALC3DWT or calc3dwt to calc massof solid\t<<<")
(prin1)
(vl-load-com)
```

##### Share on other sites

• 2 weeks later...

dear friend

i can't use your lisp .would you please send the lisp file to me?email:naserrishehri@yahoo.com

##### Share on other sites

Here are the attachments.

Calcwt.lsp

Calc3dwt.lsp

##### Share on other sites

dear friend

thanks a lot for lisps.i could get use them.

kind regards

##### Share on other sites

• 1 month later...

I like the 3D solid lisp but should be modified to select more then one solid.

##### Share on other sites

• 1 year later...

I am excited by the CalcWt.lsp I like it a lot.

I am experiencing a problem with it though. Sometimes the text is inserted but not in the desired point. It seems to work when the code is first run, but if I go back to it in the same drawing session after doing other drawing work, the problem materialises.

I wonder if anybody can easily see the reason for this.

If anybody is inclined to help further, I would like to see the Dynamic Input turned off after running the code (DYNMODE 0) and for the text height (TEXTHEIGHT) to match the drawing default text height rather than the 3mm text height in the code.

Thanks in advance to any contributors.

##### Share on other sites

Only time i see that happening is when you have USC other than world

(vlax-3d-point (trans pt 1 0))

and

```
(vla-addtext
ospace
area
(vlax-3d-point pt)
[color=blue](getvar 'Textsize)[/color];;<-- change text height here
)
)
```

```
(defun C:Calcwt (.....)
[color=#0000ff](setvar 'Dynmode 3)[/color]
[color=#0000ff]......[/color]

(prompt "\rPress Any Key to Exit <Enter/Space>:")
)
[color=blue](setvar 'Dynmode -3)[/color](princ)
)
```

HTH

Well of course a *error* routine will take care of switching system variables from current to desired and vice versa.

```;;;-------------------------------------------------------------------------------------
;;;error trap

(defun *error* (msg)
(command "._undo" "_end")
(setvar 'dynmode old_Dyn)
) ;_ end_defun
;;;-------------------------------------------------------------------------------------
;;;initalize

(setq old_Dyn (getvar 'dynmode))
(setvar 'dynmode 3)
(command "._undo" "_begin")

;before closing
[color=blue](*error* "")[/color]
(princ)
);defun <--------
```

Edited by pBe
##### Share on other sites

Hi pBe,

Thanks for your helpful response.

You are indeed correct, the reason for the incorrect insert point of the text was that the UCS was not in the world position.

I adapted your fix for this and it works well now.

I also added the textheight fix and this also works well.

The problem I am having is with turning the Dynmode off. I think I must be pasting into the wrong part of the code.

Many thanks again for your help.

##### Share on other sites

You want Dynmode permanently off?

##### Share on other sites

Hi pBe,

Yes, I am a little old fashioned. I am sure it has good uses, but I do not like Dynamic Input, so I would indeed like to have the Dynamic Input always off. Dynmode set to 0 as such.

Thanks.

##### Share on other sites

I should add, Dynamic input whilst the code is running is good, but when the code is run, Dynamic input to be switched off.

##### Share on other sites

delete these lines then. or put a ";" at to maintain the original code from the author

;;main part

;;(setvar "dynmode" 3)

;;(setvar "dynprompt" 1)

(setq ospace (vla-get-block.......

EDIT: i ddint see you other post:

see attached file

CALCWT.LSP

##### Share on other sites

pBe,

You have given me everything I asked for. I am really chuffed.

Sincere thanks to you for applying your knowledge and for the time you spent on this.

I also thank petkovski, the original thread starter, for sharing this lisp.

From a very happy Manila Wolf.

##### Share on other sites

pBe,

You have given me everything I asked for. I am really chuffed.

Sincere thanks to you for applying your knowledge and for the time you spent on this.

I also thank petkovski, the original thread starter, for sharing this lisp.

From a very happy Manila Wolf.

Yes, appreciate you giving credit to the original author

You are welcome Manila Wolf

Cheers

##### Share on other sites

• 1 month later...

Thanks to all contributors who take part in this thread.

Long time before, two separate lisp function that i post (Calcwt & Calc3dwt) i combined in ONE, mean calculating weight for 2d and 3d object in one command.

I attach the lisp, now by name WT.lsp.

NOTE: From orig. lisps i update ONLY text input "(getvar 'Textsize)"

Petkovski

WT.lsp

##### Share on other sites

• 5 months later...

Hello friends.

I tried your code for weight. It works very good.

I was hoping to get the weight of multiple solids in one answer.

Can you please help me?

Thanks in advance.

##### Share on other sites

• 1 year later...

This LISP is really good. Can anyone explain how to change the LISP from kg to lbs per foot?

##### Share on other sites

This LISP is really good. Can anyone explain how to change the LISP from kg to lbs per foot?

Not sure if this helps you: -

I did modify the lisp given to me by PBE, to give weight in lbs per ft. I called it "CalcWtin.lsp"

I also added my own materials.

It should be noted that this modified lisp is designed to be applied to 2D closed polyline profiles that are drawn in metric units (mm's).

CalcWtin.LSP

## 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.

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...