diff --git a/source/export/exporter3dm.js b/source/export/exporter3dm.js index 81d7049..77bb1c9 100644 --- a/source/export/exporter3dm.js +++ b/source/export/exporter3dm.js @@ -93,7 +93,7 @@ OV.Exporter3dm = class extends OV.ExporterBase writeOptions.version = 6; let rhinoDocBuffer = rhinoDoc.toByteArray (writeOptions); - rhinoFile.SetContent (rhinoDocBuffer); + rhinoFile.SetBufferContent (rhinoDocBuffer); onFinish (); } }; diff --git a/source/export/exporterbase.js b/source/export/exporterbase.js index 092dff1..0d4e761 100644 --- a/source/export/exporterbase.js +++ b/source/export/exporterbase.js @@ -16,12 +16,24 @@ OV.ExportedFile = class this.name = name; } - GetContent () + GetTextContent () + { + let text = OV.ArrayBufferToUtf8String (this.content); + return text; + } + + GetBufferContent () { return this.content; } - SetContent (content) + SetTextContent (content) + { + let buffer = OV.Utf8StringToArrayBuffer (content); + this.content = buffer; + } + + SetBufferContent (content) { this.content = content; } diff --git a/source/export/exportergltf.js b/source/export/exportergltf.js index 54ba5fd..0f534ec 100644 --- a/source/export/exportergltf.js +++ b/source/export/exportergltf.js @@ -51,7 +51,7 @@ OV.ExporterGltf = class extends OV.ExporterBase let textureIndex = fileNameToIndex[fileName]; if (textureIndex === undefined) { let textureFile = new OV.ExportedFile (fileName); - textureFile.SetContent (texture.buffer); + textureFile.SetBufferContent (texture.buffer); files.push (textureFile); textureIndex = mainJson.textures.length; @@ -67,8 +67,8 @@ OV.ExporterGltf = class extends OV.ExporterBase return textureIndex; }); - gltfFile.SetContent (JSON.stringify (mainJson, null, 4)); - binFile.SetContent (mainBuffer); + gltfFile.SetTextContent (JSON.stringify (mainJson, null, 4)); + binFile.SetBufferContent (mainBuffer); } ExportBinaryContent (model, files) @@ -164,7 +164,7 @@ OV.ExporterGltf = class extends OV.ExporterBase } WriteCharacters (glbWriter, 0, mainBinaryBufferAlignedLength - mainBinaryBufferLength); - glbFile.SetContent (glbWriter.GetBuffer ()); + glbFile.SetBufferContent (glbWriter.GetBuffer ()); } GetMeshData (model) diff --git a/source/export/exporterobj.js b/source/export/exporterobj.js index cb7e6a9..e981f71 100644 --- a/source/export/exporterobj.js +++ b/source/export/exporterobj.js @@ -25,7 +25,7 @@ OV.ExporterObj = class extends OV.ExporterBase }); if (fileIndex === -1) { let textureFile = new OV.ExportedFile (fileName); - textureFile.SetContent (texture.buffer); + textureFile.SetBufferContent (texture.buffer); files.push (textureFile); } } @@ -50,7 +50,7 @@ OV.ExporterObj = class extends OV.ExporterBase WriteTexture (mtlWriter, 'map_Ks', material.specularMap, files); WriteTexture (mtlWriter, 'bump', material.bumpMap, files); } - mtlFile.SetContent (mtlWriter.GetText ()); + mtlFile.SetTextContent (mtlWriter.GetText ()); let objWriter = new OV.TextWriter (); objWriter.WriteLine (this.GetHeaderText ()); @@ -105,7 +105,7 @@ OV.ExporterObj = class extends OV.ExporterBase uvOffset += mesh.TextureUVCount (); } - objFile.SetContent (objWriter.GetText ()); + objFile.SetTextContent (objWriter.GetText ()); onFinish (); } diff --git a/source/export/exporteroff.js b/source/export/exporteroff.js index 38348fe..d2116b1 100644 --- a/source/export/exporteroff.js +++ b/source/export/exporteroff.js @@ -28,7 +28,7 @@ OV.ExporterOff = class extends OV.ExporterBase } }); - offFile.SetContent (offWriter.GetText ()); + offFile.SetTextContent (offWriter.GetText ()); onFinish (); } }; diff --git a/source/export/exporterply.js b/source/export/exporterply.js index 52fde7d..cad1775 100644 --- a/source/export/exporterply.js +++ b/source/export/exporterply.js @@ -41,7 +41,7 @@ OV.ExporterPly = class extends OV.ExporterBase } }); - plyFile.SetContent (plyWriter.GetText ()); + plyFile.SetTextContent (plyWriter.GetText ()); } ExportBinary (model, files) @@ -75,7 +75,7 @@ OV.ExporterPly = class extends OV.ExporterBase } }); - plyFile.SetContent (plyWriter.GetBuffer ()); + plyFile.SetBufferContent (plyWriter.GetBuffer ()); } GetHeaderText (format, vertexCount, triangleCount) diff --git a/source/export/exporterstl.js b/source/export/exporterstl.js index 904a764..33d3adb 100644 --- a/source/export/exporterstl.js +++ b/source/export/exporterstl.js @@ -42,7 +42,7 @@ OV.ExporterStl = class extends OV.ExporterBase }); stlWriter.WriteLine ('endsolid Model'); - stlFile.SetContent (stlWriter.GetText ()); + stlFile.SetTextContent (stlWriter.GetText ()); } ExportBinary (model, files) @@ -80,6 +80,6 @@ OV.ExporterStl = class extends OV.ExporterBase stlWriter.WriteUnsignedInteger16 (0); }); - stlFile.SetContent (stlWriter.GetBuffer ()); + stlFile.SetBufferContent (stlWriter.GetBuffer ()); } }; diff --git a/source/import/importer.js b/source/import/importer.js index 3ee1219..b8d4ee7 100644 --- a/source/import/importer.js +++ b/source/import/importer.js @@ -14,13 +14,11 @@ OV.File = class this.name = OV.GetFileName (file.name); this.extension = OV.GetFileExtension (file.name); } - this.format = null; this.content = null; } - SetContent (format, content) + SetContent (content) { - this.format = format; this.content = content; } }; @@ -141,36 +139,22 @@ OV.FileList = class complete (); return; } - let format = this.GetFileFormat (file); let loaderPromise = null; if (file.source === OV.FileSource.Url) { - loaderPromise = OV.RequestUrl (file.fileUrl, format); + loaderPromise = OV.RequestUrl (file.fileUrl, OV.FileFormat.Binary); } else if (file.source === OV.FileSource.File) { - loaderPromise = OV.ReadFile (file.fileObject, format); + loaderPromise = OV.ReadFile (file.fileObject, OV.FileFormat.Binary); } else { complete (); return; } loaderPromise.then ((content) => { - file.SetContent (format, content); + file.SetContent (content); }).catch (() => { }).finally (() => { complete (); }); } - - GetFileFormat (file) - { - for (let importerIndex = 0; importerIndex < this.importers.length; importerIndex++) { - let importer = this.importers[importerIndex]; - let extension = file.extension; - let knownFormats = importer.GetKnownFileFormats (); - if (knownFormats[extension] !== undefined) { - return knownFormats[extension]; - } - } - return OV.FileFormat.Binary; - } FindImporter (file) { diff --git a/source/import/importer3dm.js b/source/import/importer3dm.js index 9f1cdca..f5f87a9 100644 --- a/source/import/importer3dm.js +++ b/source/import/importer3dm.js @@ -10,14 +10,7 @@ OV.Importer3dm = class extends OV.ImporterBase { return extension === '3dm'; } - - GetKnownFileFormats () - { - return { - '3dm' : OV.FileFormat.Binary - }; - } - + GetUpDirection () { return OV.Direction.Z; diff --git a/source/import/importer3ds.js b/source/import/importer3ds.js index c44de06..168c279 100644 --- a/source/import/importer3ds.js +++ b/source/import/importer3ds.js @@ -56,13 +56,6 @@ OV.Importer3ds = class extends OV.ImporterBase return extension === '3ds'; } - GetKnownFileFormats () - { - return { - '3ds' : OV.FileFormat.Binary - }; - } - GetUpDirection () { return OV.Direction.Z; diff --git a/source/import/importerbase.js b/source/import/importerbase.js index 137103c..4fe116c 100644 --- a/source/import/importerbase.js +++ b/source/import/importerbase.js @@ -55,11 +55,6 @@ OV.ImporterBase = class return false; } - GetKnownFileFormats () - { - return {}; - } - GetUpDirection () { return OV.Direction.Z; diff --git a/source/import/importergltf.js b/source/import/importergltf.js index 55056f1..01e74dd 100644 --- a/source/import/importergltf.js +++ b/source/import/importergltf.js @@ -483,15 +483,6 @@ OV.ImporterGltf = class extends OV.ImporterBase return extension === 'gltf' || extension === 'glb'; } - GetKnownFileFormats () - { - return { - 'gltf' : OV.FileFormat.Text, - 'glb' : OV.FileFormat.Binary, - 'bin' : OV.FileFormat.Binary - }; - } - GetUpDirection () { return OV.Direction.Y; @@ -520,7 +511,8 @@ OV.ImporterGltf = class extends OV.ImporterBase ProcessGltf (fileContent, onFinish) { - let gltf = JSON.parse (fileContent); + let textContent = OV.ArrayBufferToUtf8String (fileContent); + let gltf = JSON.parse (textContent); if (gltf.asset.version !== '2.0') { this.SetError ('Invalid glTF version.'); onFinish (); diff --git a/source/import/importerifc.js b/source/import/importerifc.js index c7edc36..4647a00 100644 --- a/source/import/importerifc.js +++ b/source/import/importerifc.js @@ -11,13 +11,6 @@ OV.ImporterIfc = class extends OV.ImporterBase return extension === 'ifc'; } - GetKnownFileFormats () - { - return { - 'ifc' : OV.FileFormat.Binary - }; - } - GetUpDirection () { return OV.Direction.Y; diff --git a/source/import/importero3dv.js b/source/import/importero3dv.js index b7f1538..1986896 100644 --- a/source/import/importero3dv.js +++ b/source/import/importero3dv.js @@ -9,14 +9,7 @@ OV.ImporterO3dv = class extends OV.ImporterBase { return extension === 'o3dv'; } - - GetKnownFileFormats () - { - return { - 'o3dv' : OV.FileFormat.Text - }; - } - + GetUpDirection () { return OV.Direction.Z; @@ -34,7 +27,8 @@ OV.ImporterO3dv = class extends OV.ImporterBase ImportContent (fileContent, onFinish) { - let content = JSON.parse (fileContent); + let textContent = OV.ArrayBufferToUtf8String (fileContent); + let content = JSON.parse (textContent); if (content.materials !== undefined) { for (let i = 0; i < content.materials.length; i++) { const materialContent = content.materials[i]; diff --git a/source/import/importerobj.js b/source/import/importerobj.js index 3aaa5ef..4df68ff 100644 --- a/source/import/importerobj.js +++ b/source/import/importerobj.js @@ -9,14 +9,6 @@ OV.ImporterObj = class extends OV.ImporterBase { return extension === 'obj'; } - - GetKnownFileFormats () - { - return { - 'obj' : OV.FileFormat.Text, - 'mtl' : OV.FileFormat.Text - }; - } GetUpDirection () { @@ -55,7 +47,8 @@ OV.ImporterObj = class extends OV.ImporterBase ImportContent (fileContent, onFinish) { - OV.ReadLines (fileContent, (line) => { + let textContent = OV.ArrayBufferToUtf8String (fileContent); + OV.ReadLines (textContent, (line) => { if (!this.WasError ()) { this.ProcessLine (line); } @@ -212,7 +205,8 @@ OV.ImporterObj = class extends OV.ImporterBase let fileName = OV.NameFromLine (line, keyword.length, '#'); let fileBuffer = this.callbacks.getFileBuffer (fileName); if (fileBuffer !== null) { - OV.ReadLines (fileBuffer, (line) => { + let textContent = OV.ArrayBufferToUtf8String (fileBuffer); + OV.ReadLines (textContent, (line) => { if (!this.WasError ()) { this.ProcessLine (line); } diff --git a/source/import/importeroff.js b/source/import/importeroff.js index fc23777..f3a191f 100644 --- a/source/import/importeroff.js +++ b/source/import/importeroff.js @@ -9,14 +9,7 @@ OV.ImporterOff = class extends OV.ImporterBase { return extension === 'off'; } - - GetKnownFileFormats () - { - return { - 'off' : OV.FileFormat.Text - }; - } - + GetUpDirection () { return OV.Direction.Y; @@ -42,7 +35,8 @@ OV.ImporterOff = class extends OV.ImporterBase ImportContent (fileContent, onFinish) { - OV.ReadLines (fileContent, (line) => { + let textContent = OV.ArrayBufferToUtf8String (fileContent); + OV.ReadLines (textContent, (line) => { if (!this.WasError ()) { this.ProcessLine (line); } diff --git a/source/import/importerply.js b/source/import/importerply.js index c799760..21420a4 100644 --- a/source/import/importerply.js +++ b/source/import/importerply.js @@ -164,13 +164,6 @@ OV.ImporterPly = class extends OV.ImporterBase return extension === 'ply'; } - GetKnownFileFormats () - { - return { - 'ply' : OV.FileFormat.Binary - }; - } - GetUpDirection () { return OV.Direction.Y; diff --git a/source/import/importerstl.js b/source/import/importerstl.js index 8b22b7c..9fc6efc 100644 --- a/source/import/importerstl.js +++ b/source/import/importerstl.js @@ -10,13 +10,6 @@ OV.ImporterStl = class extends OV.ImporterBase return extension === 'stl'; } - GetKnownFileFormats () - { - return { - 'stl' : OV.FileFormat.Binary - }; - } - GetUpDirection () { return OV.Direction.Z; diff --git a/source/threejs/threeimporter.js b/source/threejs/threeimporter.js index c5c1344..4004737 100644 --- a/source/threejs/threeimporter.js +++ b/source/threejs/threeimporter.js @@ -231,16 +231,6 @@ OV.ThreeImporter = class extends OV.ImporterBase return false; } - GetKnownFileFormats () - { - let result = {}; - for (let i = 0; i < this.loaders.length; i++) { - let loader = this.loaders[i]; - result[loader.GetExtension ()] = OV.FileFormat.Binary; - } - return result; - } - GetUpDirection () { return this.loader.GetUpDirection (); diff --git a/test/tests/exporter_test.js b/test/tests/exporter_test.js index 37288f3..23f7e59 100644 --- a/test/tests/exporter_test.js +++ b/test/tests/exporter_test.js @@ -90,7 +90,7 @@ describe ('Exporter', function () { let mtlFile = result[0]; assert.strictEqual (mtlFile.GetName (), 'model.mtl'); - assert.strictEqual (mtlFile.GetContent (), + assert.strictEqual (mtlFile.GetTextContent (), [ '# exported by https://3dviewer.net', 'newmtl TestMaterial1', @@ -112,7 +112,7 @@ describe ('Exporter', function () { ].join ('\n')); let objFile = result[1]; assert.strictEqual (objFile.GetName (), 'model.obj'); - assert.strictEqual (objFile.GetContent (), + assert.strictEqual (objFile.GetTextContent (), [ '# exported by https://3dviewer.net', 'mtllib model.mtl', @@ -141,15 +141,15 @@ describe ('Exporter', function () { let textureFile1 = result[2]; assert.strictEqual (textureFile1.GetName (), 'texture1.png'); - assert.strictEqual (textureFile1.GetContent ().byteLength, 1); + assert.strictEqual (textureFile1.GetBufferContent ().byteLength, 1); let textureFile2 = result[3]; assert.strictEqual (textureFile2.GetName (), 'texture2.png'); - assert.strictEqual (textureFile2.GetContent ().byteLength, 2); + assert.strictEqual (textureFile2.GetBufferContent ().byteLength, 2); let textureFile3 = result[4]; assert.strictEqual (textureFile3.GetName (), 'texture3.png'); - assert.strictEqual (textureFile3.GetContent ().byteLength, 3); + assert.strictEqual (textureFile3.GetBufferContent ().byteLength, 3); done (); }); @@ -162,7 +162,7 @@ describe ('Exporter', function () { let stlFile = result[0]; assert.strictEqual (stlFile.GetName (), 'model.stl'); - assert.strictEqual (stlFile.GetContent (), + assert.strictEqual (stlFile.GetTextContent (), [ 'solid Model', 'facet normal 0 0 1', @@ -208,9 +208,9 @@ describe ('Exporter', function () { let stlFile = result[0]; assert.strictEqual (stlFile.GetName (), 'model.stl'); - assert.strictEqual (stlFile.GetContent ().byteLength, 284); + assert.strictEqual (stlFile.GetBufferContent ().byteLength, 284); - let contentBuffer = stlFile.GetContent (); + let contentBuffer = stlFile.GetBufferContent (); let importer = new OV.ImporterStl (); importer.Import (contentBuffer, 'stl', { getDefaultMaterial () { @@ -235,7 +235,7 @@ describe ('Exporter', function () { let offFile = result[0]; assert.strictEqual (offFile.GetName (), 'model.off'); - assert.strictEqual (offFile.GetContent (), + assert.strictEqual (offFile.GetTextContent (), [ 'OFF', '8 4 0', @@ -265,7 +265,7 @@ describe ('Exporter', function () { let plyFile = result[0]; assert.strictEqual (plyFile.GetName (), 'model.ply'); - assert.strictEqual (plyFile.GetContent (), + assert.strictEqual (plyFile.GetTextContent (), [ 'ply', 'format ascii 1.0', @@ -302,9 +302,9 @@ describe ('Exporter', function () { let plyFile = result[0]; assert.strictEqual (plyFile.GetName (), 'model.ply'); - assert.strictEqual (plyFile.GetContent ().byteLength, 315); + assert.strictEqual (plyFile.GetBufferContent ().byteLength, 315); - let contentBuffer = plyFile.GetContent (); + let contentBuffer = plyFile.GetBufferContent (); let importer = new OV.ImporterPly (); importer.Import (contentBuffer, 'ply', { getDefaultMaterial () { @@ -332,9 +332,9 @@ describe ('Exporter', function () { assert.strictEqual (binFile.GetName (), 'model.bin'); assert.strictEqual (textureFile.GetName (), 'texture1.png'); - assert.strictEqual (textureFile.GetContent ().byteLength, 1); + assert.strictEqual (textureFile.GetBufferContent ().byteLength, 1); - let contentBuffer = gltfFile.GetContent (); + let contentBuffer = gltfFile.GetBufferContent (); let importer = new OV.ImporterGltf (); importer.Import (contentBuffer, 'gltf', { getDefaultMaterial () { @@ -342,7 +342,7 @@ describe ('Exporter', function () { }, getFileBuffer (filePath) { if (filePath == 'model.bin') { - return binFile.GetContent (); + return binFile.GetBufferContent (); } return null; }, @@ -372,7 +372,7 @@ describe ('Exporter', function () { let glbFile = result[0]; assert.strictEqual (glbFile.GetName (), 'model.glb'); - let contentBuffer = glbFile.GetContent (); + let contentBuffer = glbFile.GetBufferContent (); let importer = new OV.ImporterGltf (); importer.Import (contentBuffer, 'glb', { getDefaultMaterial () { diff --git a/test/utils/testfiles.js b/test/utils/testfiles.js index 7dd2880..ac8c259 100644 --- a/test/utils/testfiles.js +++ b/test/utils/testfiles.js @@ -5,72 +5,51 @@ module.exports = ImportObjFile : function (fileName, onReady) { var importer = new OV.ImporterObj (); - this.ImportFile (importer, OV.FileFormat.Text, 'obj', fileName, onReady); + this.ImportFile (importer, 'obj', fileName, onReady); }, ImportStlFile : function (fileName, onReady) { var importer = new OV.ImporterStl (); - this.ImportFile (importer, OV.FileFormat.Binary, 'stl', fileName, onReady); + this.ImportFile (importer, 'stl', fileName, onReady); }, ImportOffFile : function (fileName, onReady) { var importer = new OV.ImporterOff (); - this.ImportFile (importer, OV.FileFormat.Text, 'off', fileName, onReady); + this.ImportFile (importer, 'off', fileName, onReady); }, ImportPlyFile : function (fileName, onReady) { var importer = new OV.ImporterPly (); - this.ImportFile (importer, OV.FileFormat.Binary, 'ply', fileName, onReady); + this.ImportFile (importer, 'ply', fileName, onReady); }, Import3dsFile : function (fileName, onReady) { var importer = new OV.Importer3ds (); - this.ImportFile (importer, OV.FileFormat.Binary, '3ds', fileName, onReady); + this.ImportFile (importer, '3ds', fileName, onReady); }, ImportGltfFile : function (folderName, fileName, onReady) { - let extension = OV.GetFileExtension (fileName); - let format = OV.FileFormat.Text; - if (extension == 'glb') { - format = OV.FileFormat.Binary; - } var importer = new OV.ImporterGltf (); - this.ImportFile (importer, format, 'gltf/' + folderName, fileName, onReady); + this.ImportFile (importer, 'gltf/' + folderName, fileName, onReady); }, ImportO3dvFile : function (fileName, onReady) { var importer = new OV.ImporterO3dv (); - this.ImportFile (importer, OV.FileFormat.Text, 'o3dv', fileName, onReady); + this.ImportFile (importer, 'o3dv', fileName, onReady); }, - ImportFile : function (importer, format, folder, fileName, onReady) + ImportFile : function (importer, folder, fileName, onReady) { - var content = null; - if (format == OV.FileFormat.Text) { - content = testUtils.GetTextFileContent (folder, fileName); - } else if (format == OV.FileFormat.Binary) { - content = testUtils.GetArrayBufferFileContent (folder, fileName); - } + let content = testUtils.GetArrayBufferFileContent (folder, fileName); var extension = OV.GetFileExtension (fileName); let buffers = new OV.ImportBuffers (function (filePath) { - let extension = OV.GetFileExtension (filePath); - let knownFormats = importer.GetKnownFileFormats (); - let format = OV.FileFormat.Binary; - if (knownFormats[extension] !== undefined) { - format = knownFormats[extension]; - } - let fileContent = null; - if (format == OV.FileFormat.Text) { - fileContent = testUtils.GetTextFileContent (folder, filePath); - } else if (format == OV.FileFormat.Binary) { - fileContent = testUtils.GetArrayBufferFileContent (folder, filePath); - } + let fileContent = testUtils.GetArrayBufferFileContent (folder, filePath); return fileContent; }); importer.Import (content, extension, { diff --git a/website/o3dv/js/exportdialog.js b/website/o3dv/js/exportdialog.js index 2ee3422..099c044 100644 --- a/website/o3dv/js/exportdialog.js +++ b/website/o3dv/js/exportdialog.js @@ -171,7 +171,7 @@ OV.ExportDialog = class } else if (files.length === 1) { progressDialog.Hide (); let file = files[0]; - OV.DownloadArrayBufferAsFile (file.GetContent (), file.GetName ()); + OV.DownloadArrayBufferAsFile (file.GetBufferContent (), file.GetName ()); } else if (files.length > 1) { progressDialog.Hide (); this.ShowExportedFiles (files); @@ -211,7 +211,7 @@ OV.ExportDialog = class for (let i = 0; i < files.length; i++) { let file = files[i]; - let url = OV.CreateObjectUrl (file.GetContent ()); + let url = OV.CreateObjectUrl (file.GetBufferContent ()); let fileLink = $('').addClass ('ov_dialog_file_link').appendTo (fileList); fileLink.attr ('href', url); fileLink.attr ('download', file.GetName ());