internal override IntPtr _InternalGetConstPointer() { MaterialHolder mh = m__parent as MaterialHolder; if (mh != null) { return(mh.ConstMaterialPointer()); } #if RHINO_SDK if (m_is_default) { return(UnsafeNativeMethods.CRhinoMaterial_DefaultMaterial()); } if (m_doc != null) { return(UnsafeNativeMethods.CRhinoMaterialTable_GetMaterialPointer(m_doc.m_docId, m_id)); } #endif Rhino.FileIO.File3dm parent_file = m__parent as Rhino.FileIO.File3dm; if (parent_file != null) { IntPtr pModel = parent_file.ConstPointer(); return(UnsafeNativeMethods.ONX_Model_GetMaterialPointer(pModel, m_id)); } return(IntPtr.Zero); }
public _OdDb.ObjectIdCollection BakeGhGeometry(IGH_GeometricGoo gg) { var tmpFile = new Rhino.FileIO.File3dm(); AddGeometry(tmpFile, gg); return(BakeGhGeometry(tmpFile)); }
private static Mesh GetMeshes(string model) { var meshes = new List <Mesh>(); Mesh mesh = new Mesh(); // string folder = $@"{Util.AssemblyDirectory}\robots\tools"; string folder = Path.Combine(LibraryPath, "robots"); if (Directory.Exists(folder)) { var files = Directory.GetFiles(folder, "*.3dm"); Rhino.DocObjects.Layer layer = null; foreach (var file in files) { Rhino.FileIO.File3dm geometry = Rhino.FileIO.File3dm.Read($@"{file}"); layer = geometry.AllLayers.FirstOrDefault(x => x.Name == $"{model}"); if (layer != null) { meshes = geometry.Objects.Where(x => x.Attributes.LayerIndex == layer.Index).Select(x => x.Geometry as Mesh).ToList(); for (int i = 0; i < meshes.Count; ++i) { mesh.Append(meshes[i]); } } } if (layer == null) { throw new InvalidOperationException($" Tool \"{model}\" is not in the geometry file."); } } return(mesh); }
internal ViewInfo(Rhino.FileIO.File3dm parent, Guid id, IntPtr ptr, bool named_view_table) { m_parent = parent; m_id = id; m_ptr = ptr; m_named_view_table = named_view_table; }
private static Mesh GetMeshes(string model) { Mesh mesh = new Mesh(); if (Directory.Exists(LibraryPath)) { var files = Directory.GetFiles(LibraryPath, "*.3dm"); Rhino.DocObjects.Layer layer = null; foreach (var file in files) { Rhino.FileIO.File3dm geometry = Rhino.FileIO.File3dm.Read(file); layer = geometry.AllLayers.FirstOrDefault(x => x.Name == model); if (layer != null) { var meshes = geometry.Objects.Where(x => x.Attributes.LayerIndex == layer.Index).Select(x => x.Geometry as Mesh); foreach (var part in meshes) { mesh.Append(part); } break; } } if (layer == null) { throw new InvalidOperationException($" Tool \"{model}\" is not in the geometry file."); } } return(mesh); }
static List <Mesh> GetMeshes(string model) { var meshes = new List <Mesh>(); /* * using (var stream = new MemoryStream(Properties.Resources.Meshes)) * { * var formatter = new BinaryFormatter(); * JointMeshes jointMeshes = formatter.Deserialize(stream) as JointMeshes; * index = jointMeshes.Names.FindIndex(x => x == model); * if (index != -1) meshes = jointMeshes.Meshes[index]; * } */ string folder = LibraryPath; if (Directory.Exists(folder)) { var files = Directory.GetFiles(folder, "*.3dm"); foreach (var file in files) { Rhino.FileIO.File3dm geometry = Rhino.FileIO.File3dm.Read(file); var layer = geometry.AllLayers.FirstOrDefault(x => x.Name == model); if (layer != null) { int i = 0; while (true) { string name = $"{i++}"; var jointLayer = geometry.AllLayers.FirstOrDefault(x => (x.Name == name) && (x.ParentLayerId == layer.Id)); if (jointLayer == null) { break; } var mesh = geometry.Objects.FirstOrDefault(x => x.Attributes.LayerIndex == jointLayer.Index)?.Geometry as Mesh ?? new Mesh(); meshes.Add(mesh); } return(meshes); } } throw new InvalidOperationException($" Robot \"{model}\" is not in the geometry file."); } else { throw new DirectoryNotFoundException($" Robots folder not found in the Documents folder."); } }
// Returns: True if .3dm file was successfully read into a File3dmModel class. static bool ReadFileHelper(string filename, bool verboseTextDump, bool chunkDump, Rhino.FileIO.TextLog dump) { if (chunkDump) { return(Dump3dmFileHelper(filename, dump)); } dump.Print("\nOpenNURBS Archive File: {0}\n", filename); // open file containing opennurbs archive Rhino.FileIO.File3dm model = Rhino.FileIO.File3dm.Read(filename); if (model == null) { dump.Print(" Unable to open file.\n"); return(false); } dump.PopIndent(); dump.PopIndent(); dump.PopIndent(); dump.PopIndent(); dump.PopIndent(); dump.PopIndent(); dump.PopIndent(); dump.PushIndent(); // print diagnostic dump.Print("Successfully read.\n"); // see if everything is in good shape if (model.IsValid(dump)) { dump.Print("Model is valid.\n"); } else { dump.Print("Model is not valid.\n"); } // create a text dump of the model if (verboseTextDump) { dump.PushIndent(); model.DumpToTextLog(dump); dump.PopIndent(); } // destroy this model model.Dispose(); dump.PopIndent(); return(true); }
internal override IntPtr _InternalGetConstPointer() { #if RHINO_SDK if (m_doc != null) { return(UnsafeNativeMethods.CRhinoHatchPatternTable_GetHatchPatternPointer(m_doc.m_docId, m_id)); } #endif Rhino.FileIO.File3dm parent_file = m__parent as Rhino.FileIO.File3dm; if (parent_file != null) { IntPtr pModel = parent_file.NonConstPointer(); return(UnsafeNativeMethods.ONX_Model_GetHatchPatternPointer(pModel, m_id)); } return(IntPtr.Zero); }
internal override IntPtr _InternalGetConstPointer() { #if RHINO_SDK if (m_doc != null) { return(UnsafeNativeMethods.CRhinoLinetypeTable_GetLinetypePointer2(m_doc.m_docId, m_id)); } #endif Rhino.FileIO.File3dm file_parent = m__parent as Rhino.FileIO.File3dm; if (file_parent != null) { IntPtr pModel = file_parent.NonConstPointer(); return(UnsafeNativeMethods.ONX_Model_GetLinetypePointer(pModel, m_id)); } return(IntPtr.Zero); }
static void Main(string[] args) { Rhino.Compute.ComputeServer.AuthToken = "Set Your AuthToken"; var model = new Rhino.FileIO.File3dm(); // Create a couple Circles using a Local copy of rhino3dmIO var c1 = new Circle(new Point3d(0, 0, 0), 100); var c2 = new Circle(new Point3d(30, 30, 0), 70); model.Objects.AddCircle(c1); model.Objects.AddCircle(c2); // call compute to perform a curve boolean operation var intersectionCurves = Rhino.Compute.CurveCompute.CreateBooleanIntersection(c1.ToNurbsCurve(), c2.ToNurbsCurve()); Mesh[] intersectionMeshes = null; if (intersectionCurves != null) { // use local Rhino3dmIO to Create a Brep from curves var brep = Brep.CreateTrimmedPlane(c1.Plane, intersectionCurves); // call compute to mesh the Brep intersectionMeshes = Rhino.Compute.MeshCompute.CreateFromBrep(brep, MeshingParameters.FastRenderMesh); for (int i = 0; i < intersectionMeshes.Length; i++) { model.Objects.AddMesh(intersectionMeshes[i]); } } // just some helper routines to create on SVG files of the results so we can see what was generated string path = "circle_intersection.svg"; WriteSvgFiles(path, c1, c2, intersectionMeshes); System.Diagnostics.Process.Start(path); // Output Rhino 3dm file var path_3dm = System.IO.Directory.GetCurrentDirectory() + "/Outputs/circle_intersection.3dm"; model.Write(path_3dm, 5); Console.WriteLine("press any key to exit"); Console.ReadKey(); }
public bool WorldDraw(WorldDraw wd) { if (_geometry is Rhino.Geometry.Mesh mesh) { var faces = mesh.Faces.ToHost(); var points = new Point3dCollection(mesh.Vertices.ToHost()); var vertexData = new VertexData(); vertexData.SetNormalVectors(mesh.Normals.ToHost()); bool hasVertColor = mesh.VertexColors.Count != 0; vertexData.SetTrueColors(hasVertColor ? mesh.VertexColors.ToHost() : null); wd.Geometry.Shell(points, faces, null, null, vertexData, false); } else if (_geometry is Rhino.Geometry.Curve curve) { double deviation = System.Math.Max(wd.Deviation(DeviationType.MaxDevForCurve, curve.PointAtStart.ToHost()), 0.01 * curve.GetLength()); var polyline = curve.ToPolyline(10E+4 * Convert.VertexTolerance, Convert.AngleTolerance, deviation, 0.0); if (polyline != null) { var giPoly = new Polyline(); giPoly.Points = new Point3dCollection(polyline.ToPolyline().ToArray().ToHost()); wd.Geometry.Polyline(giPoly); } #if DEBUG else { var tmpFile = new Rhino.FileIO.File3dm(); tmpFile.Objects.AddCurve(curve); tmpFile.Write(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "BricsCAD", "CurveFail.3dm"), new Rhino.FileIO.File3dmWriteOptions()); } #endif } else if (_geometry is Rhino.Geometry.Point) { return(false); } return(true); }
public void Write(string path) { if (!System.IO.Directory.Exists(path)) { throw new Exception("Path does not exist."); } if (g == null || beam == null) { throw new Exception("Must add Glulam and Beam geometry!"); } Rhino.FileIO.File3dm file = new Rhino.FileIO.File3dm(); // Setup layers Layer lRoot = new Layer(); lRoot.Name = "tas"; file.AllLayers.Add(lRoot); lRoot = file.AllLayers.Last(); Layer lNC = new Layer(); lNC.Name = "NC"; lNC.IsVisible = false; lNC.IsLocked = true; file.AllLayers.Add(lNC); lNC = file.AllLayers.Last(); lNC.ParentLayerId = lRoot.Id; Layer lBL = new Layer(); lBL.Name = "BL"; lBL.IsLocked = true; file.AllLayers.Add(lBL); lBL = file.AllLayers.Last(); lBL.ParentLayerId = lRoot.Id; Layer lBE = new Layer(); lBE.Name = "BE"; lBE.IsLocked = true; file.AllLayers.Add(lBE); lBE = file.AllLayers.Last(); lBE.ParentLayerId = lRoot.Id; Layer lBL_Centreline = new Layer(); lBL_Centreline.Name = "BL_Centreline"; lBL_Centreline.Index = 1; lBL_Centreline.Color = Color.FromArgb(0, 0, 255); lBL_Centreline.ParentLayerId = lBL.Id; file.AllLayers.Add(lBL_Centreline); lBL_Centreline = file.AllLayers.Last(); Layer lBL_Geometry = new Layer(); lBL_Geometry.Name = "BL_Geometry"; lBL_Geometry.Color = Color.FromArgb(125, 200, 125); lBL_Geometry.ParentLayerId = lBL.Id; file.AllLayers.Add(lBL_Geometry); lBL_Geometry = file.AllLayers.Last(); Layer lBL_Safety = new Layer(); lBL_Safety.Name = "BL_Safety"; lBL_Safety.IsVisible = false; lBL_Safety.Color = Color.FromArgb(200, 200, 200); lBL_Safety.ParentLayerId = lBL.Id; file.AllLayers.Add(lBL_Safety); lBL_Safety = file.AllLayers.Last(); Layer lBE_Geometry = new Layer(); lBE_Geometry.Name = "BE_Geometry"; lBE_Geometry.Color = Color.FromArgb(100, 100, 100); lBE_Geometry.ParentLayerId = lBE.Id; file.AllLayers.Add(lBE_Geometry); lBE_Geometry = file.AllLayers.Last(); Layer lNC_Surface = new Layer(); lNC_Surface.Name = "NC_Surface"; lNC_Surface.Color = Color.FromArgb(255, 0, 255); lNC_Surface.ParentLayerId = lNC.Id; file.AllLayers.Add(lNC_Surface); lNC_Surface = file.AllLayers.Last(); Layer lNC_Normals = new Layer(); lNC_Normals.Name = "NC_SurfaceNormals"; lNC_Normals.Color = Color.FromArgb(255, 0, 150); lNC_Normals.ParentLayerId = lNC.Id; file.AllLayers.Add(lNC_Normals); lNC_Normals = file.AllLayers.Last(); Layer lNC_GlueFace = new Layer(); lNC_GlueFace.Name = "NC_GlueFace"; lNC_GlueFace.Color = Color.FromArgb(255, 0, 255); lNC_GlueFace.ParentLayerId = lNC.Id; file.AllLayers.Add(lNC_GlueFace); lNC_GlueFace = file.AllLayers.Last(); Layer lNC_Joints = new Layer(); lNC_Joints.Name = "NC_Joints"; lNC_Joints.Color = Color.FromArgb(255, 0, 255); lNC_Joints.ParentLayerId = lNC.Id; file.AllLayers.Add(lNC_Joints); lNC_Joints = file.AllLayers.Last(); Layer lNC_Drill = new Layer(); lNC_Drill.Name = "NC_Drill"; lNC_Drill.Color = Color.FromArgb(255, 0, 0); lNC_Drill.ParentLayerId = lNC.Id; file.AllLayers.Add(lNC_Drill); lNC_Drill = file.AllLayers.Last(); // Add objects ObjectAttributes oa; oa = new ObjectAttributes(); oa.LayerIndex = lBL_Centreline.Index; oa.Name = "Glulam_Centreline"; file.Objects.AddCurve(g.Centreline, oa); oa.LayerIndex = lBL_Geometry.Index; oa.UserDictionary.Set("LamWidth", g.Data.LamWidth); oa.UserDictionary.Set("LamHeight", g.Data.LamHeight); oa.UserDictionary.Set("NumWidth", g.Data.NumWidth); oa.UserDictionary.Set("NumHeight", g.Data.NumHeight); oa.Name = "Glulam_BoundingMesh"; Guid blank_id = file.Objects.AddBrep(g.GetBoundingBrep(), oa); oa.LayerIndex = lBE_Geometry.Index; oa.UserDictionary.Clear(); oa.Name = "Beam_Geometry"; file.Objects.AddBrep(beam, oa); oa.LayerIndex = lBL_Safety.Index; oa.Name = "Glulam_Safety"; Brep blank_safety = g.GetBoundingBrep(50.0); if (blank_safety != null) { file.Objects.AddBrep(blank_safety, oa); } for (int i = 0; i < Cuts_Surface.Count; ++i) { oa.LayerIndex = lNC_Surface.Index; oa.Name = string.Format("Machining_Surface_{0:00}", i); file.Objects.AddBrep(Cuts_Surface[i], oa); oa.LayerIndex = lNC_Normals.Index; oa.Name = string.Format("Machining_Surface_Normal_{0:00}", i); BrepFace bf = Cuts_Surface[i].Faces[0]; Vector3d normal = bf.NormalAt(bf.Domain(0).Mid, bf.Domain(1).Mid); Point3d point = bf.PointAt(bf.Domain(0).Mid, bf.Domain(1).Mid); file.Objects.AddTextDot(string.Format("NC_Srf_{0:00}", i), point + normal * 100.0, oa); file.Objects.AddLine(new Line(point, point + normal * 100.0), oa); } for (int i = 0; i < Cuts_GlueSurface.Count; ++i) { oa.LayerIndex = lNC_Surface.Index; oa.Name = string.Format("Glue_Surface_{0:00}", i); file.Objects.AddBrep(Cuts_GlueSurface[i], oa); oa.LayerIndex = lNC_Normals.Index; oa.Name = string.Format("Glue_Surface_Normal_{0:00}", i); BrepFace bf = Cuts_GlueSurface[i].Faces[0]; Vector3d normal = bf.NormalAt(bf.Domain(0).Mid, bf.Domain(1).Mid); Point3d point = bf.PointAt(bf.Domain(0).Mid, bf.Domain(1).Mid); file.Objects.AddTextDot(string.Format("NC_Glu_{0:00}", i), point + normal * 100.0, oa); file.Objects.AddLine(new Line(point, point + normal * 100.0), oa); } for (int i = 0; i < Cuts_Joint.Count; ++i) { oa.LayerIndex = lNC_Joints.Index; oa.Name = string.Format("Joint_Surface_{0:00}", i); file.Objects.AddBrep(Cuts_Joint[i], oa); oa.LayerIndex = lNC_Normals.Index; oa.Name = string.Format("Joint_Surface_Normal_{0:00}", i); BrepFace bf = Cuts_Joint[i].Faces[0]; Vector3d normal = bf.NormalAt(bf.Domain(0).Mid, bf.Domain(1).Mid); Point3d point = bf.PointAt(bf.Domain(0).Mid, bf.Domain(1).Mid); file.Objects.AddTextDot(string.Format("NC_Jnt_{0:00}", i), point + normal * 100.0, oa); file.Objects.AddLine(new Line(point, point + normal * 100.0), oa); } for (int i = 0; i < Drills_Joint.Count; ++i) { oa.LayerIndex = lNC_Drill.Index; oa.Name = string.Format("Joint_Drill_{0:00}", i); Vector3d dir = Drills_Joint[i].Direction; dir.Unitize(); file.Objects.AddTextDot(string.Format("NC_Drill_{0:00}", i), Drills_Joint[i].From - dir * 40.0, oa); file.Objects.AddLine(Drills_Joint[i], oa); } // Write notes and data string notes = ""; notes += "This file was created with tasTools (Tom Svilans, 2017).\n\n"; notes += "Blank data:\n\n"; notes += string.Format("LamWidth\n{0}\n", g.Data.LamWidth); notes += string.Format("LamHeight\n{0}\n", g.Data.LamHeight); notes += string.Format("NumWidth\n{0}\n", g.Data.NumWidth); notes += string.Format("NumHeight\n{0}\n", g.Data.NumHeight); notes += string.Format("Length\n{0}\n", g.Centreline.GetLength()); file.Notes.Notes = notes; file.Settings.ModelUnitSystem = UnitSystem.Millimeters; file.Write(path + "\\" + Name + ".3dm", 5); }
internal DimensionStyle(Guid id, Rhino.FileIO.File3dm parent) { m_id = id; m__parent = parent; }
// Start is called before the first frame update void Start() { ComputeServer.AuthToken = authToken; var model = new Rhino.FileIO.File3dm(); var curves = new List <Rhino.Geometry.NurbsCurve>(); for (int i = 0; i < 20; i++) { var s = 10f; var pt = new Rhino.Geometry.Point3d(Random.Range(-s, s), Random.Range(-s, s), 0); // model.Objects.AddPoint(pt); var r = Random.Range(1f, 3f); var circle = new Rhino.Geometry.Circle(pt, r); var curve = circle.ToNurbsCurve(); curves.Add(curve); } var unionCrvsC = CurveCompute.CreateBooleanUnion(curves); var height = Random.Range(1f, 5f); var extrusions = new List <Rhino.Geometry.Extrusion>(); foreach (var unionCrvC in unionCrvsC) { var extrusion = Rhino.Geometry.Extrusion.Create(unionCrvC, height, true); model.Objects.AddExtrusion(extrusion); extrusions.Add(extrusion); } var meshList = new List <Rhino.Geometry.Mesh>(); foreach (var extrusion in extrusions) { var brep = extrusion.ToBrep(); var meshes = MeshCompute.CreateFromBrep(brep); meshList.AddRange(meshes.ToList()); } foreach (var mesh in meshList) { Mesh meshObj = new Mesh(); var vertices = new List <Vector3>(); // mesh.RebuildNormals(); foreach (var meshVertex in mesh.Vertices) { var vertex = new Vector3(meshVertex.X, meshVertex.Z, meshVertex.Y); vertices.Add(vertex); } var triangles = new List <int>(); foreach (var meshFace in mesh.Faces) { if (meshFace.IsTriangle) { triangles.Add(meshFace.C); triangles.Add(meshFace.B); triangles.Add(meshFace.A); } else if (meshFace.IsQuad) { triangles.Add(meshFace.C); triangles.Add(meshFace.B); triangles.Add(meshFace.A); triangles.Add(meshFace.D); triangles.Add(meshFace.C); triangles.Add(meshFace.A); } } meshObj.vertices = vertices.ToArray(); meshObj.triangles = triangles.ToArray(); meshObj.RecalculateNormals(); GameObject gb = new GameObject(); gb.AddComponent <MeshFilter>().mesh = meshObj; gb.AddComponent <MeshRenderer>().material = mat; } var path = Application.dataPath + "/../Outputs/model.3dm"; model.Write(path, 5); }
internal Layer(Guid id, Rhino.FileIO.File3dm onxModel) { m_id = id; m_onx_model = onxModel; m__parent = onxModel; }
internal Linetype(Guid id, Rhino.FileIO.File3dm onxModel) { m_id = id; m__parent = onxModel; }
private void GenerateGeo() { model = new Rhino.FileIO.File3dm(); int num = 10; float outerRad = 2f; var curves = new List <Rhino.Geometry.NurbsCurve>(); for (int i = 0; i < num; i++) { var pt = new Rhino.Geometry.Point3d(0, 0, height / (num - 1) * i); var circle = new Rhino.Geometry.Circle(pt, pipeRad); var polygon = Rhino.Geometry.Polyline.CreateInscribedPolygon(circle, segments); var curve = polygon.ToNurbsCurve(); curve.Rotate(i * Mathf.Deg2Rad * angle, new Rhino.Geometry.Vector3d(0, 0, 1), new Rhino.Geometry.Point3d(0, 0, 0)); curve.Translate(new Rhino.Geometry.Vector3d(Mathf.Cos(Mathf.Deg2Rad * angle * i), Mathf.Sin(Mathf.Deg2Rad * angle * i), 0) * outerRad); curves.Add(curve); } var breps = BrepCompute.CreateFromLoft(curves, Rhino.Geometry.Point3d.Unset, Rhino.Geometry.Point3d.Unset, Rhino.Geometry.LoftType.Normal, false); var meshList = new List <Rhino.Geometry.Mesh>(); foreach (var brep in breps) { var brep2 = brep.CapPlanarHoles(0.001); // generates two additional breps var meshes = MeshCompute.CreateFromBrep(brep2); meshList.AddRange(meshes.ToList()); model.Objects.AddBrep(brep2); } foreach (var mesh in meshList) { Mesh meshObj = new Mesh(); var vertices = new List <Vector3>(); foreach (var meshVertex in mesh.Vertices) { var vertex = new Vector3(meshVertex.X, meshVertex.Z, meshVertex.Y); vertices.Add(vertex); } var triangles = new List <int>(); foreach (var meshFace in mesh.Faces) { if (meshFace.IsTriangle) { triangles.Add(meshFace.C); triangles.Add(meshFace.B); triangles.Add(meshFace.A); } else if (meshFace.IsQuad) { triangles.Add(meshFace.C); triangles.Add(meshFace.B); triangles.Add(meshFace.A); triangles.Add(meshFace.D); triangles.Add(meshFace.C); triangles.Add(meshFace.A); } } var normals = new List <Vector3>(); foreach (var normal in mesh.Normals) { normals.Add(new Vector3(normal.X, normal.Z, normal.Y)); } meshObj.vertices = vertices.ToArray(); meshObj.triangles = triangles.ToArray(); meshObj.normals = normals.ToArray(); GameObject gb = new GameObject(); gb.AddComponent <MeshFilter>().mesh = meshObj; gb.AddComponent <MeshRenderer>().material = mat; } }
public static void OptToRhino(OptFile opt, string rhinoPath, bool scale) { if (opt == null) { throw new ArgumentNullException("opt"); } string rhinoDirectory = Path.GetDirectoryName(rhinoPath); string rhinoName = Path.GetFileNameWithoutExtension(rhinoPath); var distances = opt.Meshes .SelectMany(t => t.Lods) .Select(t => t.Distance) .Distinct() .OrderByDescending(t => t) .ToArray(); for (int distance = 0; distance < distances.Length; distance++) { using (var file = new Rhino.FileIO.File3dm()) { file.Settings.ModelUnitSystem = Rhino.UnitSystem.Meters; int objectsIndex = 0; List<string> textureNames = new List<string>(); foreach (var mesh in opt.Meshes) { var lod = mesh.Lods.FirstOrDefault(t => t.Distance <= distances[distance]); if (lod == null) { continue; } foreach (var textureName in lod.FaceGroups .Where(t => t.Textures.Count > 0) .Select(t => t.Textures[0])) { if (!textureNames.Contains(textureName)) { textureNames.Add(textureName); } } string meshName = string.Format(CultureInfo.InvariantCulture, "{0}.{1:D3}", mesh.Descriptor.MeshType, objectsIndex); using (var layer = new Rhino.DocObjects.Layer()) { layer.Name = meshName; file.Layers.Add(layer); } foreach (var faceGroup in lod.FaceGroups) { using (var rhinoMesh = new Rhino.Geometry.Mesh()) using (var rhinoAttributes = new Rhino.DocObjects.ObjectAttributes()) { rhinoAttributes.Name = meshName; rhinoAttributes.LayerIndex = objectsIndex; if (faceGroup.Textures.Count > 0) { rhinoAttributes.MaterialIndex = textureNames.IndexOf(faceGroup.Textures[0]); } Action<Vector> addVertex; if (scale) { addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X * OptFile.ScaleFactor, vertex.Y * OptFile.ScaleFactor, vertex.Z * OptFile.ScaleFactor); } else { addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X, vertex.Y, vertex.Z); } Action<TextureCoordinates> addTexCoords = texCoords => rhinoMesh.TextureCoordinates.Add(texCoords.U, -texCoords.V); Action<Vector> addNormal = normal => rhinoMesh.Normals.Add(normal.X, normal.Y, normal.Z); int facesIndex = 0; foreach (var face in faceGroup.Faces) { var verticesIndex = face.VerticesIndex; var texCoordsIndex = face.TextureCoordinatesIndex; var normalsIndex = face.VertexNormalsIndex; addVertex(mesh.Vertices[verticesIndex.A]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.A]); addNormal(mesh.VertexNormals[normalsIndex.A]); facesIndex++; addVertex(mesh.Vertices[verticesIndex.B]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.B]); addNormal(mesh.VertexNormals[normalsIndex.B]); facesIndex++; addVertex(mesh.Vertices[verticesIndex.C]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.C]); addNormal(mesh.VertexNormals[normalsIndex.C]); facesIndex++; if (verticesIndex.D >= 0) { addVertex(mesh.Vertices[verticesIndex.D]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.D]); addNormal(mesh.VertexNormals[normalsIndex.D]); facesIndex++; } if (verticesIndex.D < 0) { rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3); } else { rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3, facesIndex - 4); } } rhinoMesh.Compact(); file.Objects.AddMesh(rhinoMesh, rhinoAttributes); } } objectsIndex++; } foreach (var textureName in textureNames) { Texture texture; opt.Textures.TryGetValue(textureName, out texture); using (var material = new Rhino.DocObjects.Material()) { material.Name = textureName; if (texture == null) { material.DiffuseColor = System.Drawing.Color.White; } else { texture.Save(Path.Combine(rhinoDirectory, textureName + ".png")); material.SetBitmapTexture(textureName + ".png"); if (texture.HasAlpha) { texture.SaveAlphaMap(Path.Combine(rhinoDirectory, textureName + "_alpha.png")); material.SetTransparencyTexture(textureName + "_alpha.png"); } } file.Materials.Add(material); } } file.Write(Path.Combine(rhinoDirectory, string.Format(CultureInfo.InvariantCulture, "{0}_{1}.3dm", rhinoName, distance)), 4); } } }
internal HatchPattern(Guid id, Rhino.FileIO.File3dm parent) { m_id = id; m__parent = parent; }
internal Material(Guid id, Rhino.FileIO.File3dm parent) { m_id = id; m__parent = parent; }
public static void OptToRhino(OptFile opt, string rhinoPath, bool scale) { if (opt == null) { throw new ArgumentNullException("opt"); } string rhinoDirectory = Path.GetDirectoryName(rhinoPath); string rhinoName = Path.GetFileNameWithoutExtension(rhinoPath); var distances = opt.Meshes .SelectMany(t => t.Lods) .Select(t => t.Distance) .Distinct() .OrderByDescending(t => t) .ToArray(); for (int distance = 0; distance < distances.Length; distance++) { using (var file = new Rhino.FileIO.File3dm()) { file.Settings.ModelUnitSystem = Rhino.UnitSystem.Meters; int objectsIndex = 0; List <string> textureNames = new List <string>(); foreach (var mesh in opt.Meshes) { var lod = mesh.Lods.FirstOrDefault(t => t.Distance <= distances[distance]); if (lod == null) { continue; } foreach (var textureName in lod.FaceGroups .Where(t => t.Textures.Count > 0) .Select(t => t.Textures[0])) { if (!textureNames.Contains(textureName)) { textureNames.Add(textureName); } } string meshName = string.Format(CultureInfo.InvariantCulture, "{0}.{1:D3}", mesh.Descriptor.MeshType, objectsIndex); using (var layer = new Rhino.DocObjects.Layer()) { layer.Name = meshName; file.Layers.Add(layer); } foreach (var faceGroup in lod.FaceGroups) { using (var rhinoMesh = new Rhino.Geometry.Mesh()) using (var rhinoAttributes = new Rhino.DocObjects.ObjectAttributes()) { rhinoAttributes.Name = meshName; rhinoAttributes.LayerIndex = objectsIndex; if (faceGroup.Textures.Count > 0) { rhinoAttributes.MaterialIndex = textureNames.IndexOf(faceGroup.Textures[0]); } Action <Vector> addVertex; if (scale) { addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X * OptFile.ScaleFactor, vertex.Y * OptFile.ScaleFactor, vertex.Z * OptFile.ScaleFactor); } else { addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X, vertex.Y, vertex.Z); } Action <TextureCoordinates> addTexCoords = texCoords => rhinoMesh.TextureCoordinates.Add(texCoords.U, -texCoords.V); Action <Vector> addNormal = normal => rhinoMesh.Normals.Add(normal.X, normal.Y, normal.Z); int facesIndex = 0; foreach (var face in faceGroup.Faces) { var verticesIndex = face.VerticesIndex; var texCoordsIndex = face.TextureCoordinatesIndex; var normalsIndex = face.VertexNormalsIndex; addVertex(mesh.Vertices[verticesIndex.A]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.A]); addNormal(mesh.VertexNormals[normalsIndex.A]); facesIndex++; addVertex(mesh.Vertices[verticesIndex.B]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.B]); addNormal(mesh.VertexNormals[normalsIndex.B]); facesIndex++; addVertex(mesh.Vertices[verticesIndex.C]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.C]); addNormal(mesh.VertexNormals[normalsIndex.C]); facesIndex++; if (verticesIndex.D >= 0) { addVertex(mesh.Vertices[verticesIndex.D]); addTexCoords(mesh.TextureCoordinates[texCoordsIndex.D]); addNormal(mesh.VertexNormals[normalsIndex.D]); facesIndex++; } if (verticesIndex.D < 0) { rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3); } else { rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3, facesIndex - 4); } } rhinoMesh.Compact(); file.Objects.AddMesh(rhinoMesh, rhinoAttributes); } } objectsIndex++; } foreach (var textureName in textureNames) { Texture texture; opt.Textures.TryGetValue(textureName, out texture); using (var material = new Rhino.DocObjects.Material()) { material.Name = textureName; if (texture == null) { material.DiffuseColor = System.Drawing.Color.White; } else { texture.Save(Path.Combine(rhinoDirectory, textureName + ".png")); material.SetBitmapTexture(textureName + ".png"); if (texture.HasAlpha) { texture.SaveAlphaMap(Path.Combine(rhinoDirectory, textureName + "_alpha.png")); material.SetTransparencyTexture(textureName + "_alpha.png"); } } file.Materials.Add(material); } } file.Write(Path.Combine(rhinoDirectory, string.Format(CultureInfo.InvariantCulture, "{0}_{1}.3dm", rhinoName, distance)), 4); } } }