AutoCAD LT AutoLISP - Hatch LISP routine colour issues

AutoCAD LT AutoLISP - Hatch LISP routine colour issues

BigBoyCAD
Enthusiast Enthusiast
185 Views
10 Replies
Message 1 of 11

AutoCAD LT AutoLISP - Hatch LISP routine colour issues

BigBoyCAD
Enthusiast
Enthusiast

AutoCAD LT 2025

 

I would like to create a lisp routine that:

- creates layer (if not already created) called 'Earth Hatch 2' - colour '11'

- enables hatching of multiple pick points

- Hatch pattern 'Earth'

- Hatch scale '35'

- Hatch angle '45'

- Successfully places hatch on colour 11 regardless of previous hatch colour settings being set to:

   'ByLayer', 'ByBlock', 'Use Current' or set to an index colour. 

- Allows for furture hatches, created via the normal hatch command, to be created on the current layer at the time as opposed to continuing to be created on 'Earth Hatch 2' layer.

- Allows for furture hatches, created via the normal hatch command, not to retain the same attributes set by the LISP routine i.e. scale, angle, colour, though to remain what ever the settings were previously set to.

 

The following code works perfectly except:

- requires you to press 'enter' to confirm 'none' for background colour.

   I would like this to happen automatically.

- Future hatches are created on colour 11

 

As I understand it AutoCAD LT has limits regarding how you can set the colour for hatches through LISP routines. 

- The background color prompt cannot be suppressed or auto-answered through LISP.

- LT doesn’t let you set it directly by LISP

Can someone please help me remove the prompt for 'new background colour' after initiating the LISP routine?

Many thanks.

;; ERH17M-MP18 — Earth hatch, multiple pick points
(defun c:ERH17M-MP18 ( / oldce oldlay oldhpname oldhpscale oldhpang oldhplayer lay)
;; Save environment & hatch defaults
(setq oldce (getvar "CMDECHO"))
(setq oldlay (getvar "CLAYER"))
(setq oldhpname (getvar "HPNAME"))
(setq oldhpscale (getvar "HPSCALE"))
(setq oldhpang (getvar "HPANG"))
(setq oldhplayer (getvar "HPLAYER"))

(setvar "CMDECHO" 0) ; quiet mode

(setq lay "Earth Hatch 2")

;; Ensure layer exists and is colour 11
(if (tblsearch "LAYER" lay)
(vl-cmdf "_.-LAYER" "C" "11" lay "")
(vl-cmdf "_.-LAYER" "N" lay "C" "11" lay "")
)

;; Temporarily make Earth Hatch 2 current
(setvar "CLAYER" lay)

;; Start hatch with full property setup
(vl-cmdf
"_.-HATCH"
"_Properties" "EARTH" "35" "45"
"_Layer" lay
"_Color" "11" ;; <- force hatch colour to 11, independent of HPCOLOR
"_PickPoints"
)

;; Allow multiple picks until user presses Enter
(while (= 1 (getvar "CMDACTIVE"))
(command pause)
)

;; Restore hatch defaults
(if oldhpname (setvar "HPNAME" oldhpname))
(if oldhpscale (setvar "HPSCALE" oldhpscale))
(if oldhpang (setvar "HPANG" oldhpang))
(if oldhplayer (setvar "HPLAYER" oldhplayer))

;; Restore previous layer and CMDECHO
(setvar "CLAYER" oldlay)
(setvar "CMDECHO" oldce)
(princ)
)

 

 

0 Likes
Accepted solutions (1)
186 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Use 'HPCOLOR sysvar in the same way you used other HP* sysvars.

0 Likes
Message 4 of 11

BigBoyCAD
Enthusiast
Enthusiast

Thanks @ВeekeeCZ and @pendean.

 

Though the issue is that LT doesn't seem to allow you to set the hatch colour or hatch back ground colour via LISP.

 

I was hoping to find a work around to avoid the prompt coming  that requires you to press 'enter' to confirm 'none' for background colour.

The following results occur when trying to designate the HPBACKGROUNDCOLOR system variable.

 

1.

;; Set hatch background color to None
(setvar "HPBACKGROUNDCOLOR" "None")

Results in:
; error: AutoCAD variable setting rejected: "HPBACKGROUNDCOLOR" "None"

 

2.
;; Set hatch background color to None
(setvar "HPBACKGROUNDCOLOR" ".")

Results in:
A color number or standard color name is required.

 

You are then required to press enter to confirm 'none' for hatch back ground colour.

0 Likes
Message 5 of 11

ВeekeeCZ
Consultant
Consultant

If there is a glitch with this particular variable, I doubt it would be specific to LT (2024+).

 

You should be able to use this syntax

(setvar "HPBACKGROUNDCOLOR" ".").

That would be a preferable way.

 

If not, you can try (command "HPBACKGROUNDCOLOR" ".")

0 Likes
Message 6 of 11

komondormrex
Mentor
Mentor
Accepted solution

check this subtle mod to your code

(defun c:ERH17M-MP18 ( / oldce oldlay oldhpname oldhpscale oldhpang oldhplayer lay)
;; Save environment & hatch defaults
(setq oldce (getvar "CMDECHO"))
(setq oldlay (getvar "CLAYER"))
(setq oldhpname (getvar "HPNAME"))
(setq oldhpscale (getvar "HPSCALE"))
(setq oldhpang (getvar "HPANG"))
(setq oldhplayer (getvar "HPLAYER"))

(setvar "CMDECHO" 0) ; quiet mode

(setq lay "Earth Hatch 2")

;; Ensure layer exists and is colour 11
(if (tblsearch "LAYER" lay)
(vl-cmdf "_.-LAYER" "C" "11" lay "")
(vl-cmdf "_.-LAYER" "N" lay "C" "11" lay "")
)

;; Temporarily make Earth Hatch 2 current
(setvar "CLAYER" lay)

;; Start hatch with full property setup
(vl-cmdf
"_-HATCH"
"_Properties" "EARTH" "35" "45"
"_Layer" lay
"_Color" "11" ;; <- force hatch colour to 11, independent of HPCOLOR
;;;"_PickPoints"	;	komondormrex
""			;	komondormrex
)

;; Allow multiple picks until user presses Enter
(while (= 1 (getvar "CMDACTIVE"))
(command pause)
)

;; Restore hatch defaults
(if oldhpname (setvar "HPNAME" oldhpname))
(if oldhpscale (setvar "HPSCALE" oldhpscale))
(if oldhpang (setvar "HPANG" oldhpang))
(if oldhplayer (setvar "HPLAYER" oldhplayer))


;; Restore previous layer and CMDECHO
(setvar "CLAYER" oldlay)
(setvar "CMDECHO" oldce)
(princ)
)
0 Likes
Message 7 of 11

BigBoyCAD
Enthusiast
Enthusiast

Thank you @ВeekeeCZ 

Unfortunately neither of these approaches solved the issue of eliminating the dialogue box / the need to press enter to confirm background colour.

0 Likes
Message 8 of 11

BigBoyCAD
Enthusiast
Enthusiast

Thank you so much @komondormrex .

This was the fix.

 

Much appreciated! 👍

0 Likes
Message 9 of 11

BigBoyCAD
Enthusiast
Enthusiast

May I please ask..

How would I adjust this to make versions that allow for:

- single selection  'picK internal point' hatch

- single selection 'Select objects' hatch

- multiple selection 'Select objects' hatch 

 

with everything else staying the same?

0 Likes
Message 10 of 11

Sea-Haven
Mentor
Mentor

Just a quick maybe (getvar 'hpbackgroundcolor) = "NONE" not "None". It worked though in my Bricscad. Part 2 (setvar 'hpbackgroundcolor "32") note string value of color number. or primary color eg "Red"

0 Likes
Message 11 of 11

komondormrex
Mentor
Mentor

@BigBoyCAD wrote:

with everything else staying the same?
- single selection  'picK internal point' hatch

- single selection 'Select objects' hatch

- multiple selection 'Select objects' hatch 


pure single internal point imho may be done with using boundary to check if there is applicable area for hatching, etc. otherwise double space using code from message 6... 

single pick may be done just fine. see attached  ERH17M-MP18_Single_Pick.lsp

multiple picks will require double space to end picking, which is not good for the program. see attached ERH17M-MP18_Multiple_Picks.lsp

multiple selection will also require double space to end selecting, ... see attached ERH17M-MP18_Multiple_Select.lsp 

 

0 Likes