AUtoLISP: Subract Objects from a Closed Polyline

Link to www.autocadtips.wordpress.com

Here is a nice routine that lets you subtract closed objects (polylines, ellipses, circles…) from a closed polyline. That might sound restrictive but at least it is an option…

TIP: this routine is finicky – when you select the objects that are to be subtracted from the outside of the main polyline (refer to animation below).

Here’s how:

  • PLSUB <enter> (Polyline Subtract)
  • Select the main Polyline that you want the other objects subtracted from <enter>
  • Select the objects that you want subtracted from their outer portion <enter>

(defun c:PLSUB ()
(princ "\n Subtract Polylines")
(princ "\n First selected object must be a Polyline")
(setq *error* *myerror*)
(SD1028)
(princ "\n Select Objects subtract from :")
(setq ObjSet nil)
(while (= ObjSet nil)
(setq ObjSet (ssget '((-4 . "<OR")
(0 . "LWPOLYLINE")
(0 . "ELLIPSE")
(0 . "CIRCLE")
(0 . "POLYLINE")
(0 . "LINE")
(0 . "ARC")
(-4 . "OR>")
)
)
)
)
(setq i -1
ObjNameL1 nil
)
(repeat (setq m (sslength ObjSet))
(setq ObjNameL1 (cons (ssname ObjSet (setq i (1+ i))) ObjNameL1))
)
(princ "\n Select Objects subtract :")
(setq ObjSet2 nil)
(while (= ObjSet2 nil)
(setq ObjSet2 (ssget '((-4 . "<OR")
(0 . "LWPOLYLINE")
(0 . "ELLIPSE")
(0 . "CIRCLE")
(0 . "POLYLINE")
(0 . "LINE")
(0 . "ARC")
(-4 . "OR>")
)
)
)
)
(setq i -1
ObjNameL2 nil
)
(repeat (setq m (sslength ObjSet2))
(setq ObjNameL2 (cons (ssname ObjSet2 (setq i (1+ i))) ObjNameL2))
)
;region
(setq MadeObjL1 (Procedure_1707 ObjNameL1))
(setq MadeObjL2 (Procedure_1707 ObjNameL2))
(Procedure_1707_2 MadeObjL1 MadeObjL2) ;SUBTRACT
(SD2056)
(setq *error* nil)
(princ)
)
;*********;SUBTRACT
(defun Procedure_1707_2 (MadeObjL1 MadeObjL2 /)
(command ".SUBTRACT")
(mapcar 'command MadeObjL1)
(command "")
(mapcar 'command MadeObjL2)
(command "")
(setq LastOb (entlast)
MadeObjL nil
)
(command ".EXPLODE" LastOb)
(while (setq LastOb (entnext LastOb))
(setq MadeObjL (cons LastOb MadeObjL))
)
(command ".PEDIT" "M")
(mapcar 'command MadeObjL)
(command "" "Y" "J" "0.000" "")
)
;*********;Region
(defun Procedure_1707 (ObjL /)
(setq LastOb (entlast))
(command ".region")
(mapcar 'command ObjL)
(command "")
(while (setq LastOb (entnext LastOb))
(setq MadeObjL (cons LastOb MadeObjL))
)
MadeObjL
)
;
(defun SD1028 ()
(setq OldCmdEcho (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(command "undo" "be")
(setq OldOsmode (getvar "OSMODE"))
(setq OldLayer (getvar "CLAYER"))
(setq OldLType (getvar "CeLType"))
(setq OldCeLWeight (getvar "CeLWeight"))
(setq OldColor (getvar "CeColor"))
(setq OldOrtho (getvar "ORTHOMODE"))
(setq OldDStyle (getvar "DIMSTYLE"))
(setq OldExpert (getvar "Expert"))
(setvar "EXPERT" 0)
(princ)
)
;********************************
(defun SD2056 ()
(setvar "OSMODE" OldOsmode)
(command "undo" "end")
(setvar "CLAYER" OldLayer)
(setvar "CeLType" OldLType)
(setvar "CeLWeight" OldCeLWeight)
(setvar "CeColor" OldColor)
(setvar "ORTHOMODE" OldOrtho)
(setvar "Expert" OldExpert)
(if (and (/= (getvar "DIMSTYLE") OldDStyle)
(tblsearch "DIMSTYLE" OldDStyle)
)
(command "-dimstyle" "Restore" OldDStyle)
)
(setvar "CMDECHO" OldCmdEcho)
(princ)
)
;********************************
(defun *myerror* (msg)
(setq *error* nil)
(SD2056)
(princ "\n Error Cancelled")
(princ)
)
(princ "\n Command Name: PLSUB to Subtract Polylines\n")
(princ)
Advertisements

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 Attributes, AutoLISP: Modify, AutoLISP: Polylines. Bookmark the permalink.

2 Responses to AUtoLISP: Subract Objects from a Closed Polyline

  1. zarko says:

    all i am getting is this : Select objects:
    no function definition: LIB:ZOOM2LST
    what s the problem??

    • AutoCAD Tips says:

      I looked over the code and can’t find a “call” for that function and I don’t seem to be able to recreate the same error that you are getting.

      I have tested it in both 2011 and 2013 and it is working fine for me…

      What version of AutoCAD are you using?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s