public static bool CastCurveTo <Q>(Rhino.Geometry.Curve curve, out Q target) { if (curve != null) { // cast to GH_Curve (Caution: this loses all structural information) if (typeof(Q).IsAssignableFrom(typeof(GH_Curve))) { var gc = new GH_Curve(curve); target = (Q)(object)gc; return(true); } // cast to GH_Line (Caution: this loses all structural information) else if (typeof(Q).IsAssignableFrom(typeof(GH_Line))) { if (curve is Rhino.Geometry.LineCurve) { var gl = new GH_Line((curve as Rhino.Geometry.LineCurve).Line); target = (Q)(object)gl; return(true); } } // cast to GH_Arc (Caution: this loses all structural information) else if (typeof(Q).IsAssignableFrom(typeof(GH_Arc))) { if (curve is Rhino.Geometry.ArcCurve) { var ga = new GH_Arc((curve as Rhino.Geometry.ArcCurve).Arc); target = (Q)(object)ga; return(true); } } else { throw new Exception("Unable to cast to type: " + typeof(Q).ToString()); } } target = default(Q); return(false); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { object obj = null; DA.GetData(0, ref obj); // Remarks: // 1) why lines, rectangles or *any* rhinocommon object can't have user dicts? // 2) @David: i hate grasshopper typecasting and the hassle below // (why isn't there a GH_DefaultType, where i can access the .Value regardless of type...?) GeometryBase myObj = null; GH_Mesh mesh = obj as GH_Mesh; if (mesh != null) { myObj = mesh.Value; } GH_Brep brep = obj as GH_Brep; if (brep != null) { myObj = brep.Value; } GH_Surface srf = obj as GH_Surface; if (srf != null) { myObj = srf.Value; } GH_Box box = obj as GH_Box; if (box != null) { myObj = box.Value.ToBrep(); } GH_Curve crv = obj as GH_Curve; if (crv != null) { myObj = crv.Value; } GH_Line line = obj as GH_Line; if (line != null) { myObj = line.Value.ToNurbsCurve(); } GH_Rectangle rect = obj as GH_Rectangle; if (rect != null) { myObj = rect.Value.ToNurbsCurve(); } GH_Circle circle = obj as GH_Circle; if (circle != null) { myObj = circle.Value.ToNurbsCurve(); } GH_Arc arc = obj as GH_Arc; if (arc != null) { myObj = arc.Value.ToNurbsCurve(); } GH_Point pt = obj as GH_Point; if (pt != null) { myObj = new Point(pt.Value); } if (myObj == null) { // get the object out DA.SetData(0, obj); this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Failed to set user dictionary to object. Probably an unsupported type."); return; } myObj.UserDictionary.Clear(); object value = null; DA.GetData(1, ref value); GH_ObjectWrapper temp = value as GH_ObjectWrapper; if (temp == null) { DA.SetData(0, obj); this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not cast object to GH_ObjectWrapper."); return; } ArchivableDictionary dict = ((GH_ObjectWrapper)value).Value as ArchivableDictionary; if (dict == null) { DA.SetData(0, obj); this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not cast object to Dictionary."); return; } myObj.UserDictionary.ReplaceContentsWith(dict); DA.SetData(0, myObj); }
/// <summary> /// Determines object type and calls the appropriate conversion call. /// </summary> /// <param name="o">Object to convert.</param> /// <param name="getEncoded">If set to true, will return a base64 encoded value of more complex objects (ie, breps).</param> /// <returns></returns> private static SpeckleObject fromGhRhObject(object o, bool getEncoded = false, bool getAbstract = true) { GH_Interval int1d = o as GH_Interval; if (int1d != null) { return(GhRhConveter.fromInterval(int1d.Value)); } if (o is Interval) { return(GhRhConveter.fromInterval((Interval)o)); } GH_Interval2D int2d = o as GH_Interval2D; if (int2d != null) { return(GhRhConveter.fromInterval2d(int2d.Value)); } if (o is UVInterval) { return(GhRhConveter.fromInterval2d((UVInterval)o)); } // basic stuff GH_Number num = o as GH_Number; if (num != null) { return(SpeckleConverter.fromNumber(num.Value)); } if (o is double || o is int) { return(SpeckleConverter.fromNumber((double)o)); } GH_Boolean bul = o as GH_Boolean; if (bul != null) { return(SpeckleConverter.fromBoolean(bul.Value)); } if (o is Boolean) { return(GhRhConveter.fromBoolean((bool)o)); } GH_String str = o as GH_String; if (str != null) { return(SpeckleConverter.fromString(str.Value)); } if (o is string) { return(SpeckleConverter.fromString((string)o)); } // simple geometry GH_Point point = o as GH_Point; if (point != null) { return(GhRhConveter.fromPoint(point.Value)); } if (o is Point3d) { return(GhRhConveter.fromPoint((Point3d)o)); } // added because when we assign user data to points they get converted to points. // the above comment does makes sense. check the sdk. if (o is Rhino.Geometry.Point) { return(GhRhConveter.fromPoint(((Rhino.Geometry.Point)o).Location)); } GH_Vector vector = o as GH_Vector; if (vector != null) { return(GhRhConveter.fromVector(vector.Value)); } if (o is Vector3d) { return(GhRhConveter.fromVector((Vector3d)o)); } GH_Plane plane = o as GH_Plane; if (plane != null) { return(GhRhConveter.fromPlane(plane.Value)); } if (o is Plane) { return(GhRhConveter.fromPlane((Plane)o)); } GH_Line line = o as GH_Line; if (line != null) { return(GhRhConveter.fromLine(line.Value)); } if (o is Line) { return(GhRhConveter.fromLine((Line)o)); } GH_Arc arc = o as GH_Arc; if (arc != null) { return(GhRhConveter.fromArc(arc.Value)); } if (o is Arc) { return(GhRhConveter.fromArc((Arc)o)); } GH_Circle circle = o as GH_Circle; if (circle != null) { return(GhRhConveter.fromCircle(circle.Value)); } if (o is Circle) { return(GhRhConveter.fromCircle((Circle)o)); } GH_Rectangle rectangle = o as GH_Rectangle; if (rectangle != null) { return(GhRhConveter.fromRectangle(rectangle.Value)); } if (o is Rectangle3d) { return(GhRhConveter.fromRectangle((Rectangle3d)o)); } GH_Box box = o as GH_Box; if (box != null) { return(GhRhConveter.fromBox(box.Value)); } if (o is Box) { return(GhRhConveter.fromBox((Box)o)); } // getting complex GH_Curve curve = o as GH_Curve; if (curve != null) { Polyline poly; if (curve.Value.TryGetPolyline(out poly)) { return(GhRhConveter.fromPolyline(poly)); } return(GhRhConveter.fromCurve(curve.Value, getEncoded, getAbstract)); } if (o is Polyline) { return(GhRhConveter.fromPolyline((Polyline)o)); } if (o is Curve) { return(GhRhConveter.fromCurve((Curve)o, getEncoded, getAbstract)); } GH_Surface surface = o as GH_Surface; if (surface != null) { return(GhRhConveter.fromBrep(surface.Value, getEncoded, getAbstract)); } GH_Brep brep = o as GH_Brep; if (brep != null) { return(GhRhConveter.fromBrep(brep.Value, getEncoded, getAbstract)); } if (o is Brep) { return(GhRhConveter.fromBrep((Brep)o, getEncoded, getAbstract)); } GH_Mesh mesh = o as GH_Mesh; if (mesh != null) { return(GhRhConveter.fromMesh(mesh.Value)); } if (o is Mesh) { return(GhRhConveter.fromMesh((Mesh)o)); } return(new SpeckleObject() { hash = "404", value = "Type not supported", type = "404" }); }
/// <summary> /// Updates the geometry for an input chromosome /// </summary> /// <param name="chromo">The chromosome used to get geometry from the gh canvas</param> /// <returns></returns> public int GetGeometry(Chromosome chromo) { // Collect the object at the current instance List <object> localObjs = new List <object>(); // Thank you Dimitrie :) foreach (IGH_Param param in Params.Input[1].Sources) { foreach (Object myObj in param.VolatileData.AllData(true)) // AllData flattens the tree { localObjs.Add(myObj); } } // Gets lists of different geometry List <Mesh> meshGeometry = new List <Mesh>(); List <PolylineCurve> polyGeometry = new List <PolylineCurve>(); // Get only mesh geometry from the object list for (int i = 0; i < localObjs.Count; i++) { // Need to replace with a Switch if (localObjs[i] is GH_Mesh) { GH_Mesh myGHMesh = new GH_Mesh(); myGHMesh = (GH_Mesh)localObjs[i]; Mesh myLocalMesh = new Mesh(); GH_Convert.ToMesh(myGHMesh, ref myLocalMesh, GH_Conversion.Primary); myLocalMesh.Faces.ConvertQuadsToTriangles(); meshGeometry.Add(myLocalMesh); //Mesh joinedMesh = new Mesh(); //joinedMesh.Append(myLocalMesh); } if (localObjs[i] is GH_Brep) { GH_Brep myBrep = new GH_Brep(); myBrep = (GH_Brep)localObjs[i]; Mesh[] meshes = Mesh.CreateFromBrep(myBrep.Value, MeshingParameters.FastRenderMesh); Mesh mesh = new Mesh(); mesh.Append(meshes); mesh.Faces.ConvertQuadsToTriangles(); meshGeometry.Add(mesh); } if (localObjs[i] is GH_Box) { GH_Box myBox = new GH_Box((GH_Box)localObjs[i]); Mesh[] meshes = Mesh.CreateFromBrep(myBox.Brep(), MeshingParameters.FastRenderMesh); Mesh mesh = new Mesh(); mesh.Append(meshes); mesh.Faces.ConvertQuadsToTriangles(); meshGeometry.Add(mesh); } if (localObjs[i] is GH_Surface) { GH_Surface mySurface = (GH_Surface)localObjs[i]; Mesh[] meshes = Mesh.CreateFromBrep(mySurface.Value, MeshingParameters.FastRenderMesh); Mesh mesh = new Mesh(); mesh.Append(meshes); mesh.Faces.ConvertQuadsToTriangles(); meshGeometry.Add(mesh); } if (localObjs[i] is GH_Curve) { GH_Curve myGHCurve = (GH_Curve)localObjs[i]; PolylineCurve myPoly = myGHCurve.Value.ToPolyline(0, 0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, true); polyGeometry.Add(myPoly); } if (localObjs[i] is GH_Arc) { GH_Arc myGHArc = (GH_Arc)localObjs[i]; PolylineCurve myPoly = myGHArc.Value.ToNurbsCurve().ToPolyline(0, 0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, true); polyGeometry.Add(myPoly); } if (localObjs[i] is GH_Line) { GH_Line myGHLine = (GH_Line)localObjs[i]; List <Point3d> pts = new List <Point3d> { myGHLine.Value.From, myGHLine.Value.To }; PolylineCurve myPoly = new PolylineCurve(pts); polyGeometry.Add(myPoly); } } // Get performance data List <double> performas = new List <double>(); List <string> criteria = new List <string>(); // Cap at eight criteria max. int pCount = 0; int repeatCounter = 1; foreach (IGH_Param param in Params.Input[2].Sources) { foreach (Object myObj in param.VolatileData.AllData(true)) { if (myObj is GH_Number && pCount < 8) { GH_Number temp = (GH_Number)myObj; performas.Add(temp.Value); if (!criteria.Contains(param.NickName)) { criteria.Add(param.NickName); } else { criteria.Add(param.NickName + " (" + repeatCounter + ")"); repeatCounter++; } pCount++; } else if (myObj is GH_Integer && pCount < 8) { GH_Integer temp = (GH_Integer)myObj; performas.Add((double)temp.Value); if (!criteria.Contains(param.NickName)) { criteria.Add(param.NickName); } else { criteria.Add(param.NickName + " (" + repeatCounter + ")"); repeatCounter++; } pCount++; } } } // Set the phenotype within the chromosome class chromo.SetPhenotype(meshGeometry, polyGeometry, performas, criteria); // Return the number of performance criteria return(performas.Count); }
/*******************************************/ /**** Public Methods ****/ /*******************************************/ public static bool CastToGoo(object value, ref GH_Arc target) { return(GH_Convert.ToGHArc(value, GH_Conversion.Both, ref target)); }