Lisp for freezing layers (and ignore non-existing layers)

Lisp for freezing layers (and ignore non-existing layers)

larsr2866
Enthusiast Enthusiast
2,684 Views
18 Replies
Message 1 of 19

Lisp for freezing layers (and ignore non-existing layers)

larsr2866
Enthusiast
Enthusiast

Hi everyone,

 

I have been trying to add a command to my lisp, that needs to freeze a few specific layers. The intension is to use this lisp for different drawings, wich means that i want to list up all these concerning layers from different drawings in one lisp.  So when i want to use this in one drawing, it should just recognize the existing layers in the drawing to freeze and ignore the non-existing layers.

 

I have tried to write this, but when i run my lisp it's giving some troubles (freezing the wrong layers). I think the problem comes from the fact that it cant't find the non existing layers? (while these should be ignored).

 

(command "_.LAYER" "_freeze" "A-concrete" "")
(command "_.LAYER" "_freeze" "Electricity" "")
(command "_.LAYER" "_freeze" "Lines-up-2" "")
(command "_.LAYER" "_freeze" "Lines-down-3" "")
(command "_.LAYER" "_freeze" "VENT" "")
(command "_.LAYER" "_freeze" "0.23" "")

and so on...

 

Can someone help me out with this?

Thanks in advance.

 

Lars

0 Likes
Accepted solutions (1)
2,685 Views
18 Replies
Replies (18)
Message 2 of 19

dlanorh
Advisor
Advisor

Try this :

 

(vl-load-com)
;;Freeze Layer List
(defun c:fll (/ *error* c_doc c_lyrs lyr_lst clyr)
  
	(defun *error* ( msg )
		(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
		(if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nAn Error : " msg " occurred.")))
		(princ)
	);end_*error*_defun
	
	(setq c_doc (vla-get-activedocument (vlax-get-acad-object))
        c_lyrs (vla-get-layers c_doc)
        lyr_lst (list "1" "0" "A-concrete" "Electricity" "Lines-up-2" "Lines-down-3" "VENT" "0.23")
        clyr (getvar 'clayer)
	);end_setq
	
	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(vla-startundomark c_doc)

  (foreach lyr lyr_lst
    (if (tblobjname "layer" lyr)
      (if (/= (strcase clyr) (strcase lyr)) (vlax-put-property (vla-item c_lyrs lyr) 'freeze :vlax-true))
    );end_if
  );end_foreach
  (if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
);end_defun

It will not freeze the current layer even if it is on the list. Just add layers to the lyr_lst variable, or construct the list from an external file/

I am not one of the robots you're looking for

0 Likes
Message 3 of 19

andkal
Collaborator
Collaborator

Hello
Maybe try adding then a condition that checks if layer exists.


• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 4 of 19

CodeDing
Advisor
Advisor

@larsr2866 ,

 

Hope this helps:

(defun c:TEST ( / cmd lyrList lyr)
(setq cmd (getvar 'CMDECHO))
(setvar 'CMDECHO 0)
(setq lyrList (list "A-concrete" "Electricity"
		    "Lines-up-2" "Lines-down-3"
		    "VENT" "0.23"))
(setq lyrList (mapcar '(lambda (x) (strcase x)) lyrList))
(foreach lyr lyrList
  (if (and (tblsearch "LAYER" lyr) (not (eq lyr (strcase (getvar 'CLAYER)))))
    (command "_.LAYER" "_freeze" lyr "")
    (prompt (strcat "\nUnable to Freeze layer: " lyr))
  );if
);foreach
(setvar 'CMDECHO cmd)
(princ)
);defun

Best,

~DD

0 Likes
Message 5 of 19

Kent1Cooper
Consultant
Consultant

This is much easier than you're imagining.  There's no need to check whether the Layers exist.  You can pile all those Layer names together with comma separators, in one  Layer command and one  Freeze option within that.  If any of them don't exist, it won't care, and it won't cause any error, and it will Freeze all those that do  exist.  If none  of them exist, it will notify you that it didn't find any matching Layers, but if any of them do, it will just freeze those, and not complain.  Add your "and so on..." additional Layers with further comma separators, as many as you want.

 

(command "_.LAYER" "_freeze" "A-concrete,Electricity,Lines-up-2,Lines-down-3,VENT,0.23" "")

Kent Cooper, AIA
Message 6 of 19

scot-65
Advisor
Advisor
In addition to Kent's suggestion, add a layer to the freeze section
that is known to be existing in the drawing file. This will keep the
command prompt quiet if none found. From there, thaw the known
layer. Note: Use a layer that is not typically set as current.

(command ".LAYER" "Freeze" "a-con*,elec*, lin*,ven*,0.23,Def*" "Thaw" "Def*" "")

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 7 of 19

john.uhden
Mentor
Mentor

Thank you, Kent, for delivering the wisdom of simplicity.

However, your wisdom of grammar is lacking...

You wrote, "If any of them don't exist..."

That should be "If any of them doesn't exist..." because "any" is short for "any one" which is singular.  "of them" is just an adjectival phrase.  I trust you would not say, "Them don't exist."

Well, unless you are quoting an old western, as in "Them mangy varmints done stole my horse!"

John F. Uhden

0 Likes
Message 8 of 19

john.uhden
Mentor
Mentor

I think that many of us here (and elsewhere) would cringe at the idea of freezing layer "0."

John F. Uhden

Message 9 of 19

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

....

You wrote, "If any of them don't exist..."

That should be "If any of them doesn't exist..." because "any" is short for "any one" which is singular.  "of them" is just an adjectival phrase.  ....

 


 

No, "any" could just as well be "any seventeen" of them that don't exist.

Kent Cooper, AIA
0 Likes
Message 10 of 19

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

I think that many of us here (and elsewhere) would cringe at the idea of freezing layer "0."


[That hasn't been suggested.  There's a Layer 0.23  in Message 1, but no Layer 0 anywhere.]

Kent Cooper, AIA
0 Likes
Message 11 of 19

Kent1Cooper
Consultant
Consultant

@scot-65 wrote:
In addition to Kent's suggestion, add a layer to the freeze section that is known to be existing in the drawing file. This will keep the command prompt quiet if none found. From there, thaw the known layer. Note: Use a layer that is not typically set as current. ….

I don't see any point in that approach.  The only time the command line would not  be "quiet" about it is with not even one  of the listed Layers in the drawing, which seems worth being told about, especially as it only notifies you and goes on, without any error.  Besides, they're talking about a lot of drawings with different Layer combinations, so I imagine finding a Layer name that will be known to exist in all of them might not be possible [other than Layer 0].

 

But there is the issue that if, in a given drawing, one of the routine's Layers-to-be-Frozen might be current, it will not be Frozen.  [Again, it will only notify  you that it can't Freeze the current Layer -- that won't prevent the routine from going on to do whatever else it does.]  It may be worth doing something like setting Layer 0 current before the Freezing, assuming Layer 0 is not going to be one that's in the Freeze-'em list.

Kent Cooper, AIA
0 Likes
Message 12 of 19

larsr2866
Enthusiast
Enthusiast

Thanks for your reactions.

 

First, i tried the suggestion from Kent (wich seems  the most easy one), but when i appload my lisp i get an error:

";error: string too long on input"

 

(i have about 50 layers wich i would like to list up)

 

Any ideas to fix this?

Thanks!

0 Likes
Message 13 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

@larsr2866 wrote:

.... i get an error:

";error: string too long on input"

(i have about 50 layers ….


 

You can do two [or more as needed] Freeze options:

 

(command "_.layer"

  "_freeze" "Layer1,Layer2,Layer3,Layer4,Layer5"

  "_freeze" "Layer46,Layer47,Layer48,Layer49,Layer50"

  "" ; [end Layer command]

); command

 

Another thing you can do to shorten the string(s), depending on the nature of the Layer names, is to combine some with wildcards.  If you have Layers called JUNK-EXISTING, JUNK-NEW, JUNK-OVERHEAD and JUNK-UNDERGROUND, you can get all  of them by using just JUNK*.  But take care, because that will get all  Layers whose names start with JUNK, whatever else may be in their names, so if there could be any such Layers that you don't  want included, don't simplify it to that degree.  Help for the (wcmatch) function lists the wildcards you can use.

Kent Cooper, AIA
0 Likes
Message 14 of 19

dlanorh
Advisor
Advisor

@john.uhden wrote:

I think that many of us here (and elsewhere) would cringe at the idea of freezing layer "0."


 

Oops, didn't remove test layers.Robot Sad

I am not one of the robots you're looking for

0 Likes
Message 15 of 19

larsr2866
Enthusiast
Enthusiast

Thanks, it loads my lisp without any troubles.

 

However, when ik run my lisp, it freezes all my layers (also the layers that are not listed up in my lsp file)?

Are there maybe some special characters (in the layer names) that could give this problem?

 

(command "_.layer" "_freeze" "Beton-Extra Info,Hatches on layer,Staal-Boven,Wap Plaat-Boven-Wapening-Hoofd,Wap Plaat-Onder-Wapening-Hoofd,WAP0.25,WAP0.35,WAPboven,WAPtext25,0.50,WAPstaal16mm,WAPstaal12mm,WAPstaal10mm,WAPstaal8mm,WAPstaal25mm,0arceringDRAGEND,0arceringNDdragend,0gevelarcering,0-MATEN,0-MAATLIJN,0-WAPENING,LEGENDE-WAP,WAP,LAYER01,LAYER105,LAYER32,LAYER22,LAYER53,A-DETL,A-DETL-HDLN,G-ANNO-SYMB,G-ANNO-DIMS,A-FLOR-IDEN,DragmetsARC,Stabilisé,platen - houten roostering,ARCHNUTS,WAPNETHID,WAPTEKSTB,WAPSTROOKB,WAPSTAAFB1,WAPLAYOUTO,WAPLAYOUTB,WAPSCHEMA,~DSV-Legende,PLAATLIJN,PREDAL,BETANZ,TXT18,WAPSTAAF,WAPSTAAFO1,WAPTEKSTO,c-HACH_COF,c-HACHURE,wap tekst,bovenwapening,ds bereik,ds plaat,meetstaat,hulplijn,wapening,tekst,A04-_ARCERING,S-wapening_1,S-wap_lijn,0-TXT-25,S-waptekst_25,025-TEKST,0-afmeting,035-streepjeslijn,050-streepjeslijn,050-streepjeslijn_L,025-buigstaat,009,035-TEKST,ARCERING,HATCH,ALG1,ALG2,Bijlegwapening,Pijl,Staal,2B-G-arcering metselwerk,2B-G-uitstekende wapening,2B-G-wapening,2B-G-draagrichting,2B-G-tekst025,2B-G-netten,2B-mini details onzichtbaar,2B-G-arcering poutrels,DIM,A281-wapening-onder,arcering-heel licht,DIM50 constructie,ARCHHATCH,AUTOCADHULP,25,35,PEN4,A050,A030" "_freeze" "40 - Arcering algemeen,30 - Detail 010,17 - Plan – Dun,19 - Plan – Bovenwapening,18 - Plan – Onderwapening,21 - Text wapening op plan,20 - Maatvoering wapening,37 - Hulplijn vloeren,COBE_ARC-60_,Floor Tags,S-FLORSYMB,A-----NPP,SOLID,_SOLID,10,dragmetsARC,vents,vent,ALG4,Hulp,ALG1,ALG2,Maten,Wapening,PE-METSELWERK-ARCERING-DRAGEND-SNEDE,PE-WAP.PLAAT-BOVEN-1,PE-BETONPLAAT-PREDALLEN,PREDALLEN,PE-ALGEMEEN-MAATVOERING,PE-TEKST-STAAL,PE-PREDALPLAAT,PE-TEKST-WAPENING,PE-WAPENING-PLAAT-BOVEN-1,PE-WAP.PLAAT-ONDER-2,PE-WAPENING-ALGEMEEN-SNEDE,PE-BETON-ALGEMEEN-SNEDE,stab wapening,stab draagrichting vloeren,stabtekst,36MetselwerkSolid,05Hout,32BetonS02,CPlum 0.18,Metaal_arcering,arcering,hach-maçonnerie-plan,0.18,armature,0.3,Hach-détail" "_freeze" "Text 0.5,50,0.5,tekst-0.5,-Betonplaat-solid,-Wapening-netten-boven-2-nummer,-Wapening-netten-onder-2-nummer,-Betonplaat-2-dikte,-Betonplaat-3-peil,-Binnenwand-dragend-solid,-Wapening-boven-1-richting,-Wapening-boven-1-tabel,-Wapening-netten-boven-1-richting,-Wapening-boven-2-afstand,-Predallen-1-richting,-Predallen-2-afstand,-Dim-tabel-10,-Predallen-3-tekst,-Onzichtbaar-Bekisting,-Wapening-op-predal-1-richting,-Wapening-boven-3-nummer,-Onzichtbaar-Wapening,-Wapening-onder-1-richting,-Betonplaat-openingen,-Wapening-op-predal-3-nummer,-Wapening-onder-predal-3-nummer,-Balk-1-tabel,-Profielen-4-AE235,-Balk-4-bovenkant,-Balk-5-onderkant,-Balk-3-sectie,-Balk-omgekeerd-3-sectie,-Balk-omgekeerd-4-bovenkant,-Balk-omgekeerd-5-onderkant,Betonplaat,Wapening2,Wapening,Aanduidingen,HULPLIJNEN-controle,T-20,T-25,T-50,T-100,DIM,W-opleg,W-boven,W-onder,W0-WAPENING,HULPLIJNEN,BET_arcering_lijn_1_0.09,BET_pl_aanduiding onderwapening,BET_pl_2e L_verdeelwap onder,BET_pl_1e L_wapening onder,BET__tekst pen H 0.20,BET_pl_aanduiding onderwapening,A-32-_Binnenschrijnwerk,STAB - ARCERING-STAAL,HACHURE,ARMATURE,PRÉDALLE,COTATION,DALLE,Staven" ""; [end Layer command]

); command

0 Likes
Message 16 of 19

john.uhden
Mentor
Mentor
I don't think seventeen is permitted. Maybe 16 or 18, but not 17. Hmm,
I'll have to check with my wife, the teacher. I don't think Mr. Finan from
7th grade is with us anymore.

John F. Uhden

0 Likes
Message 17 of 19

Kent1Cooper
Consultant
Consultant

@larsr2866 wrote:

.... when ik run my lisp, it freezes all my layers ....

Are there maybe some special characters (in the layer names) that could give this problem?

 

(command "_.layer" "_freeze" "....


 

That's a scary-long list....  It makes me wonder whether you might be better off freezing everything  [except a current Layer such as 0], and then thawing a more limited set of Layer names, if that's viable for your typical drawing structure.

 

The only thing I can think of that could be causing it to freeze more Layers than you want [though I have serious doubts] is that a period [.] is a wildcard  for any single punctuation-type [non-alphanumeric] character.  So your having 0.50  in the list would also "catch" a Layer called 0:50, and one called 0^50, etc. [whichever such characters are allowable in Layer names -- not all are].  But it seems highly unlikely that every  other Layer in your drawing would be named in such a way as to be caught by that.  However, if that's possible, you can force it to read that period literally, and not  as a wildcard, by preceding it with the reverse-quote [`] "escape" character, i.e.  0`.50  [and similarly in the other such instances].

Kent Cooper, AIA
Message 18 of 19

larsr2866
Enthusiast
Enthusiast

I figured it out 🙂

It was the special character ~

Now it works like a charm!

Thank you all for your help!

 

Lars

0 Likes
Message 19 of 19

TomBeauford
Advisor
Advisor

Oops! replied to wrong post. Nevermind.

64bit AutoCAD Map & Civil 3D 2023
Architecture Engineering & Construction Collection
2023
Windows 10 Dell i7-12850HX 2.1 Ghz 12GB NVIDIA RTX A3000 12GB Graphics Adapter
0 Likes