public void BakeGeometry(RhinoDoc doc, ObjectAttributes att, List<Guid> obj_ids) { List<Text3d>.Enumerator tag; if (att == null) { att = doc.CreateDefaultAttributes(); } try { tag = this.m_tags.GetEnumerator(); while (tag.MoveNext()) { Text3d tag3d = tag.Current; Guid id = doc.Objects.AddText(tag3d, att); if (!(id == Guid.Empty)) { obj_ids.Add(id); } } } finally { tag.Dispose(); } }
public static Result ModifyObjectColor(RhinoDoc doc) { ObjRef obj_ref; var rc = RhinoGet.GetOneObject("Select object", false, ObjectType.AnyObject, out obj_ref); if (rc != Result.Success) return rc; var rhino_object = obj_ref.Object(); var color = rhino_object.Attributes.ObjectColor; bool b = Rhino.UI.Dialogs.ShowColorDialog(ref color); if (!b) return Result.Cancel; rhino_object.Attributes.ObjectColor = color; rhino_object.Attributes.ColorSource = ObjectColorSource.ColorFromObject; rhino_object.CommitChanges(); // an object's color attributes can also be specified // when the object is added to Rhino var sphere = new Sphere(Point3d.Origin, 5.0); var attributes = new ObjectAttributes(); attributes.ObjectColor = Color.CadetBlue; attributes.ColorSource = ObjectColorSource.ColorFromObject; doc.Objects.AddSphere(sphere, attributes); doc.Views.Redraw(); return Result.Success; }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { List <IGH_GeometricGoo> Geoms = new List <IGH_GeometricGoo>(); bool IsBake = false; Rhino.DocObjects.ObjectAttributes Attr = null; if (!DA.GetDataList(0, Geoms)) { return; } if (!DA.GetData(1, ref Attr)) { return; } if (!DA.GetData(2, ref IsBake)) { return; } if (!IsBake) { return; } foreach (IGH_GeometricGoo Geom in Geoms) { if (Geom != null) { Rhino.RhinoDoc.ActiveDoc.Objects.Add(GH_Convert.ToGeometryBase(Geom), Attr); } } }
public void GeneratePreViewObjectsI(GH_Structure <IGH_GeometricGoo> inGeoTree) { string key = "Leopard(" + this.InstanceGuid + ")"; RhinoDoc.ActiveDoc.Layers.Add("Leopard_Preview", System.Drawing.Color.Maroon); int layer = RhinoDoc.ActiveDoc.Layers.Find("Leopard_Preview", true); Rhino.DocObjects.RhinoObject[] obj = RhinoDoc.ActiveDoc.Objects.FindByUserString(key, "*", true); if (obj.Length == 0) //if no preview item { int count = 0; foreach (IGH_GeometricGoo goo in inGeoTree.AllData(false)) { Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes(); att.SetUserString(key, count.ToString()); att.LayerIndex = layer; count++; if (goo is IGH_BakeAwareData) { IGH_BakeAwareData data = (IGH_BakeAwareData)goo; Guid guid; data.BakeGeometry(RhinoDoc.ActiveDoc, att, out guid); } } } }
/// <summary> /// Bakes the problem geometry to the document on a special layer /// Geometry is colored -> Orange and will have the a name that is showing what the problem is! /// </summary> /// <param name="erGeo">List of error geometry</param> /// <param name="erName">List of error names</param> public static IList <Guid> BakeErrorGeo(IEnumerable <GeometryBase> erGeo, IEnumerable <string> erName) { var addedGeoIds = new List <Guid>(); var doc = Rhino.RhinoDoc.ActiveDoc; var errLayer = new Rhino.DocObjects.Layer() { Name = "<<Error_Geometry>>" }; if (!doc.Layers.Any(x => x.Name == errLayer.Name)) { doc.Layers.Add(errLayer); } errLayer = doc.Layers.First(x => x.Name == "<<Error_Geometry>>"); doc.Views.RedrawEnabled = false; foreach (var geoName in erGeo.Zip(erName, (x, y) => new { geo = x, name = y })) { var attributesError = new Rhino.DocObjects.ObjectAttributes { ObjectColor = System.Drawing.Color.OrangeRed, ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject, Name = geoName.name, LayerIndex = errLayer.LayerIndex }; addedGeoIds.Add(doc.Objects.Add(geoName.geo, attributesError)); } return(addedGeoIds); }
public void GeneratePreViewMeshVertices(GH_Structure <IGH_GeometricGoo> inGeoTree) { string key = "Leopard(" + this.InstanceGuid + ")"; RhinoDoc.ActiveDoc.Layers.Add("Leopard_Preview", System.Drawing.Color.Maroon); int layer = RhinoDoc.ActiveDoc.Layers.Find("Leopard_Preview", true); Rhino.DocObjects.RhinoObject[] obj = RhinoDoc.ActiveDoc.Objects.FindByUserString(key, "*", true); if (obj.Length == 0) //if no preview item { int count = 0; foreach (IGH_GeometricGoo goo in inGeoTree.AllData(false)) { PlanktonMesh pMesh = new PlanktonMesh(); Mesh mesh; if (!goo.CastTo <Mesh>(out mesh)) { RhinoApp.WriteLine("input invalid"); } pMesh = mesh.ToPlanktonMesh(); PlanktonXYZ[] xyz = pMesh.Vertices.GetPositions(); List <Point3d> oVertices = new List <Point3d>(); for (int i = 0; i < xyz.Length; i++) { oVertices.Add(xyz[i].ToPoint3d()); } count++; int vCount = 0; foreach (Point3d p in oVertices) { string keyV = "Leopard(" + this.InstanceGuid + vCount + ")"; Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes(); att.SetUserString(keyV, count.ToString()); att.LayerIndex = layer; GH_Point point = new GH_Point(p); if (goo is IGH_BakeAwareData) { IGH_BakeAwareData data = (IGH_BakeAwareData)point; Guid guid; data.BakeGeometry(RhinoDoc.ActiveDoc, att, out guid); } vCount++; } } } }
public void BakeGeometry(Rhino.RhinoDoc doc, Rhino.DocObjects.ObjectAttributes att, List <Guid> obj_ids) { if (att == null) { att = doc.CreateDefaultAttributes(); } foreach (IGH_BakeAwareObject item in m_data) { if (item != null) { List <Guid> idsOut = new List <Guid>(); item.BakeGeometry(doc, att, idsOut); obj_ids.AddRange(idsOut); } } }
public static void BakeObjectToLayer(GeometryBase obj, string layerName, string rootLayerPath) { string parentOfNestedLayer = null; RhinoDoc doc = RhinoDoc.ActiveDoc; //Check geometry object is valid if (obj == null) { RhinoApp.WriteLine("Object is null..."); //this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Geometry object is null"); return; } if (layerName == null) { RhinoApp.WriteLine("Target layer is null..."); //this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Target Layer is null"); return; } if (!String.IsNullOrEmpty(rootLayerPath)) { parentOfNestedLayer = CheckLayerStructure(rootLayerPath); } //Check layer name is valid if (string.IsNullOrEmpty(layerName) || !Rhino.DocObjects.Layer.IsValidName(layerName)) { RhinoApp.WriteLine("Layer name " + layerName + " is invalid"); return; //this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Layer name " + layerName + " is invalid"); } //Ensure the target layer exists (at the end of the optional root path) RhinoApp.WriteLine("Ensuring child layer..."); string ensuredLayer = EnsureLayer(layerName, parentOfNestedLayer); RhinoApp.WriteLine("-------------------------"); //Make new attribute to set name Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes(); Rhino.DocObjects.Tables.LayerTable layerTable = doc.Layers; int layerIndex = layerTable.FindByFullPath(ensuredLayer, -1); att.LayerIndex = layerIndex; Bake(obj, att); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { Rhino.Geometry.Point3d[] corners; Result rc = Rhino.Input.RhinoGet.GetRectangle(out corners); if (rc != Result.Success) { return(rc); } Rhino.Geometry.Plane plane = new Rhino.Geometry.Plane(corners[0], corners[1], corners[2]); Rhino.Geometry.PlaneSurface plane_surface = new Rhino.Geometry.PlaneSurface( plane, new Rhino.Geometry.Interval(0, corners[0].DistanceTo(corners[1])), new Rhino.Geometry.Interval(0, corners[1].DistanceTo(corners[2])) ); rc = Result.Cancel; if (plane_surface.IsValid) { string layer_name = doc.Layers.GetUnusedLayerName(true); rc = Rhino.Input.RhinoGet.GetString("Name of layer to create", true, ref layer_name); if (rc == Result.Success && !string.IsNullOrEmpty(layer_name)) { int layer_index = doc.Layers.FindByFullPath(layer_name, true); if (-1 == layer_index) { layer_index = doc.Layers.Add(layer_name, System.Drawing.Color.Black); } if (layer_index >= 0) { Rhino.DocObjects.ObjectAttributes attribs = doc.CreateDefaultAttributes(); attribs.LayerIndex = layer_index; attribs.Name = layer_name; doc.Objects.AddSurface(plane_surface, attribs); doc.Views.Redraw(); rc = Result.Success; } } } return(rc); }
/// <summary> /// bake an object in the given rhino document /// </summary> /// <param name="doc">document to bake into</param> /// <param name="att">attributes to bake with (should not be null)</param> /// <param name="obj_guid">the id of the baked object</param> /// <returns>true on success. ifalse, obj_guid and obj_inst are not guaranteed to be valid pointers</returns> public bool BakeGeometry(Rhino.RhinoDoc doc, Rhino.DocObjects.ObjectAttributes att, out Guid obj_guid) { obj_guid = Guid.Empty; if (Value == null) { return(false); } if (!Value.IsValid) { return(false); } doc.Objects.AddMesh(Value.Display); return(true); }
public static void Write <T>(string name, T geometry) where T : GeometryBase { var doc = Rhino.RhinoDoc.ActiveDoc; var layer = doc.Layers.FindName(_layerName); if (layer is null) { int index = doc.Layers.Add(_layerName, Color.Black); layer = doc.Layers.FindIndex(index); } var attributes = new Rhino.DocObjects.ObjectAttributes { LayerIndex = layer.Index, Name = name }; doc.Objects.Add(geometry, attributes); }
public bool BakeGeometry(Rhino.RhinoDoc doc, Rhino.DocObjects.ObjectAttributes att, out Guid obj_guid) { if (_polylines == null) { ClearCaches(); } obj_guid = Guid.Empty; if (_polylines == null) { return(false); } for (int i = 0; i < _polylines.Length; i++) { doc.Objects.AddPolyline(_polylines[i]); } return(true); }
public static Rhino.Commands.Result AddMaterial(Rhino.RhinoDoc doc) { // materials are stored in the document's material table int index = doc.Materials.Add(); Rhino.DocObjects.Material mat = doc.Materials[index]; mat.DiffuseColor = System.Drawing.Color.Chocolate; mat.SpecularColor = System.Drawing.Color.CadetBlue; mat.CommitChanges(); // set up object attributes to say they use a specific material Rhino.Geometry.Sphere sp = new Rhino.Geometry.Sphere(Rhino.Geometry.Plane.WorldXY, 5); Rhino.DocObjects.ObjectAttributes attr = new Rhino.DocObjects.ObjectAttributes(); attr.MaterialIndex = index; attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject; doc.Objects.AddSphere(sp, attr); // add a sphere without the material attributes set sp.Center = new Rhino.Geometry.Point3d(10, 10, 0); doc.Objects.AddSphere(sp); doc.Views.Redraw(); return Rhino.Commands.Result.Success; }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { /* * Original Version Written by Giulio Piacentino - 2010 11 21 - for Grasshopper 0.8.002 * Enhanced by Co-de-iT (Alessio) - now bakes on chosen layer(s) and in groups */ GeometryBase obj = null; if (!DA.GetData(0, ref obj)) { return; } string name = ""; DA.GetData(1, ref name); string layer = ""; DA.GetData(2, ref layer); Color color = new Color(); DA.GetData(3, ref color); Object material = new Object(); DA.GetData(4, ref material); int group_n = 0; DA.GetData(5, ref group_n); bool group = false; DA.GetData(6, ref group); bool bake_iT = false; DA.GetData(7, ref bake_iT); if (!bake_iT) { return; } //Make new attribute to set name Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes(); //Set object name if (!string.IsNullOrEmpty(name)) { att.Name = name; } //Set color if (!color.IsEmpty) { att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject; //Make the color type "by object" att.ObjectColor = color; att.PlotColorSource = Rhino.DocObjects.ObjectPlotColorSource.PlotColorFromObject; //Make the plot color type "by object" att.PlotColor = color; } // Set group if (group) { Rhino.RhinoDoc.ActiveDoc.Groups.Add(Convert.ToString(group_n)); att.AddToGroup(group_n); } //Set layer if (!string.IsNullOrEmpty(layer) && Rhino.DocObjects.Layer.IsValidName(layer)) { //Get the current layer index Rhino.DocObjects.Tables.LayerTable layerTable = Rhino.RhinoDoc.ActiveDoc.Layers; //int layerIndex = layerTable.Find(layer, true); int layerIndex = layerTable.FindByFullPath(layer, -1); if (layerIndex < 0) //This layer does not exist, we add it { Rhino.DocObjects.Layer onlayer = new Rhino.DocObjects.Layer(); //Make a new layer onlayer.Name = layer; onlayer.Color = Color.Gainsboro; // sets new layer color - future dev: choose new layers color layerIndex = layerTable.Add(onlayer); //Add the layer to the layer table if (layerIndex > -1) //We managed to add the layer! { att.LayerIndex = layerIndex; //Print("Added new layer to the document at position " + layerIndex + " named " + layer + ". "); } //else //Print("Layer did not add. Try cleaning up your layers."); //This never happened to me. } else { att.LayerIndex = layerIndex; //We simply add to the existing layer } } //Set plotweight //if (pWidth > 0) //{ // att.PlotWeightSource = Rhino.DocObjects.ObjectPlotWeightSource.PlotWeightFromObject; // att.PlotWeight = pWidth; //} //Set material bool materialByName = !string.IsNullOrEmpty(material as string); Rhino.Display.DisplayMaterial inMaterial; if (material is GH_Material) { GH_Material gMat = material as GH_Material; inMaterial = gMat.Value as Rhino.Display.DisplayMaterial; } else { inMaterial = material as Rhino.Display.DisplayMaterial; } if (material is Color) { inMaterial = new Rhino.Display.DisplayMaterial((Color)material); } if (material != null && inMaterial == null && !materialByName) { if (!(material is string)) { try //We also resort to try with IConvertible { inMaterial = (Rhino.Display.DisplayMaterial)Convert.ChangeType(material, typeof(Rhino.Display.DisplayMaterial)); } catch (InvalidCastException) { } } } if (inMaterial != null || materialByName) { string matName; if (!materialByName) { matName = string.Format("D:{0}-E:{1}-S:{2},{3}-T:{4}", Format(inMaterial.Diffuse), Format(inMaterial.Emission), Format(inMaterial.Specular), inMaterial.Shine.ToString(), inMaterial.Transparency.ToString() ); } else { matName = (string)material; } int materialIndex = Rhino.RhinoDoc.ActiveDoc.Materials.Find(matName, true); if (materialIndex < 0 && !materialByName) //Material does not exist and we have its specs { materialIndex = Rhino.RhinoDoc.ActiveDoc.Materials.Add(); //Let's add it if (materialIndex > -1) { //Print("Added new material at position " + materialIndex + " named \"" + matName + "\". "); Rhino.DocObjects.Material m = Rhino.RhinoDoc.ActiveDoc.Materials[materialIndex]; m.Name = matName; m.DiffuseColor = inMaterial.Diffuse; m.EmissionColor = inMaterial.Emission; //m.ReflectionColor = inMaterial.Specular; m.SpecularColor = inMaterial.Specular; m.Shine = inMaterial.Shine; m.Transparency = inMaterial.Transparency; //m.TransparentColor = no equivalent m.CommitChanges(); att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject; att.MaterialIndex = materialIndex; } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Material did not add. Try cleaning up your materials."); //This never happened to me. } } else if (materialIndex < 0 && materialByName) //Material does not exist and we do not have its specs. We do nothing { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Warning: material name not found. I cannot set the source to this material name. Add a material with name: " + matName); } else { //If this material exists, we do not replace it! att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject; att.MaterialIndex = materialIndex; } } //Set wire density //if (wires == -1 || wires > 0) //{ // att.WireDensity = wires; //} //Bake to the right type of object switch (obj.ObjectType) { case Rhino.DocObjects.ObjectType.Brep: Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(obj as Brep, att); break; case Rhino.DocObjects.ObjectType.Curve: Rhino.RhinoDoc.ActiveDoc.Objects.AddCurve(obj as Curve, att); break; case Rhino.DocObjects.ObjectType.Point: Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint((obj as Rhino.Geometry.Point).Location, att); break; case Rhino.DocObjects.ObjectType.Surface: Rhino.RhinoDoc.ActiveDoc.Objects.AddSurface(obj as Surface, att); break; case Rhino.DocObjects.ObjectType.Mesh: Rhino.RhinoDoc.ActiveDoc.Objects.AddMesh(obj as Mesh, att); break; case Rhino.DocObjects.ObjectType.Extrusion: typeof(Rhino.DocObjects.Tables.ObjectTable).InvokeMember("AddExtrusion", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, Rhino.RhinoDoc.ActiveDoc.Objects, new object[] { obj, att }); break; case Rhino.DocObjects.ObjectType.PointSet: Rhino.RhinoDoc.ActiveDoc.Objects.AddPointCloud(obj as Rhino.Geometry.PointCloud, att); //This is a speculative entry break; default: AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "The script does not know how to handle this type of geometry: " + obj.GetType().FullName); break; } }
public bool UpdateToAngularDimension(Geometry.AngularDimension dimension, ObjectAttributes attributes) { IntPtr pConstDimension = dimension.ConstPointer(); IntPtr pConstAttributes = (attributes == null) ? IntPtr.Zero : attributes.ConstPointer(); return UnsafeNativeMethods.CRhinoObjectPairArray_UpdateResult7(m_parent.m_pObjectPairArray, m_index, pConstDimension, pConstAttributes); }
public bool UpdateToClippingPlane(Geometry.Plane plane, double uMagnitude, double vMagnitude, IEnumerable<Guid> clippedViewportIds, ObjectAttributes attributes) { List<Guid> ids = new List<Guid>(clippedViewportIds); Guid[] clippedIds = ids.ToArray(); int count = clippedIds.Length; if (count < 1) return false; IntPtr pConstAttributes = (attributes == null) ? IntPtr.Zero : attributes.ConstPointer(); return UnsafeNativeMethods.CRhinoObjectPairArray_UpdateResult4(m_parent.m_pObjectPairArray, m_index, ref plane, uMagnitude, vMagnitude, count, clippedIds, pConstAttributes); }
public bool UpdateToClippingPlane(Geometry.Plane plane, double uMagnitude, double vMagnitude, Guid clippedViewportId, ObjectAttributes attributes) { return UpdateToClippingPlane(plane, uMagnitude, vMagnitude, new Guid[] { clippedViewportId }, attributes); }
public bool UpdateToPointCloud(Rhino.Geometry.PointCloud cloud, ObjectAttributes attributes) { IntPtr pCloud = cloud.ConstPointer(); IntPtr pConstAttributes = (attributes == null) ? IntPtr.Zero : attributes.ConstPointer(); return UnsafeNativeMethods.CRhinoObjectPairArray_UpdateResult2(m_parent.m_pObjectPairArray, m_index, pCloud, pConstAttributes); }
/// <summary> /// We create a DisplayMesh object for each render mesh object we find. If we encounter an Mesh object, /// we create a DisplayMesh object from the Mesh. For any Brep objects, we create Displaymesh objects /// from the render mesh. For any Extrusion objects, we create a DisplayMesh from the RenderMesh. For /// InstanceReference objects, _____ /// </summary> protected virtual void PrepareObject (GeometryBase pObject, ObjectAttributes attr) { while (LayerBBoxes.Count < ModelFile.Layers.Count) { BoundingBox invalidBBox = BoundingBox.Empty; LayerBBoxes.Add (invalidBBox); } // Geometry sorting... // Meshes-------------------------------------------------------------------------------------------- if (pObject.ObjectType == ObjectType.Mesh /*&& attr.Mode != ObjectMode.InstanceDefinitionObject*/) { if (!(pObject as Mesh).IsValid) { (pObject as Mesh).Compact (); (pObject as Mesh).Normals.ComputeNormals (); } // Check to see if any of the vertices are hidden...if they are, remove them from the list for (int i = 0; i < (pObject as Mesh).Vertices.Count; i++) { bool vertexIsHidden = (pObject as Mesh).Vertices.IsHidden (i); if (vertexIsHidden) { (pObject as Mesh).Vertices.Remove (i, true); } } // Some 3dm files have meshes with no normals and this messes up the shading code... if (((pObject as Mesh).Normals.Count == 0) && ((pObject as Mesh).Vertices.Count > 0) && ((pObject as Mesh).Faces.Count > 0)) { (pObject as Mesh).Normals.ComputeNormals (); } GeometryCount++; AddModelMesh ((pObject as Mesh), attr); // Breps ------------------------------------------------------------------------------------------- } else if (pObject.ObjectType == ObjectType.Brep) { BRepCount++; List<Mesh> meshes = new List<Mesh> (); //Loop through each BrepFace in the BRep and GetMesh, adding it to meshes int count = 0; foreach (BrepFace face in (pObject as Brep).Faces) { var renderMesh = face.GetMesh (MeshType.Render); if (renderMesh != null) { meshes.Add (face.GetMesh (MeshType.Render)); count++; } } if (count > 0) BRepWithMeshCount++; if (count == 1) { if ((meshes [0] != null) && (meshes [0].Vertices.Count > 0)) { GeometryCount++; AddModelMesh (meshes [0], attr); } } else if (count > 0) { Mesh meshCandidate = new Mesh (); // Sometimes it's possible to have lists of NULL ON_Meshes...Rhino // can put them there as placeholders for badly formed/meshed breps. // Therefore, we need to always make sure we're actually looking at // a mesh that contains something and/or is not NULL. for (int i = 0; i < count; i++) { // If we have a valid pointer, append the mesh to our accumulator mesh... if (meshes [i] != null) { meshCandidate.Append (meshes [i]); } } // See if the end result actually contains anything and add it to our model if it does... if (meshCandidate.Vertices.Count > 0) { GeometryCount++; AddModelMesh (meshCandidate, attr); } } // Extrusions -------------------------------------------------------------------------------------- } else if (pObject.ObjectType == ObjectType.Extrusion) { ExtrusionCount++; Rhino.Geometry.Mesh meshCandidate = (pObject as Extrusion).GetMesh (Rhino.Geometry.MeshType.Render); // See if the end result actually contains anything and add it to our model if it does... if (meshCandidate != null) { if (meshCandidate.Vertices.Count > 0) { GeometryCount++; AddModelMesh(meshCandidate, attr); } } // Instance References ----------------------------------------------------------------------------- } else if (pObject.ObjectType == ObjectType.InstanceReference) { Rhino.Geometry.InstanceReferenceGeometry instanceRef = (InstanceReferenceGeometry)pObject; ModelInstanceRef iRef = new ModelInstanceRef(attr.ObjectId, instanceRef.ParentIdefId, instanceRef.Xform); if (iRef != null) { iRef.LayerIndex = attr.LayerIndex; GeometryCount++; AddModelObject (iRef, attr); } } }
/// <summary> /// Adds a ModelObject to the ModelObjects List /// </summary> protected virtual void AddModelObject (ModelObject obj, ObjectAttributes attr) { if (obj != null) { Material material = new Material (); int materialIndex = -1; switch (attr.MaterialSource) { case (ObjectMaterialSource.MaterialFromLayer): if (attr.LayerIndex >= 0 && attr.LayerIndex < ModelFile.Layers.Count) materialIndex = ModelFile.Layers [attr.LayerIndex].RenderMaterialIndex; break; case (ObjectMaterialSource.MaterialFromObject): materialIndex = attr.MaterialIndex; break; case (ObjectMaterialSource.MaterialFromParent): materialIndex = attr.MaterialIndex; break; } if (materialIndex < 0 || materialIndex >= ModelFile.Materials.Count) { materialIndex = -1; material.Default (); } else { material = ModelFile.Materials [materialIndex]; } obj.Material = material; obj.LayerIndex = attr.LayerIndex; obj.Visible = attr.Visible; ModelObjectsDictionary.Add (obj.ObjectId, obj); // If the object is not an instance object, add it to the ModelObjects list if (attr.Mode != ObjectMode.InstanceDefinitionObject) ModelObjects.Add (obj); } }
/// <summary> /// This procedure contains the user code. Input parameters are provided as regular arguments, /// Output parameters as ref arguments. You don't have to assign output parameters, /// they will have a default value. /// </summary> private void RunScript(GeometryBase obj, List<Color> color, string layer, bool bake) { try{ var objAttr = new ObjectAttributes(); if(color.Count() != 0){ objAttr.ColorSource = ObjectColorSource.ColorFromObject; objAttr.ObjectColor = color[0]; } if(layer != ""){ objAttr.LayerIndex = GetLayerNumber(RhinoDocument, layer); } if(bake){ RhinoDocument.Objects.Add(obj, objAttr); } }catch(Exception e){ Print(e.ToString()); } }
/// <summary> /// Adds a Mesh to the ModelObjects List /// </summary> protected virtual void AddModelMesh (Mesh mesh, ObjectAttributes attr) { //Add this to the list of all the meshes if (AllMeshes == null) AllMeshes = new RhinoList<Mesh> (); AllMeshes.Add (mesh); Material material = new Material (); int materialIndex = -1; switch (attr.MaterialSource) { case (ObjectMaterialSource.MaterialFromLayer): if (attr.LayerIndex >= 0 && attr.LayerIndex < ModelFile.Layers.Count) materialIndex = ModelFile.Layers [attr.LayerIndex].RenderMaterialIndex; break; case (ObjectMaterialSource.MaterialFromObject): materialIndex = attr.MaterialIndex; break; case (ObjectMaterialSource.MaterialFromParent): materialIndex = attr.MaterialIndex; break; } if (materialIndex < 0 || materialIndex >= ModelFile.Materials.Count) { materialIndex = -1; material.Default (); } else { material = ModelFile.Materials [materialIndex]; } object[] displayMeshes = DisplayMesh.CreateWithMesh (mesh, attr, material, materialIndex); if (displayMeshes != null) { var modelMesh = new ModelMesh (displayMeshes, attr.ObjectId); AddModelObject (modelMesh, attr); } }
public static void Bake(object obj, Rhino.DocObjects.ObjectAttributes att) { RhinoDoc doc = RhinoDoc.ActiveDoc; if (obj == null) { return; } Guid id; //Bake to the right type of object if (obj is GeometryBase) { GeometryBase geomObj = obj as GeometryBase; switch (geomObj.ObjectType) { case Rhino.DocObjects.ObjectType.Brep: id = doc.Objects.AddBrep(obj as Brep, att); break; case Rhino.DocObjects.ObjectType.Curve: id = doc.Objects.AddCurve(obj as Curve, att); break; case Rhino.DocObjects.ObjectType.Point: id = doc.Objects.AddPoint((obj as Rhino.Geometry.Point).Location, att); break; case Rhino.DocObjects.ObjectType.Surface: id = doc.Objects.AddSurface(obj as Surface, att); break; case Rhino.DocObjects.ObjectType.Mesh: id = doc.Objects.AddMesh(obj as Mesh, att); break; case (Rhino.DocObjects.ObjectType) 1073741824: //Rhino.DocObjects.ObjectType.Extrusion: id = (Guid)typeof(Rhino.DocObjects.Tables.ObjectTable).InvokeMember("AddExtrusion", BindingFlags.Instance | BindingFlags.InvokeMethod, null, doc.Objects, new object[] { obj, att }); break; case Rhino.DocObjects.ObjectType.PointSet: id = doc.Objects.AddPointCloud(obj as Rhino.Geometry.PointCloud, att); //This is a speculative entry break; default: RhinoApp.WriteLine("The script does not know how to handle this type of geometry: " + obj.GetType().FullName); return; } } else { Type objectType = obj.GetType(); if (objectType == typeof(Arc)) { id = doc.Objects.AddArc((Arc)obj, att); } else if (objectType == typeof(Box)) { id = doc.Objects.AddBrep(((Box)obj).ToBrep(), att); } else if (objectType == typeof(Circle)) { id = doc.Objects.AddCircle((Circle)obj, att); } else if (objectType == typeof(Ellipse)) { id = doc.Objects.AddEllipse((Ellipse)obj, att); } else if (objectType == typeof(Polyline)) { id = doc.Objects.AddPolyline((Polyline)obj, att); } else if (objectType == typeof(Sphere)) { id = doc.Objects.AddSphere((Sphere)obj, att); } else if (objectType == typeof(Point3d)) { id = doc.Objects.AddPoint((Point3d)obj, att); } else if (objectType == typeof(Line)) { id = doc.Objects.AddLine((Line)obj, att); } else if (objectType == typeof(Vector3d)) { RhinoApp.WriteLine("Impossible to bake vectors"); return; } else { RhinoApp.WriteLine("Unhandled type: " + objectType.FullName); return; } } }
/// <summary> /// NOTE: Not called in current implementation. /// Models with large meshes must partition the meshes before displaying them on the device. /// Partitioning meshes is a lengthy process and can take > 80% of the model loading time. /// We save the raw VBO data of a mesh in an archive after a mesh has been partitioned /// and use that archive to create the VBOs next time we display the model. /// </summary> protected virtual bool LoadMeshCaches(Mesh mesh, ObjectAttributes attr, DisplayMaterial material) { string meshGUIDString = attr.ObjectId.ToString (); string meshCachePath = SupportPathForName (meshGUIDString + ".meshes"); List<DisplayObject> displayMeshes = new List<DisplayObject> (); try { using (var meshFile = File.Open (meshCachePath, FileMode.Open)) { BinaryFormatter bin = new BinaryFormatter (); displayMeshes = (List<DisplayObject>)bin.Deserialize (meshFile); } } catch (IOException ex) { System.Diagnostics.Debug.WriteLine ("Could not Deserialize the DisplayMeshes with exception: {0}", ex.Message); Rhino.Runtime.HostUtils.ExceptionReport (ex); } if (displayMeshes == null) return false; foreach (DisplayMesh me in displayMeshes) me.RestoreUsingMesh (mesh, material); if (Math.Abs (material.Transparency) < double.Epsilon) DisplayObjects.AddRange (displayMeshes); else TransparentObjects.AddRange ((IEnumerable<DisplayMesh>)displayMeshes); return true; }
/// <summary> /// Writes the displayMeshes to disk. /// </summary> public virtual void SaveDisplayMeshes (List<DisplayMesh> meshesToSave, Mesh mesh, ObjectAttributes attr, Material material) { string meshGUIDString = attr.ObjectId.ToString (); string meshCachePath = SupportPathForName (meshGUIDString + ".meshes"); try { using (var meshFile = File.Open (meshCachePath, FileMode.Create)) { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(meshFile, meshesToSave); } } catch (IOException ex) { Rhino.Runtime.HostUtils.ExceptionReport (ex); System.Diagnostics.Debug.WriteLine ("Could not Serialize the DisplayMeshes with exception: {0}", ex.Message); } }
/// <summary> /// Moves changes made to this RhinoObject into the RhinoDoc. /// </summary> /// <returns> /// true if changes were made. /// </returns> /// <example> /// <code source='examples\vbnet\ex_addlayout.vb' lang='vbnet'/> /// <code source='examples\cs\ex_addlayout.cs' lang='cs'/> /// <code source='examples\py\ex_addlayout.py' lang='py'/> /// </example> public bool CommitChanges() { bool rc = false; if (null != m_edited_geometry) { CommitGeometryChangesFunc func = GetCommitFunc(); if (null == func) return false; IntPtr pConstGeometry = m_edited_geometry.ConstPointer(); uint serial_number = func(m_rhinoobject_serial_number, pConstGeometry); if (serial_number > 0) { rc = true; m_rhinoobject_serial_number = serial_number; m_edited_geometry = null; } else return false; } if (null != m_edited_attributes) { rc = Document.Objects.ModifyAttributes(this, m_edited_attributes, false); if (rc) m_edited_attributes = null; } return rc; }