Author Topic: Deleting Layers  (Read 3536 times)

0 Members and 1 Guest are viewing this topic.

jonesy

  • SuperMod
  • Seagull
  • Posts: 15568
Deleting Layers
« on: December 02, 2004, 09:20:43 AM »
I have many hundreds of map bases, those map bases contain loads of layers which we will never need to be displayed in our field of work.

My questions to you programming experts are...
Exept for express tools, is there a way of deleting layers from a file?
Can this be done as a batch file and if so how?

Please explain in words of two or less sylables cos I'm still a lisp virgin :oops:
Thanks in advance
Tracey
Thanks for explaining the word "many" to me, it means a lot.

M-dub

  • Guest
Deleting Layers
« Reply #1 on: December 02, 2004, 10:33:31 AM »
Do these layers contain data?
Are the layer names all the same in each drawing?
Any other details you think might help?

jonesy

  • SuperMod
  • Seagull
  • Posts: 15568
Deleting Layers
« Reply #2 on: December 02, 2004, 11:52:48 AM »
Quote from: M-dub
Do these layers contain data?

Yes some contain notes and others contain lines etc.

Quote from: M-dub
Are the layer names all the same in each drawing?

We have 3 seperate sources for these files, each source sent us around 300 drawings. Each source has standard layers set up in the file, and the  ones I want to delete will be the same in each drawing that that source has sent to us. (if you see what I mean) :?

Quote from: M-dub
Any other details you think might help?

The files in question are maps. We never use things like eastings and northings, or the key they supply as the maps are just used as a background. So my ultimate goal is to have all unused (by us) layers deleted, then to create a template file for each source which has the layers we use changed to the correct lineweights/colours etc.
Hope this gives a better idea to what I need
Thanks for your help
Tracey
Thanks for explaining the word "many" to me, it means a lot.

M-dub

  • Guest
Deleting Layers
« Reply #3 on: December 02, 2004, 01:05:16 PM »
I'm sorry Tracey...
I thought there was a method for deleting layers from the command line.  If that was true, I could have written a script for you.  Unfortunately, I can't write lisp yet either, so maybe one of the other gurus will be able to help.  I've been looking on the web for something that might help, but haven't yet found anything.  :oops:

jonesy

  • SuperMod
  • Seagull
  • Posts: 15568
Deleting Layers
« Reply #4 on: December 03, 2004, 07:19:56 AM »
Ok thanks for trying.

Spent some time trying to work out what I want. This is the Pseudo code I have come up with.  :horror:

Open
Laydel (or command that doesnt rely on express tools)
Layer names to be deleted(these will be the same for one client)
Delete Layers
Purge
Save
Close
Open next drawing etc

Dont know whether it will be do-able, but any advice would be fab.

Kind regards
Tracey
Thanks for explaining the word "many" to me, it means a lot.

Big G

  • Bull Frog
  • Posts: 415
Deleting Layers
« Reply #5 on: December 03, 2004, 10:45:51 AM »
Tracey,
When I was using Maps from OSNI before they generally came with hundreds of layers too, Parcel, Lim_Veg, and all that nonsense. I have written a script file (sorry guys) to basically turn off the layers i didnt want/need, save the os-tile as a new drg name, invert the filter, and delete the unwanted layers. ultimately turning the background map to color 8 and to a layer called Map.
I still might have something kicking around somewhere if it helps??
I thought i seen the light at the end of the tunnel. But it was just someone with a torch bringing me more work.
"You have to accept that somedays youre the pigeon and  somedays youre the statue"

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Deleting Layers
« Reply #6 on: December 03, 2004, 11:48:03 AM »
Code: [Select]


;;;
;;; several function for erasing and purging entities on a given layer
;;;

(defun c:del-layer (/ ent l_name ss cntr amt ssent)

  ;;;
  ;;; erases all enities on selected layer then purges that layer
  ;;;

  (setvar 'clayer "0") ; set layer to 0

  (if
    ; make sure we get something
    (setq ent (car (entsel "\nSelect layer to remove: "))); test
    (progn
      ; extract the layer name from the entity
      (setq l_name (cdr (assoc 8 (entget ent))))

      ; create a selection set of all entites on layer 'l_name'
      (setq ss (ssget "X" (list (cons 8 l_name)))
            ; set 'cntr' to number of items in selection set
            cntr (1- (sslength ss))
            amt (itoa cntr); make a string from an integer
            )

      (if
        ; does the sel set have anything in it
        (> cntr 0); test

        (while
          ; as long as 'cntr' is greater than or equal to 0
          ; keep looping
          (>= cntr 0)

          ; extract the ename from the sel set
          (setq ssent (ssname ss cntr))
          (entdel ssent); delete that entity
          (setq cntr (1- cntr)); subtract 1 from cntr
          )

        )
      )
    )
  (command "_.purge" "LA" l_name "N")
  (princ (strcat "\nErased " amt " items"))
  (princ)
  )

;;;
;;; ===================================================================
;;;

(defun fx-rm-layer (l_name / ss cntr amt ssent)

  ;;;
  ;;; erase and purges all entites on 'l_name', a string
  ;;; returns the number of entites erased
  ;;;

  (setvar 'clayer "0")

  (if
    ; make sure the layer exists in the dwg
    (tblsearch "layer" l_name); test
    (progn ; continue
      (setq ss (ssget "X" (list (cons 8 l_name)))
            cntr (1- (sslength ss))
            amt cntr
            )

      (if (> cntr 0)
        (while
          (>= cntr 0)
          (setq ssent (ssname ss cntr))
          (entdel ssent)
          (setq cntr (1- cntr))
          )
        )
      )
    )
  (if (> amt 0)
    (command "_.purge" "LA" l_name "N")
    )
  ; return the amount of entites erased
  amt
  )

;;;
;;; ===================================================================
;;;

(defun fx-make-layer-list (/ ent l_name entlst)

  ;;;
  ;;; generate a list of layer names based on user selection
  ;;; and returns that list
  ;;;

  (while
    ; while user is selecting something continue loop
    (setq ent (car (entsel "\nSelect Item on Layer: "))); test

    ; extract layer name from selected entity
    (setq l_name (cdr (assoc 8 (entget ent))))
    (prompt l_name); output the layer

    (if
      ; make sure the layer isn't already in the list
      (not (vl-position l_name entlst)); test

      ; if not then add it to the list
      (setq entlst (cons l_name entlst))
      )
    )
  )

;;;
;;; ===================================================================
;;;

(defun c:del-purge (/ lst)
  (setq cmd (getvar 'cmdecho))

  ; turn off the echo!
  (setvar 'cmdecho 0)

  (if
    ; make sure we get a list before we continue
    (setq lst (fx-make-layer-list)); test

    ; now that we have a list run 'fx-rm-layer' on each of those items
    (mapcar '(lambda (x) (fx-rm-layer x)) lst)

    )

  ; reset the variable
  (setvar 'cmdecho cmd)
  (princ)
  )


TheSwamp.org  (serving the CAD community since 2003)

Jeff_M

  • King Gator
  • Posts: 4099
  • C3D user & customizer
Deleting Layers
« Reply #7 on: December 03, 2004, 04:43:37 PM »
Quote from: jonesy

Dont know whether it will be do-able, but any advice would be fab.

Kind regards
Tracey


Well here's something that I've been playing with off & on for a while. Your post encouraged me to finish it up. It does MOST everything you want, it falls short when trying to delete a layer that is referenced in a Dimension style or has been used inadvertently in a block's "endblk" definition. I'm out of time for today, but it really should have a few lines added to delete any unused block definitions from the files prior to the layer deletion. It went through my sample folder of 10 drawings and fixed them all to my specifications in about 12 seconds.

To use this, place all of your drawings from each client in their own folders. Open a drawing for the first client, load this routine and type "layers2delete". Follow the prompts to freeze all the layers you DON'T want...IOW, make this drawing look how you want to keep it. If you make a mistake and select a layer that you really want to keep, DON'T CANCEL OUT, instead just continue on and you will be taken back to the prompt to Select, Type, Dialog, Exit....at this point use the dialog option to thaw the layer you wanted to keep.

Read the header for additional information.
Code: [Select]

;| Routine to collect the layers in a typical drawing file that you want to delete
   from a number of other drawings and subsequently create new lisp routines for specific
   clients that can be run any number of times. The new routines will delete ALL matching layer names
   (and the objects on those layers) and then save the drawing as "clean-<originalname>.dwg"
   The new commands are loaded via a lisp in the Support folder named <client>.lsp and the new
   commands are named <client>-1 and <client>-all, where <client> is the name you supply in the routine.
   The <client-all> will iterate through ALL the drawings in the current drawing's folder and may take
   some time to complete, be patient.....Also, this lisp file must be loaded prior to running the new
   commands at a later time.

   Jeff Mishler, jeff_m@cadvault.com *** You may use this program as you wish, but the author makes no
   warranties, expressed or implied, as to it's suitability for your particulafr task.
   Use at your own risk!!!
   |;

;;;Main function to run the first time for each client's set of drawings
(defun c:layers2delete (/ ans layers acadpath client jm:path fname JM:ACAD JM:DOC JM:LAYS)
  (vl-load-com)
  ;;;Function to select layers on screen
  (defun picklayers (/ ent)
    (while (setq ent (car (entsel "\nSelect object on layer to freeze: ")))
      (vla-put-freeze (vla-item jm:lays (cdr (assoc 8 (entget ent)))) :vlax-true)
      )
    )
  ;;;Function to type in layers
  (defun typelayers (/ lays)
    (while (/= "" (setq lays (getstring "\nType layers to freeze, wildcards OK: ")))
      (command "-layer" "f" lays "")
      )
    )
  ;;;Make sure all layers are on, thawed and unlocked
  (command "-layer" "on" "*" "t" "*" "u" "*" "s" "0" "")
  (setq jm:acad (vlax-get-acad-object)
jm:doc (vla-get-activedocument jm:acad)
jm:lays (vla-get-layers jm:doc)
)
  (initget 1 "Select Type Dialog eXit")
  ;;;Freeze all layers that are not wanted, leaving the drawing how you want it to appear
  (setq ans (getkword "\nPlease freeze all unwanted layers by Select, Typing, Dialog, eXit: "))
  (while (not (eq ans "eXit"))
    (cond ((eq ans "Select")(picklayers))
 ((eq ans "Type")(typelayers))
 ((eq ans "Dialog")(initdia)(command "layer"))
 )
    (initget 1 "Select Type Dialog eXit")
    (setq ans (getkword "\nPlease freeze all unwanted layers by Select, Typing, Dialog, eXit: "))
    )
  ;;;Create list of frozen layers
  (vlax-for lay jm:lays
    (if (eq (vla-get-freeze lay) :vlax-true)
      (setq layers (cons (vla-get-name lay) layers))
      )
    )
  ;;;If there are frozen layers, write out list to a file for later use
  (if layers
    (progn
      (setq acadpath (strcat (vla-get-path jm:acad) "\\Support\\")
   jm:path (strcat acadpath "JM-Utils"))
      (if (not (findfile jm:path))
(vl-mkdir jm:path)
)
      (initget 1)
      (setq client (getstring "\nClient name?: "))
      (setq fname (open (strcat jm:path "\\" client ".txt") "w"))
      (foreach lay layers
(write-line lay fname)
)
      (close fname)
      ;;;create new commands based on Client's name
      (makedefun acadpath client)
      )
    )
  ;;;Load the new lisp file and run the 2 new commands
  (load (strcat client ".lsp"))
  (eval (read (strcat "(c:" client "-1)")))
  (eval (read (strcat "(c:" client "-all)")))
  (princ)
  )
;;Function to create new lisp file and 2 new commands
(defun makedefun (path name / fname )
  (setq fname (open (strcat path "\\" name ".lsp") "w"))
  (write-line (strcat "(defun c:" name "-1 ()") fname)
  (write-line (strcat "(delete_layers_from_file \"" name "\" nil)") fname)
  (write-line "(princ))" fname)
  (write-line (strcat "(defun c:" name "-all ()") fname)
  (write-line (strcat "(delete_layers_from_file \"" name "\" (vl-directory-files (getvar \"dwgprefix\") \"*.dwg\" 1) )")fname)
  (write-line "(princ))" fname)
  (close fname)
  )

;;;actual layer deletion function. If multi arg is nil then only the current drawing
;;; is affected. If multi is supplied as a list of drawing names (must be in the same folder as the active dwg)
;;; then all drawings EXCEPT the current one are affected
(defun delete_layers_from_file (client multi / fname layers newline acadpath lay JM:ACAD JM:DOC JM:LAYS)
  (setq jm:acad (vlax-get-acad-object)
jm:doc (vla-get-activedocument jm:acad)
jm:lays (vla-get-layers jm:doc)
acadpath (strcat (vla-get-path jm:acad) "/Support/"))
  (if (setq fname (open (strcat acadpath "/JM-Utils/" client ".txt") "r"))
    (progn
      (while (setq newline (read-line fname))
(setq layers (cons newline layers))
)
      (close fname)
  (if (not multi)
    (progn
      (vlax-for blk (vla-get-blocks jm:doc)
(vlax-for ent blk
 (if (member (setq lay (vla-get-layer ent)) layers)
   (progn (vla-put-lock (vla-item jm:lays lay) :vlax-false)
     (vla-delete ent)
     )
   )
 )
)
      (vla-purgeall jm:doc)
      (vla-save jm:doc)
      )
    (progn
      ;;;multi was supplied, use ObjectDBX to cycle thru the drawing list
      (if (= (atoi (getvar "AcadVer")) 15)
(progn
 (if (not (vl-registry-read "HKEY_CLASSES_ROOT\\ObjectDBX.AxDbDocument\\CLSID"))
   (startapp "regsvr32.exe" (strcat "/s \"" (findfile "axdb15.dll") "\""))
   )
 (setq jm:doc (vla-GetInterfaceObject jm:acad "ObjectDBX.AxDbDocument"))
 )
(setq jm:doc (vla-GetInterfaceObject jm:acad "ObjectDBX.AxDbDocument.16"))
)
      (foreach dwg multi
(if (not (eq (vla-get-name (vla-get-activedocument jm:acad)) dwg))
 (progn
   (vla-open jm:doc (strcat (getvar "dwgprefix") dwg))
   (vlax-for blk (vla-get-blocks jm:doc)
     (vlax-for ent blk
(if (member (setq lay (vla-get-layer ent)) layers)
 (progn (vla-put-lock (vla-item (vla-get-layers jm:doc) lay) :vlax-false)
   (vla-delete ent)
   )
 )
)
     )
   (vlax-for lay (vla-get-layers jm:doc)
     (if (member (vla-get-name lay) layers)
(vl-catch-all-apply 'vla-delete (list lay))
)
     )
   (vla-SaveAs jm:doc (strcat (getvar "dwgprefix") "clean-" dwg))
   )
 )
)
      )
    )
      )
    )
  (princ)
  )


Enjoy!

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Deleting Layers
« Reply #8 on: December 03, 2004, 07:00:59 PM »
This was in my collection of borrowed code.
Code: [Select]
(vl-load-com)

(defun C:FIXSEQEND ( / ss i)
   (if (setq ss (ssget "x" '((0 . "INSERT") (66 . 1))))
      (repeat (setq i (sslength ss))
         (fixseqend (ssname ss (setq i (1- i))))
      )
   )
   (princ)
)

(defun FixSeqEnd (e / d la)
   (setq la (cdr (assoc 8 (entget e))))
   (while
      (/= "SEQEND"
          (cdr (assoc 0 (setq d (entget (setq e (entnext e))))))))
   (if (/= la (cdr (assoc 8 d)))
      (vla-put-layer (vlax-ename->vla-object e) la)
   )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.