Jump to content
Developer Wiki and Function Reference Links Read more... ×
Christiaan

Script to toggle class + layer on and off

Recommended Posts

Is it possible to have a script that executes the following logic:

 

1. If visibility of either "Design Layer-1" or "Class Name-1" is off, then toggle them both on

2. If visibility of both "Design Layer-1" and "Class Name-1" is on, then toggle them both off

Edited by Christiaan

Share this post


Link to post

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

Share this post


Link to post

haha, warnings heeded, thanks Pat.

 

Couple of things: 

1. Any idea why it checks out (and modifies) the design layer in a Project Sharing file, and how to stop it?

2. How do I stop this VectorScript message appearing?

 

2091965300_ScreenShot2018-11-12at15_26_10.thumb.png.772ca8f048b6c82119f018ab12aec7bf.png

Share this post


Link to post

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

Share this post


Link to post
7 minutes ago, Pat Stanford said:

Try moving the left brace "{" to the beginning of the line to comment it out and see if that fixes the project sharing issue.

 

That didn't work Pat. Curious.

Edited by Christiaan

Share this post


Link to post

Then it must be the changing of the visibility requires project sharing access. I don't think there is anything I can do about that as the script does not specifically ask for access. It does not seem to me (with zero experience in project sharing) that you should need to own the layer to change the visibility.

 

 

 

Share this post


Link to post
16 minutes ago, Pat Stanford said:

It does not seem to me ... that you should need to own the layer to change the visibility.

 

That's correct, you don't. It must be something peculiar about the VS command and PS.

Share this post


Link to post

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

Share this post


Link to post

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

Share this post


Link to post

Awesome, cheers for that. Does the trick but it also gives the following errors and it's creates a new design layer each time.

 

1724463632_ScreenShot2018-11-13at11_09_55.thumb.png.eae81665026995d8bfc137d924c3de50.png

 

1066489133_ScreenShot2018-11-13at11_12_44.thumb.png.0a603091cbef30f32ac0cda86f00c642.png

 

 

Edited by Christiaan

Share this post


Link to post

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

Share this post


Link to post

Thanks @twk I was being lazy and hoped to not have to switch the active layer to be able to use the Show/Hide/GrayLayer options.

 

Good catch, but I am a little surprised that the Show/Hide/Grey don't do exactly the same as the pref does under the hood.

Share this post


Link to post

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

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×