Lisp to set solid hatches to bylayer

Lisp to set solid hatches to bylayer

Anonymous
Not applicable
1,322 Views
2 Replies
Message 1 of 3

Lisp to set solid hatches to bylayer

Anonymous
Not applicable
Please help me by providing a lisp that can set all solid hatches to bylayer, preferably color 252
0 Likes
1,323 Views
2 Replies
Replies (2)
Message 2 of 3

kerry_w_brown
Mentor
Mentor

 

DXF group Code  62 indicates :

Color number (present if not BYLAYER);

zero indicates the BYBLOCK (floating) color;

256 indicates BYLAYER;

a negative value indicates that the layer is turned off (optional)

 

So the option you want is either/or  

 

256 for BYLAYER 

252 for the color you mentioned

 

You can't have both.

 

 

Have a play with something like this :

(defun c:doit (/ ss collection n hatch layerObj)
  
  ;; select ALL the applicable hatches
  (setq ss         (ssget "X" '((0 . "HATCH") (2 . "SOLID")))
        collection nil
  )
  
  ;; convert the selection set to a list of ActiveX objects
  (if ss
    (repeat (setq n (sslength ss))
      (setq collection (cons (vlax-ename->vla-object
                               (ssname ss (setq n (1- n)))
                             )
                             collection
                       )
      )
    )
  )

  ;; make the layer (assume it doesn't exixts )
  
  (setq layerObj
         (vla-Add (vla-get-Layers
                    (vla-get-ActiveDocument (vlax-get-acad-object))
                  )
                  "HATCH"
         )
  )

  ;; assign the HATCH layer to each object
  (foreach hatch collection (vla-put-layer hatch "HATCH"))
  
  ;; set the color of each hatch object to 252
  (foreach hatch collection (vla-put-Color hatch 252))
  (princ)
)
(princ)

// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 3

Kent1Cooper
Consultant
Consultant

@KerryBrown wrote:

 

....
  ;; select ALL the applicable hatches
....  
  ;; convert the selection set to a list of ActiveX objects
....
  ;; make the layer (assume it doesn't exixts )
....
  ;; assign the HATCH layer to each object
....
;; set the color of each hatch object to 252 ....

This is a situation in which a (command) function has advantages over the VLA-Property approach.  The CHPROP command can change the Layer or the color or both of a whole selection all at once, rather than one at a time, and without the need to convert them to VLA objects, and with far fewer variables.  Assuming the Layer does not yet exist [you can eliminate that line if it always will]:

 

(defun c:doit (/ ss)
  (if (setq ss (ssget "X" '((0 . "HATCH") (2 . "SOLID"))))
(command ; then
"_layer" "_make" "HATCH" "_color" 252 "" ""
"_chprop" ss "" "_layer" "HATCH" "_color" "_bylayer" ""
); command
); if (princ) )

Or, if you want to assign the color 252 by color override on the objects, rather than by putting them on a Layer with that color, just this:

 

(defun c:doit (/ ss)
  (if (setq ss (ssget "_X" '((0 . "HATCH") (2 . "SOLID"))))
(command "_chprop" ss "" "_color" 252 ""); then
); if (princ) )

If you know there will always be such Hatch patterns when you run it, you don't even need any variables at all.  Back to the by-the-Layer's-color approach:

 

(defun c:doit ()
  (command
"_layer" "_make" "HATCH" "_color" 252 "" ""
"_chprop" (ssget "_X" '((0 . "HATCH") (2 . "SOLID"))) "" "_layer" "HATCH" "_color" "_bylayer" ""
); command
(princ)
)

However, the VLA-Property approach will work on things in multiple spaces, whereas the CHPROP approach will work only on Hatch patterns in the current space.  So if you might have such patterns in both Model and Paper space, or in different Paper space Layouts, don't do it with CHPROP.

Kent Cooper, AIA
0 Likes