Jump to content
Guite

automate flattening all entities residing inside blocks

Recommended Posts

Guite

I am looking for ways to automate flattening all entities residing inside blocks. I have searched all over the internet but haven't found one, maybe because my search term is not correct, or something. Here is what I am looking for:-

 

Open a file (this can be manually or automate through batch file)

Run a script / routine which does all things below all by itself:-

-- edit a block by opening it in block editor

-- runs flatten command or any other method to set all lines, arcs, etc to Z=0 coordinate

-- save block and exit block editor

-- open next block and repeat above steps

-- stops only when all block in the drawing are redefined as above

 

Is it possible?

 

Note: using AutoCAD 2012. Lines are not imported from 3D content, just a result of careless drafting.

Share this post


Link to post
Share on other sites
Lee Mac

Here is a very simple program to flatten all objects in all blocks & layouts:

(defun c:allflat ( / d )
   (vlax-for b (vla-get-blocks (setq d (vla-get-activedocument (vlax-get-acad-object))))
       (if (= :vlax-false (vla-get-isxref b))
           (vlax-for o b
               (if (vlax-write-enabled-p o)
                   (foreach e '(1e99 -1e99)
                       (vlax-invoke o 'move '(0.0 0.0 0.0) (list 0.0 0.0 e))
                   )
               )
           )
       )
   )
   (vla-regen d acallviewports)
   (princ)
)
(vl-load-com) (princ)

You can very easily run the above program on multiple drawings using a batch processing program, such as my Script Writer program.

Edited by Lee Mac

Share this post


Link to post
Share on other sites
Guite
Here is a very simple program to flatten all objects in all blocks & layouts:

 

You can very easily run the above program on multiple drawings using a batch processing program, such as my Script Writer program.

 

Thanks a lot Lee Mac. Works like a charm. On a simple file it finished so fast I first thought it did not do anything!

 

I did a little test to test how your insertion points behave.

 

zy47.jpg

 

Block 01:- Rectangle at z=0, block defined and base point selected at z=0. Rectangle moved to z=50 in block editor. Block inserted on line lying at z=0. Block rectangle still comes on z=0. Good.

 

Block 02:- lines at various z coordinate. Block defined and base point selected at z=50 (intersection of two green lines).

Instance 1:-Insert at Z=0

Instance 2:- insert at Z=50 (on RHS)

 

Run "Allflat" script and voila! everything is at z=0. What amazes me most is that second instance of block 02 is also now lying flat on z 0. My own conclusion is that base point definition does not have z value.

 

Can't wait to try it out on all those big messy drawings come Monday.

 

PS: I am somehow unable to upload image to CADTutor. Moderators please edit as required.

Share this post


Link to post
Share on other sites
Lee Mac
Thanks a lot Lee Mac. Works like a charm. On a simple file it finished so fast I first thought it did not do anything!

 

You're most welcome! :)

 

What amazes me most is that second instance of block 02 is also now lying flat on z 0. My own conclusion is that base point definition does not have z value.

 

This is because the base point of a block reference is merely the position of the block definition geometry relative to the origin of the block definition (very rarely is the origin of the block definition not at 0,0,0).

 

Hence, when running the program, since the objects within the block definition are also 'flattened', the base point is automatically repositioned accordingly.

 

PS: I am somehow unable to upload image to CADTutor. Moderators please edit as required.

 

The images appeared fine for me :)

Share this post


Link to post
Share on other sites
SLW210
PS: I am somehow unable to upload image to CADTutor. Moderators please edit as required.

 

I do not see them at work which filters images from third party sites. Is the image hosted at a third party site? If you just post them to CADTutor, they should show up.

Share this post


Link to post
Share on other sites
Guite

The image is hosted on third party site. I could not upload to CADTutor for some reason. It just never finishes uploading

Share this post


Link to post
Share on other sites
SLW210
The image is hosted on third party site. I could not upload to CADTutor for some reason. It just never finishes uploading

 

What is the size of the file? Images load just fine here for me.

Share this post


Link to post
Share on other sites
Guite

Dear Lee Mac,

 

I am encountering weird behaviors after running Allflat. This is not to say that it does not work at all. Infact it runs pretty well in most files. It handled dynamic blocks pretty well.

 

- Some blocks behave strangely (files attached). The blocks were carelessly defined to start with. Re-defining them will be too time consuming.

- Hatches disappear in one drawing, may have moved to very far coordinate. X-ref moves away in same file. E-transmit file attached.

- Entities get scattered. Please refer Cutouts before and after.

 

I have also tried native flatten command on some of the files. While there is less or no scattering, block insertion Z do not move every time to 0.

 

I am attaching these files incase you would like to troubleshoot and develop the script further. If you don't feel like, I am okay with it. I have restored my backups and will just as well live with it.

 

Cheers,

Guite

Cadtutor.zip

E-Transmit.zip

Share this post


Link to post
Share on other sites
Guite

Dear Lee Mac,

 

Update: Allflat seems to have problem with handling blocks rotated other than 90 degrees to UCS axes. File attached for reference: run Allflat on this file and all angled parking blocks will scatter away.

 

Cheers,

Guite

XR_Parking.zip

Share this post


Link to post
Share on other sites
Lee Mac

Guite,

 

The posted program is incredibly simplistic and will simply set the Z-component to zero for all objects processed - I don't have time to develop it further. For a more developed program, please search for 'SuperFlatten' by Joe Burke.

 

All the best,

 

Lee

Share this post


Link to post
Share on other sites
Guite

Lee,

 

Thanks a lot for the pointer. Could not get latest version but v1.1 works fine for me.

 

Guite

Share this post


Link to post
Share on other sites
Lee Mac

You're welcome Guite.

Share this post


Link to post
Share on other sites
Guite

It would be very nice if someone could modify Lee Mac's lisp routine to address the two issues I have highlighted:-

 

1. Blocks rotated other then 90 degrees wrt WCS. Right now it moves and scatters rotated blocks.

2. Some hatches move from original position. The routine should ignore hatches and/or disassociate hatches from its boundaries.

 

I have tried native Flatten and SuperFlatten. Flatten throws up error messages. Solutions I found n the net does not correspond to my situation. Moreover it does not allow All selection through a script.

 

Even after trying all options within Superflatten I always get exploded blocks. Even with Explodable Blocks option chosen, the routine does not rebuild the blocks. So its not working out for me.

 

Cheers,

Guite

Share this post


Link to post
Share on other sites
GP_
The image is hosted on third party site. I could not upload to CADTutor for some reason. It just never finishes uploading

 

 

I had the same issue with IE10, I settled with IE11.

Share this post


Link to post
Share on other sites
SLW210

I have no problem uploading with IE10.

Share this post


Link to post
Share on other sites
GP_
I have no problem uploading with IE10.

 

#\@kx/##@ [censored]

 

 

---> "Compatibility View Settings"

Share this post


Link to post
Share on other sites
Jarry
On 15/11/2013 at 13:57, Lee Mac said:

Hi Lee Max,

let me express my full respect to you as I highly appreciate your achievements.

 

Well, is it possible to adapt the below simple routine compatible for AutoCad Mac users as me?

 

Many thanks


(defun c:allflat ( / d )
   (vlax-for b (vla-get-blocks (setq d (vla-get-activedocument (vlax-get-acad-object))))
       (if (= :vlax-false (vla-get-isxref b))
           (vlax-for o b
               (if (vlax-write-enabled-p o)
                   (foreach e '(1e99 -1e99)
                       (vlax-invoke o 'move '(0.0 0.0 0.0) (list 0.0 0.0 e))
                   )
               )
           )
       )
   )
   (vla-regen d acallviewports)
   (princ)
)
(vl-load-com) (princ)

 

 

Share this post


Link to post
Share on other sites
Lee Mac

Thank you for your kind words @Jarry, I appreciate your compliments for my work.

 

Unfortunately AutoCAD for Mac does not support the ActiveX component of the Visual LISP API (since this is a proprietary Microsoft technology), and consequently it would not be possible to port the above code for compatibility on the Mac platform, without abandoning the method currently implemented in the program.

Share this post


Link to post
Share on other sites
BIGAL

This is an old one and it would be a start point as it uses entmod to flatten to zero, needs a is a block and change nested entities. Would change all the defuns to accept a single object at a time using a cond.

 

; changes all level to zero

(vl-load-com)

(defun dxf(code elist)
(cdr (assoc code elist))
)
(defun circ0 (/ g n lg e os ns el nl)
(princ "\nChanging CIRCLES: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "CIRCLE"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
(setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
(entmod e)
)
)
(setq n (1+ n))
)
))
)
(defun line0 (/ g n lg e os ns of nf el nl)
(princ "\nChanging LINES: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "LINE"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
(setq of (assoc 11 e))
(setq nf (list(car of)(cadr of)(caddr of) 0.0))
(setq e (subst nf of e))
(setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
(entmod e)
)
)
(setq n (1+ n))
)
))
)
(defun arc0 (/ g n lg e os ns el nl)
(princ "\nChanging ARCS: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "ARC"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
(setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
 (entmod e)
)
)
(setq n (1+ n))
)
))
)	
(defun sol0 (/ g n lg e o1 n1 o2 n2 o3 n3 o4 n4 el nl)
(princ "\nChanging SOLIDS:" )
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "SOLID"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq o1 (assoc 10 e))
(setq n1 (list(car o1)(cadr o1)(caddr o1) 0.0))
(setq e (subst n1 o1 e))
(setq o2 (assoc 11 e))
(setq n2 (list(car o2)(cadr o2)(caddr o2) 0.0))
(setq e (subst n2 o2 e))
	        (setq o3 (assoc 12 e))
(setq n3 (list(car o3)(cadr o3)(caddr o3) 0.0))
(setq e (subst n3 o3 e))
	        (setq o4 (assoc 13 e))
(setq n4 (list(car o4)(cadr o4)(caddr o4) 0.0))
(setq e (subst n4 o4 e))
	        (setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
 (entmod e)
)
)
(setq n (1+ n))
)
))
)
(defun text0 (/ g n lg e os ns of nf el nl)
(princ "\nChanging TEXT: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "TEXT"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
	        (setq of (assoc 11 e))
(setq nf (list(car of)(cadr of)(caddr of) 0.0))
(setq e (subst nf of e))
	        (setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
 (entmod e)
)
)
(setq n (1+ n))
)
))
)
(defun pt0 (/ g n lg e os el nl)
(princ "\nChanging POINTS: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "POINT"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
	        (setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
 (entmod e)
)
)
(setq n (1+ n))
)
))
)
(defun dim0 (/ g n lg e o1 n1 o2 n2 o3 n3 o4 n4 o5 n5 o6 n6 o7 n7 el nl)
(princ "\nChanging DIMS: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "DIMENSION"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq o1 (assoc 10 e))
(setq n1 (list(car o1)(cadr o1)(caddr o1) 0.0))
(setq e (subst n1 o1 e))
(setq o2 (assoc 11 e))
(setq n2 (list(car o2)(cadr o2)(caddr o2) 0.0))
(setq e (subst n2 o2 e))
	        (setq o3 (assoc 12 e))
(setq n3 (list(car o3)(cadr o3)(caddr o3) 0.0))
(setq e (subst n3 o3 e))
	        (setq o4 (assoc 13 e))
(setq n4 (list(car o4)(cadr o4)(caddr o4) 0.0))
(setq e (subst n4 o4 e))
(setq o5 (assoc 14 e))
(setq n5 (list(car o5)(cadr o5)(caddr o5) 0.0))
(setq e (subst n5 o5 e))
	        (setq o6 (assoc 15 e))
(setq n6 (list(car o6)(cadr o6)(caddr o6) 0.0))
(setq e (subst n6 o6 e))
	        (setq o7 (assoc 16 e))
(setq n7 (list(car o7)(cadr o7)(caddr o7) 0.0))
(setq e (subst n7 o7 e))
	        (setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
(entmod e)
)
)
(setq n (1+ n))
)
))
)

(defun pline0 (/ g n lg e os ns el nl ed en)
(princ "\nChanging PLINES and DONUTS: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "POLYLINE"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
(setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
(entmod e)
(SETQ en (DXF -1 e))	
(while (and (setq en (entnext en))
(SETQ ED (ENTGET EN))
(/= "SEQEND" (dxf 0 ed))
)
(progn
(setq os (assoc 10 ed))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq ed (subst ns os ed))
(setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
(entmod ed)
)
)
(entupd en)
)
)
(setq n (1+ n))

)
))
)

(defun lwpline0 ( / ss num x)
(setq ss (ssget "P"))
(setq num (sslength ss))
(setq x num)
(repeat num
(setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
(if (= (vla-get-objectname obj) "AcDbPolyline")  
(vla-put-elevation obj 0.0)
)
)
)


(defun ins0 (/ g n lg e os ns el nl)
(princ "\nChanging BLOCK INSERTS: ")
(setq g (ssget "P"))
(if g (progn
(setq n 0 lg (sslength g))
(while (< n lg)
(if (= "INSERT"
(cdr (assoc 0 (setq e (entget (ssname g n))))))
(progn
(setq os (assoc 10 e))
(setq ns (list(car os)(cadr os)(caddr os) 0.0))
(setq e (subst ns os e))
(setq el (assoc 38 e))
(setq nl (cons 38 0.0))
(setq e (subst nl el e))
 (entmod e)
)
)
(setq n (1+ n))
)
))
)
(defun c:zero ()
(setq g (ssget))
(circ0)
(line0)
(arc0)
(sol0)
(text0)
; (pt0) ; dont want to change points to zero
(dim0)
(pline0)
;(lwpline0)
(ins0)
(princ)
)

 

Share this post


Link to post
Share on other sites

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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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