Two failures in one on_click event handler


#1

I’ve noted the nature and location of each failure. Any ideas?

    private void button3_Click(object sender, EventArgs e)
    {
        bButton3 = true;
        string strLayerName = "";
        Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;

        foreach (Rhino.DocObjects.Layer lyr in doc.Layers) <--This works - turns off all layers, except Walls and Text
        {
            if (lyr.Name != "Walls" && lyr.Name != "Text") 
            {
                lyr.IsVisible = false;
                lyr.CommitChanges();
            }
            else
            {
                lyr.IsVisible = true;
                lyr.CommitChanges();
            }
        }
        doc.Views.Redraw();
      
        foreach (DataGridViewRow dgvr in DataGridView1.Rows)
        {
            strLayerName = dgvr.Cells[0].Value.ToString();
            int layer_index = doc.Layers.Find(strLayerName, true);
            if (layer_index >= 0)
            {
                try
                {
                    var file_name = "";

                    doc.Layers[layer_index].IsVisible = true;
                    doc.Layers[layer_index].CommitChanges();

                    var bitmap = doc.Views.ActiveView.CaptureToBitmap(true, true, true);  <---  doc.Layers[layer_index] is not visible
                    // copy bitmap to clipboard
                    Clipboard.SetImage(bitmap);  <-- I don't think I need this, but kept it just in case.

                    // save bitmap to file
                    file_name = doc.Path + "\\Cabinet Labels\\" + strLayerName + ".BMP";
                    bitmap.Save(file_name);   <-- "A generic error occurred in GDI+."

                    doc.Layers[layer_index].IsVisible = false;
                    doc.Layers[layer_index].CommitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

    }

(Menno Deij - van Rijswijk) #2

Maybe your file_name is not correct.

Or the file has been opened by code. Once opened, the file remains locked for the lifetime of the object. Make sure to Dispose of all Bitmap objects after use.


#3

Thx Menno.

That was it on the file_name issue.

Any idea why the layers are not changing state in the first issue?


(Menno Deij - van Rijswijk) #4

I’m not sure where the problem is, but it seems that after you set the layer of layer_index visible, you don’t give a doc.Redraw() in the second half of the code.