Hatch all closed objects by layer

Hatch all closed objects by layer

Bartosz_MalinowskiCSG4A
Observer Observer
1,144 Views
10 Replies
Message 1 of 11

Hatch all closed objects by layer

Bartosz_MalinowskiCSG4A
Observer
Observer

Hello everyone,

I hope this message finds you well. I am currently working on a project in AutoCAD where I need to automate the process of assigning specific hatches to different layers. My goal is to hatch all closed objects on a given layer with the designated hatch pattern assigned to that layer.

I would greatly appreciate any guidance or examples of LISP routines that could help me achieve this. Specifically, I'm looking for a solution that would:

  1. Identify all layers in the drawing.
  2. Assign a specific hatch pattern to each layer based on predefined criteria.
  3. Hatch all closed objects within each layer using the corresponding hatch pattern.

If anyone has experience with this type of automation or can provide a sample LISP routine that accomplishes these tasks, I would be incredibly grateful.

Thank you in advance for your assistance!

Bart

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

LDShaw
Collaborator
Collaborator

A few questions.

 

Is there a layer list that will normally be used? How many layers?
How are the patterns determined for each layer? Can we make a list of layers and a list of possible hatches line them up and go or do you have to choose the hatches?
is it all closed objects or just plines?
Do the closed objects overlap?

 

0 Likes
Message 3 of 11

pendean
Community Legend
Community Legend

@Bartosz_MalinowskiCSG4A I see a lot of identical posts over the years for this type of LISP, did you get a chance to try any of them and which of them got close or actually meet you need yet?

 

And what are you calling "closed objects" please: actual PLINE "closed" outlines?
Or just random lines that create what you consider a closed area?
Or blocks with "closed areas" in them?

Splines? Text 'boundaries'?

In other words, what would the LISP look for to hatch and not hatch please?

 

PLUS the excellent questions asked above too.

0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant

@pendean wrote:

...

And hat are you calling "closed objects" please: actual PLINE "closed" outlines?
Or just random lines that create what you consider a closed area?
Or blocks with "closed areas" in them?

Splines? Text 'boundaries'?
.....


Circles?  Closed (full) Ellipses?  Regions?  Mlines?

Kent Cooper, AIA
0 Likes
Message 5 of 11

Bartosz_MalinowskiCSG4A
Observer
Observer

Yes, the drawing will have a pre-set number of layers—let's say 10—and each layer will have a unique hatch assigned to it. These layer and hatch assignments will remain constant and unchanging. Additional layers, such as those for dimensions, viewports, etc., may be present, but it would be ideal if the LISP routine could omit these non-relevant layers.

The objects in question will predominantly be closed polylines, with occasional instances of overlap, which can be adjusted if necessary. No blocks will be used, only polylines.

What i try to achive is use of same hatches on same materials, and of course like all the lisp get some automatization on my sections/elevations.

0 Likes
Message 6 of 11

komondormrex
Mentor
Mentor

and what is predefined criteria supposed to be?

0 Likes
Message 7 of 11

Kent1Cooper
Consultant
Consultant

So if "predefined criteria" constitute a list of sublists, with each sublist being a Layer name and the Hatch patterns that should be used in all closed Polylines on that Layer, then it should not be difficult to have a routine find all closed Polylines on the Layer that is the first entry in a sublist, and Hatch each one using the pattern that is the second entry.  Does that description sound like what you want?

 

(setq theList '(("LayerA" "SOLID") ("LayerB" "ANSI31") ("LayerXYZ" "ESCHER")))

 

Might there be differences other than just the pattern name, such as in pattern scale and/or rotation?  Those could easily be included in the sublists.

Kent Cooper, AIA
0 Likes
Message 8 of 11

Bartosz_MalinowskiCSG4A
Observer
Observer

Yes! Attributes like scale will be an excellent addition as well.

Yesterday, chat GPT was created for this, but it's not working. 

(defun c:AutoHatch ()
(vl-load-com)

;; Define your layer-hatch pattern mappings here with scale and angle for each layer
;; Format: (layer-name hatch-pattern hatch-scale hatch-angle)
(setq layer-hatch-alist '(
("Layer1" "ANSI31" 1.0 0)
("Layer2" "BRICK" 1.5 45)
("Layer3" "SOLID" 1.0 0)
("Layer4" "CROSSHATCH" 0.8 90)
))

;; Function to close an open polyline
(defun ClosePolyline (ent)
(if (and (= (vla-get-ObjectName ent) "AcDbPolyline")
(= (vla-get-Closed ent) :vlax-false))
(progn
(vla-put-Closed ent :vlax-true) ;; Close the polyline
(princ (strcat "\nClosed polyline: " (vla-get-Handle ent)))
)
)
)

;; Iterate over each layer in the layer-hatch list
(foreach layer-hatch layer-hatch-alist
(setq layer (nth 0 layer-hatch))
(setq hatch-pattern (nth 1 layer-hatch))
(setq hatch-scale (nth 2 layer-hatch))
(setq hatch-angle (nth 3 layer-hatch))

;; Set the current layer
(setvar 'clayer (tblobjname "LAYER" layer))

;; Check if the layer exists
(if (tblsearch "LAYER" layer)
(progn
;; Select all closed objects (polylines) on the layer, including open polylines to close
(setq ss (ssget "X" (list (cons 8 layer) (cons 0 "LWPOLYLINE, POLYLINE")))) ;; Select polylines only

;; If selection is valid, iterate over entities and close polylines if necessary
(if ss
(progn
(setq i 0)
(while (< i (sslength ss))
(setq ent (vlax-ename->vla-object (ssname ss i)))
;; Close the polyline if it's open
(ClosePolyline ent)
(setq i (1+ i))
)
;; Hatch the now-closed polylines
(command "HATCH" "P" hatch-pattern "S" (rtos hatch-scale 2 3) "A" hatch-angle ss "")
)
(princ (strcat "\nNo polylines found in layer: " layer))
)
)
(princ (strcat "\nLayer does not exist: " layer))
)
)

(princ "\nHatching process completed.")
)

(princ "\nType 'AutoHatch' to run the hatch automation.")
(princ)

0 Likes
Message 9 of 11

Kent1Cooper
Consultant
Consultant

@Bartosz_MalinowskiCSG4A wrote:

.... chat GPT was created for this, but it's not working. ....)


That is never enough information.  What does it do, how far does it get, are there any messages, etc., etc.?

 

AI is still far from competent at AutoLisp, as we have seen in many topics here.

 

By the way, don't use the "Reply to the topic..." slot under the last Message.  Anything entered there shows up as being in reply to Message 1 and its author [you in this case].  Always use the REPLY button at lower right in the Message you're replying to.  And preferably quote it, editing down to just the aspect you are replying about.

Kent Cooper, AIA
0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant

So that code has some problems and raises some questions.

 

This:

  (setvar 'clayer (tblobjname "LAYER" layer))

is just wrong.  It wants only the Layer name as a text string, which is already in the 'layer' variable:

  (setvar 'clayer layer)

 

But there's no need to set the Layer current to find things on it.  I would do the selection of things on it first, and only if it finds any, then set the Layer, if you want the Hatch on that same Layer.  [Or don't set the Layer at all, but change the Layer of the Hatch to match afterwards.]

 

It looks like it is intended to make one Hatch pattern in all such Polylines together.  Is that what you want, or should the Hatch in each one be independent?

And the big question:  Do you want any open Polylines closed and Hatched [as it would do], or left un-Hatched because you want only the closed ones Hatched?  If the latter, it is not difficult to have the selection find only closed ones, and not have to consider whether each is closed in the selection, because all will be.

Kent Cooper, AIA
0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

For example [minimally tested]:

 

(defun C:HPL () ; = Hatch with Pattern by Layer
  (foreach class
    '(
      ("Layer1" "ANSI31" 1.0 0)
      ("Layer2" "BRICK" 1.5 45)
      ("Layer3" "SOLID")
      ("Layer4" "CROSSHATCH" 0.8 90)
    )
    (if (setq ss (ssget "_X" (list (cons 8 (car class)) '(0 . "LWPOLYLINE") '(-4 . "&") '(70 . 1))))
      (repeat (setq n (sslength ss)); then
        (command "_.hatch" (cadr class)); pattern name
        (if (/= (cadr class) "SOLID") (command (nth 2 class) (nth 3 class))); scale/rotation
        (command (ssname ss (setq n (1- n))) "" "_.chprop" (entlast) "" "_layer" (car class) "")
      ); repeat
    ); if
  ); foreach
  (prin1)
)

 

[I don't have a pattern called "CROSSHATCH," so I used a different one for testing -- ensure that you have such a pattern.]

That assumes you want only closed Polylines processed -- it will find only those.  [It could be made to work like the AI version, also finding un-closed ones and closing them for you.]  And it could be made to find other closed objects [see earlier Messages].

It Hatches each one individually, for independence from each other.

And I removed the scale and rotation entries for the "SOLID" pattern [not asked for], and had the Hatch command feed those in only for other patterns.

Kent Cooper, AIA
0 Likes