Script deletes lines in layers it shouldn't

Script deletes lines in layers it shouldn't

Anonymous
Not applicable
969 Views
10 Replies
Message 1 of 11

Script deletes lines in layers it shouldn't

Anonymous
Not applicable

Hi everyone, 

I am pretty new to Visual LISP and this is my first post, but I've been reading alot on this forum to help me on my way. I'm not a big AutoCAD expert either, but I work with Revit alot and sometimes I just need to manipulate dwgs.

 

I made a script to delete all objects and layers I don't need for further use. It works, except that it also deletes some lines in layers I specified as where it shouldn't delete objects. I use the same phrasing to not delete those layers and that works perfectly. I even tried locking those layers, but that doesn't seem to help either. The only solution I can think of is to add another if statement on a higher level so it won't even consider the blocks on those layers, but I feel there has to be a more elegant solution.

 

Anyway, I hope someone has an answer for me! Here's the code I've been working on:

 

(vl-load-com) 
(defun c:extract-rooms nil
  (setq aDoc (vla-get-activedocument (vlax-get-acad-object)))	

  ; Make all layers visible and thawed
  (vlax-for layers1 (vla-get-layers aDoc)
    (vla-put-layeron layers1 :vlax-true)
    (if (not (eq (getvar 'CLAYER) (vla-get-name layers1)))
      (vla-put-freeze layers1 :vlax-false))
    (if (wcmatch (vla-get-name layers1) "G_CR_PL$,G_RM_PL$")
      (vla-put-lock layers1 :vlax-true)))
  (princ) 
  
  ; Delete all objects on all layers in all viewports except for the ones specified
  (vlax-for blocks (vla-get-blocks aDoc)
    (if (= :vlax-false (vla-get-isxref blocks))
      (vlax-for object blocks
	(if (not (wcmatch (vla-get-layer object) "G_CR_PL$,G_CR_PL$TXT,G_RM_PL$,G_RM_PL$TXT"))
	  (vla-delete object)))))
  (princ)
 
  ; Delete all layers except for the ones specified
  (vlax-for layers2 (vla-get-layers aDoc)
    (if (not (wcmatch (vla-get-name layers2) "0,G_CR_PL$,G_CR_PL$TXT,G_RM_PL$,G_RM_PL$TXT"))
      (vla-delete layers2)
      (vla-put-lock layers2 :vlax-false)))
  (princ)

  )
(princ)

 

0 Likes
Accepted solutions (3)
970 Views
10 Replies
Replies (10)
Message 2 of 11

pbejse
Mentor
Mentor

Quick look at your code, you might try using strcase along with wcmatch. 

 

(strcase (vla-get-layer object))

 

 or if your are matching the complete  name , use the function member (but you still need strcase

 

0 Likes
Message 3 of 11

Anonymous
Not applicable

Just tried this, same result Smiley Sad

0 Likes
Message 4 of 11

pbejse
Mentor
Mentor

hang on. i'll try it on my end... one more thing, the way wcmatch works is to use a wildcard 

 

"G_CR_PL$*,G_CR_PL$*TXT,*G_RM_PL$*,G_RM_PL$TXT*" specially if you are trying to match the prefix and not the entire layer name,

0 Likes
Message 5 of 11

Anonymous
Not applicable

Upon a little more testing, it seems that this is totally random. I have about 140 of these dwgs I need to purge (I already know my way around scripts and AutoScript, so that step is already set up), and in some it will delete the lines and in others it won't. In all those drawings the lines are Polylines or 2D Polylines, so that doesn't seem to be the issue.

0 Likes
Message 6 of 11

pbejse
Mentor
Mentor

Try adding the "*" on the list of your layer name as posted on my previous post. 

 

 

(wcmatch (strcase (vla-get-layer object) )"G_CR_PL$*,G_CR_PL$TXT*,G_RM_PL$*,G_RM_PL$TXT*")

This makes a lot of sense if you are targeting just a part of the Layer name. without a drawing sample it would be hard to replicate what you are describing.

 

Oops: i ddint see your reply there.

 

specially when "it seems that this is totally random" 

0 Likes
Message 7 of 11

Anonymous
Not applicable

Hi pbesje, 

 

Thanks for your help so far. I added the strcase and the * to the names, but no change. 

I figured since it isn't consistent over the drawings I should send over some files, so here are 2 drawings that behave differently. 

In 173L1 the lines dissappear and in 174L4 they remain.

0 Likes
Message 8 of 11

pbejse
Mentor
Mentor
Accepted solution

Trying the code on both drawings , it gives the same result, in 173L1, the lines you think disappeared is still there, but frozen. 

 

But is that the result you want? leave those objects that are "locked" at runtime? 

 

 

 

 

0 Likes
Message 9 of 11

Anonymous
Not applicable

So for you it works, but the lines in the 173 are just frozen? 

That is not the result I'm getting (see image below).

 

No layers should be frozen anyway, since I thought I thawed all of them at the start of the code.

 

; Make all layers visible and thawed
  (vlax-for layers1 (vla-get-layers aDoc)
    (vla-put-layeron layers1 :vlax-true)
    (if (not (eq (getvar 'CLAYER) (vla-get-name layers1)))
      (vla-put-freeze layers1 :vlax-false))
    (if (wcmatch (vla-get-name layers1) "G_CR_PL$*,G_RM_PL$*")
      (vla-put-lock layers1 :vlax-true)))
  (princ) 

I only locked those layers beacuse I thought those layers maybe couldn't be touched by the part that deletes objects. And I unlock them at the end...

 

; Delete all layers except for the ones specified
  (vlax-for layers2 (vla-get-layers aDoc)
    (if (not (wcmatch (strcase(vla-get-name layers2)) "0*,G_CR_PL$*,G_RM_PL$*"))
      (vla-delete layers2)
      (vla-put-lock layers2 :vlax-false)))
  (princ)

 

acad_2017-07-05_16-41-50.png

 

Well, in the end I really only need the room names for further use in Revit&Dynamo. The lines would have been welcome for guidance on the room boundaries, and I just couldn't figure out why they were dispappearing. I'll try my script on another computer later to see if I get different results as well.

0 Likes
Message 10 of 11

Anonymous
Not applicable
Accepted solution

Oh, now I found what happened and what you meant! As I said, I'm not a big AutoCAD expert.

I only looked at the layers, whether they were frozen or not, but somehow it's the lines themselves that are hidden. I got them back by right-clicking the model space > Isolate > End Object Isolation. Still don't understand why they became hidden in the first place though.

 

I guess I'll just have to put in a piece of code that will do that for me. Thanks for your help! 

 

EDIT:
Should anyone ever read this and look for the complete solution, I just added the following line: 

  (command-s "_.UnIsolateObjects")

 

0 Likes
Message 11 of 11

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Oh, now I found what happened and what you meant! As I said, I'm not a big AutoCAD expert.

I only looked at the layers, whether they were frozen or not, but somehow it's the lines themselves that are hidden. I got them back by right-clicking the model space > Isolate > End Object Isolation. Still don't understand why they became hidden in the first place though.

 

I guess I'll just have to put in a piece of code that will do that for me. Thanks for your help! 


Yes, that's I meant to say, "Hidden". Just add  (vla-regen aDoc acAllViewports) before the very end. 

 

Here's a quick code similar to the one you posted

 

(defun c:extract-rooms (/ aDoc layers aclayer data status layeritem)
      (vl-load-com)
(setq aDoc    (vla-get-ActiveDocument (vlax-get-acad-object))
      Blocks  (vla-get-blocks aDoc)
      layers  (vla-get-layers adoc)
      aclayer (vla-get-name (vla-get-ActiveLayer adoc))
      status  '("LayerOn" "lock" "Freeze")
)
  (vlax-for itm	layers
    (mapcar '(lambda (a b)
		       (vlax-put itm a b))
	    status (if (eq (Vla-get-name itm) aclayer)
			   '(-1 0) '(-1 0 0 ))
	  )
        
    
    )
      (vlax-for blk Blocks
      (if  	(eq :vlax-false (vla-get-isXref blk))
                 (vlax-for h blk
                       (if
                             (and (vlax-write-enabled-p h)
                                   (not (member (strcase (vla-get-layer h)) '("G_CR_PL$" "G_CR_PL$TXT" "G_RM_PL$" "G_RM_PL$TXT"))))
                             (vla-delete h))
                             
                             )
                       )
	)
       (repeat 4  (vla-purgeall aDoc)  )
      (vla-regen aDoc acAllViewports)
      (princ)
      )

HTH

0 Likes