Jump to content
JovanG

Is it possible to convert an ellipse to a circle with autolisp

Recommended Posts

JovanG

Hello everyone.

 

I wonder if there is a way to convert an ellipse to a circle with the help of autolisp. I have a DWG that contains some circles and ellipses in coordinate representation. But I have noticed that even when the minor radius is equal to the major radius (apparently a circle), the center coordinate that appears in the properties is not the same that I have inserted. So, I believe that if I change the ellipse to circle problem will be solved.

 

Thank you in advance for your time and help. Have a nice day

Share this post


Link to post
Share on other sites
pkenewell

Here is a function to do it. Note - this only works if the Major to Minor radius ratio is 1 (in other words - looks like a circle). If you want it different then let me know.

(defun C:EL2CIR (/ cen cnt en ent maj mad)
   (if (setq ss (ssget '((0 . "ELLIPSE"))))
      (repeat (setq cnt (sslength ss))
         (setq ent (entget (setq en (ssname ss (setq cnt (1- cnt)))))
               cen (cdr (assoc 10 ent))
               maj (cdr (assoc 11 ent))
               mad (distance '(0.0 0.0 0.0) maj)
         )
         (if (= (cdr (assoc 40 ent)) 1.0)
            (progn
               (command "._circle" "_non" cen mad)
               (entdel en)
            )
         )
      )
   )
   (princ)
)

 

  • Like 2

Share this post


Link to post
Share on other sites
ronjonp
18 minutes ago, pkenewell said:

Here is a function to do it. Note - this only works if the Major to Minor radius ratio is 1 (in other words - looks like a circle). If you want it different then let me know.


(defun C:EL2CIR (/ cen cnt en ent maj mad)
   (if (setq ss (ssget '((0 . "ELLIPSE"))))
      (repeat (setq cnt (sslength ss))
         (setq ent (entget (setq en (ssname ss (setq cnt (1- cnt)))))
               cen (cdr (assoc 10 ent))
               maj (cdr (assoc 11 ent))
               mad (distance '(0.0 0.0 0.0) maj)
         )
         (if (= (cdr (assoc 40 ent)) 1.0)
            (progn
               (command "._circle" "_non" cen mad)
               (entdel en)
            )
         )
      )
   )
   (princ)
)

 

Nice :) .. FWIW if you create the circle like so it will retain all by object properties too:
 

(defun c:el2cir	(/ cen cnt en ent maj mad)
  (if (setq ss (ssget '((0 . "ELLIPSE"))))
    (repeat (setq cnt (sslength ss))
      (setq ent	(entget (setq en (ssname ss (setq cnt (1- cnt)))) '("*"))
	    cen	(cdr (assoc 10 ent))
	    maj	(cdr (assoc 11 ent))
	    mad	(distance '(0.0 0.0 0.0) maj)
      )
      (if (= (cdr (assoc 40 ent)) 1.0)
	;; (progn (command "._circle" "_non" cen mad) (entdel en))
	(and (entmake
	       (append
		 '((0 . "CIRCLE"))
		 (vl-remove-if '(lambda (x) (vl-position (car x) '(-1 0 100 330 5 11 40 41 42))) ent)
		 (list (cons 40 mad))
	       )
	     )
	     (entdel en)
	)
      )
    )
  )
  (princ)
)

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
pkenewell
20 minutes ago, ronjonp said:

Nice :) .. FWIW if you create the circle like so it will retain all by object properties too:

 

Very Nice @ronjonp! Very efficient way to retain the original layer, linetype, color, etc.

Share this post


Link to post
Share on other sites
JovanG

It works like a charm! I really thank you @pkenewell and @ronjonp.

Just in some cases it didn`t works but it was because Major to minor radius ratio was apparently diffetent of 1, so I just deleted the conditional and it works great.

  • Like 1

Share this post


Link to post
Share on other sites
pkenewell
54 minutes ago, JovanG said:

so I just deleted the conditional and it works great.

 

FWIW rather than deleting the conditional, you could use the "equal" function with a fuzz factor, if the ellipses are only off by a small amount. depends on whether you need to prevent changing an ellipse that is supposed to be an ellipse.

 

I.e. this:

(setq fuz 0.005) ;; Set error fuzz value
(if (equal (cdr (assoc 40 ent)) 1.0 fuz)

instead of this:

(if (= (cdr (assoc 40 ent)) 1.0)
Edited by pkenewell
  • Like 1

Share this post


Link to post
Share on other sites
Lee Mac

For what it's worth, here's an existing program to handle elliptical arcs too.

  • Like 4

Share this post


Link to post
Share on other sites
JovanG
On 2/26/2021 at 4:43 PM, pkenewell said:

 

rather than deleting the conditional, you could use the "equal" function with a fuzz factor

That`s a great idea @pkenewell, thank you a lot for your help.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...