コード例 #1
0
        /// <summary>
        /// Reads the model in ASCII format from the specified stream.
        /// </summary>
        private ImportedModelContainer TryReadAscii(ResourceLink source, Stream stream, StlImportOptions importOptions)
        {
            using (var reader = new StreamReader(stream, s_encoding, false, 128, true))
            {
                var newGeometry = new Geometry();

                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();

                    if (line == null)
                    {
                        continue;
                    }

                    line = line.Trim();

                    if (line.Length == 0 || line.StartsWith("\0") || line.StartsWith("#") || line.StartsWith("!") ||
                        line.StartsWith("$"))
                    {
                        continue;
                    }

                    ParseLine(line, out var id, out var values);
                    switch (id)
                    {
                    // Header.. not needed here
                    case "solid":
                        break;

                    // Geometry data
                    case "facet":
                        this.ReadFacet(reader, values, newGeometry, importOptions);
                        break;

                    // End of file
                    case "endsolid":
                        break;
                    }
                }

                // Generate result container
                var modelContainer = new ImportedModelContainer(source, importOptions);
                var resGeometryKey = modelContainer.GetResourceKey(RES_KEY_GEO_CLASS, RES_KEY_GEO_NAME);
                var resMaterialKey = modelContainer.GetResourceKey(RES_KEY_MAT_CLASS, RES_KEY_MAT_NAME);
                modelContainer.AddResource(new ImportedResourceInfo(
                                               resGeometryKey,
                                               _ => new GeometryResource(newGeometry)));
                modelContainer.AddResource(new ImportedResourceInfo(
                                               resMaterialKey,
                                               _ => new StandardMaterialResource()));
                var loadedMesh = new Mesh(resGeometryKey, resMaterialKey);
                modelContainer.AddObject(loadedMesh);

                // Append an object which transform the whole coordinate system
                modelContainer.FinishLoading(newGeometry.GenerateBoundingBox());

                return(modelContainer);
            }
        }
コード例 #2
0
        /// <summary>
        /// Reads the model from the specified binary stream.
        /// </summary>
        private ImportedModelContainer?TryReadBinary(ResourceLink source, Stream stream, StlImportOptions importOptions)
        {
            // Check length
            var length = stream.Length;

            if (length < 84)
            {
                throw new SeeingSharpException("Incomplete file (smaller that 84 bytes)");
            }

            // Read number of triangles
            using (var reader = new BinaryReader(stream, Encoding.GetEncoding("us-ascii"), true))
            {
                // Read header (is not needed)
                //  (solid stands for Ascii format)
                var header = s_encoding.GetString(reader.ReadBytes(80), 0, 80).Trim();

                if (header.StartsWith("solid", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }

                // Read and check number of triangles
                var numberTriangles = ReadUInt32(reader);
                if (length - 84 != numberTriangles * 50)
                {
                    throw new SeeingSharpException("Incomplete file (smaller that expected byte count)");
                }

                // Read geometry data
                var newGeometry = new Geometry((int)numberTriangles * 3);
                newGeometry.CreateSurface((int)numberTriangles);

                for (var loop = 0; loop < numberTriangles; loop++)
                {
                    ReadTriangle(reader, newGeometry, importOptions);
                }

                // Generate result container
                var modelContainer = new ImportedModelContainer(source, importOptions);
                var resGeometryKey = modelContainer.GetResourceKey(RES_KEY_GEO_CLASS, RES_KEY_GEO_NAME);
                var resMaterialKey = modelContainer.GetResourceKey(RES_KEY_MAT_CLASS, RES_KEY_MAT_NAME);
                modelContainer.AddResource(new ImportedResourceInfo(
                                               resGeometryKey,
                                               _ => new GeometryResource(newGeometry)));
                modelContainer.AddResource(new ImportedResourceInfo(
                                               resMaterialKey,
                                               _ => new StandardMaterialResource()));
                var loadedMesh = new Mesh(resGeometryKey, resMaterialKey);
                modelContainer.AddObject(loadedMesh);

                // Append an object which transform the whole coordinate system
                modelContainer.FinishLoading(newGeometry.GenerateBoundingBox());

                return(modelContainer);
            }
        }
コード例 #3
0
        /// <summary>
        /// Imports a model from the given file.
        /// </summary>
        /// <param name="sourceFile">The source file to be loaded.</param>
        /// <param name="importOptions">Some configuration for the importer.</param>
        public ImportedModelContainer ImportModel(ResourceLink sourceFile, ImportOptions importOptions)
        {
            // Get import options
            if (importOptions is not ACImportOptions acImportOptions)
            {
                throw new SeeingSharpException("Invalid import options for ACImporter!");
            }

            // Create result container
            var modelContainer = new ImportedModelContainer(sourceFile, acImportOptions);

            // Read the AC file
            var fileInfo = ACFileLoader.LoadFile(sourceFile.OpenInputStream());
            var geometry = ACFileLoader.GenerateGeometry(fileInfo);

            // Generate GeometryResource
            var resGeometry = modelContainer.GetResourceKey("Geometry", "Main");

            modelContainer.AddResource(new ImportedResourceInfo(
                                           resGeometry,
                                           _ => new GeometryResource(geometry)));

            // Generate Material resources
            var materialKeys = new NamedOrGenericKey[fileInfo.Materials.Count];

            for (var loop = 0; loop < materialKeys.Length; loop++)
            {
                var actACMaterial = fileInfo.Materials[loop];
                materialKeys[loop] = modelContainer.GetResourceKey("Material", actACMaterial.Name);

                modelContainer.AddResource(new ImportedResourceInfo(
                                               materialKeys[loop],
                                               _ => new StandardMaterialResource
                {
                    UseVertexColors      = false,
                    MaterialDiffuseColor = actACMaterial.Diffuse
                }));
            }

            // Create the mesh
            modelContainer.AddObject(new Mesh(resGeometry, materialKeys));

            modelContainer.FinishLoading(geometry.GenerateBoundingBox());

            return(modelContainer);
        }