Lisp for rectangle with hatch

Lisp for rectangle with hatch

Anonymous
Not applicable
5,017 Views
20 Replies
Message 1 of 21

Lisp for rectangle with hatch

Anonymous
Not applicable

Kindly, I need to draw a rectangle with hatch on a specific layer with color by layer and​ the hatch is ANSI31 angle 0 size 500 and on another specific layer with color by layer

thx

0 Likes
Accepted solutions (3)
5,018 Views
20 Replies
Replies (20)
Message 2 of 21

hak_vz
Advisor
Advisor

What is the name of your specific layers or how you want to define them?

Here is something to start with.

Assuming you have specific layers created in this function named "Layer1" and "Layer 2" .

 

 

(defun c:hrc ( / *error* eo p1 p2 adoc)
(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(if adoc (vla-endundomark adoc))
		(setvar 'cmdecho 1)
		(princ)
	)
	(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(setvar 'cmdecho 0)	
	(command "_.rectangle" (setq p1 (getpoint "\nFirst rectangle point >")) (setq p2(getcorner p1 "\nSecond rectangle point>")))
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "layer1") ;< change this for first layer name
	(command "_.-hatch" "S" (entlast) "" "P" "ANSI31" 500 0 "L" "layer1" "") 
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "layer1");< change this for first layer name
	(command "_.rectangle" p1 p2)
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "layer2") ;< change this for second layer name
	(command "_.-hatch" "S" (entlast) "" "P" "ANSI31" 500 0 "L" "layer1" "")
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "layer2") ;< change this for second layer name
	(vla-endundomark adoc)
	(setvar 'cmdecho 1)	
	(princ "\nDone!")
	(princ)
)

 

 

In code you have " ;< change this for second layer name" and you have to replace

(vlax-put eo 'layer "layer1")  to
(vlax-put eo 'layer "your_layer_1")

(vlax-put eo 'layer "layer2")  to
(vlax-put eo 'layer "your_layer_2")

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 21

Anonymous
Not applicable
  • crectangle layer name (CCL)
  • Rectangle color by Layer
  • Layer Color Cyan

HAtch propertis

  • Hatch layer name ( HCL)
  • Layer Color 250
  • Hatch Type ANSI31
  • Angle 0
  • Size 500
  • Hatch color by layer
0 Likes
Message 4 of 21

hak_vz
Advisor
Advisor

Is this what you looking for

 

 

 

(defun c:hrc ( / *error* eo p1 p2 adoc)
(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(if adoc (vla-endundomark adoc))
		(setvar 'cmdecho 1)
		(princ)
	)
	(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(setvar 'cmdecho 0)	
	(command "_.rectangle" (setq p1 (getpoint "\nFirst rectangle point >")) (setq p2(getcorner p1 "\nSecond rectangle point>")))
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "CCL")
	(vlax-put eo 'color "256")
	(command "_.-hatch" "S" (entlast) "" "P" "ANSI31" 500 0 "L" "CCL""") 
	(command "_.rectangle" p1 p2)
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "HCL")
	(vlax-put eo 'color "256")
	(command "_.-hatch" "S" (entlast) "" "P" "ANSI31" 500 0 "L" "HCL"  "")
	(vlax-put eo 'color "256")
	(setq eo (vlax-ename->vla-object (entlast)))
	(vla-endundomark adoc)
	(setvar 'cmdecho 1)	
	(princ "\nDone!")
	(princ)
)

 

 

 

Create layers HCL and CCL and define colors however you wish. Entities collect color by layer.

 

Here is a version that creates layers HCL AND CCL if not created

 

 

(defun c:hrc ( / *error* eo p1 p2 adoc create_layer)
	(defun *error* ( msg )
			(if (not (member msg '("Function cancelled" "quit / exit abort")))
				(princ (strcat "\nError: " msg))
			)
			(if adoc (vla-endundomark adoc))
			(setvar 'cmdecho 1)
			(princ)
		)
	(defun create_layer (name linetype color)
		(setq lyr (tblsearch "layer" name))
		(if (not lyr)
			(entmake
				(list
					'(0 . "LAYER")
					'(100 . "AcDbSymbolTableRecord")
					'(100 . "AcDbLayerTableRecord")
					(cons 2 name)
					(cons 70 0)
					(cons 62 color)
					(cons 6  linetype)
				)
			)
		)
		(princ)
	)
	(create_layer "CCL" "CONTINUOUS" 4)
	(create_layer "HCL" "CONTINUOUS" 250)
	

	(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(setvar 'cmdecho 0)	
	(command "_.rectangle" (setq p1 (getpoint "\nFirst rectangle point >")) (setq p2(getcorner p1 "\nSecond rectangle point>")))
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "CCL")
	(vlax-put eo 'color "256")
	(command "_.-hatch" "S" (entlast) "" "P" "ANSI31" 500 0 "L" "CCL""") 
	(command "_.rectangle" p1 p2)
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "HCL")
	(vlax-put eo 'color "256")
	(command "_.-hatch" "S" (entlast) "" "P" "ANSI31" 500 0 "L" "HCL"  "")
	(vlax-put eo 'color "256")
	(setq eo (vlax-ename->vla-object (entlast)))
	(vla-endundomark adoc)
	(setvar 'cmdecho 1)	
	(princ "\nDone!")
	(princ)
)

Select as a solution if this solves your problem!

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 21

Anonymous
Not applicable

when I create the rectangle I found the rectangle on layer HCL by layer as the hatch

0 Likes
Message 6 of 21

hak_vz
Advisor
Advisor
Accepted solution

@Anonymous wrote:

when I create the rectangle I found the rectangle on layer HCL by layer as the hatch


Try this.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 21

diagodose2009
Collaborator
Collaborator

ecoblend_f1.jpg

0 Likes
Message 8 of 21

Anonymous
Not applicable

thank you for your help but this lisp needs to modify as it creates three entities one rectangle and two hatch entities

0 Likes
Message 9 of 21

hak_vz
Advisor
Advisor

Please define what you want created on each layer.

From your originally request I  assumed that you want  a hatched rectangle (rectangle + hatch) in layer HCL and same thing  (rect and hatch) in layer CCL.

I'll modify it later today, currently at work. 

So is it just a rectangle in layer HCL and hatch in layer CCL? Or something else?

 

In other words. Create rectangle and put it to desired layer, hatch it and transfer hatch to desired layer?

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 10 of 21

Kent1Cooper
Consultant
Consultant

Can't it be as simple as this?

 

(defun C:R2H () ; = Rectangle with 2 Hatches
  (setvar 'cecolor "BYLAYER")
  (command
    "_.layer"
      "_make" "HCL" "_color" 250 ""
      "_make" "CCL" "_color" 4 "" ""
    "_.rectang" pause pause
    "_.hatch" "ANSI31" 500 0 "_last" ""
    "_.copy" "_last" "" "0,0" "0,0"
    "_.chprop" "_last" "" "_layer" "HCL" ""
  ); command
  (princ)
); defun

 

[It's not quite clear from Message 3, but from Message 1 I think one of the Hatch patterns is supposed to be on the same Layer as the rectangle.]

Kent Cooper, AIA
0 Likes
Message 11 of 21

Anonymous
Not applicable

I modified as you told me and it is great thank you for your great help.

May I ask how can I set line type for the rectangle to be HIDDEN2 which line I should change.

THX AGAIN

 

Message 12 of 21

hak_vz
Advisor
Advisor
Accepted solution

Code is in attachment, you can modify it as you like.

Parts of a code are having comments so I think you'll manage to make it yourself.

If help is needed, just ask.

 

 

;here we add rectangle and put its property	
	(command "_.rectangle" (setq p1 (getpoint "\nFirst rectangle point >")) (setq p2(getcorner p1 "\nSecond rectangle point>")))
	(setq eo (vlax-ename->vla-object (entlast)))
	(vlax-put eo 'layer "CCL")
	(vlax-put eo 'color "256")
	(vlax-put eo 'linetype "HIDDEN2")
	(vlax-put eo 'LinetypeGeneration 1)
	(vlax-put eo 'LinetypeScale 150)
	(vlax-put eo 'Lineweight 50) ;lineweigth set to 0.5
	;end of rectangle and linetype properties definition

Either modify value or put comment sign ";" in front of that line to suppress it.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 13 of 21

Anonymous
Not applicable
Accepted solution

THANK YOU SO MUCH  FOR YOUR GREAT HELP.

Just I need a solution for the attached photo for the difference between two rectangles the one on the right was created by lisp, the other one on the left was created by command rectangleAEC-STANDARD-2020-Model.jpg

0 Likes
Message 14 of 21

hak_vz
Advisor
Advisor

Two different dashed line representations, depending on linetype generation turned on or off (0 or 1).

If linetype generation is turned on dashes are created so they don't break or restart at edges or vertexes.

In may code I have set it on, just to show you possible option.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 15 of 21

Anonymous
Not applicable

I SOLVED IT (LINETYPE GENERATION 0)

Message 16 of 21

Anonymous
Not applicable
if i want to use hatch pattern AR-CONC
how to add it cuz is not working
0 Likes
Message 17 of 21

hak_vz
Advisor
Advisor

Used hatch scale - 500 is probably too large. Try with some smaller value. 

 

Problem

If you create a very dense hatch, AutoCAD may reject the hatch and display a message indicating that the hatch scale is too small or its dash length too short. Wit very dense patterns You may think that an AutoCAD crash has occurred.

 

Solution

Option 1

Change you hatch pattern scale

Option 2

You can change the maximum number of hatch segments by setting the MaxHatch system registry variable using (setenv MaxHatch n) where n is a number between 100 and 10000000 (ten million)

The default setting is 10000

 Description from cadhatch.com

Problem

If you create a very dense hatch, AutoCAD may reject the hatch and display a message indicating that the hatch scale is too small or its dash length too short. Wit very dense patterns You may think that an AutoCAD crash has occurred.

 

Solution

Option 1

Change you hatch pattern scale

Option 2

You can change the maximum number of hatch segments by setting the MaxHatch system registry variable using (setenv MaxHatch n) where n is a number between 100 and 10000000 (ten million)

The default setting is 10000

 

To change the MaxHatch setting, on the command line type:-

 (setenv "MaxHatch" "1000000")

Important: the text is case sensitive, quotes are needed around Maxhatch and the value and that brackets must be used.

The point of having this setting is to prevent dense patterns from locking up the PC or causing AutoCAD  to crash, If the value is raised too high you will also need to consider other users who will be using the drawing.

 

AR-CONC is very complex hatch so it can hang when used on large areas.  Option is to either use different scale, or divide hatching area in smaller segments.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 18 of 21

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

I SOLVED IT (LINETYPE GENERATION 0)


For some reason the RECTANG command always makes a Polyline with linetype generation disabled, even when the current PLINEGEN setting is to make Polylines with it enabled.  So you could just omit the:

 

  (vlax-put eo 'LinetypeGeneration 1)

 

line entirely, rather than change the 1 to 0.

Kent Cooper, AIA
0 Likes
Message 19 of 21

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... how can I set line type for the rectangle to be HIDDEN2 which line I should change.

....


Incorporating that, I still think this can be hugely simpler -- all you need is this:

(defun C:R2H () ; = Rectangle with 2 Hatches
  (setvar 'cecolor "BYLAYER")
  (command
    "_.layer"
      "_make" "HCL" "_color" 250 ""
      "_make" "CCL" "_color" 4 "" ""
    "_.rectang" pause pause
    "_.chprop" "_last" "" "_ltype" "HIDDEN2" ""
    "_.hatch" "ANSI31" 500 0 "_last" ""
    "_.copy" "_last" "" "0,0" "0,0"
    "_.chprop" "_last" "" "_layer" "HCL" ""
  ); command
  (princ)
); defun

One advantage is that when a Linetype that's part of AutoCAD's standard collection is assigned to an object in a CHPROP command [or to a Layer in a command-line LAYER command], it is not necessary to have the Linetype already loaded in the drawing -- it will find it.

Kent Cooper, AIA
0 Likes
Message 20 of 21

hak_vz
Advisor
Advisor

Yes, chprop don't need linetype to be loaded.

I like this command queue method, although it looks to me more like a macro and not lisp code.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes