Search code examples
c#winformsplotilnumerics

ILNumerics hide PlotCube


I have a scene with two different PlotCubes, which have to be displayed individually. What is the best procedure to hide and show the PlotCubes. I have tried with remove, but this seems to alter the PlotCube-objects.

The code-lines are:

IlPanel1.Scene.add(PlotCube1)  
IlPanel1.Scene.add(PlotCube2)  

Now both cubes are visible. Now I want to show only PlotCube2:

IlPanel1.Scene.Remove(PlotCube1)  
IlPanel1.Scene.add(PlotCube2)

For switching back to PlotCube1:

IlPanel1.Scene.Remove(PlotCube2)  
IlPanel1.Scene.add(PlotCube1)  

But this does not work. The remove statement seems to delete the whole object. Is there a way to add/remove elements as LinePlots, SurfacePlots, PlotCubes without affecting the original object?


Solution

  • Use the Visible property of the plot cube to toogle its visibility:

    // stores the current state. (You may even use one of the ILPlotCube.Visible flags directly)
    bool m_panelState = false;
    
    // give the plot cubes unique tags so we can find them later.
    private void ilPanel1_Load(object sender, EventArgs e) {
        ilPanel1.Scene.Add(new ILPlotCube("plotcube1") {
            Plots = {
                Shapes.Circle100, // just some arbitrary content
                new ILLabel("Plot Cube 1")
            },
            // first plot cube starts invisible
            Visible = false
        });
        ilPanel1.Scene.Add(new ILPlotCube("plotcube2") {
            Plots = {
                Shapes.Hemisphere, // just some content
                new ILLabel("Plot Cube 2")
            }
        });
    }
    // a button is used to switch between the plot cubes
    private void button1_Click(object sender, EventArgs e) {
        m_panelState = !m_panelState;
        SetState(); 
    }
    // both plot cubes are made (un)visible depending on the value of the state variable
    private void SetState() {
        ilPanel1.Scene.First<ILPlotCube>("plotcube1").Visible = m_panelState;
        ilPanel1.Scene.First<ILPlotCube>("plotcube2").Visible = !m_panelState;
        ilPanel1.Refresh(); 
    }
    

    The important part is to call Refresh() on the panel in order for the modifications to show up immediately.

    Note that, in general it is better to keep drawing objects around if you may need them later again. Instead of removing them from the scene graph and later recreating a similar object, setting the object to Visible = false is much faster and does not incur the rather large cost of (re)creating graphical objects.