A handy LISP routine to delete all white hatching (equal to colour number 7 or 255)

A handy LISP routine to delete all white hatching (equal to colour number 7 or 255)

MaccyDee
Enthusiast Enthusiast
1,161 Views
10 Replies
Message 1 of 11

A handy LISP routine to delete all white hatching (equal to colour number 7 or 255)

MaccyDee
Enthusiast
Enthusiast

A handy LISP routine that will delete all white hatching equal to colour number 7 or 255.

 

it was taking me too long to open drawings and delete large white hatched areas using traditional methods so i just thought id share this lisp i wrote the other day for deleting all hatching of a particular colour in the dwg, it can be easily modified to change or add colours.

 

any questions feel free to ask!

 

 

" (defun c:dwh ()
(vl-load-com) ; load the Visual LISP ActiveX interface
(setq ss (ssget "X" '((0 . "HATCH")))) ; select all hatches in the drawing
(if ss ; if the selection set is not nil
(progn ; do the following
(setq i 0) ; initialize a counter
(repeat (sslength ss) ; loop through the selection set
(setq ent (vlax-ename->vla-object (ssname ss i))) ; get the hatch object
(if (or (= (vla-get-Color ent) 7) ; if the hatch color is white
(= (vla-get-Color ent) 255) ; or if the hatch color is 255
) ; end or
(vla-delete ent) ; delete the hatch object
) ; end if
(setq i (1+ i)) ; increment the counter
) ; end repeat
) ; end progn
) ; end if
(princ) ; exit quietly
) ; end defun "

 

 

0 Likes
1,162 Views
10 Replies
Replies (10)
Message 2 of 11

paullimapa
Mentor
Mentor

nice start...comments:

entdel will not succeed if hatch object is on a locked layer

hatches inside blocks will not be accounted for


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 11

MaccyDee
Enthusiast
Enthusiast

I didn't want it to affect locked layers or blocks, sometimes columns are white hatched and i lock these before i do anything else, as for blocks i have a lisp routine called DOIB (delete objects in blocks) which works really well for any objects in blocks, ill attach for reference 

0 Likes
Message 4 of 11

paullimapa
Mentor
Mentor

that's a nice lisp routine to be able to specify what kind of objects to delete inside a block


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 11

Sea-Haven
Mentor
Mentor

Just one comment 7 is not the same as 255, change your screen color background, one is black the other is white. Hope you can see one line is black the other is white.

 

SeaHaven_0-1681987423845.png

 

0 Likes
Message 6 of 11

Kent1Cooper
Consultant
Consultant

That accounts for Hatches that are color 7 or 255 by color property override only.  Should it be expanded to include any that are color ByLayer on Layers that have those colors?

Kent Cooper, AIA
0 Likes
Message 7 of 11

MaccyDee
Enthusiast
Enthusiast
for our setup (black background) any black colors aside from 255 are set to display as white, if youre using this routine for yourself, its straight forward to open with txt editor and remove the line or change the number to something else
0 Likes
Message 8 of 11

MaccyDee
Enthusiast
Enthusiast
that's a good point, ill get onto that!
0 Likes
Message 9 of 11

MaccyDee
Enthusiast
Enthusiast

alright ive given it a go, 

 

 

(defun c:delhatch ()
(vl-load-com) ; load the Visual LISP ActiveX interface
(setq ss (ssget "X" '((0 . "HATCH")))) ; select all hatches in the drawing
(if ss ; if the selection set is not nil
(progn ; do the following
(setq i 0) ; initialize a counter
(repeat (sslength ss) ; loop through the selection set
(setq ent (vlax-ename->vla-object (ssname ss i))) ; get the hatch object
(if (= (vla-get-Color ent) 256) ; if the hatch color is bylayer
(progn ; do the following
(setq layerObj (vla-item
(vla-get-Layers
(vla-get-ActiveDocument
(vlax-get-acad-object)
)
)
(vla-get-Layer ent)
)) ; get the layer object of the hatch
;; check if the layer is locked using DXF code 70
;; if it is locked, skip to the next object using continue
(if (= (logand (cdr (assoc 70 layerObj)) 4) 4) ; if the layer is locked
(continue) ; skip to the next object
;; else, check if the layer color is index colour 7, 8 or 255 and delete the hatch object if true
(if (or (= (vla-get-Color layerObj) 7) ; if the layer color is index colour 7
(= (vla-get-Color layerObj) 😎 ; or if the layer color is index colour 8
(= (vla-get-Color layerObj) 255) ; or if the layer color is index colour 255
) ; end or
(vla-delete ent) ; delete the hatch object
) ; end if
) ; end if
) ; end progn
;; else, check if the hatch color is index colour 7, 8 or 255 and delete the hatch object if true
(if (or (= (vla-get-Color ent) 7) ; if the hatch color is index colour 7
(= (vla-get-Color ent) 😎 ; or if the hatch color is index colour 8
(= (vla-get-Color ent) 255) ; or if the hatch color is index colour 255
) ; end or
(vla-delete ent) ; delete the hatch object
) ; end if
) ; end if
(setq i (1+ i)) ; increment the counter
) ; end repeat
) ; end progn
) ; end if
(princ) ; exit quietly
) ; end defun

 

but I feel this is a bit clunky, I'm not very good at lisp..

this occasionally will result in an error that says "error: bad argument type: listp #<VLA-OBJECT IAcadLayer 000001ec2eeb0808>" but its only doing it on some drawings, the others seem to be working as intended

 

 

my test file is attached, also i cant get the emoji to not appear but the characters are meant to be "8_)" no underscore (_)  8)8)8)8)

0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant

@MaccyDee wrote:

.... i cant get the emoji to not appear but the characters are meant to be "8_)" no underscore (_)  8)8)8)8)


Theoretically, you should be able to put it in a code window [pick on the </> in the toolbar] to prevent that:

 

(= (vla-get-Color layerObj)  ; or if the layer color is index colour 8
(= (vla-get-Color layerObj) 255) ; or if the layer color is index colour 255
) ; end or
(vla-delete ent) ; delete the hatch object
) ; end if
) ; end if
) ; end progn
;; else, check if the hatch color is index colour 7, 8 or 255 and delete the hatch object if true
(if (or (= (vla-get-Color ent) 7) ; if the hatch color is index colour 7
(= (vla-get-Color ent)  ; or if the hatch color is index colour 8

 

but that doesn't work, either [no smiley, but no 8 and ) either].  If you're aware of the issue as you post things, put a space between the 8 and the ).

Kent Cooper, AIA
0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

@MaccyDee wrote:

.... this occasionally will result in an error that says "error: bad argument type: listp #<VLA-OBJECT IAcadLayer 000001ec2eeb0808>" ....


Does that problem occur only with locked Layers, which might explain why it occurs only in some drawings?  I ask because for those, you call for a (continue) function, but you didn't include the definition of that.

Kent Cooper, AIA
0 Likes