/// <summary> /// Calculates the area of an object /// </summary> public static double GetArea(IRhinoObject obj, double tol) { if (null != obj) { IOnCurve crv = OnCurve.ConstCast(obj.Geometry()); if (null != crv) { return(GetCurveArea(crv, tol)); } IOnSurface srf = OnSurface.ConstCast(obj.Geometry()); if (null != srf) { return(GetSurfaceArea(srf)); } IOnBrep brep = OnBrep.ConstCast(obj.Geometry()); if (null != brep) { return(GetBrepArea(brep)); } IOnMesh mesh = OnMesh.ConstCast(obj.Geometry()); if (null != mesh) { return(GetMeshArea(mesh)); } } return(0.0); }
public override bool CustomGeometryFilter(IRhinoObject obj, IOnGeometry geo, OnCOMPONENT_INDEX ci) { if (geo != null) { IOnCurve crv = OnCurve.ConstCast(geo); if (crv != null) { if (crv.IsClosed() && crv.IsPlanar()) { return(true); } else { return(false); } } IOnBrep brep = OnBrep.ConstCast(geo); if (brep != null) { if (brep.m_F.Count() == 1) { return(true); } else { return(false); } } IOnSurface srf = OnSurface.ConstCast(geo); if (srf != null) { return(true); } IOnMesh mesh = OnMesh.ConstCast(geo); if (mesh != null) { return(true); } } return(false); }
///<summary> This gets called when when the user runs this command.</summary> public override IRhinoCommand.result RunCommand(IRhinoCommandContext context) { MRhinoGetObject go = new MRhinoGetObject(); go.SetCommandPrompt("Select rectangular light"); go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.light_object); go.GetObjects(1, 1); if (go.CommandResult() != IRhinoCommand.result.success) { return(go.CommandResult()); } IOnLight light = go.Object(0).Light(); if (null == light) { return(IRhinoCommand.result.failure); } if (!light.IsRectangularLight()) { RhUtil.RhinoApp().Print("Not a rectangular light.\n"); return(IRhinoCommand.result.nothing); } On3dPoint origin = light.Location(); On3dVector xaxis = light.Length(); On3dVector yaxis = light.Width(); OnPlane plane = new OnPlane(origin, xaxis, yaxis); OnInterval x_interval = new OnInterval(0.0, xaxis.Length()); OnInterval y_interval = new OnInterval(0.0, yaxis.Length()); OnMesh mesh = RhUtil.RhinoMeshPlane(plane, x_interval, y_interval, 2, 2); if (null != mesh) { mesh.ConvertQuadsToTriangles(); context.m_doc.AddMeshObject(mesh); context.m_doc.Redraw(); } return(IRhinoCommand.result.cancel); }
///<summary> This gets called when when the user runs this command.</summary> public override IRhinoCommand.result RunCommand(IRhinoCommandContext context) { On3dPoint center = new On3dPoint(OnUtil.On_origin); OnSphere sphere = new OnSphere(center, 5.0); OnMesh mesh = RhUtil.RhinoMeshSphere(sphere, 10, 10); MRhinoMeshObject mesh_obj = context.m_doc.AddMeshObject(mesh); context.m_doc.Redraw(); MRhinoGetString gs = new MRhinoGetString(); gs.SetCommandPrompt("Press <Enter> to continue"); gs.AcceptNothing(); gs.GetString(); context.m_doc.DeleteObject(new MRhinoObjRef(mesh_obj)); context.m_doc.Redraw(); return(IRhinoCommand.result.success); }
/// <summary> /// MRhinoVisualAnalysisMode override /// </summary> public override void UpdateVertexColors(IRhinoObject obj, OnMesh[] meshes) { // Rhino calls this function when it is time for you // to set the false colors on the analysis mesh vertices. // For breps, there is one mesh per face. For mesh objects, // there is a single mesh. int count = meshes.Length; if (count > 0) { // A "mapping tag" is used to determine if the colors need to be set OnMappingTag mt = MappingTag(); for (int mi = 0; mi < count; mi++) { OnMesh mesh = meshes[mi]; if (null != mesh && 0 != mt.Compare(mesh.m_Ctag)) { // The mesh's mapping tag is different from ours. Either // the mesh has no false colors, has false colors set by // another analysis mode, has false colors set using // different m_z_range[]/m_hue_range[] values, or the // mesh has been moved. In any case, we need to set // the false colors to the ones we want. int vcount = mesh.m_V.Count(); ArrayOnColor vertex_colors = mesh.m_C; vertex_colors.SetCount(0); // in case something else had set the colors vertex_colors.Reserve(vcount); // for efficiency for (int vi = 0; vi < vcount; vi++) { double z = mesh.m_V[vi].z; OnColor color = FalseColor(z); vertex_colors.Append(color); } // set the mesh's color tag mesh.m_Ctag = mt; } } } }
/// <summary> /// Calculates the volume of an object /// </summary> public static double GetVolume(IRhinoObject obj) { if (null != obj && obj.IsSolid()) { IOnSurface srf = OnSurface.ConstCast(obj.Geometry()); if (null != srf) { return(GetSurfaceVolume(srf)); } IOnBrep brep = OnBrep.ConstCast(obj.Geometry()); if (null != brep) { return(GetBrepVolume(brep)); } IOnMesh mesh = OnMesh.ConstCast(obj.Geometry()); if (null != mesh) { return(GetMeshVolume(mesh)); } } return(0.0); }
//writing mesh, material is now set to default but will be selected //by layer or object, material definitions will be got from material table public string writeMeshObject(MRhinoObjectMesh rhinoMesh, string materialName) { OnMesh mesh = new OnMesh(); mesh = rhinoMesh.GetMesh(); System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en"); //use buffer for strings (much faster than string + string... ) StringBuilder buffer = new StringBuilder(); //materials //IRhinoMaterial material = rhinoMesh.m_parent_object.ObjectMaterial(); //string materialName = material.m_material_id.ToString(); //mesh int v_count = mesh.m_V.Count(); int f_count = mesh.m_F.Count(); buffer.Append(string.Format(ci, "<mesh vertices=\"{0}\" faces=\"{1}\" has_orco=\"off\" has_uv=\"false\" type=\"0\">", v_count, f_count)); buffer.Append(string.Format(ci, "<set_material sval=\"{0}\"/>", materialName)); //list verticles foreach (On3fPoint v in mesh.m_V) { buffer.Append(string.Format(ci, " <p x=\"{0}\" y=\"{1}\" z=\"{2}\"/>", v.x, v.y, v.z)); v_count++; } //list faces foreach (OnMeshFace f in mesh.m_F) { buffer.Append(string.Format(ci, " <f a=\"{0}\" b=\"{1}\" c=\"{2}\"/>\n", f.get_vi(0), f.get_vi(1), f.get_vi(2))); //quad ? if (!f.IsTriangle()) buffer.Append(string.Format(ci, " <f a=\"{0}\" b=\"{1}\" c=\"{2}\"/>\n", f.get_vi(0), f.get_vi(2), f.get_vi(3))); f_count++; } buffer.Append(string.Format(ci, "</mesh>")); mesh.Destroy(); return buffer.ToString(); }
/// <summary> /// MRhinoVisualAnalysisMode override /// </summary> public override void UpdateVertexColors(IRhinoObject obj, OnMesh[] meshes) { // Rhino calls this function when it is time for you // to set the false colors on the analysis mesh vertices. // For breps, there is one mesh per face. For mesh objects, // there is a single mesh. int count = meshes.Length; if (count > 0 ) { // A "mapping tag" is used to determine if the colors need to be set OnMappingTag mt = MappingTag(); for ( int mi = 0; mi < count; mi++ ) { OnMesh mesh = meshes[mi]; if (null != mesh && 0 != mt.Compare(mesh.m_Ctag) ) { // The mesh's mapping tag is different from ours. Either // the mesh has no false colors, has false colors set by // another analysis mode, has false colors set using // different m_z_range[]/m_hue_range[] values, or the // mesh has been moved. In any case, we need to set // the false colors to the ones we want. int vcount = mesh.m_V.Count(); ArrayOnColor vertex_colors = mesh.m_C; vertex_colors.SetCount(0); // in case something else had set the colors vertex_colors.Reserve(vcount); // for efficiency for (int vi = 0; vi < vcount; vi++ ) { double z = mesh.m_V[vi].z; OnColor color = FalseColor(z); vertex_colors.Append(color); } // set the mesh's color tag mesh.m_Ctag = mt; } } } }
/// <summary> /// Rhino calls WriteFile() to write document geometry to an external file. /// </summary> public override int WriteFile(string filename, int index, ref MRhinoDoc doc, ref IRhinoFileWriteOptions options) { int rc = 0; // false // Are we saving or exporting? bool bExport = options.Mode(IRhinoFileWriteOptions.ModeFlag.SelectedMode); // Are we in interactive or scripted mode? bool bScript = options.Mode(IRhinoFileWriteOptions.ModeFlag.BatchMode); List <IRhinoObject> objects = new List <IRhinoObject>(); // Get objects to save/export MRhinoObjectIterator it = new MRhinoObjectIterator(doc, IRhinoObjectIterator.object_state.undeleted_objects); if (bExport) { it.EnableSelectedFilter(); it.EnableVisibleFilter(); } // Do the iteration... MRhinoObject obj = null; for (obj = it.First(); null != obj; obj = it.Next()) { objects.Add(obj); } ArrayMRhinoObjectMesh meshes = new ArrayMRhinoObjectMesh(objects.Count); OnMeshParameters mesh_parameters = _mesh_parameters; int mesh_ui_style = (bScript) ? 2 : _mesh_ui_style; // Get the meshes to save/export IRhinoCommand.result res = RhUtil.RhinoMeshObjects(objects.ToArray(), ref mesh_parameters, ref mesh_ui_style, ref meshes); if (res == IRhinoCommand.result.success) { if (mesh_ui_style >= 0 && mesh_ui_style <= 1) { _mesh_ui_style = mesh_ui_style; } _mesh_parameters = mesh_parameters; } else { if (bExport) { RhUtil.RhinoApp().Print("No meshes to export.\n"); } else { RhUtil.RhinoApp().Print("No meshes to save.\n"); } return(rc); } try { // Open the file System.IO.StreamWriter file = new System.IO.StreamWriter(filename); // Write mesh count file.WriteLine(string.Format("meshcount={0}\n", meshes.Count())); // Write each mesh for (int i = 0; i < meshes.Count(); i++) { MRhinoObjectMesh obj_mesh = meshes[i]; OnMesh mesh = obj_mesh.GetMesh(); if (null != mesh) { // Write mesh number file.WriteLine(string.Format("mesh={0}\n", i)); // Write mesh vertex count file.WriteLine(string.Format("vertexcount={0}\n", mesh.m_V.Count())); // Write mesh face count file.WriteLine(string.Format("facecount={0}\n", mesh.m_F.Count())); // Write mesh vertices for (int vi = 0; vi < mesh.m_V.Count(); vi++) { On3fPoint p = mesh.m_V[vi]; file.WriteLine(string.Format("vertex=({0},{1},{2})\n", p.x, p.y, p.z)); } // Write mesh faces for (int fi = 0; fi < mesh.m_F.Count(); fi++) { OnMeshFace f = mesh.m_F[fi]; file.WriteLine(string.Format("face=({0},{1},{2},{3})\n", f.get_vi(0), f.get_vi(1), f.get_vi(2), f.get_vi(3))); } } } file.Close(); rc = 1; // true } catch (Exception e) { RhUtil.RhinoApp().Print(string.Format("{0}\n", e.Message)); } return(rc); }
///<summary> This gets called when when the user runs this command.</summary> public override IRhinoCommand.result RunCommand(IRhinoCommandContext context) { MRhinoGetObject go = new MRhinoGetObject(); go.SetCommandPrompt("Select surface, polysurface, or mesh to export"); go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object | IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object | IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object); go.GetObjects(1, 1); if (go.CommandResult() != IRhinoCommand.result.success) { return(go.CommandResult()); } MRhinoObjRef obj_ref = go.Object(0); IRhinoObject obj = obj_ref.Object(); if (null == obj) { return(IRhinoCommand.result.failure); } List <IRhinoObject> obj_list = new List <IRhinoObject>(); obj_list.Add(obj); ArrayMRhinoObjectMesh mesh_list = new ArrayMRhinoObjectMesh(obj_list.Count); OnMeshParameters mesh_parameters = _mesh_parameters; int mesh_ui_style = !context.IsInteractive() ? 2 : _mesh_ui_style; IRhinoCommand.result res = RhUtil.RhinoMeshObjects(obj_list.ToArray(), ref mesh_parameters, ref mesh_ui_style, ref mesh_list); if (res == IRhinoCommand.result.success) { if (mesh_ui_style >= 0 && mesh_ui_style <= 1) { _mesh_ui_style = mesh_ui_style; } _mesh_parameters = mesh_parameters; } else { RhUtil.RhinoApp().Print("No mesh to export.\n"); return(res); } string filename = string.Empty; if (context.IsInteractive()) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Title = "Export"; dialog.Filter = "Geomview files|*.off"; dialog.InitialDirectory = DirectoryManager.DefaultDirectory(DirectoryManager.FileTypes.ftExport); if (dialog.ShowDialog() != DialogResult.OK) { return(IRhinoCommand.result.cancel); } filename = dialog.FileName; } else { MRhinoGetString gs = new MRhinoGetString(); gs.SetCommandPrompt("Export file name"); gs.GetString(); if (gs.CommandResult() != IRhinoCommand.result.success) { return(gs.CommandResult()); } filename = gs.String().Trim(); } try { OnMesh mesh = mesh_list.First().GetMesh(); int vertex_count = mesh.VertexCount(); int face_count = mesh.FaceCount(); int edge_count = mesh.Topology().m_tope.Count(); System.IO.StreamWriter file = new System.IO.StreamWriter(filename); // Write out the first line of the file header file.WriteLine("OFF"); // Write the header information file.WriteLine(string.Format("{0} {1} {2}", vertex_count, face_count, edge_count)); file.WriteLine(); // Write out all the vertices in order for (int i = 0; i < vertex_count; i++) { On3fPoint p = mesh.m_V[i]; file.WriteLine(string.Format("{0} {1} {2}", p.x.ToString("F"), p.y.ToString("F"), p.z.ToString("F"))); } file.WriteLine(); // Write out all the faces for (int i = 0; i < face_count; i++) { OnMeshFace f = mesh.m_F[i]; if (f.IsQuad()) { file.WriteLine(string.Format("4 {0} {1} {2} {3}", f.get_vi(0), f.get_vi(1), f.get_vi(2), f.get_vi(3))); } else { file.WriteLine(string.Format("3 {0} {1} {2}", f.get_vi(0), f.get_vi(1), f.get_vi(2))); } } file.Close(); } catch (Exception e) { RhUtil.RhinoApp().Print(string.Format("{0}\n", e.Message)); } return(IRhinoCommand.result.success); }