Seting a Brep color

Hi all,

I am quite new to grasshopper plugins development, so some of my codes might sound awkward. Using a C# component I create a Brep (myBrep). Now I want to set its color. In my final project, I will have a collections of Breps, each of them having its own color, and then each will have their color accordingly set, before being merged. I suspect that the method below is not the right one, or the most appropriate one.

Color couleur = System.Drawing.Color.FromArgb(10, 23, 30); // color definition
Rhino.Display.DisplayMaterial material = new Rhino.Display.DisplayMaterial(couleur); // material definition
Rhino.Display.DisplayPipeline.DrawBrepShaded(myBrep, material); // this is were things crash

Could you please tell me how can I solve this?

Thank you in advance.

Hi Germain
You can click on this eyes,Use it to rewrite the display mode.like this


I hope this help you.
——NARUTO

Hi Naruto. Thank you very much for your help, I just tried it for a single brep and it worked fine.
I will now try to do it with a collections of Brep with their corresponding color.

Many thanks.

Hello,

I am now trying to do this within a single code block (the private void RunScript(***) one). I tried to copy the code from the public override void DrawViewportWires(IGH_PreviewArgs args) function but it didn’t work.
Any suggestion?

Thank you in advance.

Drawing custom stuff in a Script component typically involves (1) clearing all caches at the start of the solution, (2) slowly building the caches during successive calls to RunScript() and (3) drawing the caches by overriding the DrawViewportWires() or DrawViewportMeshes() or both methods.

Click on the green/red arrow button to insert the methods that are called before the first invoke of RunScript and after the last invoke of RunScript. This is where you clear your class-level cached data.

Then during each call to RunScript() you add more data to the caches. It sounds like you need to cache breps with shading meshes and DisplayMaterials. You will also have to make sure that you compute a boundingbox for all the geometry you wish to draw.

Finally click on the Eye icon to insert the preview method overrides. Return your total boundingbox from the ClippingBox property and then draw your stuff inside the appropriate DrawViewportXXXX() method.

See: drawbrepswithcolours.gh (193.4 KB)

Hello David,

Thank you very much for your so valuable help.

I just opened the file you sent, and it sounds very great. I will read the code carefully to make sure I understood everything so that I can redeploy it otherwise for my specific project.

I don’t know whether it is worth mentioning, but just in case: I am doing the scripting using the C# component, just as a way to rapidly test and validate codes before developing a component which will be part of a home-grown grasshopper plugin. Basically that component, will - among others - call a function like void DisplayBreps( List <Breps_With_CorespondingColors> breps). So I will have to write a function/method version of the code you just sent me.

Once again, thank you very much for your help.

It’ll work a bit different in a GH_Component derived class, but the basic principle is the same. Just find the proper methods to override, they’ll have similar names.

@DavidRutten

Hi David, this is an old post but still very useful indeed.

How can the transparency be adjusted? In other words, have various components that output various color breps, but with the same transparency setting as the default grasshopper breps(Transparent red)

I have several components I developed as VS C# components and overwrote the “BeforeSolveInstance”, “AfterSolveInstance”, “DrawViewportMeshes” and “DrawViewportWires” methods. In the DrawViewportMeshes I set the DisplayMaterial(Color.Gold, 0.75), but this transparency does not seem to render properly.

More concerning is that it’s duplicating the breps for some reason. If I remove the override methods I am back to single breps again.

public class ColumnComponent : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the ColumnComponent class.
        /// </summary>
        public ColumnComponent() : base("Column", "C", "Import column elements", "Link", "Link")
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Content", "C", "Content of the file.", GH_ParamAccess.list);
            pManager.AddTextParameter("Tag", "Tag", "tag.", GH_ParamAccess.item);
            pManager.AddNumberParameter("Relative Height", "RL", "Height (Z) of level.", GH_ParamAccess.item);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddBrepParameter("Brep", "Brep", "Brep of Beam Element", GH_ParamAccess.list);
            pManager.AddCurveParameter("Curve", "C", "Line from start point to end point", GH_ParamAccess.list);
            pManager.AddTextParameter("Profile", "P", "Beam size (profile)", GH_ParamAccess.list);
            pManager.AddNumberParameter("Rotation", "R", "Column rotation", GH_ParamAccess.list);
        }

        private readonly List<Brep> Breps = new List<Brep>();
        private BoundingBox _clippingBox;

        /// <summary>
        /// BeforeSolveInstance method
        /// </summary>
        protected override void BeforeSolveInstance()
        {
            foreach (var brep in Breps)
            {
                brep.Dispose();
            }
            Breps.Clear();
            _clippingBox = BoundingBox.Empty;
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //Column collection
            List<Column> Columns = new List<Column>();

            //Declare input variables.
            List<string> contents = new List<string>();
            string tag = string.Empty;
            double relativelevel = double.NaN;

            //Check if input parameters are not null
            if (!DA.GetDataList("Content", contents)) return;
            if (!DA.GetData("Level Tag", ref tag)) return;
            if (!DA.GetData("Relative Level", ref relativelevel)) return;

            Columns = GetData.GetColumns(contents, tag, relativelevel);

            //Declare output variables
            List<Line> Curves = new List<Line>();
            List<string> ProfileStrings = new List<string>();
            List<double> Rotations = new List<double>();

            if (Columns != null)
            {
                foreach (var column in Columns)
                {
                    Brep brep = column.CreateBrep();
                    Breps.Add(brep);

                    Line line = column.CreateColumnAxis();
                    Curves.Add(line);

                    string profilestring = column.CreateProfileString();
                    ProfileStrings.Add(profilestring);

                    double rotate = column.Theta;
                    Rotations.Add(rotate);
                }
            }
            else
            {
                return;
            }
            //Assign output variables to output parameters
            DA.SetDataList("Brep", Breps);
            DA.SetDataList("Curve", Curves);
            DA.SetDataList("Profile", ProfileStrings);
            DA.SetDataList("Rotation", Rotations);
        }

        /// <summary>
        /// AfterSolveInstance method
        /// </summary>
        protected override void AfterSolveInstance()
        {
            _clippingBox = BoundingBox.Empty;
            foreach (var brep in Breps)
            {
                _clippingBox.Union(brep.GetBoundingBox(false));
            }
        }

        /// <summary>
        /// Return the unified bouding box
        /// </summary>
        public override BoundingBox ClippingBox
        {
            get { return _clippingBox; }
        }

        /// <summary>
        /// DrawViewportMeshes to override the color of the brep and brepwires
        /// </summary>
        /// <param name="args"></param>
        public override void DrawViewportMeshes(IGH_PreviewArgs args)
        {
            DisplayMaterial brepShadeMaterial = new DisplayMaterial(Color.DarkBlue, 0.5);
            foreach (var brep in Breps)
            {
                args.Display.DrawBrepShaded(brep, brepShadeMaterial);
                args.Display.DrawBrepWires(brep, Color.DarkBlue);
            }
            brepShadeMaterial.Dispose();
        }

        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon
        {
            get
            {
                //You can add image files to your project resources and access them like this:
                // return Resources.IconForThisComponent;
                return Properties.Resources.Ghc_Column_Icon;
            }
        }

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid
        {
            get { return new Guid("ef596c29-1d26-4b2d-ab76-ea11cc842792"); }
        }
    }