Lisp all solid to hatch

Lisp all solid to hatch

renanemeyer
Advocate Advocate
9,886 Views
11 Replies
Message 1 of 12

Lisp all solid to hatch

renanemeyer
Advocate
Advocate
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-change-all-selected-hatches-...

Hello everybody. I saw this post on the forum

I would like to know if it is possible to do the reverse with a lisp? Transform all solid of the drawing into hatch, without having to do selection. it's possible?

 

Thanks

0 Likes
Accepted solutions (1)
9,887 Views
11 Replies
Replies (11)
Message 2 of 12

pbejse
Mentor
Mentor

@renanemeyer wrote:

I would like to know if it is possible to do the reverse with a lisp? Transform all solid of the drawing into hatch, without having to do selection. it's possible?

Transform to what hatch pattern ? If want the "no selection" mode,  you need to hardcode the target hatch pattern name on the code.

 

Drawing sample perhaps?

 

0 Likes
Message 3 of 12

hak_vz
Advisor
Advisor

Try this.

As @pbejse told in his post, always attach sample drawing with current state and what you want to achieve.

It helps us to create code and to grasp  your idea i.e. what you actually want to do.

This code will convert solids to hatch (ANSI31 pattern). 

What you would probably want is to join all solids to single area, and then hatch it with a singe hatch.

 To merge hatches to single one you can use code at this link.

 

 

(defun c:solids2hatch ( / take pointlist entmakex-hatch ss i )
(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
(defun pointlist (lst / ret) (while lst (setq	ret (cons (take 2 lst) ret) lst (cdddr lst))) (reverse ret))
(defun entmakex-hatch (L a n s)
 ;; By ElpanovEvgeniy
 ;; L - list point
 ;; A - angle hatch
 ;; N - name pattern
 ;; S - scale

 ;; returne - 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)
          '(2 . "ANSI31")
          (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 . 0) '(73 . 1) (cons 93 (length a)))
                                  (mapcar '(lambda (b) (cons 10 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

	(setq ss (ssget '((0 . "SOLID"))) i -1)
	(while (< (setq i (1+ i)) (sslength ss))
		(entmakex-hatch (list(pointlist (vlax-get (vlax-ename->vla-object (ssname ss i)) 'Coordinates))) 0 "ANSI31" 1 )
		(entdel (ssname ss i))
	)
	(princ)
)

 

If this works for you, select is as a solution.

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.
Message 4 of 12

renanemeyer
Advocate
Advocate

Thanks pbejse
Thanks hack_vz


@pbejse "Transform to what hatch pattern ?" its for just simple hatch

@hak_vz its not working for me.

 

I was trying to develop one that I wouldn't need to select for hatching I have this as an example, but you select the solid and then it turns it into a hatch

 

(defun convert2dsolids (/ 2dcoords coords count hatch obj pline space ss)
  (and (setq ss (ssget '((0 . "SOLID"))))
       (setq count -1)
       (setq space (get_space))
       (while (< (setq count (1+ count))
		 (sslength ss))
	 (setq obj (vlax-ename->vla-object (ssname ss count)))
	 (setq coords (vlax-get obj 'coordinates))
	 (setq 2dcoords (list (car coords)(cadr coords)))
	 (setq coords (cdr (cdr (cdr coords))))
	 (setq 2dcoords (append 2dcoords (list (car coords)(cadr coords))))
	 (setq coords (cdr (reverse (cdr (cdr (cdr coords))))))
	 (setq 2dcoords (append 2dcoords (list (cadr coords)(car coords))))
	 (setq coords (reverse coords)) (setq 2dcoords (append 2dcoords (list (car coords)(cadr coords))))
	 (setq pline (vlax-invoke space 'addlightweightpolyline 2dcoords))
	 (vla-put-closed pline :vlax-true)
	 (setq hatch (vlax-invoke space 'addhatch acHatchPatternTypePredefined "SOLID" :vlax-true))
	 (vlax-invoke hatch 'appendouterloop (list pline))
	 (vla-put-color pline (vla-get-color obj))
	 (vla-put-color hatch (vla-get-color obj))
	 (vla-put-layer pline (vla-get-layer obj))
	 (vla-put-layer hatch (vla-get-layer obj))
	 (vla-delete obj)
         (vla-delete pline)
	 )
       )
  );; convert2dsolids

(defun c:SolidToHatch ()
  (convert2dsolids)
  (princ)
);; SolidToHatch
0 Likes
Message 5 of 12

ronjonp
Mentor
Mentor
Accepted solution

Here's another quickie:

(defun c:foo (/ el h p p2 pl s sp x)
  ;; RJP » 2021-07-07
  ;; Converts solids to hatches
  (if (setq s (ssget "_X" '((0 . "SOLID"))))
    (progn
      (foreach e (mapcar 'cadr (ssnamex s))
	(setq el (entget e))
	(setq sp (vlax-ename->vla-object (cdr (assoc 330 el))))
	(setq p (mapcar 'cdr (vl-remove-if-not '(lambda (x) (member (car x) '(10 11 12 13))) el)))
	(setq p2 (list (car p) (cadr p) (last p) (caddr p)))
	(setq pl (entmakex (apply 'append
				  (list	(list '(0 . "LWPOLYLINE")
					      '(100 . "AcDbEntity")
					      (assoc 8 el)
					      (cons 100 "AcDbPolyline")
					      '(90 . 4)
					      '(70 . 1)
					)
					(mapcar '(lambda (x) (list 10 (car x) (cadr x))) p2)
				  )
			   )
		 )
	)
	(setq h (vlax-invoke sp 'addhatch achatchobject "SOLID" :vlax-true))
	(vlax-invoke h 'appendouterloop (list (vlax-ename->vla-object pl)))
	(vla-put-layer h (cdr (assoc 8 el)))
	(entdel e)
      )
    )
  )
  (princ)
)
Message 6 of 12

renanemeyer
Advocate
Advocate

@ronjonp 

it's the second time today you've helped my lisp. 😁  thank you so much!! its perfect

0 Likes
Message 7 of 12

hak_vz
Advisor
Advisor

My code works ok. Anyway you have @ronjonp 's code that works for you.

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 8 of 12

ronjonp
Mentor
Mentor

@renanemeyer wrote:

@ronjonp 

it's the second time today you've helped my lisp. 😁  thank you so much!! its perfect


Glad to help 🍻

0 Likes
Message 9 of 12

philsogood
Collaborator
Collaborator

hello

unfortunately i'm really bad for writing Lisp. could it be possible to modify the lisp in order to select items instead of converting all objects in the file?

Phil

0 Likes
Message 10 of 12

Kent1Cooper
Consultant
Consultant

@philsogood wrote:

..... could it be possible to modify the lisp in order to select items instead of converting all objects in the file?


If you're talking about the accepted-solution routine in Message 5, you should be able to do that by simply removing the "_X" [= find everything] selection mode element:

  (if (setq s (ssget '((0 . "SOLID"))))

EDIT:  Not quite, after all, at least sometimes.  With only that change, this part:

(foreach e (mapcar 'cadr (ssnamex s))

will step through a list that, depending on the way the Solids were selected, may contain items other than only entity names of Solids, that relate to the selection method(s).  Later I'll try posting something to have it process only the entity names, if someone doesn't beat me to it.

Further EDIT:  I notice that some suggestions [including the accepted one] involve first converting the Solids to Polylines, and then Hatching the resulting Polylines.  That's a waste -- the Hatches can be drawn directly from the Solids' information, without the middleman.  I worked on an approach that does that, and does not have the list problem, and works in any UCS, and prevents selection of Solids on locked Layers, and has an option to retain or delete the Solids, and so on, but questions arise:

Would one want to convert all Solids to Hatches always in the SOLID pattern [if the only purpose is to change the object type but not the appearance], or some other always pattern, or have the option to specify the pattern name?
Would one want the option of a Hatch of lines [not SOLID] over retained Solids, contrasting in color and maybe on different Layers, or should the Hatch always get all properties of the Solid [which would make the Hatch invisible if Solids are retained]?

If contrasting, should that be done by keeping the Solid behind the Hatch, or using a background color in the Hatch itself?

If overlaying like that, should pulling the Hatch to the front in draw order be built in?

Kent Cooper, AIA
0 Likes
Message 11 of 12

ronjonp
Mentor
Mentor

@philsogood Try this update for user selection:

(defun c:foo (/ el h p p2 pl s sp x)
  ;; Converts solids to hatches
  (if (setq s (ssget '((0 . "SOLID"))))
    (progn
      (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
	(setq el (entget e))
	(or sp (setq sp (vlax-ename->vla-object (cdr (assoc 330 el)))))
	(setq p (mapcar 'cdr (vl-remove-if-not '(lambda (x) (member (car x) '(10 11 12 13))) el)))
	(setq p2 (list (car p) (cadr p) (last p) (caddr p)))
	(setq pl (entmakex (apply 'append
				  (list	(list '(0 . "LWPOLYLINE")
					      '(100 . "AcDbEntity")
					      (assoc 8 el)
					      (cons 100 "AcDbPolyline")
					      '(90 . 4)
					      '(70 . 1)
					)
					(mapcar '(lambda (x) (list 10 (car x) (cadr x))) p2)
				  )
			   )
		 )
	)
	(setq h (vlax-invoke sp 'addhatch achatchobject "SOLID" :vlax-true))
	(vlax-invoke h 'appendouterloop (list (vlax-ename->vla-object pl)))
	(vla-put-layer h (cdr (assoc 8 el)))
	(entdel e)
      )
    )
  )
  (princ)
)
Message 12 of 12

philsogood
Collaborator
Collaborator

thank you @ronjonp !! = )

 

0 Likes