Only SOLID hatching in the background - lisp

Only SOLID hatching in the background - lisp

pavel
Explorer Explorer
124 Views
12 Replies
Message 1 of 13

Only SOLID hatching in the background - lisp

pavel
Explorer
Explorer

I need help figuring out how to change the order of SOLID hatches to the background so that they are at the very bottom. And most importantly, I need it to work on blocks and dynamic blocks. Thank you very much for your ideas. Ideally, a Lisp solution.

0 Likes
125 Views
12 Replies
Replies (12)
Message 2 of 13

Moshe-A
Mentor
Mentor

@pavel ,

 

What AutoCAD version are you using

post sample dwg

 

Moshe

0 Likes
Message 3 of 13

BlackBox_
Advisor
Advisor

This will send solid hatches to the back:

(defun c:SolidHatchToBack ( / ss )
  (if (setq ss (ssget "_x" '((0 . "HATCH")(2 . "_SOLID"))))
    (command "._draworder" ss "" "_b")
  )
  (princ)
)

 

Processing Blocks is a completely different task. 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 4 of 13

pavel
Explorer
Explorer

Thank you, this works, but it doesn't work on blocks. That's what I need most.

0 Likes
Message 5 of 13

pavel
Explorer
Explorer

Is it possible to create this from the information at this link?

https://www.cadtutor.net/forum/topic/66677-lisp-request-send-hatch-to-back-in-all-blocks/

0 Likes
Message 6 of 13

Kent1Cooper
Consultant
Consultant

Just because it occurs to me....

If you have Blocks with Solid Hatch patterns in them, you can put the Hatching behind everything else within the Block, but if the Block is inserted in front other things, the Hatching in it will still mask those other things.  I think you would need to move such a Block behind other things, though whether that works for the appearance you want may depend on what those other things are, and what the Block contains other than the Hatching.

Kent Cooper, AIA
0 Likes
Message 7 of 13

BlackBox_
Advisor
Advisor

@pavel wrote:

Is it possible to create this from the information at this link?

https://www.cadtutor.net/forum/topic/66677-lisp-request-send-hatch-to-back-in-all-blocks/


 

Absolutely; here's a routine that combines the code above and Emmanuel Delay's code posted at CADTutor here

 

(vl-load-com)

(defun c:SolidHatchToBack (/ *error* _HatchToBack ss acDoc ok result)

  (defun *error* (msg)
    (if (and acDoc ok)
      (vla-endundomark acDoc)
    )
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )

  (defun _HatchToBack (blkname / od_ent)
    (setq od_ent (tblobjname "block" blkname))
    (while (setq od_ent (entnext od_ent))
      (if t
        (vl-catch-all-apply
          (function
            (lambda ()
              (vla-movetobottom
                (vla-addobject
                  (vla-getextensiondictionary
                    (vla-item (vla-get-blocks *doc*) blkname)
                  )
                  "acad_sortents"
                  "acdbsortentstable"
                )
                (vlax-make-variant
                  (vlax-safearray-fill
                    (vlax-make-safearray vlax-vbobject '(0 . 0))
                    (list (vlax-ename->vla-object od_ent))
                  )
                )
              )
            )
          )
        )
      )
    )
  )

  ;; hatches
  (if (setq ss (ssget "_x" '((0 . "HATCH") (2 . "_SOLID"))))
    (progn
      (vla-startundomark
        (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
      )
      (setq ok t)
      (command "._draworder" ss "" "_b")
    )
  )

  ;; blocks
  (setq result (list))
  (vlax-for block (vla-get-blocks acDoc)
    (if (not (wcmatch (strcase (vla-get-name block) t) "*_space*"))
      (progn
        (setq result (append result (list (vla-get-name block))))
      )
    )
  )
  (if result (or ok (setq ok t)))
  (foreach blkname result
    (_HatchToBack blkname)
  )
  (vla-regen acDoc 1)
  (*error* nil)
)

 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 8 of 13

paullimapa
Mentor
Mentor

The code is pretty good but I had to made the following changes:

1) _HatchToBack function needs to define *doc*

  (defun _HatchToBack (blkname / od_ent *doc*) ; add *doc*
; add setq *doc*    
    (setq *doc* (vla-get-ActiveDocument (vlax-get-acad-object)))
;

2) When there are no individual Solid Hatch patterns then acdoc variable won't be set so I moved this before if statement:

  ;; set undo mark & acDoc variable
  (vla-startundomark
    (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
  )

3) Hatch pattern name is "SOLID" so "_SOLID" won't work

  ;; hatches
  (if 
    (setq ss (ssget "_x" '((0 . "HATCH") (2 . "SOLID")))) ; pattern name = SOLID
  ; (setq ss (ssget "_x" '((0 . "HATCH") (2 . "_SOLID"))))

Questions:

1) Is there a way to filter the Block objects so that only those with Hatch Solid pattern are vla-movetobottom?

2) Blocks with Hatch Solid patterns already moved to back when SOLIDHATCHTOBACK runs will move those Solid patterns inside the Block to the front.

Is there a way to filter them out so they are left alone?

I've included sample Hatch Objects.dwg for testing.


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

paullimapa
Mentor
Mentor

So I modified the _HatchToBack function and now filters the Block objects so that only Hatch Solid patterns are moved to back and for some reason also no longer impact those Hatch Solid patterns already set to back:

  (defun _HatchToBack (blkname / od_ent *doc*) ; add *doc*
; add setq *doc*    
    (setq *doc* (vla-get-ActiveDocument (vlax-get-acad-object)))
;
    (setq od_ent (tblobjname "block" blkname))
    (while (setq od_ent (entnext od_ent))
      (if t
        (if 
          (and 
            (eq "HATCH" (strcase (getpropertyvalue od_ent "LocalizedName")))  ; add to only move to back Hatch Solid Pattern
            (eq "SOLID" (strcase (getpropertyvalue od_ent "PatternName")))
          )       
        (vl-catch-all-apply
          (function
            (lambda ()
              (vla-movetobottom
                (vla-addobject
                  (vla-getextensiondictionary
                    (vla-item (vla-get-blocks *doc*) blkname)
                  )
                  "acad_sortents"
                  "acdbsortentstable"
                )
                (vlax-make-variant
                  (vlax-safearray-fill
                    (vlax-make-safearray vlax-vbobject '(0 . 0))
                    (list (vlax-ename->vla-object od_ent))
                  )
                )
              )
            )
          )
        )
       ) ; if
      )
    )
  ) ; defun

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 10 of 13

BlackBox_
Advisor
Advisor

@paullimapa wrote:

The code is pretty good but I had to made the following changes:

1) _HatchToBack function needs to define *doc*

  (defun _HatchToBack (blkname / od_ent *doc*) ; add *doc*
; add setq *doc*    
    (setq *doc* (vla-get-ActiveDocument (vlax-get-acad-object)))
;

No, it doesn't - but they typo of leaving it as *doc* does need to change to acDoc as defined in the main routine. Good catch; I was heading into a meeting and missed it when combining the CADTutor code (my bad).

 



@paullimapa wrote:

2) When there are no individual Solid Hatch patterns then acdoc variable won't be set so I moved this before if statement:

  ;; set undo mark & acDoc variable
  (vla-startundomark
    (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
  )

Moving the setq for acDoc is correct, but your suggestion to move startundomark is incorrect logic, as there's no need to do so if no solid hatches or blocks with same are found. See revised code.

 



@paullimapa wrote:

3) Hatch pattern name is "SOLID" so "_SOLID" won't work

  ;; hatches
  (if 
    (setq ss (ssget "_x" '((0 . "HATCH") (2 . "SOLID")))) ; pattern name = SOLID
  ; (setq ss (ssget "_x" '((0 . "HATCH") (2 . "_SOLID"))))

This is interesting - as I had initially used "SOLID" only to find out the code didn't work in my test drawing, so when I listed my solid hatches entity data, DXF 2 == "_SOLID", hence the change. This is a drawing-specific issue, as your test drawing clearly lists it as "SOLID".

 

I'm using Civil 3D 2026, so I wonder if there's something different between products? 

 

Either way, changing this to "*SOLID" resolves both.

 



@paullimapa wrote:

Questions:

1) Is there a way to filter the Block objects so that only those with Hatch Solid pattern are vla-movetobottom?

2) Blocks with Hatch Solid patterns already moved to back when SOLIDHATCHTOBACK runs will move those Solid patterns inside the Block to the front.

Is there a way to filter them out so they are left alone?

I've included sample Hatch Objects.dwg for testing.


I'm sure there is, and I can reproduce that issue. I must admit I'm better at .NET than I am at LISP now, so I'd have to look into it more with that CADTutor code... perhaps someone more knowledgeable like @Kent1Cooper or @pendean already knows. 

 

That said, I'm not a fan of modifying the Blocks this way, I much prefer Block Editor as I have so few blocks where this situation might apply. Just trying to combine it at OP's request. 


Revised code with @paullimapa 's apt feedback: 

(vl-load-com)

;; https://www.cadtutor.net/forum/topic/66677-lisp-request-send-hatch-to-back-in-all-blocks/#findComment-546496

(defun c:SolidHatchToBack (/ *error* _HatchToBack acDoc ss result ok)

  (defun *error* (msg)
    (if acDoc (vla-endundomark acDoc))
    (if ok (vla-regen acDoc 1))
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )

   (defun _HatchToBack (blkname / od_ent)
    (setq od_ent (tblobjname "block" blkname))
    (while (setq od_ent (entnext od_ent))
      (if t
        (if 
          (and 
            (eq "HATCH" (strcase (getpropertyvalue od_ent "LocalizedName")))  
            (wcmatch (strcase (getpropertyvalue od_ent "PatternName")) "*SOLID")
          )       
        (vl-catch-all-apply
          (function
            (lambda ()
              (vla-movetobottom
                (vla-addobject
                  (vla-getextensiondictionary
                    (vla-item (vla-get-blocks acDoc) blkname)
                  )
                  "acad_sortents"
                  "acdbsortentstable"
                )
                (vlax-make-variant
                  (vlax-safearray-fill
                    (vlax-make-safearray vlax-vbobject '(0 . 0))
                    (list (vlax-ename->vla-object od_ent))
                  )
                )
              )
            )
          )
        )
       )
      )
    )
  )
  (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))

  ;; get hatches
  (setq ss (ssget "_x" '((0 . "HATCH") (2 . "*SOLID"))))

  ;; get blocks
  (setq result (list))
  (vlax-for block (vla-get-blocks acDoc)
    (if (not (wcmatch (strcase (vla-get-name block) t) "*_space*"))
      (progn
        (setq result (append result (list (vla-get-name block))))
      )
    )
  )

  ;; if either, start undo
  (if (or ss result)
    (progn
      (vla-startundomark acDoc)
      (setq ok t)
    )
  )

  ;; process hatches
  (if ss (command "._draworder" ss "" "_b"))

  ;; process blocks
  (if result
    (foreach blkname result
      (_HatchToBack blkname)
    )
  )
  
  (*error* nil)
)

 

[Edit] - updated per this post

 

 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 11 of 13

paullimapa
Mentor
Mentor

Excellent...the revised version is definitely coded better than the original post...cheers!!!


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

BlackBox_
Advisor
Advisor

@paullimapa wrote:

Excellent...the revised version is definitely coded better than the original post...cheers!!!


That's kind of you to say; it's a joint effort! 🍻

 

Cheers


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 13 of 13

paullimapa
Mentor
Mentor

sometimes it takes a village but this time it only took two...lol


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