@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