Beispiel #1
0
        public Result Execute(UIDocument uidoc)
        {
            Document doc = uidoc.Document;

            View3D view = doc.ActiveView as View3D;

            if (view == null)
            {
                TaskDialog.Show("glTFRevitExport", "You must be in a 3D view to export.");
                return(Result.Failed);
            }

            SaveFileDialog fileDialog = new SaveFileDialog();

            fileDialog.FileName   = "xv-import"; // default file name
            fileDialog.DefaultExt = ".gltf";     // default file extension

            bool?dialogResult = fileDialog.ShowDialog();

            if (dialogResult == true)
            {
                filename = fileDialog.FileName;
                string        directory = Path.GetDirectoryName(filename) + "\\";
                glTFContainer glConter  = ExportView3D(view, filename, directory);
                resultGltf = glConter.glTF;
            }

            return(Result.Succeeded);
        }
Beispiel #2
0
        public glTFContainer Finish()
        {
            glTF model = new glTF();

            model.asset       = new glTFVersion();
            model.scenes      = scenes;
            model.nodes       = nodes;
            model.meshes      = meshes;
            model.materials   = materials;
            model.buffers     = buffers;
            model.bufferViews = bufferViews;
            model.accessors   = accessors;

            glTFContainer container = new glTFContainer();

            container.glTF     = model;
            container.binaries = binaryFileData;

            return(container);
        }
Beispiel #3
0
        /// <summary>
        /// Runs once at end of export. Serializes the gltf
        /// properties and wites out the *.gltf and *.bin files.
        /// </summary>
        public void Finish()
        {
            Debug.WriteLine("Finishing...");

            glTFContainer container = manager.Finish();

            if (_cfgs.IncludeNonStdElements)
            {
                // TODO: [RM] Standardize what non glTF spec elements will go into
                // this "BIM glTF superset" and write a spec for it. Gridlines below
                // are an example.

                // Add gridlines as gltf nodes in the format:
                // Origin {Vec3<double>}, Direction {Vec3<double>}, Length {double}
                FilteredElementCollector col = new FilteredElementCollector(_doc)
                                               .OfClass(typeof(Grid));

                var grids = col.ToElements();
                foreach (Grid g in grids)
                {
                    Line l = g.Curve as Line;

                    var origin    = l.Origin;
                    var direction = l.Direction;
                    var length    = l.Length;

                    var xtras = new glTFExtras();
                    var grid  = new GridParameters();
                    grid.origin = new List <double>()
                    {
                        origin.X, origin.Y, origin.Z
                    };
                    grid.direction = new List <double>()
                    {
                        direction.X, direction.Y, direction.Z
                    };
                    grid.length          = length;
                    xtras.GridParameters = grid;
                    xtras.UniqueId       = g.UniqueId;
                    xtras.Properties     = Util.GetElementProperties(g, true);

                    var gridNode = new glTFNode();
                    gridNode.name   = g.Name;
                    gridNode.extras = xtras;

                    container.glTF.nodes.Add(gridNode);
                    container.glTF.nodes[0].children.Add(container.glTF.nodes.Count - 1);
                }
            }

            if (_cfgs.SingleBinary)
            {
                int bytePosition  = 0;
                int currentBuffer = 0;
                foreach (var view in container.glTF.bufferViews)
                {
                    if (view.buffer == 0)
                    {
                        bytePosition += view.byteLength;
                        continue;
                    }

                    if (view.buffer != currentBuffer)
                    {
                        view.buffer     = 0;
                        view.byteOffset = bytePosition;
                        bytePosition   += view.byteLength;
                    }
                }

                glTFBuffer buffer = new glTFBuffer();
                buffer.uri        = _filename + ".bin";
                buffer.byteLength = bytePosition;
                container.glTF.buffers.Clear();
                container.glTF.buffers.Add(buffer);

                using (FileStream f = File.Create(Path.Combine(_directory, buffer.uri)))
                {
                    using (BinaryWriter writer = new BinaryWriter(f))
                    {
                        foreach (var bin in container.binaries)
                        {
                            foreach (var coord in bin.contents.vertexBuffer)
                            {
                                writer.Write((float)coord);
                            }
                            // TODO: add writer for normals buffer
                            foreach (var index in bin.contents.indexBuffer)
                            {
                                writer.Write((int)index);
                            }
                        }
                    }
                }
            }
            else
            {
                // Write the *.bin files
                foreach (var bin in container.binaries)
                {
                    using (FileStream f = File.Create(Path.Combine(_directory, bin.name)))
                    {
                        using (BinaryWriter writer = new BinaryWriter(f))
                        {
                            foreach (var coord in bin.contents.vertexBuffer)
                            {
                                writer.Write((float)coord);
                            }
                            // TODO: add writer for normals buffer
                            foreach (var index in bin.contents.indexBuffer)
                            {
                                writer.Write((int)index);
                            }
                        }
                    }
                }
            }

            // Write the *.gltf file
            string serializedModel = JsonConvert.SerializeObject(container.glTF, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            File.WriteAllText(Path.Combine(_directory, _filename + ".gltf"), serializedModel);
        }