Delete entire drawing hatches except selected

Delete entire drawing hatches except selected

sam_safinia
Advocate Advocate
2,448 Views
10 Replies
Message 1 of 11

Delete entire drawing hatches except selected

sam_safinia
Advocate
Advocate

I have this nice lisp that erase all hatches on drawings but I want to rectify it to keep only selected hatches (by picking from model) and then delete all other hatches. For example in attached drawing I want to keep only BRICK hatch only and get rid of the rest.


Thanks

0 Likes
Accepted solutions (1)
2,449 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Try this...

 

(defun C:EraseHatchesButSelected (/ doc ssr ss i en)
  (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  (if (and (princ "\nHatched to retain, ")
	   (setq ssr (ssget '((0 . "HATCH"))))
	   (setq ss (ssget "_A" '((0 . "HATCH"))))) ;Hatches in Frozen layers are not erased.
    (repeat (setq i (sslength ss))
      (if (not (ssmemb (setq en (ssname ss (setq i (1- i)))) ssr))
	(entdel en)))
    (princ "\nWrong selection, need hatches."))
  (vla-endundomark doc)
  (princ)
)
0 Likes
Message 3 of 11

hmsilva
Mentor
Mentor
Accepted solution

@s_plant wrote:

I have this nice lisp that erase all hatches on drawings but I want to rectify it to keep only selected hatches (by picking from model) and then delete all other hatches. For example in attached drawing I want to keep only BRICK hatch only and get rid of the rest.


Hi s_plant,

if I understand correctly your goal, you need to erase all the hatches in the drawing, but not all hatches with the same pattern of the selected ones.

 

If that is your goal, maybe something like this to do the trick

 

(vl-load-com)
(defun c:demo (/ i layers locklstlat lst name obj ss)
   (if (and (princ "\n Select hatch paterns to retain: ")
            (setq ss (ssget '((0 . "HATCH"))))
       )
      (progn
         (repeat (setq i (sslength ss))
            (setq obj  (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                  name (vla-get-patternname obj)
            )
            (if (not (vl-position name lst))
               (setq lst (cons name lst))
            )
         )
         (or adoc (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
         (setq layers (vla-Get-Layers adoc))
         (vlax-for lay layers
            (if (= (vla-get-lock lay) :vlax-true)
               (progn
                  (setq locklst (cons (vla-get-name lay) locklst))
                  (vla-put-lock lay :vlax-false)
               )
            )
         )
         (vlax-for blks (vla-get-blocks adoc)
            (if (= :vlax-false (vla-get-isxref blks))
               (vlax-for blk blks
                  (if (and (= (vla-get-objectname blk) "AcDbHatch")
                           (not (vl-position (vla-get-patternname blk) lst))
                      )
                     (vla-delete blk)
                  )
               )
            )
         )
         (if locklst
            (vlax-for lay layers
               (if (vl-position (vla-get-name lay) locklst)
                  (vla-put-lock lay :vlax-true)
               )
            )
         )
         (vla-Regen adoc acActiveViewport)
      )
   )
   (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 4 of 11

sam_safinia
Advocate
Advocate

Thanks Henrique another excellent job. This is exactly what I was after.Cheers mate

0 Likes
Message 5 of 11

hmsilva
Mentor
Mentor

@s_plant wrote:

Thanks Henrique another excellent job. This is exactly what I was after.Cheers mate


You're welcome, s_plant
Glad I could help

Henrique

EESignature

0 Likes
Message 6 of 11

sam_safinia
Advocate
Advocate

Only thing that I cannot figure it how include it in my lisp is: if nothing select it means all hatch patterns on drawing should be deleted. What is the lisp modification for zero-selection and delete all hatches?

 

Also it does not working on some hatches like the one, BUILDINGGP1 in attached drawing

thanks

0 Likes
Message 7 of 11

hmsilva
Mentor
Mentor

@s_plant wrote:

Only thing that I cannot figure it how include it in my lisp is: if nothing select it means all hatch patterns on drawing should be deleted. What is the lisp modification for zero-selection and delete all hatches?


Hi s_plant,

the following code deletes the hatches even without any selecion set.

 

(vl-load-com)
(defun c:demo (/ i layers locklstlat lst name obj ss)
   (if (or (and (princ "\n Select hatch paterns to retain: ")
                (setq ss (ssget '((0 . "HATCH"))))
           )
           (= (ACET-UI-MESSAGE
                 "Do you want to delete all hatches?"
                 "You are about to delete all hatches!!!"
                 (+ Acet:YESNO Acet:ICONWARNING)
              )
              6
           )
       )
      (progn
         (if ss
            (repeat (setq i (sslength ss))
               (setq obj  (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                     name (vla-get-patternname obj)
               )
               (if (not (vl-position name lst))
                  (setq lst (cons name lst))
               )
            )
         )
         (or adoc (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
         (setq layers (vla-Get-Layers adoc))
         (vlax-for lay layers
            (if (= (vla-get-lock lay) :vlax-true)
               (progn
                  (setq locklst (cons (vla-get-name lay) locklst))
                  (vla-put-lock lay :vlax-false)
               )
            )
         )
         (vlax-for blks (vla-get-blocks adoc)
            (if (= :vlax-false (vla-get-isxref blks))
               (vlax-for blk blks
                  (if (and (= (vla-get-objectname blk) "AcDbHatch")
                           (not (vl-position (vla-get-patternname blk) lst))
                      )
                     (vla-delete blk)
                  )
               )
            )
         )
         (if locklst
            (vlax-for lay layers
               (if (vl-position (vla-get-name lay) locklst)
                  (vla-put-lock lay :vlax-true)
               )
            )
         )
         (vla-Regen adoc acActiveViewport)
      )
   )
   (princ)
)

 


@s_plant wrote:

 Also it does not working on some hatches like the one, BUILDINGGP1 in attached drawing


s_plant,

in user defined hatch patterns, the pattern name property is always '_USER', so if you select one user defined hatch pattern the code will 'see' all the hatches with a pattern name '_USER' as de same pattern...

I do not use user defined hatch patterns, post a sample dwg with more user defined hatch patterns and i'll see what I can do.

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 8 of 11

Anonymous
Not applicable

Many Thanks Henrique. It is working fine with no selection. You are right as per attached when I want to retain one of the User defined pattern it keeps all and delete system patterns. I am thinking another filter set of color or scale...Maybe??

 

much appreciated mate

0 Likes
Message 9 of 11

hmsilva
Mentor
Mentor

@Anonymous wrote:

Many Thanks Henrique. It is working fine with no selection. You are right as per attached when I want to retain one of the User defined pattern it keeps all and delete system patterns. I am thinking another filter set of color or scale...Maybe??

 

much appreciated mate


You're welcome, Sam_RG

As I have said:

'I do not use user defined hatch patterns, post a sample dwg with more user defined hatch patterns and i'll see what I can do.'

post a dwg with several user defined hatch patterns, so I can do some tests to filter those patterns.

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 10 of 11

darren_maskell
Explorer
Explorer

Hi Enrique,

 

Thank you for the routines, just a quick one on this one, what do i need to type to run it?

0 Likes
Message 11 of 11

hmsilva
Mentor
Mentor

@darren_maskell wrote:

Hi Enrique,

 

Thank you for the routines, just a quick one on this one, what do i need to type to run it?


@darren_maskell the command name in a autolisp routine is the name after (defun c: 

In this case is demo

 

Hope this helps,
Henrique

EESignature

0 Likes