Change all Hatch on a Layer

Change all Hatch on a Layer

Anonymous
Not applicable
3,456 Views
25 Replies
Message 1 of 26

Change all Hatch on a Layer

Anonymous
Not applicable

(defun C:H-C ()
(Setq a (ssget "X" '((8 . "Test1"))))(command "-hatchedit" a "p" "ANSI31" "1" "0")
(Setq a (ssget "X" '((8 . "test2"))))(command "-hatchedit" a "p" "SOLID" "1" "0")

(Setq a (ssget "X" '((8 . "Test3"))))(command "-hatchedit" a "p" "ANSI33" "1" "0")
(Setq a (ssget "X" '((8 . "test4"))))(command "-hatchedit" a "p" "DASH" "1" "0")

(Setq a (ssget "X" '((8 . "Test5"))))(command "-hatchedit" a "p" "ANSI33" "1" "0")
(Setq a (ssget "X" '((8 . "test6"))))(command "-hatchedit" a "p" "ANSI31" "1" "0")

)

 

If I have a lot of objects on the layer it doesn't work, where is the mistake?

0 Likes
Accepted solutions (1)
3,457 Views
25 Replies
Replies (25)
Message 2 of 26

hak_vz
Advisor
Advisor

@Anonymous  Run command Hatchedit and try to select more then one entity. Does it work? Probably not.

 

What you have to do is run command for each object in selection set.

(setq i -1 a (ssget "X" '((8 . "Test1"))))
(while (< (setq i (1+ i)) (sslength  a ))
(command "-hatchedit" (ssname ss i a) "p" "ANSI31" "1" "0")
)
;.....

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 26

ВeekeeCZ
Consultant
Consultant

Some LISP versions of commands differ from ones currently used in AutoCAD. LISP uses older versions. If I want to check whether a specific command is a case, I run (command-s "-hatchedit") in the command-line.

 

The simplest fix is to use the (initcommandversion) prior to the command call. 

 

(defun c:test ()
  
  (if (setq a (ssget '((0 . "HATCH") (8 . "Test1"))))
    (progn
      (initcommandversion)
      (command "_.-hatchedit" a "" "_p" "ANSI31" 1 0)))
  
  (if (setq a (ssget '((0 . "HATCH") (8 . "Test2"))))
    (progn
      (initcommandversion)
      (command "_.-hatchedit" a "" "_p" "SOLID")))
  )

 

Note that if you use a SOLID type, prompts differ from other hatch patterns. 

Also filter the selection for HATCH object type, just in case... to prevent failure.

0 Likes
Message 4 of 26

Anonymous
Not applicable

Thanks, but is still not working.

 

0 Likes
Message 5 of 26

hak_vz
Advisor
Advisor

@Anonymous 

 

What have you tested? My example works fine.

If you are using some older version using initcommandversion may help.

Command hatch edit works with single entity and not entire selection set. If used with selection set that contains more then one entity, changes will apply only to first element.  That was idea of my first post.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 6 of 26

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Thanks, but is still not working.


[That Message shows as being in response to yourself, so we can't tell whose suggestion is not working.  Make sure you pick on the Reply button in an individual Message, not in the "Reply-to-the-topic" slot at the bottom, which is always listed as being in Reply to the original post.  It also helps to quote at least a small part of what you're Replying to.]

Kent Cooper, AIA
0 Likes
Message 7 of 26

hak_vz
Advisor
Advisor

Also, your selection set creation is a problem.  You have to filter only hatch entities at particular layer i.e

 

(setq a (ssget "X" '((0 . "HATCH")(8 . "Test1"))))

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 8 of 26

Kent1Cooper
Consultant
Consultant

How about pairing Layer names with their intended pattern names, and having one selection set of all Hatch patterns on Layers with appropriate names, and substituting patterns and scale and angle into each one's entity data, pulling the pattern from the list of pairings, rather than using a Hatchedit command?  Then you don't need a separate selection for each Layer, with its separate (if) function in case there are no Hatch patterns on that Layer, or if that Layer doesn't exist.  It doesn't need to handle SOLID differently from other patterns.

 

Does this work [untested]?

 

 

(defun C:H-C (/ pairs ss n hatch edata)
  (setq pairs '(("TEST1" "ANSI31") ("TEST2" "SOLID") ("TEST3" "ANSI33") ("TEST4" "DASH") ("TEST5" "ANSI33") ("TEST6" "ANSI31")))
  (if (setq ss (ssget "_X" '((0 . "HATCH") (8 . "TEST#*")))); all Hatch patterns on Layers with such names
    (repeat (setq n (sslength ss)); then
      (setq
        hatch (ssname ss (setq n (1- n))); the Hatch object
        edata (entget hatch); its entity data
        edata (subst (cons 2 (cadr (strcase (assoc 8 edata)) pairs)) (assoc 2 edata) edata); pattern
        edata (subst '(41 . 1.0) (assoc 41 edata) edata); scale
        edata (subst '(52 . 0.0) (assoc 52 edata) edata); angle
      ); setq
      (entmod edata)
    ); repeat
  ); if
  (princ)
); defun

 

 

[I used all-capitals Layer names in the 'pairs' list, and (strcase) in pulling the Layer names from entity data, because there it's case-sensitive.  The (8 . "TEST#*") Layer filter in the (ssget) function is not case-sensitive.  It uses "TEST#*" to allow for Layer names into more than one digit at the end.  Using "TEST##" would not see those with only one digit, or three if it comes to that, but "TEST#*" would see something like "TEST7demo".  If there's any need to avoid complications like that, they can be accounted for.

But maybe that's all moot, assuming those Layer names are not what you will really have.  You can use:
  (8 . "ThisLayer,ThatLayer,LayerA,LayerB,Whatever")

with all "real" Layer names comma-delimited in one string.]

 

EDIT:  And it wouldn't be much work to have different scale factors and/or angles for the patterns on each Layer if needed -- they can be added to the 'pairs' sub-lists, and pulled in a similar way as the pattern name for substituting into entity data.

Kent Cooper, AIA
0 Likes
Message 9 of 26

Kent1Cooper
Consultant
Consultant

Sorry:  this line:

 

edata (subst (cons 2 (cadr (strcase (assoc 8 edata)) pairs)) (assoc 2 edata) edata); pattern

 

should be:

 

edata (subst (cons 2 (cadr (assoc (strcase (cdr (assoc 8 edata))) pairs))) (assoc 2 edata) edata); pattern

 

 

Kent Cooper, AIA
0 Likes
Message 10 of 26

Sea-Haven
Mentor
Mentor

For kent. When will the forum allow editing of a post classic example of a simple 1 line mistake that could be fixed all other forums allow this. I am guilty of typos.

0 Likes
Message 11 of 26

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

For kent. When will the forum allow editing of a post classic example of a simple 1 line mistake that could be fixed all other forums allow this. I am guilty of typos.


You can edit posts, but only within a time limit [I think it's 30 minutes] -- in fact, I did edit that one more than once before I let it be, and I didn't look it over again closely to notice the error until after the time limit.

 

The place to suggest such a change is in the >Community Feedback Forum<, but before you do, do a Search and read the numerous threads already there that ask the same question.

Kent Cooper, AIA
0 Likes
Message 12 of 26

Sea-Haven
Mentor
Mentor

Hi Kent. Have done so and so have others about editing posts, well aware 30 minutes. Problem is Forum needs a major upgrade to fix something Autodesk knows about.

0 Likes
Message 13 of 26

Anonymous
Not applicable

Thanks, I'll test it again when I'm at my computer.

0 Likes
Message 14 of 26

john.uhden
Mentor
Mentor

The HATCHEDIT command works on only one hatch entity.

IMHO, it would be better to iterate through the selection set, converting each ename to a vla-object and changing the object's PatterName property.  Oops.  At least in 2002 that property is read-only.

So, you will have to iterate through each member of the selectionset and invoke the HATCHEDIT command on each one.

Wait a second.  If you (sssetfirst nil ss) you can use the properties dialogue to select a different pattern for all of them, though the lisp method would require less human effort for the cost of a nanosecond or two.  But using the properties dialogue provides you a popup list of hatch patterns to choose from.

What I always do is HATCHEDIT one and then use MATCHPROPS on the others, but I'm never dealing with more than say 7 hatches, so it's no big deal.

John F. Uhden

0 Likes
Message 15 of 26

john.uhden
Mentor
Mentor

After slightly more thought, I recommend you create a number of command functions, e.g. c:ANSI31, c:AR-CONC, etc. that HATCHEDIT the pattername into the selectionset of hatches.

John F. Uhden

0 Likes
Message 16 of 26

Anonymous
Not applicable

Unfortunately it doesn't work yet. Can it be due to the layer name? Inventory_Konstr_N-1_Bestand_Hatch, inventory_Konstr_N-1_Bestand_Hatch ??

0 Likes
Message 17 of 26

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

Unfortunately it doesn't work yet. Can it be due to the layer name? Inventory_Konstr_N-1_Bestand_Hatch, inventory_Konstr_N-1_Bestand_Hatch ??


I wouldn't think that would matter, as long as the Layers are all spelled correctly in the pairing list, and in the (ssget) filter comma-delimited Layer name string [which must not have spaces on either side of the commas].

 

In case there was anything about doing the "repair," here's my edit, which worked for me using your Test# Layer names:

 

(defun C:H-C (/ pairs ss n hatch edata)
  (setq pairs '(("TEST1" "ANSI31") ("TEST2" "SOLID") ("TEST3" "ANSI33") ("TEST4" "DASH") ("TEST5" "ANSI33") ("TEST6" "ANSI31")))
  (if (setq ss (ssget "_X" '((0 . "HATCH") (8 . "TEST#*")))); all Hatch patterns on Layers with such names
    (repeat (setq n (sslength ss)); then
      (setq
        hatch (ssname ss (setq n (1- n))); the Hatch object
        edata (entget hatch); its entity data
        edata (subst (cons 2 (cadr (assoc (strcase (cdr (assoc 8 edata))) pairs))) (assoc 2 edata) edata); pattern
        edata (subst '(41 . 1.0) (assoc 41 edata) edata); scale
        edata (subst '(52 . 0.0) (assoc 52 edata) edata); angle
      ); setq
      (entmod edata)
    ); repeat
  ); if
  (princ)
); defun

 

Kent1Cooper_0-1613564079973.png

The left one is a User-defined pattern, and the rest were all the same but on different Layers, with the color being the same as the number at the end of the Layer name.  The routine changed all those pattern names.

Kent Cooper, AIA
0 Likes
Message 18 of 26

Anonymous
Not applicable

Can I also search a little differently (8. "TEST # *")?

so that only the last part is always the same (_hatch)?

Layer name is:

Konstr_N-1_Bestand_Hatch

N-1_Bestand_Hatch

0 Likes
Message 19 of 26

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Can I also search a little differently (8. "TEST # *")? so that only the last part is always the same (_hatch)?

Layer name is:

Konstr_N-1_Bestand_Hatch

N-1_Bestand_Hatch


The asterisk is a wildcard for any number of characters.  You can put it before the known/fixed characters.  Try:

  (8 . "*_Hatch")

[and note that you need a space between the 8 and the period/decimal.]

 

Kent Cooper, AIA
0 Likes
Message 20 of 26

Anonymous
Not applicable

; error: bad DXF group: (2)

0 Likes