Jump to content
Developer Wiki and Function Reference Links ×

Script to toggle class + layer on and off


Christiaan

Recommended Posts

Try this.

 

Procedure Toggle;
{If either layer or class specified in Consts is invisible, then sets}
{both to visible. If both are visible then sets both to invsible.}
{Change the LName and CName in the Const section to match your desired}
{class and layer. Note code at bottom to cause proper screen and Nav}
{palette updates.}

{Warning: Use of this script may cause diziness. Alcohol may make this}
{worse. Use care when operating a locamotive while using this script.}
{If you are pregnant or considering becomeing pregnant, consult with }
{CAD Manager before using this script. Self inflicted hair loss is a }
{frequent side effect of learning to script. If script does not operate}
{as intended, Do not pass GO, Do not collect $200. Use at your own risk.}
{There be dragons. ARRRGH!}

{November 12, 2018}
{©2018 Patrick Stanford pat@coviana.com}
{Licensed under the GNU Lesser General Public License}

Const
	LName='1-Slab';
	CName='Structural-Slab';
Var
	LHand		: Handle;
	LVis, CVis	: Integer;
	
Begin
	LHand:=GetLayerByName(LName);
	LVis:=GetObjectVariableInt(LHand, 153);
	CVis:=GetCVis(CName);
	
	If (LVis + CVis) = 0 then  {If both layer & class visible}
		Begin
			SetObjectVariableInt(LHand, 153, -1);  {Layer to invisible}
			ResetObject(LHand);
			HideClass(CName);  {Class to invisibile}
		End
	Else
		Begin
			SetObjectVariableInt(LHand, 153, 0);  {Layer to visible}
			ResetObject(LHand);
			ShowClass(CName);  {Class to visible}
		End;
		RedrawAll;   {For redraw to show hide objects with changed vis}
		Layer(GetLName(ActLayer));  {Force layer to active layer to update Nav Palette}
End;

Run(Toggle);

 

  • Like 1
Link to comment

I have no idea why it effects project sharing. I have never used the project sharing. My guess is that it is the last line of the script 

 

Layer(GetLName(ActLayer)); {Force layer to active layer to update Nav Palette}

 

This sets the active layer to the active layer to force a redraw of the Nav Palette.  Try moving the left brace "{" to the beginning of the line to comment it out and see if that fixes the project sharing issue.


As for the Message block, just delete the line that reads MEssage(DAte(2,2), LVis);  This was for debugging and I missed removing it.  (I have edited the script above to remove this. You could just grab it from there again instead.)

 

Edited by Pat Stanford
Update on editing earlier script
Link to comment

Its to do with the SetObjectVariableInt(LHand, 153, -1); {Layer to invisible}; you're actually applying the change to the whole layer (LHand).

What I've done in the past is to use the ShowLayer, HideLayer, GrayLayer  calls.

 

So your script would be instead:

		Begin
			{SetObjectVariableInt(LHand, 153, -1);  {Layer to invisible}}
			Layer(GetLName(LHand)); {Jump to that particular layer}
			HideLayer;
			HideClass(CName);  {Class to invisibile}
		End

 

  • Like 1
Link to comment

Here it is in python:

# November 13, 2018
# Python converted from Patrick Stanford's script:
# https://forum.vectorworks.net/index.php?/topic/59673-script-to-toggle-class-layer-on-and-off/&do=findComment&comment=297904

import vs

LName = '1-Slab'
CName = 'Structural-Slab'

LayerHandle = vs.GetLayerByName(LName)
LayerVisibility = vs.GetObjectVariableInt(LayerHandle, 153)
ClassVisibility = vs.GetCVis(CName)

CurrentLayer_Name = vs.GetLayerByName(vs.ActLayer())

if any([LayerVisibility == 0, ClassVisibility == 0]):
    vs.Layer(LName)  # Jump to/Set Active layer name in LName variable
    vs.HideLayer()
    vs.HideClass(CName)

else:
    vs.Layer(LName)  # Jump to/Set Active layer name in LName variable
    vs.ShowLayer()
    vs.ShowClass(CName)

vs.Layer(CurrentLayer_Name)  # Jump Back to/Set Active current layer name

 

Edited by twk
  • Like 1
Link to comment

Woah!. apologies, i had edited that post.. strange the edit didnt go through..

the currentLayer_name call is incorrect

it should be this:

# November 13, 2018
# Python converted from Patrick Stanford's script:
# https://forum.vectorworks.net/index.php?/topic/59673-script-to-toggle-class-layer-on-and-off/&do=findComment&comment=297904

import vs

LName = '1-Slab'
CName = 'Structural-Slab'

LayerHandle = vs.GetLayerByName(LName)
LayerVisibility = vs.GetObjectVariableInt(LayerHandle, 153)
ClassVisibility = vs.GetCVis(CName)

CurrentLayer_Name = vs.GetLName(vs.ActLayer())#Get Layer Name of current/active layer

if any([LayerVisibility == 0, ClassVisibility == 0]):
    vs.Layer(LName)  # Jump to/Set Active layer name in LName variable
    vs.HideLayer()
    vs.HideClass(CName)

else:
    vs.Layer(LName)  # Jump to/Set Active layer name in LName variable
    vs.ShowLayer()
    vs.ShowClass(CName)

vs.Layer(CurrentLayer_Name)  # Jump Back to/Set Active current layer name

 

  • Like 1
Link to comment

theres something weird going on with the formatting, and my posts on this thread; It looks like their are hidden spaces being added between the s and ( in vs.GetCVis(CName))

ClassVisibility = vs.GetCVis(CName)

if you copy in the code above, or type it in manually it should work. (Replace this line in the script, with your manually typed one, or the snippet above)

Edited by twk
  • Like 1
Link to comment
  • 3 years later...

Could one of you guys help? I want to add a 'Show/Hide NonPlot' menu command to my Document Context menu (right-click) or keyboard shortcut. Can I do this by modifying @twk's last script above by removing the LName = '1-Slab'  line + changing the class line to CName = 'NonPlot', then using the 'Vectorscript Plug-in Manager...' command to create a VS Menu command plug-in...? Or will this not work because it's Python...?

 

Sorry I am utterly ignorant of these things! I just want a quick + easy way to turn the 'NonPlot' class on + off on the design layer whilst modelling. I have millions of Loci bound up in symbols: I want to see them when placing the symbol but not afterwards. And it's a pain finding NonPlot in the Navigation palette each time. Am not confident ploughing into this without someone pointing the way!

 

Thanks

Link to comment

@Tom W. it's super easy if you are willing to have 2 different scripts rather than toggle visibility. I use this method all the time.

 

"Show" script - ShowClass('NonPlot');

"Hide" script -  HideClass('NonPlot');

 

You can create plug-ins with these simple scripts and give yourself menu items. If you have more than one class to show/hide at a time ("NonPlot-Loci" for instance), you can add additional lines to the script. Make sure to use the semicolon at the end of each line.

  • Like 2
Link to comment

Or you can use a single line that will toggle between Visible and Invisible and have the added benefit of making a class that is Grey first visible then being able to toggle it.

 

If GetCVis('NonPlot')=0 then HideClass('NonPlot') else ShowClass('NonPlot');

 

Go to Tools:Plugins:Plugin Manager

Click the New button

Make a Command type PIO and click the OK button

Click the Edit Script button and paste the above into the editor window.

Click OK

Click Close

 

Edit your workspace Find Document Context in the right windows. Find the new PIO you created in the left window. Probably under Miscelaneous. Drag it to the right window. Click OK to save and reload the workspace.

 

HTH. Ask again if you need more information.

 

  • Like 2
Link to comment

I actually understand this!

 

I also find having to return to the nav palette to turn classes off a pain and typically before we export a file we like to turn off all the "NonPlot" classes. It would be nice to have one button that does it all.

 

I have a whole bunch of classes with "NonPlot" in the class name. Is it possible to have one script that will toggle them all off if any of them are on but turn them all on if they are all off. Also if I create a new class with "NonPlot" in the name can it also work on this class without changing the script?

Link to comment

It gets more complicated to handle multiple classes.  This example toggles each class whose name contains 'NonPlot' in the file. Each class is toggled individually, so if you want them to all react the same, they all need to be in the same state to begin.

 

It gets even more complicated if you want to get only classes that begin or end with a given string.

 

Set the portion of the name you want to match between the single quote marks in the CONST CString='NonPlot';  line. Only change what is between the quotes.

 

Procedure ToggleMultiClasses;
{February 2, 2022}
{©2022 Patrick Stanford pat@coviana.com}
{Licensed under the GNU Lesser General Public License}

{No Warranty Expressed of Implied. Use at your own risk.}

{Toggles the class visibility of each class whose name contains the}
{string defined in the constant CString below.}
{Each class is toggles individually, so if you want all classes}
{to be turned off, they all must be the same before running the toggle.}

CONST	CString='NonPlot';

VAR		L1, L2:LongInt;
		S1:String;

BEGIN
	L1:=ClassNum;
	For L2:= 1 to L1 DO
		BEGIN	
			S1:=ClassList(L2);
			If Pos(CString, S1)<>0 then 
				If GetCVis(S1)=0 then HideClass(S1) else ShowClass(S1);
		End;
End;

Run(ToggleMultiClasses);

 

  • Like 1
Link to comment
11 hours ago, Pat Stanford said:

Or you can use a single line that will toggle between Visible and Invisible and have the added benefit of making a class that is Grey first visible then being able to toggle it.

 

If GetCVis('NonPlot')=0 then HideClass('NonPlot') else ShowClass('NonPlot');

 

Go to Tools:Plugins:Plugin Manager

Click the New button

Make a Command type PIO and click the OK button

Click the Edit Script button and paste the above into the editor window.

Click OK

Click Close

 

Edit your workspace Find Document Context in the right windows. Find the new PIO you created in the left window. Probably under Miscelaneous. Drag it to the right window. Click OK to save and reload the workspace.

 

HTH. Ask again if you need more information.

 

 

Thanks so much Pat but I've tried it + it's not working...? I followed exactly the same process of creating a Plug-in Menu command + assigning it to my right mouse click as I did with the ShowClass('NonPlot'); and HideClass('NonPlot'); scripts but whilst they work your one doesn't...

 

1411271095_Screenshot2022-02-03at07_25_15.thumb.png.360b6b26a09c3d37d5e3a684b8675124.png

 

2090896159_Screenshot2022-02-03at07_20_42.thumb.png.356553c17bc0ab19899030b8079142a1.png

 

Does it work at your end? Thanks!

 

What does If GetCVis('NonPlot')=0 mean in layman's terms?

 

Link to comment

@Tom W.Four things to check as the troubleshooting.

 

1. Is your class name exactly 'NonPlot'?  No space at the end. No underscore or dash between.

2. Rather than as a PIO just go to the resource manager and create a new script. Paste the single line script into there and run it. Does it work?

3. When you pasted the script into the PIO script edit window at the top was a pull down to choose Vectorscript or Python. The code I sent is Vectorscript.

4. Can you send me a blank file with your classes in it so I can test here?

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...