Lisp routine to convert multiple entities to circles

Lisp routine to convert multiple entities to circles

b.mccarthy
Collaborator Collaborator
887 Views
9 Replies
Message 1 of 10

Lisp routine to convert multiple entities to circles

b.mccarthy
Collaborator
Collaborator

Hello.

 

I received a large 300M file that appears to be various sizes of circle. When I dove into it, I saw that they were not true circles, rather short connected lines. the smallest of which has 181 lines. I am tasked with converting these entities into true circles.

 

I have run across this before and use 2 LISP routines, one to convert the lines into a polyline, and the other to convert the polyline into a circle. They work fine for small jobs, but not something of this scale. I have attached a small sample of the pattern, along with the routines.

 

Can one of you gurus combine the routines to convert a large number of entities? This does not necessarily need to be done in LISP. I have a python script but I cannot get it to function.

 

TIA

0 Likes
Accepted solutions (4)
888 Views
9 Replies
Replies (9)
Message 2 of 10

vladimir_michl
Advisor
Advisor
Accepted solution

It would be quite difficult to identify the related short lines, detect their center and convert the individual geometries to circles. I would recommend to use pattern recognition. First publish your drawing to PDF, with maximum accuracy. Then use PDFIMPORT to bring it back into an empty DWG and let AutoCAD recognize the circular patterns. Works quite well.

 

Vladimir Michl, www.arkance.worldwww.cadforum.cz

 

0 Likes
Message 3 of 10

b.mccarthy
Collaborator
Collaborator

@vladimir_michl

 

Thank you for the suggestion.

 

I have not used this before, and obviously I am missing something, as this is the result:

 

2025-02-09 1047.png

 

The command imported the graphic at 1:11 scale, not 1:1 as I was expecting.

 

Can you walk me through this, please?

 

TIA

0 Likes
Message 4 of 10

komondormrex
Mentor
Mentor
Accepted solution

@b.mccarthy 

hey,

after converting all lines to closed plines with your joinlines.lsp run the following pl2cir.lsp

 

(defun c:pl2cir (/ pline_sset)
  (if (setq pline_sset (ssget '((0 . "lwpolyline")(90 . 181) (-4 . "&=") (70 . 1))))
    (mapcar '(lambda (pline) (vla-addcircle (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
			       		(vlax-3d-point (mapcar '* '(0.5 0.5) (mapcar '+ (vlax-curve-getpointatparam pline 0) (vlax-curve-getpointatparam pline 90.5))))
			       		(* 0.5 (distance (vlax-curve-getpointatparam pline 0) (vlax-curve-getpointatparam pline 90.5)))
			     )
       				(entdel pline)
	     )
	    (vl-remove-if 'listp (mapcar 'cadr (ssnamex pline_sset)))
    )
  )
  (princ)
)

 

0 Likes
Message 5 of 10

vladimir_michl
Advisor
Advisor

Looks like a precision problem in the PDF - use maximum resolution (I have tried it with your test file and it worked fine).

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

0 Likes
Message 6 of 10

Sea-Haven
Mentor
Mentor
Accepted solution

Another method, I just used JOIN and selected all linework to convert to plines.

(defun c:seg2circle ( / ss pt1 pt2 pt3 ent cen rad oldsnap)
(setq ss (ssget '((0 . "lwpolyline"))))
(if (= ss nil)(progn ("alert No polylines selected \nwill now exit")(Exit)))

(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)

(setq blk (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object)))))
(repeat (setq x (sslength ss))
  (setq ent (ssname ss (setq x (1- x))))
  (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget ent))))
  (setq pt1 (car co-ord))
  (setq pt2 (cadr co-ord))
  (setq pt3 (caddr co-ord))
  (command "arc" pt1 pt2 pt3)
  (setq ent (entlast))
  (setq cen (cdr (assoc 10 (entget ent))))
  (setq rad (cdr (assoc 40 (entget ent))))
  (command "erase" ent "")
  (vla-addcircle blk (vlax-3d-point cen) rad)
)

(setvar 'osmode oldsnap)

(princ)
)

 

0 Likes
Message 7 of 10

b.mccarthy
Collaborator
Collaborator

@vladimir_michl 

 

Can you send a screenshot of your settings?

0 Likes
Message 8 of 10

vladimir_michl
Advisor
Advisor

Sure, here are the important plot settings (you can also increase vector resolution in PDF settings). The PDFIMPORT settings can stay at defaults. The only issue with this method is that the "converted" file (attached) is scaled and offset - you will need to Align it to the original file.

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

Message 9 of 10

Kent1Cooper
Consultant
Consultant
Accepted solution

After using PEDIT/Multiple/Join to connect all the little Lines collectively into Polylines, try the Pg2C command defined in PolygoToCircle2.lsp, >here<.  It can work with them if they're not fully regular, and it can take multiple selection.  See the comments at the link and in the file.  [The 2 is to distinguish it from PolygonToCircle.lsp, which requires regular polygons.]

Kent Cooper, AIA
0 Likes
Message 10 of 10

b.mccarthy
Collaborator
Collaborator

All, I apologize for the delayed response. Fires and all that...

 

Thank you for your assistance, and all of the suggestions worked. You guys are the best!!

0 Likes