Working through issues in my previous forum topic, I now wish to toggle the DisplayConduit within Grasshopper. If I open my .gh file with the toggle (closeViz input) set as false(Off), the DisplayConduit is Enabled = false; and the ViewPort does not show the Brep under evaluation. When I switch the toggle On, it shows and scales the Brep ReVesselShape correctly. However, when I am done with scaling and satisfied with the result, I want to close this DisplayConduit and not show the Brep.
(The purpose of this step is to confirm that the shape fits inside the block of material to be processed by CNC. I hope to use this technique at other stages of developing GCode to confirm that process parameters are correct and reasonable.)
Please see the relevant code below:
What code disposes or closes the DisplayConduit?
Thanks ahead for your advice.
Dave
After covering much C# ground, I have found a dependable solution to toggling a DisplayConduit in the context of a Grasshopper Custom Component. Though there are plenty of great examples on GitHub to toggle off a DisplayConduit while in a pure Rhino environment, (set the Enabled = false method and then redraw the doc views), this alone did not work for my circumstance. There are other forum examples using Grasshopper’s DrawViewportWires(…) or DrawViewportMeshes(…), but these were not helpful for me as the items to be previewed must be part of the DA logic and therefore must be inputs to the component; not variables from a referenced dll source.
Using a ‘IterationCt’ property, the solution involved tracking when a DisplayConduit was first instanced by the component, (Other DisplayConduits may also be active. In my case the SuperHelper plugin creates a DisplayConduit by default.) Multiple DisplayConduits are referenced in a private static dictionary called _enabledConduits. Only setting Enabled to false followed by doc.Views.Refresh(), will not clear all the DisplayConduits. Using Reflection, I was able to access _enabledConduits. The following sequence did the magic:
- Track when the first DisplayConduit is instanced by the component. (There may be more DisplayConduits in play.)
When the Component input closeViz (or property CloseVis) is set to false.
2) clear the _enabledConduits dictionary
3) set Enabled = false;
4) call doc.Views.Refresh();
For those that might be interested the code is as follows:
As I am still quite inexperienced with C#, this was my first experience with Reflection. Sharing my code with ChatGBT helped me understand how to use Reflection to reach a private field within the DisplayConduit class. What an exciting hour of learning!
Here is a gif showing my intended design.
Any advise toward improvement in my code is greatly appreciated.
Dave