News:

Happy New Year!

Main Menu

Layer Control across several pages,

Started by Biomedmike, December 18, 2024, 03:12:04 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Biomedmike

Attached is a file I am using to collect the knowledge I have received from this form. I have a new question that is best asked using the attached file.

I have layers on 3 pages. I know that Visio layers are associated to a single page. My question is;

How do I turn layers on or off on additional pages by clicking the layer off using a Checkbox on a single page.

Thank you,

Biomedmike

Thomas Winkel

This is a continuation of the same topics:
https://visguy.com/vgforum/index.php?topic=10580
https://visguy.com/vgforum/index.php?topic=10584

If you can guarantee that you have the same layers with the same indices on all pages:
For Each pge In ActiveDocument.Pages
    SetLayersVisible pge, Array(1, 3, 5, 7, 9, 11), False
Next pge

If not, you have to loop over all layers on each page and check the layer name.

Biomedmike

I am going to show my complete ignorance here and for that I apologize. I put the below code in the attached file and it is only toggling the layer on page 1, not all 4 pages.

Private Sub Layer1Toggle_Click()

If Layer1Toggle Then

For Each pge In ActiveDocument.Pages
Call LayerToggle(1, 1)
Next pge

    Else
For Each pge In ActiveDocument.Pages
    Call LayerToggle(1, 0)
Next pge

    End If
   
End Sub

The module below is also in the file.
Sub LayerToggle(ItemNbr As Integer, OnOff As String)

   'Enable diagram services
   
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Layer Properties")
    Dim vsoLayer1 As Visio.Layer
    Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item(ItemNbr)
    vsoLayer1.CellsC(visLayerVisible).FormulaU = OnOff
    vsoLayer1.CellsC(visLayerPrint).FormulaU = OnOff
    Application.EndUndoScope UndoScopeID1, True

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices


End Sub


Someday I hope I can help someone!
Biomedmike

Thomas Winkel

Quote from: Biomedmike on December 18, 2024, 08:47:39 PMis only toggling the layer on page 1
Of course, LayerToggle only edits the active page:
Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item(ItemNbr)

Why don't you use the functions we've developed in the other threads?
Option Explicit

Private Sub Layer1Toggle_Click()
    Dim pge As Visio.Page
   
    For Each pge In ActiveDocument.Pages
        SetLayersVisible pge, Array(1), Layer1Toggle.Value
    Next pge
End Sub

Biomedmike

As much as I try I am humbled to say I just am not doing something correct.

I believe the attached file is what you are saying will work. However - I am obviously missing something because it is not working for all pages.

Is it possible for you to see what I may be doing wrong?

Humbled, Biomedmike

Thomas Winkel

I have no time to review and debug your code.
But, if you implemented my posting #3, did you also pay attention to my statement from posting #1?

Quote from: Thomas Winkel on December 18, 2024, 04:03:10 PMIf you can guarantee that you have the same layers with the same indices on all pages

If you cannot guarantee that, then either the code will crash, or you switch the wrong layers.

wapperdude

#6
The code does push the value of next page into Module code.  Thus, no other page than initial page experiences show/hide update.

You need to add 3rd input var to Module code, and then in your main code, when you loop thru pages, pass the page.

Also, Module code will fail if the layer number doesn't exist on the page.  Need error catching of some sort.
Visio 2019 Pro

Biomedmike

Thomas and Mr. Wapperdude, THANK YOU BOTH!!!! I finally have it!!!

I will clean up the file and share ot for others to use.

THANKS FOR PUTTING UP WITH ALL MY SILLY Questions!

Bye for now.

Mike Kauffman

Biomedmike

Just when I say "I GOT IT" I am kicked in the butt.

QUESTION: Is there a way to hard code the layerIndices?

If I add more than 1 layer across pages it stops working.

I should have listened to my mom and learned the piano instead of this.

Biomedmike


Thomas Winkel

Quote from: Biomedmike on December 20, 2024, 01:42:05 PMQUESTION: Is there a way to hard code the layerIndices?
The only way is to do all layer operations (add, remove) in exactly the same order on all pages from the start.

You need a new function that addresses layers by name:
Option Explicit

Public Sub SetLayersVisibleByName(vsoPage As Visio.Page, layerNames As Variant, isVisible As Boolean)
    ' Your code here
End Sub

Private Sub TestSetLayersVisibleByName()
    SetLayersVisibleByName ActivePage, Array("LayerX", "LayerY"), False
End Sub

Thomas Winkel

#10
Actually, I think this should already work with the existing function.
Just try it:

Option Explicit

Public Sub SetLayersVisible(vsoPage As Visio.Page, layerIndicesOrNames As Variant, isVisible As Boolean)
    Dim indexOrName As Variant
    Dim vsoLayer As Visio.layer
   
    For Each indexOrName In layerIndicesOrNames
        Set vsoLayer = vsoPage.Layers(indexOrName)
        vsoLayer.CellsC(visLayerVisible) = isVisible
        vsoLayer.CellsC(visLayerPrint) = isVisible
    Next indexOrName
End Sub

Private Sub TestSetLayersVisible()
    SetLayersVisible ActivePage, Array("LayerX", "LayerY"), False
End Sub

Explanation:
* Page.Layers() accepts index or name
* Variant type can hold integer, string, everything...

wapperdude

#11
I haven't looked at Thomas' code, but I am presuming it works and handles multiple layers.  As for your code, I did slight modification to show how either layer name or index value may be used.  If layer name or index value is missing, that will produce error, I believe.  So, as this is simple hide and go seek, as it were,  and there is no other expectation of error, I just did the ol' resume next error method.

However, none of this addresses specific, arbitrary layers.  For that, you need method to select just the layers of interest.  A technique that would work, is to loop thru all layers on active page, use msgbox to select the layer, stuff the selection into an array.  Assume, for now, this list is to be applied to all pages.  Then, loop thru the pages, and on each page, loop thru the array to do show/hide.  This avoids the security issues associated with the UserForm.  Alternatively, modify the UserForm approach I did, to loop thru pages, plus add error detection.
Visio 2019 Pro

Biomedmike

#12
Hi All,

I hope that attached file is helpful to some or many of you. As simple as the code seems, I went completly bald figuring it out.

I must thank Wapperdude, Thomas Winkel and Yacine for all their help. I would have never completed this without them. THANK YOU!

This document allows control of layers across all pages in a document. I am using it to create a Scope of Work that (hopefull someday) will be my single source to develop a Scope for customers to use. I ask lots of questions, their answers are then created in a topology drawing with text that describes the topology.

I will also place a link to the actual drawing in the future - this is simply the generic code I use in my real document.

I could never have completed this without Wapperdude, Thomas and Yacine - Yes, I know I am repeating that but I am so grateful. Take care to all, and to all a good night.

Biomedmike - A Happy Boy!  :)

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 363 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1219KB.
Tokens: post-login.
Cache hits: 13: 0.00152s for 26,543 bytes (show)
Cache misses: 3: (show)
Queries used: 17.

[Show Queries]