AutoLISP: Convert 2D Solid to Polyline Outline

Since 2D solids aren’t very friendly after you place them and are hard to edit, you may need find this tool helpful for converting the solid into a polyline that represents the outline of the solid.

An example of its use is editing existing drawings… I have been adjusting blocks from an older block library where some parts of the blocks contain “arrowheads” but they are really just 2D solids. This is helpful so that the users don’t actually snap to these arrows. But a few of them no longer conform to our new CAD standards and sadly, some of the geometry was drawn incorrectly. So I have been using this routine to adjust these stubborn objects.

Here’s how:

  • OUTLINESOLID <enter>
  • Select the 2D Solid
Convert 2D Solid to Polyline

Convert 2D Solid to Polyline


;;; Select a 2D solid and this will delete the solid and replace its outline with a polyline
;;; http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Solid-object-to-Polyline/td-p/783079
;;; Posted By: Schamenek, Alex
(defun c:outlinesolid(/ solid pt1 pt2 pt3 pt4)
   (prompt "\nPlease select the solid you want to outline.")
   (WHILE (NOT (setq solid (ssget ":S" '((0 . "SOLID")))))
      (prompt "\nPlease select the solid you want to outline."))
      (SETQ pt1 (cdr (assoc 10 (entget (ssname solid 0))))
         pt2 (cdr (assoc 11 (entget (ssname solid 0))))
         pt3 (cdr (assoc 12 (entget (ssname solid 0))))
         pt4 (cdr (assoc 13 (entget (ssname solid 0))))
       );SETQ
   (COMMAND "_.PLINE" PT1 PT2 PT4 PT3 "C")
   (command ".erase" solid "")
);DEFUN
Advertisement

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, AutoLISP: Modify, AutoLISP: Polylines. Bookmark the permalink.

8 Responses to AutoLISP: Convert 2D Solid to Polyline Outline

  1. Kent Cooper says:

    See also this thread:
    http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Lisp-Help/m-p/783547/highlight/true#M9205
    which has some alternatives that allow selection of more than one Solid at a time, one of which puts the Polyline outline on the same Layer the Solid was on.

  2. MurrayC says:

    I modified the code a smidgen in case the user wants to RETAIN the solid

    (setq X
    (getstring “\nDo you wish to erase the SOLID? : “))
    (setq X (substr X 1 1))
    (if (or (= X “Y”) (= X “y”))
    (command “.erase” solid “”))

    • Kent Cooper says:

      A more sophisticated way to do that:

      (initget “Yes No”)
      (if (/= (getkword “\nDo you want to erase the Solid [Yes/No]? : “) “No”)
      (command “_.erase” solid “”) ;; or (entdel solid)
      ); if

      The big advantage of (initget)/(getkword) is that it WON’T ACCEPT invalid answers to its question. In MurrayC’s code, if you INTEND to answer “Yes,” but accidentally hit some adjacent key instead, it will take any of those as a “No,” and you’ll need to follow up by erasing the Solid separately. The (initget)/(getkword) approach will ask the question again, until it gets a valid answer.

      A lesser advantage is that it will return “Yes” whether the User types “Y” or “y” or “Yes” or “yes” or even “Ye” or “yE,” and similarly with case differences and number of letters in answering “No” — as long as it starts with the capitalized letter(s) in (initget) and if it continues, doesn’t do so with any character(s) that are NOT a continuation of the word there, regardless of case, it will return the word in the case combination it has there.

      It’s set up for Yes to be the default, so Enter will be taken as “Yes,” but could be done differently, to either have No as the default, or to require an explicit non-Enter answer.

      • Kent Cooper says:

        And just so you know, I did write that with a Y in pointy brackets after the [Yes/No] option, in the form that defaults are typically offered in AutoCAD command prompts, but somehow the “system” seems to have omitted that part. [It makes me wonder whether MurrayC may have had a No default in their (getsring) prompt that was similarly omitted.]

  3. MurrayC says:

    Hi Kent Thanks for the modification

  4. Rizwan says:

    Do you guys have any code to convert 2D solid circle too?

    • AutoCAD Tips says:

      Do you mean to select a circle and fill it with a solid?

      This routine will do that:

      
      ;;; Turns Selected Circles into filled donuts
      ;;; David Bethel
      ;;; http://www.cadtutor.net/forum/showthread.php?59947-how-to-change-circles-into-donuts
      (defun c:cir2donut (/ ss en ed ce ev ra la lt tk cl)
        (and (setq ss (ssget '((0 . "CIRCLE"))))
             (while (setq en (ssname ss 0))
                    (setq ed (entget en)
                          ce (cdr (assoc 10 ed))
                          ev (caddr ce)
                          ra (cdr (assoc 40 ed))
                          la (assoc 8 ed)
                          lt (if (assoc  6 ed) (assoc  6 ed) '(6  . "BYLAYER"))
                          tk (if (assoc 39 ed) (assoc 39 ed) '(39 . 0))
                          cl (if (assoc 62 ed) (assoc 62 ed) '(62 . 256)))
                     (entmake (list (cons 0 "POLYLINE")(cons 66 1)
                                    (list 10 0 0 ev)
                                    (cons 40 ra)(cons 41 ra)
                                    la lt tk cl
                                    (cons 70 1)
                                    (assoc 210 ed)))
                     (entmake (list (cons 0 "VERTEX")
                                    (list 10 (+ (car ce) (* ra 0.5)) (cadr ce) ev)
                                    (cons 40 ra)(cons 41 ra)
                                    la lt tk cl
                                    (cons 42 1)))
                     (entmake (list (cons 0 "VERTEX")
                                    (list 10 (- (car ce) (* ra 0.5)) (cadr ce) ev)
                                    (cons 40 ra)(cons 41 ra)
                                    la lt tk cl
                                    (cons 42 1)))
                     (entmake (list (cons 0 "SEQEND") la lt tk cl))
                     (entdel en)
                     (ssdel en ss)))
        (redraw)
        (prin1))
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s