LSP routine that loops through all layers in DWG to convert hatches to regions

LSP routine that loops through all layers in DWG to convert hatches to regions

summitgeo_cad
Observer Observer
428 Views
5 Replies
Message 1 of 6

LSP routine that loops through all layers in DWG to convert hatches to regions

summitgeo_cad
Observer
Observer

im trying to create an LSP routine that will step through every layer, activate each in turn, select all hatches on that layer (if any exist), convert to region in that layer, and then step onto the next layer, until all hatches have been converted to region. i tried creating this with chatgpt, but didnt get very far. Can anyone help?

0 Likes
429 Views
5 Replies
Replies (5)
Message 2 of 6

paullimapa
Mentor
Mentor

You don't need to cycle through the layers to get at the hatches. Lisp gives you the ability to select all hatches and then you can cycle through each of them identifying the layer so that the region created can have the matching layer.

This lisp code I modified from another thread should convert all your hatches to regions leaving them on the same layers. But what it does not do is remove the current boundaries those hatches may have.

So give HBREG.lsp a try.

; hbreg converts all hatches to regions
; Modified from:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-a-region-instead-a-polyline-around-hatch-pattern/m-p/5317853/highlight/true#M326510
(defun c:hbreg ( / clayer ctab el h hd hl hs i ss) 
(if (setq ss (ssget "_X" '((0 . "HATCH")))) ; select all hatches
 (progn
  (command "_.Undo" "_BE")
  (repeat (setq i (sslength ss))
    (setq el (entlast)
          h (ssname ss (setq i (1- i)))
          hd (entget h) ; get data
          hl (cdr (assoc 8 hd)) ; get layer
          hs (cdr (assoc 410 hd)) ; get space
          ctab (getvar "ctab") ; get current space
          clayer (getvar "clayer") ; get current layer
    )
    (if (not (eq hs ctab)) ; chk if not in current space 
      (setvar "ctab" hs) ; then set to that space
    )
    (if (zerop (getvar "tilemode")) ; chk if layout
      (command "_.Pspace") ; then make sure in pspace
    )
    (setvar "clayer" hl) ; set hatch layer as current
    (command "_.-HATCHEDIT" h "_B" "_R" "_N") ; create hatch boundary as region not associate
;    (command "_.-HATCHEDIT" h "_DI") 
    (if (not (eq el (entlast))) ; if region created
       (entdel h) ; delete the original hatch
    ) ; if
    (setvar "ctab" ctab) ; return to original space
    (setvar "clayer" clayer) ; return to original layer
  ) ; repeat
  (command "_.Undo" "_E")
 ) ; progn
 (princ "\nNo Hatches Found.")
) ; if
(princ)
) ; defun

 


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

summitgeo_cad
Observer
Observer

thank you @paullimapa . i tried this and it worked on some of the hatches but not all of them. i have now realised that i need to generate polyline boundaries not regions for the hatches. can this code be modified to work with polylines instead?

0 Likes
Message 4 of 6

summitgeo_cad
Observer
Observer

@paullimapa ignore me. simple fix, just changed this and all working now for all hatches. you're a life saver 👍

 

        (command "_.-HATCHEDIT" h "_B" "_P" "_N") ; create hatch boundary as polyline not associate
        (if (not (eq el (entlast))) ; if polyline created

 

0 Likes
Message 5 of 6

paullimapa
Mentor
Mentor

Here's the modified version HbPln.lsp to create Pline boundaries.  Note for Hatches that already have Pline boundaries you'll end up with duplicates.

; hbreg converts all hatches to Pline boundaries
; Modified from:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-a-region-instead-a-polyline-around-hatch-pattern/m-p/5317853/highlight/true#M326510
(defun c:hbpln ( / clayer ctab el h hd hl hs i ss) 
(if (setq ss (ssget "_X" '((0 . "HATCH")))) ; select all hatches
 (progn
  (command "_.Undo" "_BE")
  (repeat (setq i (sslength ss))
    (setq el (entlast)
          h (ssname ss (setq i (1- i)))
          hd (entget h) ; get data
          hl (cdr (assoc 8 hd)) ; get layer
          hs (cdr (assoc 410 hd)) ; get space
          ctab (getvar "ctab") ; get current space
          clayer (getvar "clayer") ; get current layer
    )
    (if (not (eq hs ctab)) ; chk if not in current space 
      (setvar "ctab" hs) ; then set to that space
    )
    (if (zerop (getvar "tilemode")) ; chk if layout
      (command "_.Pspace") ; then make sure in pspace
    )
    (setvar "clayer" hl) ; set hatch layer as current
    (command "_.-HATCHEDIT" h "_B" "_P" "_N") ; create hatch boundary as plines not associate
;    (command "_.-HATCHEDIT" h "_DI") 
    (if (not (eq el (entlast))) ; if plines created
       (entdel h) ; delete the original hatch
    ) ; if
    (setvar "ctab" ctab) ; return to original space
    (setvar "clayer" clayer) ; return to original layer
  ) ; repeat
  (command "_.Regen")
  (command "_.Undo" "_E")
 ) ; progn
 (princ "\nNo Hatches Found.")
) ; if
(princ)
) ; defun

 


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

paullimapa
Mentor
Mentor

you got it...and you are welcome...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes