Site Tools


Sorting 3-D Points

Developer: .NET
Summary: Demonstrates how to sort a list of points in ascending or decending order.
NOTICE: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here

Introduction

RhinoScript has two functions to sort points: SortPoints and SortPointList. Are either of these readily available in the .NET SDK? I found the On3dPointArray class has a QuickSort method, but that does seem to be working. There are methods in OnUtil for sorting curves and lines, but I have not found anything yet for sorting points.

Answer

The RhinoScript methods you mention are not available in the .NET SDK. But, you can certainly write your own version.

The following block of code uses a Rhino3dPointList class that inherits from a .NET List object. This class provides a Sort member that allows you to specify the sorting order and whether or not you want the array sorted in ascending or decending order. You can download a C# version of this class here:

http://en.wiki.mcneel.com/content/upload/files/Rhino3dPointList.zip

The following sample code demonstrates how you might use the class:

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  Rhino3dPointList points = new Rhino3dPointList();
  points.Add(new On3dPoint(0, 0, 0));
  points.Add(new On3dPoint(0, 0, 1));
  points.Add(new On3dPoint(0, 1, 0));
  points.Add(new On3dPoint(0, 1, 1));
  points.Add(new On3dPoint(1, 0, 0));
  points.Add(new On3dPoint(1, 0, 1));
  points.Add(new On3dPoint(1, 1, 0));
  points.Add(new On3dPoint(1, 1, 1));
 
  RhUtil.RhinoApp().Print("Before sort...\n");
  points.Print();
 
  points.Sort(Rhino3dPointList.sort_method.sort_xyz, true);
  RhUtil.RhinoApp().Print("Sort ascending...\n");
  points.Print();
 
  points.Sort(Rhino3dPointList.sort_method.sort_xyz, false);
  RhUtil.RhinoApp().Print("Sort descending...\n");
  points.Print();
 
  context.m_doc.AddPointCloudObject(points.ToPointArray());
  context.m_doc.Redraw();
 
  return IRhinoCommand.result.success;
}
developer/sdksamples/sortpoints.txt ยท Last modified: 2015/09/14 (external edit)