Ejemplo n.º 1
0
        /// <summary>
        ///   Export the render mesh associated with a certain object
        /// </summary>
        /// <param name="parent">The parent node in the output XML document</param>
        /// <param name="obj">The RhinoObject instance to be exported</param>
        /// <returns>true if any content was exported</returns>
        private void ExportRenderMesh(XmlElement parent, RhinoObject obj)
        {
            var type = obj.ObjectType;

            if (type != ObjectType.Surface &&
                type != ObjectType.Brep &&
                type != ObjectType.Mesh &&
                type != ObjectType.Extrusion)
            {
                RhinoApp.WriteLine("Not exporting object of type " + type);
                return;
            }

            var meshes = RhinoObject.GetRenderMeshes(new[] { obj }, true, true);

            if (meshes == null)
            {
                return;
            }

            foreach (var meshRef in meshes)
            {
                if (meshRef == null)
                {
                    continue;
                }

                var mesh  = meshRef.Mesh();
                var index = _meshStore.Store(mesh, obj.Name);
                _mitsubaXml.CreateShape(parent, obj, index, _meshStore.Filename);
            }
        }
Ejemplo n.º 2
0
        private void ParseRhinoMeshes()
        {
            HashSet <RhinoObject> DocObjects = CollectExportedRhinoObjects(RhinoDocument, InstanceDefinitionHierarchyNodeDictionary.Values);

            // Make sure all render meshes are generated before attempting to export them.
            RhinoObject.GetRenderMeshes(DocObjects, /*okToCreate=*/ true, /*returnAllObjects*/ false);

            foreach (RhinoObject CurrentObject in DocObjects)
            {
                Mesh[] RenderMeshes = CurrentObject.GetMeshes(MeshType.Render);

                if (RenderMeshes != null && RenderMeshes.Length > 0)
                {
                    List <Mesh>             ExportedMeshes   = new List <Mesh>(RenderMeshes);
                    List <ObjectAttributes> MeshesAttributes = new List <ObjectAttributes>(RenderMeshes.Length);

                    BrepObject CurrentBrep = (CurrentObject as BrepObject);
                    if (CurrentBrep != null && CurrentBrep.HasSubobjectMaterials)
                    {
                        RhinoObject[] SubObjects = CurrentBrep.GetSubObjects();

                        for (int SubObjectIndex = 0; SubObjectIndex < SubObjects.Length; ++SubObjectIndex)
                        {
                            MeshesAttributes.Add(SubObjects[SubObjectIndex].Attributes);
                        }
                    }
                    else
                    {
                        for (int RenderMeshIndex = 0; RenderMeshIndex < RenderMeshes.Length; ++RenderMeshIndex)
                        {
                            MeshesAttributes.Add(CurrentObject.Attributes);
                        }
                    }

                    DatasmithMeshInfo MeshInfo = GenerateMeshInfo(CurrentObject.Id, ExportedMeshes, MeshesAttributes);
                    if (MeshInfo != null)
                    {
                        ObjectIdToMeshInfoDictionary[CurrentObject.Id] = MeshInfo;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Export the render mesh associated with a certain object
        /// </summary>
        /// <param name="parent">The parent node in the output XML document</param>
        /// <param name="obj">The RhinoObject instance to be exported</param>
        /// <returns>true if any content was exported</returns>
        private bool ExportRenderMesh(XmlElement parent, RhinoObject obj, bool serialized)
        {
            ObjectType type = obj.ObjectType;

            if (type != ObjectType.Surface && type != ObjectType.Brep &&
                type != ObjectType.Mesh && type != ObjectType.Extrusion)
            {
                Log("Not exporting object of type " + type);
                return(false);
            }

            ObjRef[] meshes = RhinoObject.GetRenderMeshes(
                new RhinoObject[] { obj }, true, true);

            if (meshes == null)
            {
                return(false);
            }

            foreach (ObjRef meshRef in meshes)
            {
                if (meshRef == null)
                {
                    continue;
                }

                XmlElement shapeElement = m_xmlDocument.CreateElement("shape");
                if (obj.Name != null && obj.Name.Length > 0)
                {
                    shapeElement.AppendChild(m_xmlDocument.CreateComment(" Rhino object '" + obj.Name + "' "));
                }

                RhinoDoc doc  = obj.Document;
                Mesh     mesh = meshRef.Mesh();

                if (mesh.Faces == null)
                {
                    continue;
                }

                int matIdx = -1;
                switch (obj.Attributes.MaterialSource)
                {
                case ObjectMaterialSource.MaterialFromLayer:
                    matIdx = doc.Layers[obj.Attributes.LayerIndex].RenderMaterialIndex;
                    break;

                case ObjectMaterialSource.MaterialFromObject:
                    matIdx = obj.Attributes.MaterialIndex;
                    break;
                }


                RhinoApp.WriteLine("Exporting Mesh:" + obj.Name);
                if (m_meshStore.WriteSerialized)
                {
                    int index = m_meshStore.StoreSerialized(mesh, obj.Name);
                    shapeElement.AppendChild(MakeProperty("filename", m_meshStore.Filename));
                    shapeElement.AppendChild(MakeProperty("shapeIndex", index));
                    shapeElement.SetAttribute("type", "serialized");
                }
                else
                {
                    String filename = m_meshStore.StoreOBJ(mesh, obj.Name);
                    shapeElement.AppendChild(MakeProperty("filename", filename));
                    shapeElement.SetAttribute("type", "obj");
                }
                parent.AppendChild(shapeElement);

                if (matIdx >= 0 && m_xmlIdMap.ContainsKey(matIdx))
                {
                    shapeElement.AppendChild(MakeReference(m_xmlIdMap[matIdx]));

                    /* Create an area emitter if requested */
                    Material mat = doc.Materials[matIdx];
                    if (mat.EmissionColor.GetBrightness() > 0)
                    {
                        XmlElement emitterElement = m_xmlDocument.CreateElement("emitter");
                        emitterElement.SetAttribute("type", "area");
                        emitterElement.AppendChild(MakeProperty("radiance", mat.EmissionColor));
                        shapeElement.AppendChild(emitterElement);
                    }
                }
            }

            return(meshes.Length > 0);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Export the render mesh associated with a certain object
        /// </summary>
        /// <param name="parent">The parent node in the output XML document</param>
        /// <param name="obj">The RhinoObject instance to be exported</param>
        /// <returns>true if any content was exported</returns>
        private bool ExportRenderMesh(XmlElement parent, RhinoObject obj)
        {
            var type = obj.ObjectType;

            if (type != ObjectType.Surface && type != ObjectType.Brep && type != ObjectType.Mesh &&
                type != ObjectType.Extrusion)
            {
                Log("Not exporting object of type " + type);
                return(false);
            }
            var meshes = RhinoObject.GetRenderMeshes(new[] { obj }, true, true);

            if (meshes == null)
            {
                return(false);
            }
            foreach (var meshRef in meshes)
            {
                if (meshRef == null)
                {
                    continue;
                }
                var shapeElement = _xmlDocument.CreateElement("shape");
                if (obj.Name.Length > 0)
                {
                    shapeElement.AppendChild(_xmlDocument.CreateComment(" Rhino object '" + obj.Name + "' "));
                }
                var doc    = obj.Document;
                var mesh   = meshRef.Mesh();
                var matIdx = -1;
                switch (obj.Attributes.MaterialSource)
                {
                case ObjectMaterialSource.MaterialFromLayer:
                    matIdx = doc.Layers[obj.Attributes.LayerIndex].RenderMaterialIndex;
                    break;

                case ObjectMaterialSource.MaterialFromObject:
                    matIdx = obj.Attributes.MaterialIndex;
                    break;
                }

                var index = _meshStore.Store(mesh, obj.Name);
                shapeElement.AppendChild(MakeProperty("filename", _meshStore.Filename));
                shapeElement.AppendChild(MakeProperty("shapeIndex", index));
                shapeElement.SetAttribute("type", "serialized");
                parent.AppendChild(shapeElement);
                if (matIdx >= 0 && _xmlIdMap.ContainsKey(matIdx))
                {
                    //Referenciamos el material del modelo con el material mitsuba
                    shapeElement.AppendChild(MakeReference(_xmlIdMap[matIdx]));
                    /* Create an area emitter if requested */
                    var mat = doc.Materials[matIdx];
                    if (mat.EmissionColor.GetBrightness() > 0)
                    {
                        var emitterElement = _xmlDocument.CreateElement("emitter");
                        emitterElement.SetAttribute("type", "area");
                        emitterElement.AppendChild(MakeProperty("radiance", mat.EmissionColor));
                        shapeElement.AppendChild(emitterElement);
                    }
                }
            }

            return(meshes.Length > 0);
        }
Ejemplo n.º 5
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            GetObject go = new GetObject();

            go.SetCommandPrompt("Select surfaces, polysurfaces, or meshes");
            go.GeometryFilter  = ObjectType.Surface | ObjectType.PolysrfFilter | ObjectType.Mesh;
            go.GroupSelect     = true;
            go.SubObjectSelect = false;
            go.GetMultiple(1, 0);
            if (go.CommandResult() != Result.Success)
            {
                return(go.CommandResult());
            }

            List <Mesh>        InMeshes      = new List <Mesh>(go.ObjectCount);
            List <RhinoObject> InMeshObjects = new List <RhinoObject>(go.ObjectCount);
            List <RhinoObject> InObjects     = new List <RhinoObject>(go.ObjectCount);

            for (int i = 0; i < go.ObjectCount; i++)
            {
                ObjRef objRef = go.Object(i);
                Mesh   mesh   = objRef.Mesh();
                if (null == mesh)
                {
                    InObjects.Add(objRef.Object());
                }
                else
                {
                    InMeshes.Add(mesh);
                    InMeshObjects.Add(objRef.Object());
                }
            }

            ObjRef[] meshRefs = null;
            if (InObjects.Count > 0)
            {
                meshRefs = RhinoObject.GetRenderMeshes(InObjects, true, false);
                if (null != meshRefs)
                {
                    for (int i = 0; i < meshRefs.Length; i++)
                    {
                        Mesh mesh = meshRefs[i].Mesh();
                        if (null != mesh)
                        {
                            InMeshes.Add(mesh);
                        }
                    }
                }
            }

            RhinoViewport vp = doc.Views.ActiveView.ActiveViewport;

            for (int i = 0; i < InMeshes.Count; i++)
            {
                Polyline[] plines = InMeshes[i].GetOutlines(vp);
                if (null != plines)
                {
                    for (int j = 0; j < plines.Length; j++)
                    {
                        Rhino.Geometry.PolylineCurve plineCrv = new PolylineCurve(plines[j]);
                        plineCrv.RemoveShortSegments(RhinoMath.SqrtEpsilon);
                        if (plineCrv.IsValid)
                        {
                            Guid        objId = doc.Objects.AddCurve(plineCrv);
                            RhinoObject obj   = doc.Objects.Find(objId);
                            if (null != obj)
                            {
                                obj.Select(true);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < InObjects.Count; i++)
            {
                InObjects[i].Select(false);
            }
            for (int i = 0; i < InMeshObjects.Count; i++)
            {
                InMeshObjects[i].Select(false);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }