How to obtain the information of layers and delete them without open a dwg file

How to obtain the information of layers and delete them without open a dwg file

nikko_1983
Participant Participant
1,596 Views
16 Replies
Message 1 of 17

How to obtain the information of layers and delete them without open a dwg file

nikko_1983
Participant
Participant

Hi, I have a promble when I want to delete some layers withou open a dwg file.

I have found a reference in fourm, as follow:

 

(vl-load-com)
(defun OpenDrawingDBX (dwg / odbx)
  (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)  
   (setq odbx (vlax-create-object "ObjectDBX.AxDbDocument")) 
   (setq odbx (vlax-create-object (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2))))
  )
  (vla-open odbx dwg)
  odbx
)
(if (and (setq filename (getfiled "Select a drawing" "" "dwg" 0))
         (setq doc (OpenDrawingDBX filename))
    )
  (progn
    (vlax-for ll (vla-get-Layers doc))
    (vla-delete ll)
  )
)

 

Unfortunately, it doesn't work.

I found that the "doc" is a dwg file path.  Now, I have no idea how to solve this problem.

Please give me some suggestions. Thank you!

0 Likes
Accepted solutions (1)
1,597 Views
16 Replies
Replies (16)
Message 2 of 17

pbejse
Mentor
Mentor

@nikko_1983 wrote:

Hi, I have a promble when I want to delete some layers withou open a dwg file.

I have found a reference in fourm, as follow:

...

Please give me some suggestions. Thank you!


Delete the specified layer(s) regardless if its empty or not?

 

0 Likes
Message 3 of 17

ronjonp
Mentor
Mentor

First you need to include the delete line within the loop .. but if there are objects on the layer you're trying to delete you'll get an error.

 

;; BAD
(vlax-for ll (vla-get-layers doc))
(vla-delete ll)
;; GOOD :)
(vlax-for ll (vla-get-layers doc) (vla-delete ll))
;; BETTER BUT YOU WON'T KNOW OF THE ERROR :)
(vlax-for ll (vla-get-layers doc) (vl-catch-all-apply 'vla-delete (list ll)))

 

 

 

 

0 Likes
Message 4 of 17

nikko_1983
Participant
Participant

Thanks for your help.

But I encountered another promble that autolisp always feedback "Automation error. the object is referred." ,when I used (vla-delete) to delete specified layer or block.

I had tried to create a new layer or block. And then I used (vla-delete) to delete them. But it is still useless.

Are there any suggestions to delete layer or block without opening dwg file.

Thank you!

0 Likes
Message 5 of 17

nikko_1983
Participant
Participant

There are some blocks in the specificed layer.

0 Likes
Message 6 of 17

pbejse
Mentor
Mentor

@nikko_1983 wrote:

There are some blocks in the specificed layer...

... always feedback "Automation error. the object is referred."...


Which means YES? the program will delete the objects that is under the specified layer?

 

0 Likes
Message 7 of 17

ronjonp
Mentor
Mentor

@nikko_1983 wrote:

There are some blocks in the specificed layer.


@nikko_1983 

You would need to put all the objects in the drawing on another layer then try to delete the layer.

0 Likes
Message 8 of 17

nikko_1983
Participant
Participant

It can't be deleted regradless of whether the layer is empty or not.

0 Likes
Message 9 of 17

nikko_1983
Participant
Participant

I have tried to create a new empty layer. But it still work to delete this new layer.

0 Likes
Message 10 of 17

pbejse
Mentor
Mentor

@nikko_1983 wrote:

It can't be deleted regradless of whether the layer is empty or not...

I have tried to create a new empty layer. But it still work to delete this new layer...

 


Layers can and will be deleted IF there are no objects assign with the layer name.

My question was, do you want the layers to be deleted regardless if there's a object assign with that layer, which means the object(s) will be deleted as well. Is that what you want?  or assign another layer to it as suggested by @ronjonp 

 

0 Likes
Message 11 of 17

nikko_1983
Participant
Participant

Yes, i want the object(s) will be deleted too.

0 Likes
Message 12 of 17

pbejse
Mentor
Mentor

One last set of Qs, do you want this to work with multiple files? or just one per? what about supplying layer name(s) at the start of the program? (separated by comma) Ignore XREFs? 

0 Likes
Message 13 of 17

ronjonp
Mentor
Mentor

@nikko_1983 wrote:

Yes, i want the object(s) will be deleted too.


You could do something as simple as this:

(defun _foo (layers)
  (setvar 'cmdecho 0)
  (foreach l layers (command "_.laydel" "_Name" l "" "_Y"))
  (setvar 'cmdecho 1)
  (princ)
)
;; Usage
(_foo '("layername1" "layername2" "layername3"))

 

0 Likes
Message 14 of 17

nikko_1983
Participant
Participant

Yes, i want this to work with multiple files. And i don't understand your last question about ignoring XREFs. Thank you for your patience.

0 Likes
Message 15 of 17

nikko_1983
Participant
Participant

Thanks your code.

But it can't work when the drawing isn't opened. And i want to do this work in case of the dwg file is not open.

Message 16 of 17

pbejse
Mentor
Mentor
Accepted solution

@nikko_1983 wrote:

Yes, i want this to work with multiple files. And i don't understand your last question about ignoring XREFs. Thank you for your patience.


Works on multiple files The program will process ALL dwg files on the selected folder, additional coding is required otherwise such as  a list box.

(Defun C:Dellayeronlist ( / _LayDel f AllDrawingFiles odbx  layerIsCurrent layerNotDeleted)
;;		pBe July 2022			;;
(defun _LayDel (DBXDoc  llst / Blocks layers clayer RevertBackTo status layeritem)
	(setq Blocks  (vla-get-blocks DBXDoc)
	      layers  (vla-get-layers DBXDoc)
	      status  '("LayerOn" "lock" "Freeze")
	)
	      
;;;	Collect layer items and save the status 	;;;  
	 (vlax-for itm	layers
	    (setq RevertBackTo
		   (cons
		     (append (list (Vla-get-name itm) itm
				   (mapcar '(lambda (p) (vlax-get itm p))
					   status )))
		     RevertBackTo	))		   
	    (mapcar '(lambda (p v)
		(vl-catch-all-error-p (vl-catch-all-apply 'vlax-put (list itm p v))))  
		    status '(-1 0 0)
		    )
	    )

;;	Delete objects under listed layers including blocks  
	(vlax-for blk Blocks
	  (if (eq :vlax-false (vla-get-isXref blk))
	    (vlax-for h	blk
	      (if (and
		    (vlax-write-enabled-p h)
		    (member (vla-get-layer h) llst)
		  )
		(vla-delete h)
	      )
	    )
	  )
	)
;;;	Set back the original status and delete listed layers  
	(foreach lns RevertBackTo
	    (setq layeritem (cadr lns))
	   (cond
	     ( (not (member (strcase (Car lns)) llst))
		      (mapcar '(lambda (j m) (vlax-put layeritem j m))  status
				  (caddr lns)
			  )
		      )
	     ( (vl-catch-all-error-p (vl-catch-all-apply  'vla-delete (list layeritem)))
	      	(setq clayer (Car lns)))
	      )
	    )
  clayer
	  )

;;   This can be a sub to prompt the user for 	;;
;;   layernames or read a external file to be 	;;
;;   used as a source of layer names.		;;
  
(setq layerlist '("LAYERNAME1" "LAYERNAME2")) 
  
;;		Works on multiple files  	;;
;;	The program will process ALL dwg files	;;
;;	on the selected folder, additional 	;;
;;	coding is required otherwise such as	;;
;;	a list box.				;;
  
(if (and
  	(setq f (acet-ui-pickdir "Select Target Folder"))
	(setq AllDrawingFiles  (vl-directory-files f "*.dwg"))
	)
  
  (progn
      (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
	(setq odbx (vlax-get-or-create-object "ObjectDBX.AxDbDocument"))
	(setq odbx (vlax-get-or-create-object
		     (strcat "ObjectDBX.AxDbDocument."
			     (substr (getvar "ACADVER") 1 2)
		     )
		   )
	)
      )
    
    (foreach dwg AllDrawingFiles
	       (if (vl-catch-all-error-p
		     (vl-catch-all-apply 'vla-open (list odbx (strcat f "\\" dwg)))
		   )
		 (princ (strcat "Odbx connection no available with file :\n" dwg))
		 (progn
			(setq layerIsCurrent (_LayDel odbx  layerlist))
		   	(vla-saveas odbx (strcat f "\\" dwg))
			(and layerIsCurrent
				(setq layerNotDeleted (cons (list dwg layerIsCurrent)
							    layerNotDeleted)))
		 )
	     )
      	)
      (vlax-release-object odbx)
      (foreach itm layerNotDeleted (print itm))
      	    )
  )
  (princ)
  )

Command: Dellayeronlist

 

Please note:

A layer name may not be deleted if the layer name is the current layer. the program will spit out a list at the showing name of the drawing ang the layer name

("Drawing name.dwg" "Layername")

 

Also 

(setq layerlist '("LAYERNAME1" "LAYERNAME2")) <--- target layer names

This can be a sub to prompt the user for layernames or read a external file to be used as a source of layer names

 

HTH

0 Likes
Message 17 of 17

nikko_1983
Participant
Participant

Thank you very much!! 

The code work perfectly!!

0 Likes