First, I didn't notice that you also wanted the layers purged, sorry about that. Second, in light of that, there are more things that would need to be checked to truly be able to purge them.....I don't have the time right now to code this, but the idea wouold be to attempt to delete the layers after you delete the entities on them. So I'd add a variable, layobjlist, to place the (vla-object) of the layers we are deleting in the code where we get the name, then after this code:
Code:
(while (setq ent (ssname ss (setq idx (1+ idx))))
(vla-delete (vlax-ename->vla-object ent))
)
you'd add something like this:
Code:
(mapcar '(lambda (x)
(setq tmperr (vl-catch-all-apply 'vla-delete x))
(if (vl-catch-all-error-p tmperr)
(setq notdeleted (cons x notdeleted))
)
)
layoblist)
Then, if the variable 'notdeleted' is not nil, those layers are in use by a DimStyle, frozen in a PS viewport, used by an object in a block, etc. and need to be dealt with in other ways.