How cast GeometryBase to Point3d

Hi everyone,

I am using RhinoIdle to collect selected objects like Curve, Brep, Point3d etc…
Casting from GeometryBase to Curve and Brep is successful but I cannot cast to Point3d in order to collect selected points to my lists. How possibly can I collect selected points in my dictionary. I put sample of in order to compare Curve and Point3d in RhinoIdle event handler.

private void OnIdle(object sender, EventArgs e)
{
    #region Curve Selection
    List<Curve> curves = new List<Curve>();
    Dictionary<Guid, Curve> selectedCurves = new Dictionary<Guid, Curve>();

    List<RhinoObject> selectedRhinoObjectCurves = RhinoDoc.ActiveDoc.Objects.GetObjectsByType<RhinoObject>(new ObjectEnumeratorSettings()
    {
        ObjectTypeFilter = ObjectType.Curve,
        SelectedObjectsFilter = true
    }).ToList();

    foreach (RhinoObject rhinoObject in selectedRhinoObjectCurves)
    {
        Guid guidCurve = rhinoObject.Id;
        Curve curve = (Curve)rhinoObject.Geometry; //// SUCCESSFULL CASTING
        selectedCurves.Add(guidCurve, curve);
    }
    SelectedCurves = selectedCurves;
    #endregion Curve Selection


    #region Point Selection
    List<Point3d> points = new List<Point3d>();
    Dictionary<Guid, ILocation> selectedPoints = new Dictionary<Guid, ILocation>();

    List<RhinoObject> selectedRhinoObjectPoints = RhinoDoc.ActiveDoc.Objects.GetObjectsByType<RhinoObject>(new ObjectEnumeratorSettings()
    {
        ObjectTypeFilter = ObjectType.Point,
        SelectedObjectsFilter = true
    }).ToList();

    foreach (RhinoObject rhinoObject in selectedRhinoObjectPoints)
    {
        Guid guidPoint = rhinoObject.Id;
        Point3d point = (Point3d)rhinoObject.Geometry;  //// FAILED CASTING      
        selectedPoints.Add(guidPoint, point);
    }
    SelectedPoints = selectedPoints;

    #endregion Point Selection
}

Thanks in advance,
-Oğuzhan

1 Like

You cannot cast to Point3d, because Point3d is a struct which does not derive from GeometryBase. You must cast to the Rhino.Geometry.Point class instead, and then extract the Point3d coordinate later.

2 Likes

@DavidRutten, thanks for quick answer and solution.

Point point = (Rhino.Geometry.Point)rhinoObject.Geometry;
Point3d point3d = point.Location;
4 Likes

Hello all,
I’m still struggling to correctly cast GeometrBase to Point3d.
@DavidRutten, @oguzhankoral please can you check the pic, what am I doing wrong?
cast

Hi,

Seems like you are trying to map point to GeometryBase, but it is actually an abstract class, it is created internally by RhinoObject. To retrieve GeometryBase, you need to add that point object onto
Document first. Then you can cast its Point3d back to GeometryBase.

@Bakuri_Gogua there is nothing wrong with the code you have posted, and the if is an excellent way of checking. Assuming that Point class is Rhino.Geometry.Point. But what you are doing is quite strange (unless you’re trying to copy the object?), the geometry object was/is already a Point Can you elaborate on what it is you’re trying to achieve?

Just to clarify, you cannot cast GeometryBase to Point3d because Point3d does not inherit from GeometryBase.

Perhaps this helps:

public static Point3d GetPointLocation(GeometryBase geometry)
{
  Point3d rc = Point3d.Unset;
  if (null != geometry)
  {
    Point point = geometry as Point;
    if (null != point)
      rc = point.Location;
  }
  return rc;
}

– Dale

hello @dale, thank you for your reply.
I have an input type GeometryBase and I am trying it to accept a point input but getting an error: Invalid cast: Point » GeometryBase. I tried hard but nothing worked.(((
GH native component Polar Array has the same input type(Generic Geometry) but freely accepts a point as input. Please can you help me achieve the same for my component? :pray:
does it(GH native component Polar Array) use a GeometryBase as input type or IGH_GeometricGoo?

Can you post your .GH file?

Thanks,

– Dale

I’m scripting it in VS C#

Since your question isn’t related to the topic, you should be asking in the `Grasshopper Developer" category.

Without your .GH file, code, whatever, I can’t help - sorry.