Quantcast
Channel: Rhino Developer - McNeel Forum
Viewing all articles
Browse latest Browse all 8547

Find highest and lowest face of Brep - C#

$
0
0

@daniel.depoe wrote:

Hello,

I’m using C# to try to find the lowest and highest face of a given brep, as measured by their centroid z value.

@DavidRutten posted this as a scripted VB component for GH:

  Private Sub RunScript(ByVal B As Brep, ByRef lowest As Object, ByRef highest As Object) 
    'Validate input
    If (B Is Nothing) Then Return

    'We'll iterate over all the faces and simultaneously remember the lowest and highest so far.
    Dim lowPoint As Double = Double.MaxValue
    Dim highPoint As Double = Double.MinValue

    For Each face As BrepFace In B.Faces
      'Convert the Face to a new Brep.
      Dim faceBrep As Brep = face.DuplicateFace(True)

      'Compute the Area Mass Properties of the brep
      Dim ap As AreaMassProperties = AreaMassProperties.Compute(faceBrep)

      'If the Area centroid is a new low, assign the brep to the lowest output.
      If (ap.Centroid.Z < lowPoint) Then
        lowPoint = ap.Centroid.Z
        lowest = faceBrep
      End If

      'If the Area centroid is a new high, assign the brep to the highest output.
      If (ap.Centroid.Z > highPoint) Then
        highPoint = ap.Centroid.Z
        highest = faceBrep
      End If
    Next
  End Sub 

I’ve tried to replicate a command in C#, but only got so far. I’m looking for help to complete the following code. I’m certain I’m not using the if/else statements correctly (just started learning c# 3 weeks ago):

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            ObjRef obj_ref;
            var rc = RhinoGet.GetOneObject("Select brep", false, ObjectType.Brep, out obj_ref);
            if (rc != Result.Success)
                return rc;
            var brep = obj_ref.Brep();

            //double lowpoint = double.MaxValue;
            //double highpoint = double.MinValue;

            foreach (var face in brep.Faces)
            {
                var faceBrep = face.DuplicateFace(true);

                //if (ap.Centroid.Z < lowPoint)
                    //lowPoint = ap.Centroid.Z
                    //lowest = faceBrep

                //else (ap.Centroid.Z > highPoint) 
                    //highPoint = ap.Centroid.Z
                    //highest = faceBrep


                Point3d ap = AreaMassProperties.Compute(faceBrep).Centroid;

                TextDot textdot = new TextDot("hello", ap);

                doc.Objects.AddTextDot(textdot);
                               
            }

            return Result.Success;
            
        }

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 8547

Trending Articles