LISP to convert of hatches made of spline boundaries to polyline boundaries

LISP to convert of hatches made of spline boundaries to polyline boundaries

Haider_of_Sweden
Collaborator Collaborator
3,232 Views
12 Replies
Message 1 of 13

LISP to convert of hatches made of spline boundaries to polyline boundaries

Haider_of_Sweden
Collaborator
Collaborator

I have a workflow that requires hatches to be recreated from being made of splines to been made of polylines. There seems to be no way for me to retrieve the hatches in another fashion than "spline hatches", ie hatches made using splines boundaries. 

This appears to be crucial when linking DWGs with hatches in Revit.

 

I provided a DWG with a bunch of hatches.

 

The manual workflow would be to isolate one hatch, recreate the boundary, turning the spline into a polyline with PEDIT, hatch, match properties (ie colour) from the original hatch, whereby I erase the old hatch and all the lines produced.

 

Imagine doing this with hundreds of hatches 🙂

LISP is probably the way to solve this.

0 Likes
Accepted solutions (3)
3,233 Views
12 Replies
Replies (12)
Message 2 of 13

devitg
Advisor
Advisor

@Haider_of_Sweden , could it be so?

0 Likes
Message 3 of 13

Haider_of_Sweden
Collaborator
Collaborator

Thank you

It seems like the hatches in your file still had spline boundaries.

 

Take a look at the explanatory file provided, here is a screenshot of it, but please open the DWG to see the difference between the objects

Hatch - not so but so.png

0 Likes
Message 4 of 13

ВeekeeCZ
Consultant
Consultant

Islands are ignored and their boundaries are not removed, neither colored.

 

(defun c:HatchRecreatePolylineBoudary (/ s i e l f)

  (if (setq s (ssget "_:L" '((0 . "HATCH"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq l (entlast))
      (command "_.HATCHEDIT" e "_B" "_P" "_N")
      (if (and (setq f (entnext l))
	       (= "SPLINE" (cdr (assoc 0 (entget f)))))
	(command "_.PEDIT" f 1 ""))
      (setq f (entlast))
      (command "_.BHATCH" "_P" "_S" "_S" f "" "")
      (setpropertyvalue (entlast) "LayerId" (getpropertyvalue e "LayerId"))
      (setpropertyvalue (entlast) "Color" (getpropertyvalue e "Color"))
      (entdel e)
      (entdel f)))
  (princ)
  ) 

 

0 Likes
Message 5 of 13

Haider_of_Sweden
Collaborator
Collaborator

@ВeekeeCZ wrote:

Islands are ignored and their boundaries are not removed, neither colored.

 

(defun c:HatchRecreatePolylineBoudary (/ s i e l f)

  (if (setq s (ssget "_:L" '((0 . "HATCH"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq l (entlast))
      (command "_.HATCHEDIT" e "_B" "_P" "_N")
      (if (and (setq f (entnext l))
	       (= "SPLINE" (cdr (assoc 0 (entget f)))))
	(command "_.PEDIT" f 1 ""))
      (setq f (entlast))
      (command "_.BHATCH" "_P" "_S" "_S" f "" "")
      (setpropertyvalue (entlast) "LayerId" (getpropertyvalue e "LayerId"))
      (setpropertyvalue (entlast) "Color" (getpropertyvalue e "Color"))
      (entdel e)
      (entdel f)))
  (princ)
  ) 

 


 

Thank you

Is it possible to make the lisp to create a hatch out of the polyline, and with the same colour as the original hatch?

0 Likes
Message 6 of 13

ВeekeeCZ
Consultant
Consultant
Accepted solution

@Haider_of_Sweden wrote:

@ВeekeeCZ wrote:

Islands are ignored and their boundaries are not removed, neither colored.

 

(defun c:HatchRecreatePolylineBoudary (/ s i e l f)

  (if (setq s (ssget "_:L" '((0 . "HATCH"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq l (entlast))
      (command "_.HATCHEDIT" e "_B" "_P" "_N")
      (if (and (setq f (entnext l))
	       (= "SPLINE" (cdr (assoc 0 (entget f)))))
	(command "_.PEDIT" f 1 ""))
      (setq f (entlast))
      (command "_.BHATCH" "_P" "_S" "_S" f "" "")
      ;(setpropertyvalue (entlast) "LayerId" (getpropertyvalue e "LayerId"))
      (setpropertyvalue (entlast) "Color" (getpropertyvalue e "Color"))
      ;(entdel e)
      (entdel f)))
  (princ)
  ) 

 


 

Thank you

Is it possible to make the lisp to create a hatch out of the polyline, and with the same colour as the original hatch?


 

You mean like not match a layer and keep the original hatch? If so, just comment out those lines... 

0 Likes
Message 7 of 13

Haider_of_Sweden
Collaborator
Collaborator

I didn't read the code properly before running it but then I saw there was a "_.BHATCH" in it. 

It seems like the lisp might not be working properly on my computer.

 

This is what happens when I run the lisp:

It asks me to select. 

I select a hatch.

It asks me "Do you want to turn it into one", so this refers to the PEDIT-command. Here I have to reply yes or no, that seems not to be defined in the lisp, while it should have?

Pressing Y leads me to the second question, what precision, which I think is defined as 1 in 

(command "_.PEDIT" f 1 ""))

 

I get the feeling the script got stopped before it got the chance to create the hatches then, am I right?

0 Likes
Message 8 of 13

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try to set PEDTIACCEPT 1 before you run it.

If it works, you might add it to the code.

0 Likes
Message 9 of 13

Haider_of_Sweden
Collaborator
Collaborator

It worked with PEDTIACCEPT 1 🙂

 

edit: could you please also add deletion of the spline? This would result in a cleaner outcome.

0 Likes
Message 10 of 13

Haider_of_Sweden
Collaborator
Collaborator

I tried to set the variable in the LISP using (command "PEDTIACCEPT 1"), but apparently this is not how you do it.

 

Could someone help me doing a check: if PEDTIACCEPT is 1, then keep setting, else set to 1. In the end, revert the setting.

 

Questions:

;(setpropertyvalue (entlast) "LayerId" (getpropertyvalue e "LayerId"))

 

Does this part put the new hatches to the same layer?

 

;(entdel e)

 

What will be deleted? The splines?

0 Likes
Message 11 of 13

Kent1Cooper
Consultant
Consultant
Accepted solution

@Haider_of_Sweden wrote:

I tried to set the variable in the LISP using (command "PEDTIACCEPT 1"), but apparently this is not how you do it.

….


That should be spelled correctly, and with each entry in its own quotation marks, or with the 1 just as a number, not a text string, either:

 

(command "PEDITACCEPT" "1")

 

or:

 

(command "PEDITACCEPT" 1)

 

or better yet, since it's a System Variable:

 

(setvar 'PEDITACCEPT 1)

Kent Cooper, AIA
Message 12 of 13

Haider_of_Sweden
Collaborator
Collaborator

That's probably the clearest and colourful reply I've ever got! Cheers for that @Kent1Cooper  😂👍

 

This is what I ended up with so far. In red are my changes, so that the spline is deleted as well.

 

(setq p (entlast))
(command "_.BHATCH" "_P" "_S" "_S" p "" "")
(entdel p)

 

(defun c:PLineHatch (/ s i e l f)
(setvar 'PEDITACCEPT 1)
(if (setq s (ssget "_:L" '((0 . "HATCH"))))
(repeat (setq i (sslength s))
(setq e (ssname s (setq i (1- i))))
(setq l (entlast))
(command "_.HATCHEDIT" e "_B" "_P" "_N")
(if (and (setq f (entnext l))
(= "SPLINE" (cdr (assoc 0 (entget f)))))
(command "_.PEDIT" f 1 ""))
(setq p (entlast))
(command "_.BHATCH" "_P" "_S" "_S" p "" "")
(setpropertyvalue (entlast) "LayerId" (getpropertyvalue e "LayerId"))
(setpropertyvalue (entlast) "Color" (getpropertyvalue e "Color"))
(entdel e)
(entdel f)
(entdel p)
))
(setvar 'PEDITACCEPT 0)
(princ)
)

 

 

I am struggling with PEDIT > FIT.

Seems like I can't write (command "_.PEDIT" f 1 "F" "") or (command "_.PEDIT" F "F" "") ... Any clues?

 

Could someone also help me with an IF statement, please?
I need to do an (if (getvar 'PEDITACCEPT) = 1)) then do nothing, else (setvar 'PEDITACCEPT 1)
Then I need a temporary variable to set if a certain condition is met; if PEDITACCEPT is 1.

Finally, I would need to check against that temporary variable; If PEDITACCEPT was 1 at the beginning, I would keep it, otherwise (setvar 'PEDITACCEPT 0)

0 Likes
Message 13 of 13

ВeekeeCZ
Consultant
Consultant

There are plenty of examples of how to set and reset system variables. We usually don't test whether the sysvar value is or isn't value we want. It's a more complicated code for no meaningful reason.  But what you should do for sure is to ensure that the value is returned to its value even in case of error (incl. ESC ending). That means to add the *error* function - localized, has to be localized

 

Splines are removed by default - unless you mess with the DELOBJ variable.

Fit option - it makes the code significantly slower. But your first expression is correct.

Also, might be practical to wrap the code to a single undo group.

 

(defun c:HatchRecreatePolylineBoudary (/ *error* pea dlo doc s i e l f)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if pea (setvar 'peditaccept pea))
    (if dlo (setvar 'delobj dlo))
    (vla-endundomark doc)
    (princ)
    )

  ; ----------------------------------------------------------------------------------

  (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  
  (if (and (setq s (ssget "_:L" '((0 . "HATCH"))))
	   (setq pea (getvar 'peditaccept)
		 dlo (getvar 'delobj))
	   (setvar 'peditaccept 1)
	   (setvar 'delobj 3)
	   )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq l (entlast))
      (command "_.HATCHEDIT" e "_B" "_P" "_N")
      (if (and (setq f (entnext l))
	       (= "SPLINE" (cdr (assoc 0 (entget f)))))
	(command "_.PEDIT" f 1 "_F" ""))
      (setq f (entlast))
      (command "_.BHATCH" "_P" "_S" "_S" f "" "")
      (setpropertyvalue (entlast) "LayerId" (getpropertyvalue e "LayerId"))
      (setpropertyvalue (entlast) "Color" (getpropertyvalue e "Color"))
      (entdel e)
      (entdel f)))
  (*error* "end")
  )

 

BTW the code is not explosive. Learn by trials, use the HELP system, use the search in these forums. Your question should be about the principles, good practices, not about what this does, or doesn't. That is the HELP for, there you can find the explanation within the seconds. Eg. HERE is the page about the IF function - there is even an example of using = comparison. 

0 Likes