Jump to content

Lisp to hatch objects on specific layers


pyou

Recommended Posts

On 20/10/2023 at 20:09, Steven P said:

 

 

I'm not getting that, can you post an example drawing? Should be something simple

Hi Steven P, I have attached example and lisp. First I run H command manually and selected Layer as ''random layer test''  and hatch one of the  rectangles as ''pick points in boundaries'' and the second rectangle as ''select  boundary entities'' after that i tried to run the lisp and it used ''random layer test'' layer. 

test1.lsp test3.dwg

Edited by pyou
Link to comment
Share on other sites

Did not look at code but for future reference "0 height" in CAD is referred to as "Elevation". So can set the current plane Z value, For your 3dpoly I think its at current Elev value, but its a flat plane all at that Z value.

  • Agree 1
Link to comment
Share on other sites

Having a quick look at this, it mostly works for me as your link test1, apart from the blocks - I haven't got time just now to work out why

 

I changed the hatch slightly for the polylines, circles etc to be a bit quicker and added an undo marker - I'll see if I get chance to look at the blocks later

 

(defun c:test1 ( / Lay_Old MySS target_layers block_names layer_name hatch_pattern )
  (setq Lay_Old (getvar 'clayer)) ; Record the current layer

  ; Define a list of layers for hatching with different patterns
  (setq target_layers
    '(
    ("Layer1" "SOLID")
    ("Layer2" "SOLID")
    ("Layer3" "SOLID")
    ("Layer4" "SOLID")
    ("Layer5" "ANSI31")
    ("Layer6" "ANSI31")
    ("New Layer" "ANSI31")
    ("New Layer" "SOLID")
      ; Add more layers here as needed
    ) ; end list
  )   ; end setq

; Define a list of block names to hatch
  (setq block_names '("CIRCL" "Triangle" "MH3"))


;;Added undo mark
(setq thisdrawing (vla-get-activedocument (vlax-get-acad-object)))
(vla-startundomark thisdrawing)


  (foreach layer_data target_layers
    (setq layer_name (car layer_data))
    (setq hatch_pattern (cadr layer_data))

    ; Set the current layer to "Chamber_Hatch" if the hatch pattern is "ANSI31"
    (if (equal hatch_pattern "ANSI31")
      (setvar 'clayer "Chamber_Hatch")
    )

    ; Set the current layer to "Features_Hatch" if the hatch pattern is "SOLID"
    (if (equal hatch_pattern "SOLID")
      (setvar 'clayer "Features_Hatch")
    )

    ; Select objects for hatching
    (setq MySS (ssget "X" (list '(0 . "*POLYLINE,CIRCLE,ARC,RECTANGLE") (cons 8 layer_name))))
    (if MySS
      (progn
;        (setq i 0)
;        (repeat (sslength MySS)
;          (setq ename (ssname MySS i))
          (if (equal hatch_pattern "SOLID")
;            (command "-hatch" "S" ename "" "P" "SOLID" "")
;            (command "-hatch" "P" hatch_pattern 0.03 0.0 "S" ename "" "")

;;Changed to this, removes a loop and is quicker
(command "-hatch" "S" MySS "" "P" "SOLID" "")
(command "-hatch" "P" hatch_pattern 0.03 0.0 "S" MySS "" "")

          ) ; end if
;          (setq i (1+ i))
;        ) ; end repeat
      ) ; end progn
    ) ; end if

    ; Select and hatch blocks
    (foreach block_name block_names
      (setq MySS (ssget "X" (list '(0 . "INSERT") (cons 8 layer_name) (cons 2 block_names))))
      (if MySS
        (progn
          (setq i 0)
          (repeat (sslength MySS)
            (setq ename (ssname MySS i))
            (if (equal hatch_pattern "SOLID")
              (command "-hatch" "S" ename "" "P" "SOLID" "")
              (command "-hatch" "P" hatch_pattern 0.03 0.0 "S" ename "" "")
            )
            (setq i (1+ i))
          )
        )
      )
    )

    ; Reset the current layer to the original layer
    (setvar 'clayer Lay_Old)
  )

;;added this line
(vla-endundomark thisdrawing)

  (princ) ; End quietly
)

 

  • Thanks 1
Link to comment
Share on other sites

looking at this again, I think you might want to think about hatching the blocks again - I wasn't thinking straight yesterday but if you add a hatch into one block, all instances of that block will get that hatch.

 

You could 'edit' the block (either edit command or use the entity information) and grab all the entities that the block is made from, create a list of points from each part of the block, come out of the block, align these points to the instance of that block and create a hatch from points - which should sit on top of the block. That sounds like a bit of work to do this evening though.

  • Thanks 1
Link to comment
Share on other sites

On 24/10/2023 at 20:15, Steven P said:

looking at this again, I think you might want to think about hatching the blocks again - I wasn't thinking straight yesterday but if you add a hatch into one block, all instances of that block will get that hatch.

 

You could 'edit' the block (either edit command or use the entity information) and grab all the entities that the block is made from, create a list of points from each part of the block, come out of the block, align these points to the instance of that block and create a hatch from points - which should sit on top of the block. That sounds like a bit of work to do this evening though.

 

Thanks Steven P , but the first version worked better. I just can understand why it does not work when you start manually hatch things and use the lisp afterwards. It seems you can run this lisp only at the beginning.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...