Author Topic: Issues with a Palette in ZWCAD  (Read 3331 times)

0 Members and 1 Guest are viewing this topic.

velasquez

  • Newt
  • Posts: 195
Issues with a Palette in ZWCAD
« on: November 03, 2023, 08:55:59 AM »
I'm trying to convert Gile code to ZWCAD.
https://gilecad.azurewebsites.net/UserInterfaces_en.aspx#paletteWinForm

But I found a problem with DocumentManager,
look at this "automatically hide the palette while none document is active (no document state)"

The Palette is hidden in this condition but is not shown along with a new drawing is created.
Can anyone help me with this?
Thanks

Below are the Gile codes that I modified:

Commands.cs
Code: [Select]
using System.Windows;
using ZwSoft.ZwCAD.Runtime;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(ZwCadUISample.Palette.Commands))]

namespace ZwCadUISample.Palette
{
    internal class Commands
    {
        // static field
        static CustomPaletteSet palette;

        /// <summary>
        /// Command to show the palette.
        /// </summary>
       
        [CommandMethod("CMD_PALETTE", CommandFlags.Modal)]
        public void ShowPaletteSet()
        {
            // creation of the palette at the first call of the command
            if (palette == null)
                palette = new CustomPaletteSet();
            palette.Visible = true;
        }
    }
}

CustomPaletteSet.cs
Code: [Select]
using System;
using ZwSoft.ZwCAD.Windows;
using ZwApp = ZwSoft.ZwCAD.ApplicationServices.Application;

namespace ZwCadUISample.Palette
{
    internal class CustomPaletteSet : PaletteSet
    {
        // static field
        static bool wasVisible;

        /// <summary>
        /// Creates a new instance of CustomPaletteSet.
        /// </summary>
        public CustomPaletteSet()
            : base ("Palette", "CMD_PALETTE", new Guid("{D89DDA33-318B-47F5-A264-F7EECDC2A876}"))

        {
            Style =
                PaletteSetStyles.ShowAutoHideButton |
                PaletteSetStyles.ShowCloseButton |
                PaletteSetStyles.ShowPropertiesMenu;
            MinimumSize = new System.Drawing.Size(250, 150);
            Add("ShowTest", new PaletteTab());

            // automatically hide the palette while none document is active (no document state)
            var docs = ZwApp.DocumentManager;
            docs.DocumentBecameCurrent += (s, e) => Visible = e.Document == null ? false : wasVisible;
            docs.DocumentCreated += (s, e) => Visible = wasVisible;
            docs.DocumentToBeDeactivated += (s, e) => wasVisible = Visible;
            docs.DocumentToBeDestroyed += (s, e) =>
            {
                wasVisible = Visible;
                if (docs.Count == 1)
                    Visible = false;
            };
        }
    }
}




gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Issues with a Palette in ZWCAD
« Reply #1 on: November 03, 2023, 10:55:55 AM »
Hi,

You can use event handler methods instead of the lambda expressions so the you can set break points to check the value of the 'wasVisible' static field.
Code - C#: [Select]
  1.         public CustomPaletteSet()
  2.             : base("Palette", "CMD_PALETTE", new Guid("{D89DDA33-318B-47F5-A264-F7EECDC2A876}"))
  3.         {
  4.             Style =
  5.                 PaletteSetStyles.ShowAutoHideButton |
  6.                 PaletteSetStyles.ShowCloseButton |
  7.                 PaletteSetStyles.ShowPropertiesMenu;
  8.             MinimumSize = new System.Drawing.Size(250, 150);
  9.             Add("Cercle", new PaletteTab());
  10.  
  11.             // masquage automatique de la palette quand aucune instance de Document n'est active (no document state)
  12.             var docs = Application.DocumentManager;
  13.             docs.DocumentBecameCurrent += Docs_DocumentBecameCurrent;
  14.             docs.DocumentCreated += Docs_DocumentCreated;
  15.             docs.DocumentToBeDeactivated += Docs_DocumentToBeDeactivated;
  16.             docs.DocumentToBeDestroyed += Docs_DocumentToBeDestroyed;
  17.         }
  18.  
  19.         private void Docs_DocumentBecameCurrent(object sender, DocumentCollectionEventArgs e) =>
  20.             Visible = e.Document == null ? false : wasVisible;
  21.  
  22.         private void Docs_DocumentCreated(object sender, DocumentCollectionEventArgs e) =>
  23.             Visible = wasVisible;
  24.  
  25.         private void Docs_DocumentToBeDeactivated(object sender, DocumentCollectionEventArgs e) =>
  26.             wasVisible = Visible;
  27.  
  28.         private void Docs_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
  29.         {
  30.             wasVisible = Visible;
  31.             if (Application.DocumentManager.Count == 1)
  32.                 Visible = false;
  33.         }
Speaking English as a French Frog

velasquez

  • Newt
  • Posts: 195
Re: Issues with a Palette in ZWCAD
« Reply #2 on: November 03, 2023, 04:08:01 PM »
Hi,

You can use event handler methods instead of the lambda expressions so the you can set break points to check the value of the 'wasVisible' static field.
Code - C#: [Select]
  1.         public CustomPaletteSet()
  2.             : base("Palette", "CMD_PALETTE", new Guid("{D89DDA33-318B-47F5-A264-F7EECDC2A876}"))
  3.         {
  4.             Style =
  5.                 PaletteSetStyles.ShowAutoHideButton |
  6.                 PaletteSetStyles.ShowCloseButton |
  7.                 PaletteSetStyles.ShowPropertiesMenu;
  8.             MinimumSize = new System.Drawing.Size(250, 150);
  9.             Add("Cercle", new PaletteTab());
  10.  
  11.             // masquage automatique de la palette quand aucune instance de Document n'est active (no document state)
  12.             var docs = Application.DocumentManager;
  13.             docs.DocumentBecameCurrent += Docs_DocumentBecameCurrent;
  14.             docs.DocumentCreated += Docs_DocumentCreated;
  15.             docs.DocumentToBeDeactivated += Docs_DocumentToBeDeactivated;
  16.             docs.DocumentToBeDestroyed += Docs_DocumentToBeDestroyed;
  17.         }
  18.  
  19.         private void Docs_DocumentBecameCurrent(object sender, DocumentCollectionEventArgs e) =>
  20.             Visible = e.Document == null ? false : wasVisible;
  21.  
  22.         private void Docs_DocumentCreated(object sender, DocumentCollectionEventArgs e) =>
  23.             Visible = wasVisible;
  24.  
  25.         private void Docs_DocumentToBeDeactivated(object sender, DocumentCollectionEventArgs e) =>
  26.             wasVisible = Visible;
  27.  
  28.         private void Docs_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
  29.         {
  30.             wasVisible = Visible;
  31.             if (Application.DocumentManager.Count == 1)
  32.                 Visible = false;
  33.         }

Hi Gile
I edited the file, but the problem persists.
The palette is not loaded when the last document is closed and a new document is created.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Issues with a Palette in ZWCAD
« Reply #3 on: November 03, 2023, 05:51:41 PM »
Put break points on lines 23 and 29 to inspect the 'wasVisible' field value.
It should save the Visible value when a document is closed (DocumentToBeDestroyed) and restore it when a new document is opened (DocumentCreated)
« Last Edit: November 03, 2023, 06:03:57 PM by gile »
Speaking English as a French Frog

velasquez

  • Newt
  • Posts: 195
Re: Issues with a Palette in ZWCAD
« Reply #4 on: November 05, 2023, 06:52:20 AM »
Hi,

You can use event handler methods instead of the lambda expressions so the you can set break points to check the value of the 'wasVisible' static field.
Code - C#: [Select]
  1.         public CustomPaletteSet()
  2.             : base("Palette", "CMD_PALETTE", new Guid("{D89DDA33-318B-47F5-A264-F7EECDC2A876}"))
  3.         {
  4.             Style =
  5.                 PaletteSetStyles.ShowAutoHideButton |
  6.                 PaletteSetStyles.ShowCloseButton |
  7.                 PaletteSetStyles.ShowPropertiesMenu;
  8.             MinimumSize = new System.Drawing.Size(250, 150);
  9.             Add("Cercle", new PaletteTab());
  10.  
  11.             // masquage automatique de la palette quand aucune instance de Document n'est active (no document state)
  12.             var docs = Application.DocumentManager;
  13.             docs.DocumentBecameCurrent += Docs_DocumentBecameCurrent;
  14.             docs.DocumentCreated += Docs_DocumentCreated;
  15.             docs.DocumentToBeDeactivated += Docs_DocumentToBeDeactivated;
  16.             docs.DocumentToBeDestroyed += Docs_DocumentToBeDestroyed;
  17.         }
  18.  
  19.         private void Docs_DocumentBecameCurrent(object sender, DocumentCollectionEventArgs e) =>
  20.             Visible = e.Document == null ? false : wasVisible;
  21.  
  22.         private void Docs_DocumentCreated(object sender, DocumentCollectionEventArgs e) =>
  23.             Visible = wasVisible;
  24.  
  25.         private void Docs_DocumentToBeDeactivated(object sender, DocumentCollectionEventArgs e) =>
  26.             wasVisible = Visible;
  27.  
  28.         private void Docs_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
  29.         {
  30.             wasVisible = Visible;
  31.             if (Application.DocumentManager.Count == 1)
  32.                 Visible = false;
  33.         }

Hi Gile
I edited the file, but the problem persists.
The palette is not loaded when the last document is closed and a new document is created.

Hi Gile,
I placed the break points, the images show the result with 1 document open.


gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Issues with a Palette in ZWCAD
« Reply #5 on: November 05, 2023, 08:58:11 AM »
You need to understand how the program is supposed to work with this static 'wasVisible' field to be able to diagnose what's wrong with ZwCad.
When a document is about to be closed (DocumentToBeDestroyed) the program stores the current value of the Visible property in the 'wasVisible' field, and if this document is the only one open, the Visible property is set to false to hide the palette.
When a new document is opened (DocumentCreated), the program sets the Visible property to the value stored in the 'wasVisible' field.
So, if the palette is visible when you close the last opened document, 'wasVisible' is set to true and the Palette hides (No document state). When you open a document, the Visible property should be se to true (the value stored in 'wasVisible').
Knowing this, you should be able to diagnose what's wrong with ZwCad.
Speaking English as a French Frog

velasquez

  • Newt
  • Posts: 195
Re: Issues with a Palette in ZWCAD
« Reply #6 on: November 05, 2023, 09:54:34 AM »
You need to understand how the program is supposed to work with this static 'wasVisible' field to be able to diagnose what's wrong with ZwCad.
When a document is about to be closed (DocumentToBeDestroyed) the program stores the current value of the Visible property in the 'wasVisible' field, and if this document is the only one open, the Visible property is set to false to hide the palette.
When a new document is opened (DocumentCreated), the program sets the Visible property to the value stored in the 'wasVisible' field.
So, if the palette is visible when you close the last opened document, 'wasVisible' is set to true and the Palette hides (No document state). When you open a document, the Visible property should be se to true (the value stored in 'wasVisible').
Knowing this, you should be able to diagnose what's wrong with ZwCad.

Hi Gile,
The concept works well when more than one document is open, the problem happens when the last document is closed.
See the image of DocumentCreated, when a second document is created, at this point the wasVisible field and the Visible field are declared true.

I will compare it with AutoCAD to understand better.

velasquez

  • Newt
  • Posts: 195
Re: Issues with a Palette in ZWCAD
« Reply #7 on: November 07, 2023, 06:21:07 AM »
You need to understand how the program is supposed to work with this static 'wasVisible' field to be able to diagnose what's wrong with ZwCad.
When a document is about to be closed (DocumentToBeDestroyed) the program stores the current value of the Visible property in the 'wasVisible' field, and if this document is the only one open, the Visible property is set to false to hide the palette.
When a new document is opened (DocumentCreated), the program sets the Visible property to the value stored in the 'wasVisible' field.
So, if the palette is visible when you close the last opened document, 'wasVisible' is set to true and the Palette hides (No document state). When you open a document, the Visible property should be se to true (the value stored in 'wasVisible').
Knowing this, you should be able to diagnose what's wrong with ZwCad.

Hi Gile,
The DocumentToBeDeactivated event in ZWCAD behaves differently than in AutoCAD.
In ZWCAD it is triggered when the last document is deactivated.
At this point, the value of the Visible field is always false coming from the docs_DocumentToBeDestroyed event.
Code: [Select]
docs.DocumentToBeDestroyed += (s, e) =>
{
    wasVisible = Visible;
     if (docs.Count == 1)
         Visible = false;
};

Includes a condition that resolved this issue.
Now the program behaves as it should.
Code: [Select]
public void Docs_DocumentToBeDeactivated(object sender, DocumentCollectionEventArgs e)
 {
    if (ZwApp.DocumentManager.Count != 1)
     {
         wasVisible = Visible;
     }
 }