Grasshopper

algorithmic modeling for Rhino

I believe my problem is the Cast Type I need to use.  What I am trying to do is Bring in a list of objects from Rhino using another C# component I wrote.  The output of that component needs to be GeometryBase.  Then I am trying to create a new component that takes in all of those geometries, checks to see if it is a block (which is Identified as a 'InstanceReferenceGeometry') and then If it is, It outputs the Block Name and the specific instance's Insertion Point.  

Below is what I have so far.  The commented out parts are where I am stuck.  Any insight would be appreciated!

private void RunScript(List<GeometryBase> Geom, ref object InputBlock, ref object InputInsertionPoint)
    {

    int GeomMax = Geom.Count;
    int GeomCount = 0;

    List<String> GeomBlock = new List<String>();
    List<Point3d> GeomIP = new List<Point3d>();

    while (GeomCount < GeomMax)
        {
        switch(Geom[GeomCount].GetType().Name)
            {
            case "InstanceReferenceGeometry":
//             GeomBlock.Add(Geom[GeomCount].Name); - Needs to be type Rhino.DocObjects.InstanceDefinition
//             GeomIP.Add(Geom[GeomCount].InsertionPoint); - Needs to be type Rhino.DocObjects.InstanceObject
            Print("Geometry is a Block");
            break;
            default:
            Print("Geometry is Not a Block");
            break;
        }
        GeomCount++;
    }
    InputBlock = GeomBlock;
    InputInsertionPoint = GeomIP;

}

Views: 1463

Replies to This Discussion

You probably want to use language type checking (basically the 'as' and 'is' keywords) rather than comparing typenames to strings.

But I don't think you'll be able to get the information you need. GeometryBase and DocObject are not related types, one cannot be cast to the other. DocObject instances contain GeometryBase instances (amongst other things) but they are not part of the same inheritance tree.

By the time you only have a list of GeometryBase instances (some of which may be of type InstanceReferenceGeometry), it's too late to get the names and insertion points. Those properties belong to the DocObject type.

Assuming those properties were available on that type though, the code should look like this:

private void RunScript(List<GeometryBase> Geom, ref object InputBlock, ref object InputInsertionPoint)
{
  List<string> blockNames = new List<string>();
  List<Point3d> blockPoints = new List<Point3d>();
  foreach (GeometryBase geometry in Geom)
  {
    InstanceReferenceGeometry instanceReference = geometry as InstanceReferenceGeometry;
    if (instanceReference != null)
    {
      blockNames.Add(instanceReference.NAMEPROPERTYDOESNOTEXISTONTHISTYPE);
      blockPoints.Add(instanceReference.NAMEPROPERTYDOESNOTEXISTONTHISTYPE);
    }
  }
  InputBlock = blockNames;
  InputInsertionPoint = blockPoints;
}

I was afraid you were going to say that... So... new plan... I need to modify my previous component to output System.Object instead of GeometryBase.  I should then be able to cast that to a Rhino.DocObject type, correct?

If you pass Guids around, you can also get the objects from the Rhino file anew.

This Might be doable... Once I have the Guid's. How do I then get the geometries from that?

RhinoDoc.Objects.Find

Thanks for that!  I figured it was using a .Find  

Ended up needing RhinoDocument.Objects.Find though.

Last question on this topic.  For another component I made I was using nulls as place holders, but when I began using Guid instead of GeometryBase, I get an error.  Is there a way around this or an alternative (like empty) that Can be used instead of null?

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service