Lisp routine to draw a rectangle and hatch it

Ajohnson0
Contributor
Contributor

Lisp routine to draw a rectangle and hatch it

Ajohnson0
Contributor
Contributor

I am looking to draw a column by using a lisp routing. I need it to put the border on a layer called column and the hatch on a layer called column hatch. I ideally would like to just be able to use the rectangle command and just draw a rectangle and have it automatically hatched. Could anyone here make a quick lisp routine? I am not very good at making my own and any help would be greatly appreciated

0 Likes
Reply
Accepted solutions (1)
3,980 Views
22 Replies
Replies (22)

ВeekeeCZ
Consultant
Consultant

Just a quick one, as said...

 

(defun c:RecHatch ( / l)

  (setq l (getvar 'clayer))
  (command "_.layer" "_t" "column" "_m" "column" "")
  (command-s "_.rectang")
  (command "_.layer" "_t" "column hatch" "_m" "column hatch" "")
  (command "_.bhatch" "_s" "_l" "" "")
  (setvar 'clayer l)
  (princ)
  ) 

 

Ajohnson0
Contributor
Contributor

This worked great, I just had to tweak the layer names on my end (my fault). Is there a way write in the lisp routine to change the hatch to the pattern "solid"

0 Likes

Ajohnson0
Contributor
Contributor

and to leave it open ended until I close the command so I can do multiple columns

0 Likes

ronjonp
Advisor
Advisor

Here's another that inserts a block:

(defun c:foo (/ a b c)
  ;; RJP » 2020-08-03
  (cond	((null (tblobjname "block" "_hatchedbox"))
	 (entmake '((0 . "BLOCK")
		    (100 . "AcDbEntity")
		    (67 . 0)
		    (8 . "0")
		    (100 . "AcDbBlockReference")
		    (2 . "_hatchedbox")
		    (10 0. 0. 0.)
		    (70 . 0)
		   )
	 )
	 (entmake '((0 . "HATCH")
		    (100 . "AcDbEntity")
		    (8 . "Column Hatch")
		    (62 . 254)
		    (440 . 33554559)
		    (100 . "AcDbHatch")
		    (10 0. 0. 0.)
		    (210 0. 0. 1.)
		    (2 . "SOLID")
		    (70 . 1)
		    (71 . 0)
		    (91 . 1)
		    (92 . 1)
		    (93 . 4)
		    (72 . 1)
		    (10 -0.5 0.5 0.)
		    (11 0.5 0.5 0.)
		    (72 . 1)
		    (10 0.5 0.5 0.)
		    (11 0.5 -0.5 0.)
		    (72 . 1)
		    (10 0.5 -0.5 0.)
		    (11 -0.5 -0.5 0.)
		    (72 . 1)
		    (10 -0.5 -0.5 0.)
		    (11 -0.5 0.5 0.)
		    (97 . 0)
		    (75 . 1)
		    (76 . 1)
		    (98 . 1)
		    (10 0. 0. 0.)
		    (450 . 0)
		    (451 . 0)
		    (460 . 0.)
		    (461 . 0.)
		    (452 . 0)
		    (462 . 0.)
		    (453 . 2)
		    (463 . 0.)
		    (63 . 5)
		    (421 . 255)
		    (463 . 1.)
		    (63 . 2)
		    (421 . 16776960)
		    (470 . "LINEAR")
		   )
	 )
	 (entmake '((0 . "LWPOLYLINE")
		    (100 . "AcDbEntity")
		    (67 . 0)
		    (8 . "Column")
		    (62 . 5)
		    (100 . "AcDbPolyline")
		    (90 . 4)
		    (70 . 129)
		    (43 . 0.)
		    (38 . 0.)
		    (39 . 0.)
		    (10 -0.5 0.5)
		    (40 . 0.)
		    (41 . 0.)
		    (42 . 0.)
		    (91 . 0)
		    (10 0.5 0.5)
		    (40 . 0.)
		    (41 . 0.)
		    (42 . 0.)
		    (91 . 0)
		    (10 0.5 -0.5)
		    (40 . 0.)
		    (41 . 0.)
		    (42 . 0.)
		    (91 . 0)
		    (10 -0.5 -0.5)
		    (40 . 0.)
		    (41 . 0.)
		    (42 . 0.)
		    (91 . 0)
		   )
	 )
	 (entmake '((0 . "ENDBLK") (100 . "AcDbBlockEnd") (8 . "0")))
	)
  )
  (while (and (setq a (getpoint "\nSpecify first corner point: "))
	      (setq b (getcorner a "\nSpecify other corner point: "))
	      (setq c (mapcar 'abs (mapcar '- a b)))
	 )
    (entmakex (list '(0 . "INSERT")
		    '(100 . "AcDbEntity")
		    '(8 . "Column")
		    '(100 . "AcDbBlockReference")
		    '(2 . "_hatchedbox")
		    (cons 10 (polar a (angle a b) (/ (distance a b) 2.)))
		    (cons 41 (car c))
		    (cons 42 (cadr c))
		    '(43 . 1.0)
		    '(50 . 0.0)
	      )
    )
  )
  (princ)
)

CodeDing
Advisor
Advisor

@ВeekeeCZ , @Ajohnson0 ,

 


@ВeekeeCZ wrote:
.....
(command-s "_.rectang")
.....

I learned recently that this can actually cause a problem if the user exits the RECTANG command. If the user exits during the "command-s" call then the rest of the code still gets executed..

Which means, this line will Still Hatch the Last item even if the user is trying to exit your command..


  (command "_.bhatch" "_s" "_l" "" "")

This is probably not desired...

I would not recommend this approach and am posting this to help spread the awareness.

 

Best,

~DD


Need AutoLisp help? Try my custom GPT 'AutoLISP Ace':
https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace
0 Likes

ВeekeeCZ
Consultant
Consultant
Accepted solution

Here it is.

 

(defun c:RecHatch ( / l)

  (setq l (getvar 'clayer))
  (command "_.layer" "_t" "column hatch,column" "_m" "column hatch" "_m" "column" "")
  (command "_.rectang")
  (while (> (getvar 'cmdactive) 0) (command pause))
  (command "_.bhatch" "_s" "_l" "" "_p" "solid" "")
  (command "_.chprop" "_l" "" "_la" "column hatch" "")
  (while (setq pt (getpoint "\nSpecify first corner point: "))
    (command "_.rectang" "_none" pt pause)
    (command "_.bhatch" "_s" "_l" "" "_p" "solid" "")
    (command "_.chprop" "_l" "" "_la" "column hatch" ""))
  (setvar 'clayer l)
  (princ)
  ) 

Sea-Haven
Mentor
Mentor

If you want a dcl input for length and width this uses a library function Multi get vals.lsp to make a dcl on the fly. Just remove the dim stuff and add hatch.

 

screenshot235.png

 

 

 

 

0 Likes

majed.eng86
Observer
Observer

what's the command of lisp plz.?

0 Likes

devitg
Advisor
Advisor

@majed.eng86 

 

RecHatch
0 Likes

tanbqtb03
Contributor
Contributor

Please update this lisp to have DCL for input width, length value, and after that save name block is (length x width) value, such as:

1- Input length value (mm) - on dialogue on screen CAD command: 500

2- Input width value (mm) - on dialogue on screen CAD command: 300

3-Select type hatch (on dialoge), such as solid hatch type

4- Result will be: Solid_500x300 is name of block

Thanks a lot

 

0 Likes

Sea-Haven
Mentor
Mentor

Did you try to use my multigetvals yourself, you load it and run 1 line of code, look into the top of code for examples.

tanbqtb03
Contributor
Contributor

In my drawing this lisp will be make rectang with input length, width (height) and dim it. If I have any mistake please reguide me. Such as attatched image:

tanbqtb03_0-1680583485978.png

 

 

Please update this lisp to have DCL for input width, length value, and after that save name block is (length x width) value, such as:

1- Input length value (mm) - on dialogue on screen CAD command: 500 ==> OK 

2- Input width value (mm) - on dialogue on screen CAD command: 300 ==> OK

3-Select type hatch (on dialoge), such as solid hatch type

4- Result will be: Solid_500x300 is name of block

5- This lisp can be asking: Continuos with the same previous block (input value: number of blocks). If not, return for step 01 for another type block

Thanks alot

 

0 Likes

Sea-Haven
Mentor
Mentor

Like this look at the top of the code lots of examples.

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter values " "Length " 5 4 "500" "width" 5 4 "300" "Hatch pattern" 11 10 "Solid")))
(setq Len (atof (car ans)) wid (atof (cadr ans)) pat (caddr ans))

if you looked in file.

 

 

mbracciahubbard
Contributor
Contributor

for some reason my set layer is not reverting back to what it was prior to executing this lisp. 

also, right now it asks me what angle i want and then for another point.

1) Is there a way to have it skip the second prompt (the one for a point)?

2) Is there a way to get it so that when it asks me for an angle i can put in "V" for (45) and "H" for (135)?

 

(defun c:RecHatch ( / l)

(setq prevlayer (getvar 'clayer))
(command "_.layer" "_t" "ISE_Hatch,ISE_Outline BRG" "_m" "ISE_Hatch" "_m" "ISE_Outline BRG" "")
(command "_.rectang")
(while (> (getvar 'cmdactive) 0) (command pause))
(command "_.bhatch" "_s" "_l" "" "_p" "ansi31" "45")
(command "_.chprop" "_l" "" "_la" "ISE_Hatch" "")
(while (setq pt (getpoint "\nSpecify first corner point: "))
(command "_.rectang" "_none" pt pause)
(command "_.bhatch" "_s" "_l" "" "_p" "ansi31" "")
(command "_.chprop" "_l" "" "_la" "ISE_Hatch" ""))
(setvar 'clayer prevlayer)
(princ)
)

0 Likes

Kent1Cooper
Consultant
Consultant

The responses to the prompts in the BHATCH commands are incorrect.  You should run it manually at the command line, and note the sequence of what it asks for and in what order.  Do it within a (command) prompt:

(command "_.bhatch")

to start it off and leave you in the command, because some commands work a little differently there than when just typed at the command line.

 

Mostly, you don't account for the Scale prompt.  And you don't seem to have enough Enters to tell it you're done with various things.  Try [and study what's different about] this:

(defun c:RecHatch (/ prevlayer pt)
  (setq prevlayer (getvar 'clayer))
  (command
    "_.layer" "_t" "ISE_Hatch,ISE_Outline BRG" "_m" "ISE_Hatch" "_m" "ISE_Outline BRG" ""
    "_.rectang"
  )
  (while (> (getvar 'cmdactive) 0) (command pause)); finish rectangle
  (command
    "_.bhatch" "_s" "_l" "" "_p" "ansi31" "" "45" ""
    "_.chprop" "_l" "" "_la" "ISE_Hatch" ""
  )
  (while (setq pt (getpoint "\nSpecify first corner point or <exit>: "))
    (command
      "_.rectang" "_none" pt pause
      "_.bhatch" "_s" "_l" "" "_p" "ansi31" "" "" ""
      "_.chprop" "_l" "" "_la" "ISE_Hatch" ""
    )
  )
  (setvar 'clayer prevlayer)
  (princ)
)

But you probably should say something about the scale, rather than just accepting the default as the above does.  Who knows what that might be at a given time?

 

Also, the first one seems to be just like the continuation of more after that.  Is there some reason it couldn't be within one (while) function -- just the followup repeating round, without the separate first round?  Is there supposed to be something different about the first one?

 

And I would suggest you investigate using HATCH instead of BHATCH -- it seems more direct about it, to me.  And also the use of a User-specified pattern, so you can just tell it directly the line spacing and angle you want, instead of monkeying around with how ANSI31 is defined and compensating from there.  It's silly to tell it to use ANSI31 at 45° to get your lines vertical -- with a User-defined pattern, you can tell it 90° outright.

Kent Cooper, AIA

Sea-Haven
Mentor
Mentor

A update to previous code, download the Multi getvals.lsp.

SeaHaven_0-1715662749248.png

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-routine-to-draw-a-rectangle-and-hatch-it/td-p/9670021
; Simle draw rectang and hatch

(defun c:wow ( / ans len wid ang hsc pat)

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))

(if (= pat nil)(setq pat "Solid"))
(if (= len nil)(setq len 500))
(if (= wid nil)(setq wid 250))
; add more defaults use rtos 

(while (setq pt (getpoint "\nPick lower left point Enter to exit "))

(setq ans (AH:getvalsm (list "Enter values " "Length " 5 4 (rtos len 2 1) "width" 5 4 (rtos wid 2 1) "Hatch pattern" 11 10 pat "Hatch angle " 5 4 "0" "hatch scale " 5 4 "1")))
(setq Len (atof (nth 0 ans)) wid (atof (nth 1 ans)) pat (nth 2 ans) ang (atof (nth 3 ans)) hsc (atof (nth 4 ans)))

(command "_.layer" "_t" "column hatch,column" "_m" "column hatch" "_m" "column" "")

(command "rectang" pt (mapcar '+ pt (list len wid 0.0)))
(setvar 'hpname pat)
(setvar 'clayer "column hatch")
(if (= pat "Solid")
  (command "_.hatch" "S" (entlast) "" "")
  (command "_.hatch" "P" "" hsc ang "S" (entlast) "" "")
)
)
(princ)
)
(c:wow)

 

 

For me would add a CHKLAY function that checks does layer exist if not make it and set color and linetype etc.

mbracciahubbard
Contributor
Contributor
Ok this works perfectly for me! i'm new to lisps and have zero coding background so i'm going to study the difference here and try to figure this all out. thank you!
0 Likes

Kent1Cooper
Consultant
Consultant

Here's a snazzier version, using some of what I suggested earlier [User-specified pattern, direct input on spacing and angle], and with enhancements like ensuring the Layer gets reset even if ended with ESCape [unlike my earlier one, which needed Enter/space to end it to have the Layer reset], Undo begin/end wrapping, and remembering your choices and offering them as defaults.  See comments at the top of the file.

Kent Cooper, AIA

mbracciahubbard
Contributor
Contributor

any idea on how i can edit this code so that the hatch scale is always 32? i don't want it to prompt me, i just want it set to 32 

0 Likes