# Convert RhinoObjects to Brep

**URL:** <https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828>\
**Category:** Rhino Developer\
**Tags:** windows\
**Created:** [July 1, 2021, 5:20pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828 "2021-07-01T17:20:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 1, 2021, 5:20pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/1 "2021-07-01T17:20:11Z")

</div>

Hi, I am trying to create a solid component from a user defined selection. Following is my code.

```
 var go = new GetObject();
        go.SetCommandPrompt("Select objects ");
        
        go.GroupSelect = true;
        go.SubObjectSelect = false;
        
        for (; ; )
        {
            go.ClearCommandOptions();
            go.AddOptionToggle("CoordinateSystem", ref cs_option);

            var res = go.GetMultiple(1, -1);

            if (res == GetResult.Option)
                continue;
            else if (res != GetResult.Object)
                return Result.Cancel;

            break;
        }

        var n = go.ObjectCount;
        var geom = new Rhino.DocObjects.RhinoObject[n];

        for (var i = 0; i < n ; i++)
        {
            geom[i] = go.Object(i).Object();
           
        }
        var solid1 = Rhino.Geometry.Brep.CreateSolid(geom, RhinoMath.UnsetValue);

```

I start by assigning the variable ‘go’ to the objects selected by the user. Next, I check the number of objects in the selection and run a for loop to create an array called “geom” to be used with the CreateSolid method. This is of type Rhino.DocObjects.RhinoObject. But to create the solid component Rhino.Geometry.Brep is required.

Is there a way to convert RhinoObject to Brep ? Or any other alternative to this?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![mingo1214](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mingo1214/32/509663_2.png) [@mingo1214](https://discourse.mcneel.com/u/mingo1214)\
**Post date:** [July 1, 2021, 5:24pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/2 "2021-07-01T17:24:29Z")

</div>

Hi @raghav.pathak

You can do this to convert all input geometries to Brep.  
`var breps = go.Objects().Select(_ => Brep.TryConvertBrep( _.Geometry()));`

---

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 1, 2021, 5:31pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/3 "2021-07-01T17:31:03Z")

</div>

Hi @mingo1214

Thanks for the reply.

But I am not sure where to put the line of code you mentioned in my code. According to me it should be before the for loop for ‘geom’ array. But it still has the issue of mismatched types RhinoObject and Brep.  
Or I think I can get rid of all the extra loop with this one line.  
Could you help me with this?

---

<div class="post-metadata">

**Author:** ![mingo1214](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mingo1214/32/509663_2.png) [@mingo1214](https://discourse.mcneel.com/u/mingo1214)\
**Post date:** [July 1, 2021, 8:00pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/4 "2021-07-01T20:00:36Z")

</div>

Here is all you need to do after you get user’s input objects.

```auto
var breps = go.Objects().Select(_ => Brep.TryConvertBrep( _.Geometry()));
var solid1 = Rhino.Geometry.Brep.CreateSolid(breps, doc.ModelAbsoluteTolerance);

```

---

<div class="post-metadata">

**Author:** ![Asterisk](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/asterisk/32/480872_2.png) [@Asterisk](https://discourse.mcneel.com/u/Asterisk)\
**Post date:** [July 1, 2021, 8:33pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/5 "2021-07-01T20:33:02Z")

</div>

geom[i] = go.Object(i).Object().Geometry;

Does this work?

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/685184_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [July 1, 2021, 8:46pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/6 "2021-07-01T20:46:44Z")

</div>

Hi @raghav.pathak,

Try something like this:

```auto
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var go = new GetObject();
  go.SetCommandPrompt("Select intersecting surfaces and polysurfaces to automatically trim and join into a closed polysurface");
  go.GeometryFilter = ObjectType.Surface | ObjectType.PolysrfFilter;
  go.SubObjectSelect = false;
  go.GetMultiple(1, 0);
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  var in_breps = new List<Brep>();
  foreach (var objref in go.Objects())
  {
    var brep = objref.Brep();
    if (null == brep)
      return Result.Failure;
    in_breps.Add(brep);
  }

  var out_breps = Brep.CreateSolid(in_breps, doc.ModelAbsoluteTolerance);
  if (null != out_breps)
  {
    foreach (var brep in out_breps)
      doc.Objects.AddBrep(brep);
  }

  doc.Views.Redraw();

  return Result.Success;
}

```

– Dale

---

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 1, 2021, 10:22pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/7 "2021-07-01T22:22:25Z")

</div>

Hi @dale  
Thanks for making the effort.  
This code works perfectly. I have one more issue though. The solid obtained using this code has to be subtracted from another solid obtained from a brep box (which is trivial to obtain). I use the booleandifference method to do this. However, the resulting brep type array has no solid objects.  
Another issue with booleandifference method is that it only works when the 2 objects involved intersect each other.  
I was wondering if there are any other methods to perform this(referring to the A minus B in “Boolean Two Objects” and “Difference” methods in the Rhino Environment).  
Could you please give some idea about this?  
Thanks

---

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 5, 2021, 8:34am UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/8 "2021-07-05T08:34:41Z")

</div>

Hi @dale  
Could you please have a look at the previous thread?  
Thanks

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/685184_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [July 6, 2021, 4:34pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/9 "2021-07-06T16:34:22Z")

</div>

Hi @raghav.pathak,

To be of any help, I’ll need a sample model and your sample source code.

– Dale

---

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 6, 2021, 5:07pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/10 "2021-07-06T17:07:21Z")

</div>

Hi @dale  
Following are the .rhi and .3dm files.

[RhinoTdynBoundingBox2.rhi](https://discourse.mcneel.com/uploads/short-url/b4g954polfCTM8s2z4YeGDKwrCs.rhi) (584.5 KB)  
[Wigley\_hull.3dm](https://discourse.mcneel.com/uploads/short-url/aPbzX7hGHnmC5T1RvhfQ7Dr0A07.3dm) (29.1 KB)

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/685184_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [July 7, 2021, 3:13am UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/11 "2021-07-07T03:13:02Z")

</div>

Hi @raghav.pathak,

Since your surfaces join nicely, calling `Brep.CreateSolid` is a bit of a sledge hammer. Better to use `Brep.JoinBreps`.

Also, `BoundingBox.ToBrep` returns a nice, closed and oriented Brep. Thus, there is no reason to try to create a solid from the faces - as the Brep is already solid.

Not knowing what you are doing with the bounding box, I typed up a sample that demonstrates some of what I talked about.

```auto
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var go = new GetObject();
  go.SetCommandPrompt("Select surfaces");
  go.GeometryFilter = ObjectType.Surface;
  go.SubObjectSelect = false;
  go.GetMultiple(1, 0);
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  var in_breps = new List<Brep>();
  foreach (var objref in go.Objects())
  {
    var brep = objref.Brep();
    if (null == brep)
      return Result.Failure;
    in_breps.Add(brep);
  }

  var joined_breps = Brep.JoinBreps(in_breps, doc.ModelAbsoluteTolerance);
  if (null != joined_breps && 1 == joined_breps.Length)
  {
    var hull_brep = joined_breps[0];

    // Because kinked surface can cause problems down stream, Rhino
    // always splits kinked surfaces when adding Breps to the document. Since
    // we are not adding this Brep to the document, lets split the kinked
    // surfaces ourself.
    hull_brep.Faces.SplitKinkyFaces(RhinoMath.DefaultAngleTolerance, true);

    // The profile curve, created by the input points, is oriented clockwise.
    // Thus when the profile is extruded, the resulting surface will have its
    // normals pointed inwards. So lets check the orientation and, if inwards,
    // flip the face normals.
    if (BrepSolidOrientation.Inward == hull_brep.SolidOrientation)
      hull_brep.Flip();

    var bbox = hull_brep.GetBoundingBox(true);
    var origin = bbox.Corner(true, true, true);
    var sx = (bbox.Corner(false, true, true) - origin).Length / 10.0;
    bbox.Inflate(-sx, 1.0, 1.0); // deflate in x-direction...
    var box_brep = bbox.ToBrep();

    var diff_breps = Brep.CreateBooleanDifference(box_brep, hull_brep, doc.ModelAbsoluteTolerance);
    if (null != diff_breps && 1 == diff_breps.Length)
      doc.Objects.AddBrep(diff_breps[0]);

    doc.Objects.AddBrep(box_brep);
  }

  doc.Views.Redraw();

  return Result.Success;
}

```

Hope this helps.

– Dale

---

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 7, 2021, 9:14am UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/12 "2021-07-07T09:14:59Z")

</div>

Hi @dale  
Thanks for the code  
However, I have one issue which is still persistent. If I change only this part of the code according to my manipulations of the bounding box keeping everything same, the boolean difference still fails.

> [@dale](#):
>
> ```auto
> var origin = bbox.Corner(true, true, true);
> var sx = (bbox.Corner(false, true, true) - origin).Length / 10.0;
> bbox.Inflate(-sx, 1.0, 1.0); // deflate in x-direction...
> 
> ```

I am using this piece of code which is already in the file that I sent before. Here, p1x,p2x etc. are corner points considering various factors which change the lengths at front, side etc.

Rhino.Geometry.Point3d pt0 = new Rhino.Geometry.Point3d(p1x, p1y, p1z);  
Rhino.Geometry.Point3d pt1 = new Rhino.Geometry.Point3d(p2x, p2y, p2z);  
Rhino.Geometry.BoundingBox box1 = new Rhino.Geometry.BoundingBox(pt0, pt1);  
var box\_brep = box1.ToBrep();

But now the boolean difference fails. Could you please tell what could be the reason because I dont think the factor affecting p1x,p2x etc can cause this.

Edit: I just realized both methods work but only when both solids intersect each other. But what I am trying to achieve is that the original geometry is subtracted from the bigger box whose dimensions could be as large as the user gives(may or may not intersect the original geometry).

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/685184_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [July 7, 2021, 3:08pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/13 "2021-07-07T15:08:20Z")

</div>

The code I posted works with your model.

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/a/8/3/a8301acc98c8b09f94b4f36090c045ddb152f959.png)

Perhaps your box brep does not intersect with your hull brep?

– Dale

---

<div class="post-metadata">

**Author:** ![raghav.pathak](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/raghav.pathak/32/153431_2.png) [@raghav.pathak](https://discourse.mcneel.com/u/raghav.pathak)\
**Post date:** [July 7, 2021, 3:25pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/14 "2021-07-07T15:25:01Z")

</div>

Hi @dale, thanks for the clarification.

Yes, that’s what I am saying. The code works perfectly only if the box is intersecting with the brep. But, this geometry is only 1 of several test cases that may arise and ideally, the box should not intersect and still, the boolean difference operation should work.

The end result should be something like a cavity of the original brep inside a cuboidal volume.

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/685184_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [July 7, 2021, 4:02pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/15 "2021-07-07T16:02:41Z")

</div>

Rhino’s Boolean operations will not do this, as a closed Brep is hollow.

– Dale

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/685184_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [December 20, 2022, 4:37pm UTC](https://discourse.mcneel.com/t/convert-rhinoobjects-to-brep/126828/16 "2022-12-20T16:37:39Z")

</div>

Split this topic on December 20, 2022, 4:37pm UTC

A post was split to a new topic: [Conversion of Brep to RhinoObject](https://discourse.mcneel.com/t/conversion-of-brep-to-rhinoobject/152366)
