Delete everything outside of a predetermined bounday.

Delete everything outside of a predetermined bounday.

Anonymous
Not applicable
4,635 Views
8 Replies
Message 1 of 9

Delete everything outside of a predetermined bounday.

Anonymous
Not applicable

We are converting a ton of files from Microstation to AutoCAD and the Microstation users leave tons of crap outside the border.  I searching for a lisp routine which can select and delete everything outside a specific set of coordinates (No user input!) so I can run this in a batch environment.  So if the border 22x34 and starts at (Specify point:  X = 0     Y = 0     Z = 0) and goes to (Specify point:  X = 34     Y = 22     Z = 0).  Then everything outside of this area on all 4 sides would be selected and deleted and at the end do a Zoom E to recenter the drawings.

 

So everything Snap23.jpg

Y=22.5 and above

Y=-0.5 and below

X=22.5 and greater

X=-0.5 and smaller

0 Likes
Accepted solutions (1)
4,636 Views
8 Replies
Replies (8)
Message 2 of 9

CodeDing
Advisor
Advisor

@Anonymous,

 

I assumed you meant 34.5 for your Y value. Here is a script that can do it. Edit as necessary.

EDIT: Added ZOOM E. also added (princ) since I was still getting a prompt after Zoom E....

;start
(setvar 'OSMODE 0)
ERASE
ALL
REMOVE
(list 34.5 22.5)
(list -0.5 -0.5)

ZOOM
E
(princ)
;end

 

Best,

~DD

0 Likes
Message 3 of 9

Luís Augusto
Advocate
Advocate

 

AutoLISP: Delete Outside of Window

delete-outside.gif

 

 

 

;------------------------------------------------------------------------------
; PROCEDURE NAME: DOUT.LSP
;
; FUNCTIONAL DESCRIPTION: This is a complement of ERASE WINDOW.
; It will delete all entities OUTSIDE OF or CROSSING a selected window.
;
; CALLED BY: interactive
;
; CALLS: none
;
; PARAMETERS: none
;
; INPUT: interactive
;
; OUTPUT: none
;
; HEADER FILES: n/a
;
; RETURN VALUES: none
;
; CREATED BY / DATE: Daniel J. Squires, CADENCE May 92 p. 89
; UPLOADED BY: M. Shrout, CDOT R-4 Design (303) 350-2155 06/02/92
;
; MODIFIED BY / REASON / DATE:
;
; PRECONDITIONS, POSTCONDITIONS:
;
; SIDE EFFECTS / EXCEPTIONS: If any portion of an entity is outside of the
; selected window, that entity will be deleted (as in a "crossing"
; selection set).
;------------------------------------------------------------------------------
; The program
(defun c:DOUT (/ ss1 ss2 ss3)
(setq cmd (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(setq picked nil)
(setq cntr1 0)
(setq cntr2 0)
(setq cntr3 0)
(setq cntr4 0)
(setq cntr6 0)
(setq ss3 (ssadd))
; Define selection set #1 (entities to be retained)
(while (not picked)
(prompt "\nWindow entities to be retained: ")(prin1)
(setq pt1 (getpoint "\nFirst window point: "))
(setq pt2 (getcorner pt1 "\nOpposite corner: "))
;
(setq ss1 (ssget "w" pt1 pt2))
(if (/= ss1 nil)
(progn
(prompt "\nWorking...")(prin1)
(setq ss1lng (sslength ss1))
(setq picked 0))
(progn
(prompt "\nERROR: No Entities Found - Select Again...")
(prin1))
)
)
; Find all layers in current drawing
(setq lyr (tblnext "layer" T))
(while (/= cntr3 nil)
(if (and (or (= (cdr (assoc 70 lyr)) 64)
(= (cdr (assoc 70 lyr)) 0)
)
(/= (minusp (cdr (assoc 62 lyr))) T)
)
(progn
(setq incrval (itoa cntr4))
(setq cntr4 (1+ cntr4))
(set (read (strcat "lyr" incrval)) (cons 8 (cdr (assoc 2 lyr))))
(setq lyr (tblnext "layer")))
(setq lyr (tblnext "layer"))
)
(cond
((= lyr nil)
(setq cntr3 nil))
)
)
; Build delection set #2 & #3 (of displayed entities)
(setq cntr5 (1- cntr4))
(setq cntr4 0)
(while (<= cntr4 cntr5)
(setq incrval (itoa cntr4))
(setq ss2 (ssget "x" (list (eval (read (strcat "lyr" incrval))))))
(if (/= ss2 nil)
(progn
(while (< cntr6 (sslength ss2))
(setq enty (ssname ss2 cntr6))
(setq ss3 (ssadd enty ss3))
(setq cntr6 (1+ cntr6))
)
)
)
(setq cntr4 (1+ cntr4))
(setq cntr6 0)
)
; Determine if entities exist in both selection sets (#2 & #3)
(setq ss2 (ssadd))
(while (< cntr2 (sslength ss3))
(setq entnm3 (ssname ss3 cntr2))
(if (not (ssmemb entnm3 ss1))
(progn
(setq ss2 (ssadd entnm3 ss2))
(setq cntr2 (1+ cntr2)))
(setq cntr2 (1+ cntr2))
)
)
; Delete all entities outside of defined window
(if (/= (sslength ss2) 0)
(progn
(command ".ERASE" ss2 "")
(prompt "\n\n")
(princ (sslength ss2)) (princ " entities found & DELETED...")
(prin1))
(progn
(prompt "\nNO entities found outside defined window...")
(prin1))
)
(setvar "CMDECHO" cmd)
(prin1)
)
0 Likes
Message 4 of 9

Anonymous
Not applicable

Works except if any of the items are touching or inside the defined boundary.  If they are 100% outside the defined boundary this script is deleting them.  Is there anyway to adjust this?

0 Likes
Message 5 of 9

Anonymous
Not applicable

Luis, this looks great except it requires a user to manually select the window.  I am searching for something which I can set the hard-set the window and run without the user input.

0 Likes
Message 6 of 9

CodeDing
Advisor
Advisor

@Anonymous,

 

Easy, just reverse the points...

;start
(setvar 'OSMODE 0)
ERASE
ALL
REMOVE
(list -0.5 -0.5)
(list 34.5 22.5)

ZOOM
E
(princ)
;end

Best,

~DD

 

 

 


0 Likes
Message 7 of 9

dbhunia
Advisor
Advisor
Accepted solution

Try this....(for single drawing).....

 

(defun c:rob (/ p1 p2)
(setq p1 (list -0.5 -0.5 0))
(setq p2 (list 34.5 22.5 0))
(command "erase" "all" "r" "w" p1 p2 "")
(command "zoom" "e")
)

 

For.....

 


@Anonymous wrote:

We are converting a ton of files from Microstation to AutoCAD and the Microstation users leave tons of crap outside the border.  I searching for a lisp routine which can select and delete everything outside a specific set of coordinates (No user input!) so I can run this in a batch environment.  So if the border 22x34 and starts at (Specify point:  X = 0     Y = 0     Z = 0) and goes to (Specify point:  X = 34     Y = 22     Z = 0).  Then everything outside of this area on all 4 sides would be selected and deleted and at the end do a Zoom E to recenter the drawings.

 


 

Try this.......

 

First put all the drawings in a single folder & run the code it will ask to select the Folder Containing the drawings you just select the folder & move forward......Code will work itself on all the drawings in side that folder.....

 

But before running the Lisp make sure about two things-

       1. There is only one Drawing is open (other than any drawing which one you want to edit), otherwise SDI Variable can not be reset.

       2. Wait for complete execution of the command, otherwise SDI Variable will not restored back.

 

(defun c:Del_obj ( / sh folder folderobject result)
   (vl-load-com)
   (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application" ))
   (setq folder (vlax-invoke-method sh 'BrowseForFolder 0 "" 0 ))
   (vlax-release-object sh)
   (setq SDI_Val (getvar "SDI"))
   (setq LISPI_Val (getvar "LISPINIT"))
   (vl-cmdf "SDI" 1)
   (vl-cmdf "LISPINIT" 0)
   (if folder
      (progn
        (setq folderobject (vlax-get-property folder 'Self))
        (setq result (vlax-get-property FolderObject 'Path))
        (vlax-release-object folder)
        (vlax-release-object FolderObject)
	(setq Files_Folder (vl-directory-files result "*.dwg"))
	(command "save" (strcat (getvar "dwgprefix") (getvar "dwgname")) "Y")
	(setq n 0)
	(while (< n (length Files_Folder))
	(command "fileopen" (strcat result "\\" (nth n Files_Folder)))
	(setq p1 (list -0.5 -0.5 0))
	(setq p2 (list 34.5 22.5 0))
	(command "erase" "all" "r" "w" p1 p2 "")
	(command "zoom" "e")
	(vl-cmdf "save" (strcat result "\\" (nth n Files_Folder)))
	(setq n (+ 1 n))
        )
     )
   )
   (vl-cmdf "SDI" SDI_Val)
   (vl-cmdf "LISPINIT" LISPI_Val)
)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 8 of 9

Luís Augusto
Advocate
Advocate

Could you attach a file with an example so I can test and modify the code for you?

0 Likes
Message 9 of 9

ronjonp
Mentor
Mentor

Another for fun:

(defun foo (p1 p2 crossing / s s2)
  ;; RJP » 2018-12-06
  (defun _s (s) (cond ((= 'pickset (type s)) (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))))
  (cond	((and (setq s (_s (ssget "_x" (list (cons 410 (getvar 'ctab))))))
	      (setq s2 (_s (ssget (if crossing
				    "_C"
				    "_W"
				  )
				  p1
				  p2
			   )
		       )
	      )
	 )
	 (vlax-invoke (vlax-get-acad-object) 'zoomwindow p1 p2)
	 (foreach x s (or (vl-position x s2) (entdel x)))
	)
  )
  (princ)
)
(vl-load-com)
;; Deletes items crossing window and within
(foo '(-0.5 -0.5 0.) '(34.5 22.5 0.) t)
;; Only deletes items within window
(foo '(-0.5 -0.5 0.) '(34.5 22.5 0.) nil)
0 Likes