Jump to content

vs.Showclass(), vs.Hideclass(), vs.Layer() with wildcards?


Recommended Posts

You will have to script it yourself as all of those commands require an exact name. If you don't use an exact name with Layer() then you will get a new layer created.

 

If you only need the beginning of the name it will be pretty easy to iterate over the layers or classes.

 

Something like (Vectorscript not Python):

 

For N1:= 1 to ClassNum do
	Begin
		S1:=ClassList(N1);
		S2:=Copy(S1,1,2);
		If S2='A-' then ShowClass(S1);
	End;

 

Or for a more general purpose, do the compare using SubString instead of Copy.

 

HTH

 

 

  • Like 1
Link to comment

AFAIK those layer calls cannot use wildcards.

Here's some code for design layers you could use to adapt for classes as well.

 

# First build a list of design layers
def get_design_layers():
    layer_names = []
    layer_handle = vs.FLayer()
    while layer_handle != None:
        layer_type = vs.GetObjectVariableInt(layer_handle, 154) # filter out design layers only. (1 = Design Layer, 2 = Sheet Layer, 3 = Referenced Layer)
        if layer_type == 1:
            layer_names.append(vs.GetLName(layer_handle))
            layer_handle = vs.NextLayer(layer_handle)

    # we now reverse sort the layer_names list. For some reason this method retrieves layers in reverse order of the stack shown in the Navigation Pallette.
    layer_names.reverse()

    return layer_names

# Code for controling visibility of design layers
def set_visibility(design_layer_name:str, layer_visibility:int, document_design_layers:[str]):
    #  0 = Visible, -1 = Hidden, 2 = Grayed
        
    if design_layer_name in document_design_layers: # this line makes sure we don't create a new design layer using next line of code
        vs.Layer(design_layer_name) # only way to activate/jump to the layer we want, this call also creates design layers if the name provided doesnt exist
        # then we apply visibilities
        if layer_visibility == 0:
            vs.ShowLayer()
        elif layer_visibility == -1:
            vs.HideLayer()
        elif layer_visibility == 2:
            vs.GrayLayer()     

# Code for filtering visibility of design layers
def layer_visibility(layer_name:str, layer_visibility:int, document_design_layers:[str]):
    # cycle through design layer names, if layer_name parameter matches any design layer in design layer list, apply layer visibilty

    # Store current layer, this will make sense later
    cur_layer = vs.GetLName(vs.ActLayer())
    
    for design_layer_name in document_design_layers:
        if layer_name in design_layer_name:
            set_visibility(design_layer_name, layer_visibility, document_design_layers)

    # since we've activated/jumped to the design layer we're trying to control visibilty of, we will jump back to our initial design layer, stored in 'cur_layer'
    vs.Layer(cur_layer)

 

  • Like 2
Link to comment

An easy way to search a string for a prefix is to use the vs.Pos() function. Without looking it up there is a Python syntax that does the same.

 

prefix = "A-"
aClassName = vs.ActiveClass()	# or any class name
if (vs.Pos(prefix, aClassName) == 1):
      vs.HideClass(aClassName)


OK I looked it up:

prefix = "A-"
aClassName = vs.ActiveClass()	# or any class name
if (aClassName[0:2] == prefix):	# grab 1st 2 chars and compare
      vs.ShowClass(aClassName)


 

Raymond

 

  • Like 2
Link to comment

My Python is rusty, and I was curious. Here are two simple scripts to Hide and Show classes with a prefix entered by the user.

 

HIDE CLASSES:

import vs
# Prompt user for a prefix, then Hide all classes with that prefix.

prefix = vs.StrDialog("Hide Classes w/ Prefix", "")

ClassL = []
for I in range(0, vs.ClassNum()):	# build list of class names
	ClassL.append(vs.ClassList(I+1))

for aClassName in ClassL:		# test all names in list
	if (aClassName[0:len(prefix)] == prefix):
		vs.HideClass(aClassName)

 

 

SHOW CLASSES:

import vs
# Prompt user for a prefix, then Show all classes with that prefix.

prefix = vs.StrDialog("Show Classes w/ Prefix", "")

ClassL = []
for I in range(0, vs.ClassNum()):	# build list of class names
	ClassL.append(vs.ClassList(I+1))

for aClassName in ClassL:		# test all names in list
	if (aClassName[0:len(prefix)] == prefix):
		vs.ShowClass(aClassName)

 

 

Have fun,

Raymond

  • Like 1
Link to comment

@MullinRJ

At least I chose your code, because it was the simplest one (and adoptable for either classes and layers,...)

 

Now I took another step and instead of using a StrDialog i tried to handle the prefix in a variable as well.

Unfortunately I didn't make it working:

import vs;
vs.SetLayerOptions(5)

prefix = ['.','Keine','WBW']
# this should be the list of all possible prefixes
ClassL = []
for I in range(0, vs.ClassNum()):	# build list of class names
	ClassL.append(vs.ClassList(I+1))

for aprefix in prefix:		# test all prefixes in list
  	vs.AlrtDialog(aprefix) # test, wether the prefix list really works - test passed
	for aClassName in ClassL:		# test all names in list
		if (aClassName[0:len(prefix)] == aprefix):
			vs.ShowClass(aClassName)

Good Idea (i thought), but, somehow only the 'WBW'-Classes have been set visible. I don't understand, why..

 

I also made a test with "vs.AlrtDialog(aprefix)" - and the "for prefix in prefix:"-loop indeed passed all prefixes in the List.

classvisibilities.zip

I attached a/the test file as well.

  • Like 1
Link to comment

Hi @matteoluigi,

   One letter off and the whole thing comes tumbling down. It took me several minutes to spot it, so don't feel too bad.

 

You are getting the length of the LIST "prefix" (always 3), and not the VARIABLE "aprefix".

 

Change this:

        if (aClassName[0:len(prefix)] == aprefix):     # prefix is a List of length 3
To this:

        if (aClassName[0:len(aprefix)] == aprefix):   # aprefix is an element of List prefix

 

and you are good to go. All else looks good.

 

Raymond

 

Edited by MullinRJ
  • Like 1
Link to comment
On 7/9/2022 at 2:09 AM, twk said:

AFAIK those layer calls cannot use wildcards.

yeah, using a wildcard in vs.Layer instead creates a new Layer named *

 

Pity, that there doesn't exist a function like vs.ClassList for Layers as well, sth like "vs.Layerlist" (with all layers, design layers and sheetlayers, of course...) 😉

Link to comment

You can do it, and the code is really not much longer.

 

H1:=FLayer;
While H1<>Nil do
  Begin
	S1:=GetLName(H1);
	{Do Wildcard stuff here putting result of change into a variable S2}
	SetName(H1, S2);
	H1:=NextObj(H1);
  End;

 

And no you are not allowed to ask why you have to use GetLName  to get a layers name and just SetName to change it.

 

Nor are you allowed to ask about why the FLayer command is in the Layers category while NextObj is in the Document List Handling category.

 

😉

  • Like 2
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...