Entmake Hatch (with base point or object {Polyline} entity name).

Entmake Hatch (with base point or object {Polyline} entity name).

Muhammed.OPERA
Advisor Advisor
5,214 Views
28 Replies
Message 1 of 29

Entmake Hatch (with base point or object {Polyline} entity name).

Muhammed.OPERA
Advisor
Advisor

Hi everyone,

 

I have been working on a lisp file to draw custom things, then i have faced that nightmare.

Really it's a nightmare, i have searched every web page looking for a way to make hatch with Entmake  not vla not -bhatch, then i have found a code by Mr. Elpanov Evgeniy and Mr. LEE Mac but those codes can't help me with that issue.

I wanna make a hatch with entmake for a polyline (complex polyline with arcs& lines inside) and that would be with either: selecting a point inside (pick point) or selecting the polyline itself (select object by having it's entity name).

I have tried every possible trial i can make with the dxf group codes of hatch and it's boundary but i couldn't accomplish that.

Do you have any ideas?

Is that even possible?! i think it is, as it's pre-programmed in AutoCAD hatch command.

 

Thanks in advance. 


Muhammed Mamdouh (OPERA)
Structural Engineer, Instructor
Facebook |LinkedIn

EESignature

0 Likes
Accepted solutions (1)
5,215 Views
28 Replies
Replies (28)
Message 2 of 29

dlanorh
Advisor
Advisor

It is possible using visual lisp.

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-ActiveX/files...

 

Have you tried to entmakex the polyline first, or draw a complex polyline, hatch it with the hatch command ensuring the boundary is retained. Then (entget (car (entsel))) on the commandline will give you some idea of how the hatch is put together. Doing the same to the polyline should show you how the two fit together.

I am not one of the robots you're looking for

0 Likes
Message 3 of 29

Muhammed.OPERA
Advisor
Advisor

@dlanorh 

I appreciate your comment but, i don't want to use these approaches with [[Vla functions or -BHatch command]].

I wanna make that hatch with entmake or entmakex func. knowing the entity name of the boundary polyline.

Because, Entmake & Entmakex create AutoCAD drawing objects faster and less lagging during the implementation of dynamic effects.


Muhammed Mamdouh (OPERA)
Structural Engineer, Instructor
Facebook |LinkedIn

EESignature

0 Likes
Message 4 of 29

john.uhden
Mentor
Mentor

I have never entmade a hatch, but if you study what an entget returns, you will probably figure it out.  There's a code it seems for the number of sides, then a 10 and 11 for each end of each side, plus there are additional codes for arced (bulged) segments.  Plus, the DXF help will help you figure it out.

Please let us know when you have it working nicely.  It's so great to have a contributee give us back a return on our investment.

John F. Uhden

Message 5 of 29

dlanorh
Advisor
Advisor

Did you read the last part of my post?

 

Oops just seen Johns post.

I am not one of the robots you're looking for

0 Likes
Message 6 of 29

john.uhden
Mentor
Mentor
Yes. My apologies for being redundant.

<>
Virus-free.
www.avg.com
<>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

John F. Uhden

Message 7 of 29

Muhammed.OPERA
Advisor
Advisor

Hi @john.uhden  @dlanorh 

I have tried every possible thing with all dxf codes in the past two days and i saw the code of Mr. Elpanov Evgeniy and Mr. LEE Mac, they all work but i'm trying to make a lisp to hatch using the entity name of the polyline, not by it's coordinates.If you wanna see how hard it is just see these dxf group codes of the hatch entity:

 

1.PNG2.PNG3.PNG6.PNG7.PNG8.PNG9.PNG10.PNG11.PNG12.PNG13.PNG14.PNG15.PNG16.PNG


Muhammed Mamdouh (OPERA)
Structural Engineer, Instructor
Facebook |LinkedIn

EESignature

Message 8 of 29

ronjonp
Mentor
Mentor

Maybe reach out to ElpanovEvgeniy. Maybe if he is feeling generous, he could modify his code HERE to account for bulges. 

 

Have you tested the actual speed difference between entmake and vla-addhatch to see if this even worth the effort?

Message 9 of 29

john.uhden
Mentor
Mentor

I don't think anyone here said it would be easy using entmake, but you seem to be making it difficult on yourself by not pursuing the alternatives.

This is just a guess, but if the alternative method wasted 5 milliseconds, then 10,000 of your operations over a year would waste you 50 seconds, or about 1 minute per year.  What's that compared to your Googling?  I guess I don't see the problem.

John F. Uhden

Message 10 of 29

CodeDing
Advisor
Advisor

<ignore>

0 Likes
Message 11 of 29

CodeDing
Advisor
Advisor
Accepted solution

@Muhammed.OPERA ,

 

I am a big proponent of entmake also, so this was a fun challenge for me. There are 2 methods to approach this..

1) (the method I used) Running "entmakex-hatch" function after One large list-of-lists has been created. This will generate ONE hatch. (e.g. If 5 items are selected in selection set, all 5 will be hatched, but only 1 hatch will exist)

 

2) Running "entmakex-hatch" function after Each entity list has been created. This would create n hatches for n objects in Selection Set. (e.g. If 5 items are selected, all 5 will have individual hatches)

 

I made some mods to Elpanov's Function that appear to be doing the correct hatching. You might have to spend a couple minutes fine-tuning the color/layer that you want the hatch on, but I hope this can do it for you. This method uses an entity name and extracts vertices/bulges.. I would have no idea how to approach this by selecting an interior point. Here you go:

(defun c:TEST ( / ss cnt e hList)
(setq ss nil)
(prompt "\nSelect Closed Polylines to Hatch: ")
(while (not (setq ss (ssget '((0 . "LWPOLYLINE")))))
  (prompt "...invalid selection set.")
);while
(setq cnt (sslength ss))
(while (<= 0 (setq cnt (1- cnt)))
  (setq e (ssname ss cnt))
  (if (setq tmp (CreateHatchList e))
    (setq hList (cons tmp hList))
  );if
);while
(setq hList (reverse hList))
(if (entmakex-hatch hList 0.0 "ANSI31" 1.0)
  (prompt "\nSuccess!")
  (prompt "\n...Failure.")
);if
(princ)
);defun

(defun CreateHatchList (e / i j pList found)
(foreach i (entget e)
  (if (= 10 (car i))
    (progn
      (setq pList (cons i pList))
      (setq found nil j (member i (entget e)))
      (while (and (not found) (< 0 (length j)))
	(if (= 42 (car (car j)))
	  (setq pList (cons (car j) pList) found t)
	);if
	(setq j (cdr j))
      );while
    );progn
  );if
);foreach
(reverse pList)
);defun

(defun entmakex-hatch (l a n s)
 ;; By ElpanovEvgeniy
 ;; L - list point
 ;; A - angle hatch
 ;; N - name pattern
 ;; S - scale
 ;; return - hatch ename
 (entmakex
  (apply
   'append
   (list
    (list '(0 . "HATCH") '(100 . "AcDbEntity") '(410 . "Model") '(100 . "AcDbHatch")
          '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0)
          (cons 2 n)
          (if (= n "SOLID")
           '(70 . 1)
           '(70 . 0)
          ) ;_  if
          '(71 . 0)
          (cons 91 (length l))
    ) ;_  list
    (apply 'append
           (mapcar '(lambda (a)
                     (apply 'append
                            (list (list '(92 . 7) '(72 . 1) '(73 . 1) (cons 93 (/ (length a) 2)))
				  (mapcar '(lambda (b) b) a)
                                  '((97 . 0))
                            ) ;_  list
                     ) ;_  apply
                    ) ;_  lambda
                   l
           ) ;_  mapcar
    ) ;_  apply
    (list '(75 . 0) '(76 . 1) (cons 52 a) (cons 41 s) '(77 . 0) '(78 . 1) (cons 53 a)
          '(43 . 0.) '(44 . 0.) '(45 . 1.) '(46 . 1.) '(79 . 0) '(47 . 1.) '(98 . 2)
          '(10 0. 0. 0.0) '(10 0. 0. 0.0) '(451 . 0) '(460 . 0.0) '(461 . 0.0) '(452 . 1)
          '(462 . 1.0) '(453 . 2) '(463 . 0.0) '(463 . 1.0) '(470 . "LINEAR")
    ) ;_  list
   ) ;_  list
  ) ;_  apply
 ) ;_  entmakex
) ;_  defun

Before...

image.png

After...

image.png

Best,

~DD

Message 12 of 29

Muhammed.OPERA
Advisor
Advisor

@CodeDing , That's really great, thanks a lot. 

Magnificent my friend. Smiley Embarassed


Muhammed Mamdouh (OPERA)
Structural Engineer, Instructor
Facebook |LinkedIn

EESignature

0 Likes
Message 13 of 29

ronjonp
Mentor
Mentor

@CodeDing  Nice work! This could prove to be handy for AutoCAD MAC users. I'll do a benchmark using entmake and vla-addhatch in a bit. Think you could improve the entmake so the hatch is associative to its boundary?

0 Likes
Message 14 of 29

Muhammed.OPERA
Advisor
Advisor

@john.uhden  @ronjonp  @dlanorh  Thank you all for your comments, i really appreciate that.


Muhammed Mamdouh (OPERA)
Structural Engineer, Instructor
Facebook |LinkedIn

EESignature

0 Likes
Message 15 of 29

Muhammed.OPERA
Advisor
Advisor

For what you have said about pick point method : 

<I would have no idea how to approach this by selecting an interior point.>, 

Now it's so easy to be achieved. 

We can use (BPoly) function to create multiple polylines by picking internal points then run your lisp for those created polylines then, delete those drawn polylines after creating hatches.


Muhammed Mamdouh (OPERA)
Structural Engineer, Instructor
Facebook |LinkedIn

EESignature

0 Likes
Message 16 of 29

ronjonp
Mentor
Mentor

@Muhammed.OPERA wrote:

@john.uhden  @ronjonp  @dlanorh  Thank you all for your comments, i really appreciate that.


Just for reference here is a benchmark ( using Michael Puckett's code ) of entmake vs vla.

_$ 

_VLA-ADDHATCH 
ENTMAKEX-HATCH 
<Entity name: 2cc8f123bc0> 
#<VLA-OBJECT IAcadModelSpace 000002ccaadbda58> Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):

    (_VLA-ADDHATCH SP (vlax-ename->vla-o...).....1468 / 2.57 <fastest>
    (ENTMAKEX-HATCH E 0.0 "ANSI31" 1.0)..........3766 / 1.00 <slowest>

 
; 5 forms loaded from #<editor "<Untitled-0> loading...">
_$ 

image.png

(defun _vla-addhatch (space o pat ang sc / h)
  (cond	((setq h (vlax-invoke space 'addhatch achatchobject pat :vlax-true))
	 (vlax-invoke h 'appendouterloop (list o))
	 (vlax-put h 'patternangle ang)
	 (vlax-put h 'patternscale sc)
	 (vla-evaluate h)
	 h
	)
  )
)

 

I modified the 'createhatchlist' sub and the gap is closer now 🙂

(defun createhatchlist (e / i j plist found)
    (foreach i (entget e)
      ;; RJP mod .. @CodeDing does this break anything?
      (if (member (car i) '(10 42))
	(setq plist (cons i plist))
      )
    )				
    (reverse plist)
  )

Benchmark:

_$ 

_VLA-ADDHATCH 
ENTMAKEX-HATCH 
<Entity name: 2cc8f123bc0> 
#<VLA-OBJECT IAcadModelSpace 000002ccaadbda58> Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):

    (_VLA-ADDHATCH SP (vlax-ename->vla-o...).....1546 / 1.59 <fastest>
    (ENTMAKEX-HATCH E 0.0 "ANSI31" 1.0)..........2454 / 1.00 <slowest>

 
; 5 forms loaded from #<editor "<Untitled-0> loading...">
_$ 
Message 17 of 29

CodeDing
Advisor
Advisor

@ronjonp ,

 

I believe Much more work would need to be put into this to improve its effectiveness. I do not currently know how to make it associative but it will be worth looking into. 

 

@Muhammed.OPERA ,

 

Glad to help!

 

ALSO, it should be noted that this is not the ALL-PERFECT-AND-WORTHY final solution. My version only works with closed polylines and not all closed objects, for example, it will not currently work with ellipses or circles. 

Essentially the entire HATCH command would probably need replicated to set flags and bools for the entmake function to adapt to multiple shapes and also test for closed areas.

 

Still, glad to help. Best,

~DD

0 Likes
Message 18 of 29

CodeDing
Advisor
Advisor

I modified the 'createhatchlist' sub and the gap is closer now 🙂

(defun createhatchlist (e / i j plist found)
    (foreach i (entget e)
      ;; RJP mod .. @CodeDing does this break anything?
      (if (member (car i) '(10 42))
	(setq plist (cons i plist))
      )
    )				
    (reverse plist)
  )

Actually, that should work great. Jeesh, at that rate we should just implement Mapcar, I believe it's faster than foreach.

(defun createhatchlist (e / plist)
(mapcar
  '(lambda (x)
    (if (member (car x) '(10 42)) (setq pList (cons x pList))))
  (entget e))
(reverse pList)
);defun

Best,

~DD

0 Likes
Message 19 of 29

CodeDing
Advisor
Advisor

@ronjonp ,

 

Can you benchmark this one? This reflects 2 changes:

 - Mapcar in "CreateHatchList"

 - removed unnecessary mapcar from "entmakex-hatch"...

.......
(mapcar '(lambda (b) b) a)
....... to .......
a
.......

Best,

~DD

 

0 Likes
Message 20 of 29

ronjonp
Mentor
Mentor

I'd be inclined to use vl-remove-if-not like so .. but that takes the 'vanilla' out of it 🙂

(defun createhatchlist (e)
  (if e
    (vl-remove-if-not '(lambda (x) (member (car x) '(10 42))) (entget e))
  )
)
0 Likes