LSP/SCR button for selecting and lock all XREFs

LSP/SCR button for selecting and lock all XREFs

Anonymous
Not applicable
2,725 Views
16 Replies
Message 1 of 17

LSP/SCR button for selecting and lock all XREFs

Anonymous
Not applicable

I would like to add a button to my custom tab that selects all XREF layers in a drawing, locks them and also sets their transparency to 50, though I'm fairly new to scripts and coding and still struggling with the basics.

 

Alternatively; two different buttons, one for selecting and locking all XREFs and one for setting all XREF layers transparency to 50.

 

I might also want to create custom commands for the two functions, like LOCKALLXREFS/XREFTRANSPARENCY.

 

I know how to make custom buttons that calls a .scr/.lsp-file, but not the correct coding method for the script itself.

 

Could I get some help with this?

0 Likes
Accepted solutions (3)
2,726 Views
16 Replies
Replies (16)
Message 2 of 17

dlanorh
Advisor
Advisor

Attached is a Lisp that will lock all xref layers and set transparency to 50.

 

I'll leave the button to you as i'm useless at that sort of thing

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

Message 3 of 17

Anonymous
Not applicable

Hmm, the script doesn't seem to work. When I run it as an LSP nothing happens, and when I convert it into an .scr-file and run it I just get this error message (V57-P0200 is the name of the drawing FYI):

CAD_error_message_scr_2018-05-24_13-27-19.png

0 Likes
Message 4 of 17

dlanorh
Advisor
Advisor
First, it cannot be run as a script, it must be run as a lisp. You shouldn't see anything happen it does it all in the background. It works for me in 2012 with attached xrefs.

Do your xref layers have a "|" in the layer name?

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

0 Likes
Message 5 of 17

Anonymous
Not applicable

Yes, all of them have the "|" in the layers list.

 

Thanks, I seem to have gotten it to work now, except that it works very slowly. The cursor flickers for a good long while before completing the task. Doing it manually by opening Layer Options and selecting all layers in the XREF list and then clicking the 'lock' symbol / changing the transparency for all selected layers is much faster.

 

I think it's these lines that slow everything down:

(vla-put-lock lyr :vlax-true)
(setq ent (vlax-vla-object->ename lyr))

 

Is there any way to use something like

(COMMAND "_.layer" "_lock" (strcat xname "*") "")

instead? I tried replacing it, but it didn't work.

 

This is how my lsp looks now:

(defun c:lockallxrefs (/ *error* c_doc c_lyrs)

	(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 "\nOops an Error : " msg " occurred.")))
		(princ)
	)

	(setq c_doc (vla-get-ActiveDocument (vlax-get-acad-object))
        c_lyrs (vla-get-layers c_doc)
	)

	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(vla-startundomark c_doc)

	(vlax-for lyr c_lyrs
		(if (wcmatch (vla-get-name lyr) "*|*" )
			(progn
				(vla-put-lock lyr :vlax-true)
        			(setq ent (vlax-vla-object->ename lyr))
				(COMMAND "_layer" "_tr" 50 (vla-get-name lyr) "")
			)
		)
	)
	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(princ)
)

 

0 Likes
Message 6 of 17

Anonymous
Not applicable

Also, I noticed that while 99% of the XREF:s we use get the "|" in their names in the layers list, some of them (that are imported using a colleague's script) instead get the letters "XR-" followed by the file name as a new layer. Is it possible to include the "XR-" as well as the "|" when selecting XREF layers to be locked?

0 Likes
Message 7 of 17

dlanorh
Advisor
Advisor
Accepted solution

@Anonymous wrote:

Yes, all of them have the "|" in the layers list.

 

Thanks, I seem to have gotten it to work now, except that it works very slowly. The cursor flickers for a good long while before completing the task. Doing it manually by opening Layer Options and selecting all layers in the XREF list and then clicking the 'lock' symbol / changing the transparency for all selected layers is much faster.

 

I think it's these lines that slow everything down:

(vla-put-lock lyr :vlax-true)
(setq ent (vlax-vla-object->ename lyr))

 

Is there any way to use something like

(COMMAND "_.layer" "_lock" (strcat xname "*") "")

instead? I tried replacing it, but it didn't work.

 

This is how my lsp looks now:

(defun c:lockallxrefs (/ *error* c_doc c_lyrs)

	(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 "\nOops an Error : " msg " occurred.")))
		(princ)
	)

	(setq c_doc (vla-get-ActiveDocument (vlax-get-acad-object))
        c_lyrs (vla-get-layers c_doc)
	)

	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(vla-startundomark c_doc)

	(vlax-for lyr c_lyrs
		(if (wcmatch (vla-get-name lyr) "*|*" )
			(progn
				(vla-put-lock lyr :vlax-true)
        			(setq ent (vlax-vla-object->ename lyr))
				(COMMAND "-layer" "_tr" 50 (vla-get-name lyr) "")
			)
		)
	)
	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(princ)
)

 Remove the red line, it's not needed, my text editor must have been out of sync with VLIDE

change "_layer" in command line to "-layer" (green text). This was probably causing the flicker as it was trying to load a dialog which would have slowed it down.

 

If it's still slow you can try changing

(vla-put-lock lyr :vlax-true) to (command "-layer" "_lo" (vla-get-name lyr) "")

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

Message 8 of 17

Anonymous
Not applicable

Thanks a lot! Smiley Very Happy

 

It's still a little slow, but it's also a big drawing with many XREF:s, so that might be why.

 

 

(vla-put-lock lyr :vlax-true)

actually worked faster than

(command "-layer" "_lo" (vla-get-name lyr) "")

 

Did you see my other reply regarding adding "XR-" as another premise in addition to "|"? Can I just add somthing like an OR-command somewhere?

0 Likes
Message 9 of 17

dlanorh
Advisor
Advisor

@Anonymous wrote:

Did you see my other reply regarding adding "XR-" as another premise in addition to "|"? Can I just add somthing like an OR-command somewhere?


Sorry missed it.

I'm going to be away from the computer for about and hour, post an example layer name using XR and i'll look when i return.



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

0 Likes
Message 10 of 17

Anonymous
Not applicable

It's not actually an XREF layer in itself, but an automatically created layer to which all imported XREFs are sent when imported using my colleague's script. As it is never used for anything other than XREFs I might as well lock that layer too with my script. The script simply adds "XR-" at the beginning of the filename of the imported .dwg.

XR-example_2018-05-24_15-08-15.png

 

I want to lock as many unused layers as possible mainly because I use another script called LOCKSELECTION, which makes it so that a locked layer cannot be selected or interacted with other than with OSNAP. It makes creating, moving and selecting objects much easier as the XREFs are then just backdrops for the drawing.

0 Likes
Message 11 of 17

dlanorh
Advisor
Advisor
Accepted solution

Attached is revised lisp to handle xref layers and "XR-" layers.

 

Any problems post back here or PM me

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

Message 12 of 17

Anonymous
Not applicable

Perfect! Thanks!

0 Likes
Message 13 of 17

Anonymous
Not applicable

I was thinking of ways to optimize the performance of the lsp. Everytime I click the button the script goes through every layer that fits the parameters to yet again lock it and set its transparency to 50. Maybe I can add some lines to make the script ignore layers that are already locked and/or has Transparency=50, so that it doesn't stack and slows down more every time I add a new XREF?

 

Additionally, I'm trying out functions that sets the XREF Transparency to 75 if its filename begins with "V57-". I'd like to expand this function so that if my current drawing's filename begins with "57-", it sets all XREFs with the same prefix to Transparency 75, but if the current drawing has the prefix "V50-", it instead sets all the XREFs with that prefix to Transparency 75.

 

Sorry for making this more complex, but I realized a lot of time-saving possibilities with this. Smiley Happy

 

This is how my code looks at the moment:

(defun c:lockallxrefs (/ *error* c_doc c_lyrs)

	(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 "\nOops an Error : " msg " occurred.")))
		(princ)
	)

	(setq c_doc (vla-get-ActiveDocument (vlax-get-acad-object))
        c_lyrs (vla-get-layers c_doc)
	)

	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(vla-startundomark c_doc)

	(vlax-for lyr c_lyrs
		(if (or (wcmatch (vla-get-name lyr) "*|*" ) (wcmatch (vla-get-name lyr) "XR-*" ) (wcmatch (vla-get-name lyr) "XREF" ))
			(progn
				(vla-put-lock lyr :vlax-true)
				(COMMAND "-layer" "_tr" 50 (vla-get-name lyr) "")
			)
		)
		(if (and (wcmatch (vla-get-name lyr) "*|*" ) (wcmatch (vla-get-name lyr) "V57-*" ))
			(progn
				(vla-put-lock lyr :vlax-true)
				(COMMAND "-layer" "_tr" 75 (vla-get-name lyr) "")
			)
		)
	)
	(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
	(princ)
)
0 Likes
Message 14 of 17

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

<snip>

(vla-put-lock lyr :vlax-true)

actually worked faster than

(command "-layer" "_lo" (vla-get-name lyr) "")

 <snip>


As to be expected. I was actually surpised at the suggestion.

 

The (command ...) function is actually an extra "layer" around the supplied function(s).

Where as the VLA- function is more direct.

 

Using command is going to be almost always slower as any autolisp, vba or activex function will be. Unless you need a whole bunch of 'm to do the same as 1 command.

 

Also note that, although not always possible due to limitations, using (command-s ...) instead of (command ...) is significantly faster.

Message 15 of 17

dlanorh
Advisor
Advisor
Accepted solution

Attached is revised code.

 

I've taken the liberty of optimising your code. Conditionals slow up the execution, so the less the better. The main loop now checks if the layer is already locked, if it is it moves on to the next layer. I've also incorporated your second (if) group into the first and inserted a condition (cond) function for the various transparencies. This will make it easier to add further when you require them. I've provisionally added the (wcmatch ....."V57-*") to the (or), but I don't think it is needed; as you had it as part of an (and) along with (wcmatch..."*|*"). If all "V57-*" layers contain a "|" then it's not needed. I've also put some notes/comments into the file. If you read from top to bottom it denotes those that can be removed. There is a question over (wcmatch.."XREF"). Please read the comment at the end of that line.

 

At the bottom of the file is a brief description of the (cond) function, again this can be removed.

 

Happy coding Robot Happy

 

 

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

Message 16 of 17

Anonymous
Not applicable

Thanks a lot!

 

Now it works much faster!

I removed the (wcmatch lyr_name "V57-*" ) from the (if (= (vla-get-lock lyr) :vlax-false) set as you commented in the code, otherwise it works exactly as I wanted it to. Smiley Very Happy

 

Also, you comments are very helpful. Gives a better understanding of functions I'll likely keep using for other scripts.

0 Likes
Message 17 of 17

venturini.anthony
Advocate
Advocate

what would i have to delete from the current lisp to not lock all of the layers. my x-ref layers are already locked and its not a necessary step for me. 

0 Likes