Lisp routine to run mass prop for all entities for each layer of a drawing.

Lisp routine to run mass prop for all entities for each layer of a drawing.

Anonymous
Not applicable
3,054 Views
17 Replies
Message 1 of 18

Lisp routine to run mass prop for all entities for each layer of a drawing.

Anonymous
Not applicable

Hello Everyone,

I am trying to learn lisp and I have been asked to come up with something that will do the following for each layer in a drawing:

 

1) Select all entities on the layer.

2) Run the command "massprop".

3) Save the output to a file in a subfolder in the same folder as the drawing and named after the layer.

 

I don't even know where to begin with this so any pointers would be appreciated.

0 Likes
Accepted solutions (2)
3,055 Views
17 Replies
Replies (17)
Message 2 of 18

Jonathan3891
Advisor
Advisor

Give this a try.

 

Sense you requested that it select all entities on a layer, I made it name the .mpr file the selected layer name.

 

(defun c:ttt (/ ofiledia ocmdecho ent ent0 layname fpath fname)
  (setq ofiledia (getvar 'filedia))
  (setq ocmdecho (getvar 'cmdecho))
  (setvar 'filedia 0)
  (setvar 'cmdecho 0)

  (setq ent (ssget "X" (list (cons 8 (cdr (assoc 8 (entget (car (entsel "\nSelect Object : ")))))))))
  (setq ent0 (ssname ent 0))
  (setq layname (tblsearch "layer" (cdr (assoc 8 (entget ent0)))))
  (setq fpath (getvar 'dwgprefix))
  (setq fname (cdr (assoc 2 layname)))
  (command "_MASSPROP" ent "" "_Y" (strcat fpath fname))

  (setvar 'filedia ofiledia)
  (setvar 'cmdecho ocmdecho)
  (princ)
  )

 


Jonathan Norton
Blog | Linkedin
Message 3 of 18

hak_vz
Advisor
Advisor
Accepted solution

Here is a starting version.  For anything more I would need few more details.

Code takes all region or solid object in particular layer and calculates mass properties.

 

 

(defun c:mpall( / *error* app doc layers filepath layer ss path)
(defun *error* ()
(setvar 'cmdecho 1)
(setvar 'filedia 1)
(princ)
)
(vl-load-com)
(setvar 'cmdecho 0)
(setvar 'filedia 0)
(setq app (vlax-get-acad-object))
(setq doc (vla-get-activedocument app))
(setq layers (vla-get-layers doc))
(setq filepath (getvar "dwgprefix"))

(vlax-for lyr layers
(setq layer (vlax-get-property lyr "name"))
(setq ss (ssget "X" (list (cons 8 layer) '(0 . "region,solid"))))
(setq path (strcat filepath layer))
(if (and ss) (command "_.massprop" ss "" "y" path))
)
(setvar 'cmdecho 1)
(setvar 'filedia 1)
(princ)
)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 4 of 18

Anonymous
Not applicable

Hello Jonathan,

Thank you for responding so quickly. Now that I see it I can semi-understand how it works. It does what I need it to do for one layer but I need it to automatically cycle through all the layers and generate a separate file for each layer. Is that possible?

0 Likes
Message 5 of 18

devitg
Advisor
Advisor

lisp at post 3  by  hak_vz do as you need . 

0 Likes
Message 6 of 18

Anonymous
Not applicable

Hello devitig,

The routine you provided does what I need for the user selection, but I need it to select all items on each layer and produce a file for each layer. Is this something that can be done?

0 Likes
Message 7 of 18

hak_vz
Advisor
Advisor

@Anonymous wrote:

The routine you provided does what I need for the user selection, but I need it to select all items on each layer and produce a file for each layer. Is this something that can be done?


Routine I provided do just that. It selects all region or solid object at each layer and writes down to file with that name.

Test it. Or do you want it separate for each object at layer? You wasn't specific in original post.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 8 of 18

devitg
Advisor
Advisor

I was testing your lisp to select all solid 

 

(0 . "region,*solid")

But my PC hang on acad . I have to restart it. 

 

 

0 Likes
Message 9 of 18

devitg
Advisor
Advisor

It work as expected . 

0 Likes
Message 10 of 18

hak_vz
Advisor
Advisor

I've tested too, and it works ok.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 11 of 18

devitg
Advisor
Advisor

It do not work for SOLID 

 

((-1 . <Entity name: 293ab670>) (0 . "SOLID")
Select objects: 1 found

Select objects:
No solids or regions selected.

It do for region 

0 Likes
Message 12 of 18

hak_vz
Advisor
Advisor
Accepted solution
(defun c:mpall( / *error* app doc layers filepath layer ss path)
(defun *error* ()
(setvar 'cmdecho 1)
(setvar 'filedia 1)
(princ)
)
(vl-load-com)
(setvar 'cmdecho 0)
(setvar 'filedia 0)
(setq app (vlax-get-acad-object))
(setq doc (vla-get-activedocument app))
(setq layers (vla-get-layers doc))
(setq filepath (getvar "dwgprefix"))

(vlax-for lyr layers
(setq layer (vlax-get-property lyr "name"))
(setq ss (ssget "X" (list (cons 8 layer) '(0 . "region,*solid"))))
(setq path (strcat filepath layer))
(if (and ss) (command "_.massprop" ss "" "_y" path))
)
(setvar 'cmdecho 1)
(setvar 'filedia 1)
(princ)
)

It works on solid to. I have created extruded surfaces (3dsolid) from rectangle and it works. For some reason it waits for enter weather or not included in command line.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 13 of 18

hak_vz
Advisor
Advisor

I've tried to google for any posts on massprop on solids, and there are some post about problems. But for me it's time to go to sleep.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 14 of 18

Sea-Haven
Mentor
Mentor

Hak_vz "Region,3dsolid"

 

If you want a single file as a result ie for the dwg, there are two ways, you can take advantage of an old DOS command in your lisp. Use shell.

 

Copy file1+file2 file3

Rename file3 file1 

 

So file1 is the end result of all layers, massprop. file2 is a temporary massprop file that you hard code.

 

The other way is to make a temporary massprop file and then read it line by line into a final file. has advantage of removing data not required as you can skip lines etc.

 

So have a double loop 1 for layers, the second for the objects.

Message 15 of 18

devitg
Advisor
Advisor

Hi Sea.haven . Please explain how to use old reliable DOS from LISP

I  learn about dos ,  circa 1985. Still have the book. 

0 Likes
Message 16 of 18

cadffm
Consultant
Consultant

Like you can use the windows shell command condole (CTRL+R open: CMD) you can use

use SHELL command like sea.haeven wrote.

 

Command: Shell

 

Sebastian

Message 17 of 18

Sea-Haven
Mentor
Mentor

Example, I want to put my security checking at the start of every lisp before I turn into a fas for the end user where I don't want open code. Say a demo version runs 30 times. You know send the real version, check is in the mail.

 

The dos command is

copy d:\\mycode\\Security.lsp+d:\\mycode\\prog1.lsp d:\\Mycode\\outputsecure\\prog1.lsp

Just set up a batch file with all the required files, then I do a vl-compile on the files.

 

In this post case use the shell command to run the 3 lines copy, delete and rename.

0 Likes
Message 18 of 18

Anonymous
Not applicable

Thanks Hak_vz! The items I wanted to run massprop on were mep structural members so I had to convert them to 3dsolids first, but your lisp worked perfectly! Thanks again!