mcneel / rhinocommon

RhinoCommon is the .NET SDK for Rhino5 / Grasshopper
http://wiki.mcneel.com/developer/rhinocommon
244 stars 93 forks source link

Unifying interface from Non-GeometryBase types to GeometryBase #160

Closed piac closed 6 years ago

piac commented 10 years ago

We should provide a way for this code to be more straightforward, future-proof, and efficient. I would suggest a IGeometryBaseTransformable interface, but IConvertible might be enough.

A conversion overview is here: http://msdn.microsoft.com/en-us/library/yy580hbd.aspx

if (t == typeof(Line))
          target = new LineCurve((Line)target);

        else if (t == typeof(Arc))
          target = new ArcCurve((Arc)target);

        else if (t == typeof(Circle))
          target = new ArcCurve((Circle)target);

        else if (t == typeof(Ellipse))
          target = ((Ellipse)target).ToNurbsCurve();

        else if (t == typeof(Box))
          target = Brep.CreateFromBox((Box)target);

        else if (t == typeof(BoundingBox))
          target = Brep.CreateFromBox((BoundingBox)target);

        else if (t == typeof(Rectangle3d))
          target = ((Rectangle3d)target).ToNurbsCurve();

        else if (target is Polyline) // Polyline is an open type
          target = new PolylineCurve((Polyline)target);

        //possibly some other conversions here
piac commented 10 years ago

If we choose IConvertible, the only method we would override meaningfully would be http://msdn.microsoft.com/en-us/library/system.iconvertible.totype.aspx This, though, would allow the usage of Convert.ChangeType(), which is an extensively supported mechanism. Calls to Convert.ChangeType require boxing, but to Iconvertible.ToType do not necessarily.

If our interface were to contain a GeometryBase AsGeometry() method, exposed in the right flavor, no casting would be required, and a unique interface would be provided.

sbaer commented 10 years ago

How would we choose what things like a BoundingBox should be? It could be a Brep, Mesh, or Extrusion

piac commented 10 years ago

Hi Steve

I don't really have the perfect idea, that's why I was proposing two options. The code above is just the GhPython implementation.

IConvertible would have the answer, as one can specify the wanted target. But I think that BoundingBox is an edge case which does not have a precise GeometryBase counterpart and as such could even not implement the other interface.

Your question is very relevant.

piac commented 6 years ago

Today I still think this is relevant, and BoundingBox should convert to Extrusion. Types should always convert to the simples ON_Geometry/GeometryBase type available. While Line could convert to NurbsCurve, it should just convert to LineCurve.

piac commented 6 years ago

Moved to> https://mcneel.myjetbrains.com/youtrack/issue/RH-45269