Add error messages when external library loading fails.

This commit is contained in:
kovacsv 2022-02-23 17:56:49 +01:00
parent a5f4d41455
commit 722e8db2e9
4 changed files with 244 additions and 241 deletions

View File

@ -15,7 +15,7 @@ export class Importer3dm extends ImporterBase
constructor () constructor ()
{ {
super (); super ();
this.rhino = null; this.rhino = null;
} }
CanImportExtension (extension) CanImportExtension (extension)
@ -28,273 +28,274 @@ export class Importer3dm extends ImporterBase
return Direction.Z; return Direction.Z;
} }
ClearContent () ClearContent ()
{ {
this.instanceIdToObject = null; this.instanceIdToObject = null;
this.instanceIdToDefinition = null; this.instanceIdToDefinition = null;
} }
ResetContent () ResetContent ()
{ {
this.instanceIdToObject = new Map (); this.instanceIdToObject = new Map ();
this.instanceIdToDefinition = new Map (); this.instanceIdToDefinition = new Map ();
} }
ImportContent (fileContent, onFinish) ImportContent (fileContent, onFinish)
{ {
if (this.rhino === null) { if (this.rhino === null) {
LoadExternalLibrary ('loaders/rhino3dm.min.js').then (() => { LoadExternalLibrary ('loaders/rhino3dm.min.js').then (() => {
rhino3dm ().then ((rhino) => { rhino3dm ().then ((rhino) => {
this.rhino = rhino; this.rhino = rhino;
this.ImportRhinoContent (fileContent); this.ImportRhinoContent (fileContent);
onFinish (); onFinish ();
}); });
}).catch (() => { }).catch (() => {
this.SetError ('Failed to load rhino3dm.');
onFinish (); onFinish ();
}); });
} else { } else {
this.ImportRhinoContent (fileContent); this.ImportRhinoContent (fileContent);
onFinish (); onFinish ();
} }
} }
ImportRhinoContent (fileContent) ImportRhinoContent (fileContent)
{ {
let rhinoDoc = this.rhino.File3dm.fromByteArray (fileContent); let rhinoDoc = this.rhino.File3dm.fromByteArray (fileContent);
if (rhinoDoc === null) { if (rhinoDoc === null) {
this.SetError ('Failed to read Rhino file.'); this.SetError ('Failed to read Rhino file.');
return; return;
}
this.ImportRhinoDocument (rhinoDoc);
if (IsModelEmpty (this.model)) {
this.SetError ('The model doesn\'t contain any 3D meshes. Try to save the model while you are in shaded view in Rhino.');
} }
} this.ImportRhinoDocument (rhinoDoc);
if (IsModelEmpty (this.model)) {
this.SetError ('The model doesn\'t contain any 3D meshes. Try to save the model while you are in shaded view in Rhino.');
}
}
ImportRhinoDocument (rhinoDoc) ImportRhinoDocument (rhinoDoc)
{ {
this.InitRhinoInstances (rhinoDoc); this.InitRhinoInstances (rhinoDoc);
this.ImportRhinoUserStrings (rhinoDoc); this.ImportRhinoUserStrings (rhinoDoc);
this.ImportRhinoGeometry (rhinoDoc); this.ImportRhinoGeometry (rhinoDoc);
} }
InitRhinoInstances (rhinoDoc) InitRhinoInstances (rhinoDoc)
{ {
let rhinoObjects = rhinoDoc.objects (); let rhinoObjects = rhinoDoc.objects ();
for (let i = 0; i < rhinoObjects.count; i++) { for (let i = 0; i < rhinoObjects.count; i++) {
let rhinoObject = rhinoObjects.get (i); let rhinoObject = rhinoObjects.get (i);
let rhinoAttributes = rhinoObject.attributes (); let rhinoAttributes = rhinoObject.attributes ();
if (rhinoAttributes.isInstanceDefinitionObject) { if (rhinoAttributes.isInstanceDefinitionObject) {
this.instanceIdToObject.set (rhinoAttributes.id, rhinoObject); this.instanceIdToObject.set (rhinoAttributes.id, rhinoObject);
} }
} }
let rhinoInstanceDefinitions = rhinoDoc.instanceDefinitions (); let rhinoInstanceDefinitions = rhinoDoc.instanceDefinitions ();
for (let i = 0; i < rhinoInstanceDefinitions.count (); i++) { for (let i = 0; i < rhinoInstanceDefinitions.count (); i++) {
let rhinoInstanceDefinition = rhinoInstanceDefinitions.get (i); let rhinoInstanceDefinition = rhinoInstanceDefinitions.get (i);
this.instanceIdToDefinition.set (rhinoInstanceDefinition.id, rhinoInstanceDefinition); this.instanceIdToDefinition.set (rhinoInstanceDefinition.id, rhinoInstanceDefinition);
} }
} }
ImportRhinoUserStrings (rhinoDoc) ImportRhinoUserStrings (rhinoDoc)
{ {
let docStrings = rhinoDoc.strings (); let docStrings = rhinoDoc.strings ();
if (docStrings.count () > 0) { if (docStrings.count () > 0) {
let propertyGroup = new PropertyGroup ('Document user texts'); let propertyGroup = new PropertyGroup ('Document user texts');
for (let i = 0; i < docStrings.count (); i++) { for (let i = 0; i < docStrings.count (); i++) {
let docString = docStrings.get (i); let docString = docStrings.get (i);
propertyGroup.AddProperty (new Property (PropertyType.Text, docString[0], docString[1])); propertyGroup.AddProperty (new Property (PropertyType.Text, docString[0], docString[1]));
} }
this.model.AddPropertyGroup (propertyGroup); this.model.AddPropertyGroup (propertyGroup);
} }
} }
ImportRhinoGeometry (rhinoDoc) ImportRhinoGeometry (rhinoDoc)
{ {
let rhinoObjects = rhinoDoc.objects (); let rhinoObjects = rhinoDoc.objects ();
for (let i = 0; i < rhinoObjects.count; i++) { for (let i = 0; i < rhinoObjects.count; i++) {
let rhinoObject = rhinoObjects.get (i); let rhinoObject = rhinoObjects.get (i);
this.ImportRhinoGeometryObject (rhinoDoc, rhinoObject, []); this.ImportRhinoGeometryObject (rhinoDoc, rhinoObject, []);
} }
} }
ImportRhinoGeometryObject (rhinoDoc, rhinoObject, rhinoInstanceReferences) ImportRhinoGeometryObject (rhinoDoc, rhinoObject, rhinoInstanceReferences)
{ {
let rhinoGeometry = rhinoObject.geometry (); let rhinoGeometry = rhinoObject.geometry ();
let rhinoAttributes = rhinoObject.attributes (); let rhinoAttributes = rhinoObject.attributes ();
let objectType = rhinoGeometry.objectType; let objectType = rhinoGeometry.objectType;
if (rhinoAttributes.isInstanceDefinitionObject && rhinoInstanceReferences.length === 0) { if (rhinoAttributes.isInstanceDefinitionObject && rhinoInstanceReferences.length === 0) {
return; return;
} }
let rhinoMesh = null; let rhinoMesh = null;
let deleteMesh = false; let deleteMesh = false;
if (objectType === this.rhino.ObjectType.Mesh) { if (objectType === this.rhino.ObjectType.Mesh) {
rhinoMesh = rhinoGeometry; rhinoMesh = rhinoGeometry;
deleteMesh = false; deleteMesh = false;
} else if (objectType === this.rhino.ObjectType.Extrusion) { } else if (objectType === this.rhino.ObjectType.Extrusion) {
rhinoMesh = rhinoGeometry.getMesh (this.rhino.MeshType.Any); rhinoMesh = rhinoGeometry.getMesh (this.rhino.MeshType.Any);
deleteMesh = true; deleteMesh = true;
} else if (objectType === this.rhino.ObjectType.Brep) { } else if (objectType === this.rhino.ObjectType.Brep) {
rhinoMesh = new this.rhino.Mesh (); rhinoMesh = new this.rhino.Mesh ();
let faces = rhinoGeometry.faces (); let faces = rhinoGeometry.faces ();
for (let i = 0; i < faces.count; i++) { for (let i = 0; i < faces.count; i++) {
let face = faces.get (i); let face = faces.get (i);
let mesh = face.getMesh (this.rhino.MeshType.Any); let mesh = face.getMesh (this.rhino.MeshType.Any);
if (mesh) { if (mesh) {
rhinoMesh.append (mesh); rhinoMesh.append (mesh);
mesh.delete (); mesh.delete ();
} }
face.delete (); face.delete ();
} }
faces.delete (); faces.delete ();
rhinoMesh.compact (); rhinoMesh.compact ();
deleteMesh = true; deleteMesh = true;
} else if (objectType === this.rhino.ObjectType.SubD) { } else if (objectType === this.rhino.ObjectType.SubD) {
rhinoGeometry.subdivide (3); rhinoGeometry.subdivide (3);
rhinoMesh = this.rhino.Mesh.createFromSubDControlNet (rhinoGeometry); rhinoMesh = this.rhino.Mesh.createFromSubDControlNet (rhinoGeometry);
deleteMesh = true; deleteMesh = true;
} else if (objectType === this.rhino.ObjectType.InstanceReference) { } else if (objectType === this.rhino.ObjectType.InstanceReference) {
let parentDefinitionId = rhinoGeometry.parentIdefId; let parentDefinitionId = rhinoGeometry.parentIdefId;
if (this.instanceIdToDefinition.has (parentDefinitionId)) { if (this.instanceIdToDefinition.has (parentDefinitionId)) {
let instanceDefinition = this.instanceIdToDefinition.get (parentDefinitionId); let instanceDefinition = this.instanceIdToDefinition.get (parentDefinitionId);
let instanceObjectIds = instanceDefinition.getObjectIds (); let instanceObjectIds = instanceDefinition.getObjectIds ();
for (let i = 0; i < instanceObjectIds.length; i++) { for (let i = 0; i < instanceObjectIds.length; i++) {
let instanceObjectId = instanceObjectIds[i]; let instanceObjectId = instanceObjectIds[i];
if (this.instanceIdToObject.has (instanceObjectId)) { if (this.instanceIdToObject.has (instanceObjectId)) {
let instanceObject = this.instanceIdToObject.get (instanceObjectId); let instanceObject = this.instanceIdToObject.get (instanceObjectId);
rhinoInstanceReferences.push (rhinoObject); rhinoInstanceReferences.push (rhinoObject);
this.ImportRhinoGeometryObject (rhinoDoc, instanceObject, rhinoInstanceReferences); this.ImportRhinoGeometryObject (rhinoDoc, instanceObject, rhinoInstanceReferences);
rhinoInstanceReferences.pop (); rhinoInstanceReferences.pop ();
} }
} }
} }
} }
if (rhinoMesh !== null) { if (rhinoMesh !== null) {
this.ImportRhinoMesh (rhinoDoc, rhinoMesh, rhinoObject, rhinoInstanceReferences); this.ImportRhinoMesh (rhinoDoc, rhinoMesh, rhinoObject, rhinoInstanceReferences);
if (deleteMesh) { if (deleteMesh) {
rhinoMesh.delete (); rhinoMesh.delete ();
} }
} }
} }
ImportRhinoMesh (rhinoDoc, rhinoMesh, rhinoObject, rhinoInstanceReferences) ImportRhinoMesh (rhinoDoc, rhinoMesh, rhinoObject, rhinoInstanceReferences)
{ {
let rhinoAttributes = rhinoObject.attributes (); let rhinoAttributes = rhinoObject.attributes ();
let materialIndex = this.GetMaterialIndex (rhinoDoc, rhinoObject, rhinoInstanceReferences); let materialIndex = this.GetMaterialIndex (rhinoDoc, rhinoObject, rhinoInstanceReferences);
let threeJson = rhinoMesh.toThreejsJSON (); let threeJson = rhinoMesh.toThreejsJSON ();
let mesh = ConvertThreeGeometryToMesh (threeJson.data, materialIndex); let mesh = ConvertThreeGeometryToMesh (threeJson.data, materialIndex);
mesh.SetName (rhinoAttributes.name); mesh.SetName (rhinoAttributes.name);
let userStrings = rhinoAttributes.getUserStrings (); let userStrings = rhinoAttributes.getUserStrings ();
if (userStrings.length > 0) { if (userStrings.length > 0) {
let propertyGroup = new PropertyGroup ('User texts'); let propertyGroup = new PropertyGroup ('User texts');
for (let i = 0; i < userStrings.length; i++) { for (let i = 0; i < userStrings.length; i++) {
let userString = userStrings[i]; let userString = userStrings[i];
propertyGroup.AddProperty (new Property (PropertyType.Text, userString[0], userString[1])); propertyGroup.AddProperty (new Property (PropertyType.Text, userString[0], userString[1]));
} }
mesh.AddPropertyGroup (propertyGroup); mesh.AddPropertyGroup (propertyGroup);
} }
if (rhinoInstanceReferences.length !== 0) { if (rhinoInstanceReferences.length !== 0) {
let matrix = new Matrix ().CreateIdentity (); let matrix = new Matrix ().CreateIdentity ();
for (let i = rhinoInstanceReferences.length - 1; i >= 0; i--) { for (let i = rhinoInstanceReferences.length - 1; i >= 0; i--) {
let rhinoInstanceReference = rhinoInstanceReferences[i]; let rhinoInstanceReference = rhinoInstanceReferences[i];
let rhinoInstanceReferenceGeometry = rhinoInstanceReference.geometry (); let rhinoInstanceReferenceGeometry = rhinoInstanceReference.geometry ();
let rhinoInstanceReferenceMatrix = rhinoInstanceReferenceGeometry.xform.toFloatArray (false); let rhinoInstanceReferenceMatrix = rhinoInstanceReferenceGeometry.xform.toFloatArray (false);
let transformationMatrix = new Matrix (rhinoInstanceReferenceMatrix); let transformationMatrix = new Matrix (rhinoInstanceReferenceMatrix);
matrix = matrix.MultiplyMatrix (transformationMatrix); matrix = matrix.MultiplyMatrix (transformationMatrix);
} }
let transformation = new Transformation (matrix); let transformation = new Transformation (matrix);
TransformMesh (mesh, transformation); TransformMesh (mesh, transformation);
} }
this.model.AddMeshToRootNode (mesh); this.model.AddMeshToRootNode (mesh);
} }
GetMaterialIndex (rhinoDoc, rhinoObject, rhinoInstanceReferences) GetMaterialIndex (rhinoDoc, rhinoObject, rhinoInstanceReferences)
{ {
function GetRhinoMaterial (rhino, rhinoObject, rhinoInstanceReferences) function GetRhinoMaterial (rhino, rhinoObject, rhinoInstanceReferences)
{ {
let rhinoAttributes = rhinoObject.attributes (); let rhinoAttributes = rhinoObject.attributes ();
if (rhinoAttributes.materialSource === rhino.ObjectMaterialSource.MaterialFromObject) { if (rhinoAttributes.materialSource === rhino.ObjectMaterialSource.MaterialFromObject) {
let materialIndex = rhinoAttributes.materialIndex; let materialIndex = rhinoAttributes.materialIndex;
if (materialIndex > -1) { if (materialIndex > -1) {
return rhinoDoc.materials ().get (materialIndex); return rhinoDoc.materials ().get (materialIndex);
} }
} else if (rhinoAttributes.materialSource === rhino.ObjectMaterialSource.MaterialFromLayer) { } else if (rhinoAttributes.materialSource === rhino.ObjectMaterialSource.MaterialFromLayer) {
let layerIndex = rhinoAttributes.layerIndex; let layerIndex = rhinoAttributes.layerIndex;
if (layerIndex > -1) { if (layerIndex > -1) {
let layer = rhinoDoc.layers ().get (layerIndex); let layer = rhinoDoc.layers ().get (layerIndex);
let layerMaterialIndex = layer.renderMaterialIndex; let layerMaterialIndex = layer.renderMaterialIndex;
if (layerMaterialIndex > -1) { if (layerMaterialIndex > -1) {
return rhinoDoc.materials ().get (layerMaterialIndex); return rhinoDoc.materials ().get (layerMaterialIndex);
} }
} }
} else if (rhinoAttributes.materialSource === rhino.ObjectMaterialSource.MaterialFromParent) { } else if (rhinoAttributes.materialSource === rhino.ObjectMaterialSource.MaterialFromParent) {
if (rhinoInstanceReferences.length !== 0) { if (rhinoInstanceReferences.length !== 0) {
return GetRhinoMaterial (rhino, rhinoInstanceReferences[0], []); return GetRhinoMaterial (rhino, rhinoInstanceReferences[0], []);
} }
} }
return null; return null;
} }
function FindMatchingMaterial (model, rhinoMaterial) function FindMatchingMaterial (model, rhinoMaterial)
{ {
function SetColor (color, rhinoColor) function SetColor (color, rhinoColor)
{ {
color.Set (rhinoColor.r, rhinoColor.g, rhinoColor.b); color.Set (rhinoColor.r, rhinoColor.g, rhinoColor.b);
} }
function IsBlack (rhinoColor) function IsBlack (rhinoColor)
{ {
return rhinoColor.r === 0 && rhinoColor.g === 0 && rhinoColor.b === 0; return rhinoColor.r === 0 && rhinoColor.g === 0 && rhinoColor.b === 0;
} }
function IsWhite (rhinoColor) function IsWhite (rhinoColor)
{ {
return rhinoColor.r === 255 && rhinoColor.g === 255 && rhinoColor.b === 255; return rhinoColor.r === 255 && rhinoColor.g === 255 && rhinoColor.b === 255;
} }
let material = null; let material = null;
if (rhinoMaterial === null) { if (rhinoMaterial === null) {
material = new PhongMaterial (); material = new PhongMaterial ();
material.color.Set (255, 255, 255); material.color.Set (255, 255, 255);
} else { } else {
let physicallyBased = rhinoMaterial.physicallyBased (); let physicallyBased = rhinoMaterial.physicallyBased ();
if (physicallyBased.supported) { if (physicallyBased.supported) {
material = new PhysicalMaterial (); material = new PhysicalMaterial ();
material.metalness = physicallyBased.metallic ? 1.0 : 0.0; material.metalness = physicallyBased.metallic ? 1.0 : 0.0;
material.roughness = physicallyBased.roughness; material.roughness = physicallyBased.roughness;
} else { } else {
material = new PhongMaterial (); material = new PhongMaterial ();
SetColor (material.ambient, rhinoMaterial.ambientColor); SetColor (material.ambient, rhinoMaterial.ambientColor);
SetColor (material.specular, rhinoMaterial.specularColor); SetColor (material.specular, rhinoMaterial.specularColor);
} }
material.name = rhinoMaterial.name; material.name = rhinoMaterial.name;
SetColor (material.color, rhinoMaterial.diffuseColor); SetColor (material.color, rhinoMaterial.diffuseColor);
material.opacity = 1.0 - rhinoMaterial.transparency; material.opacity = 1.0 - rhinoMaterial.transparency;
UpdateMaterialTransparency (material); UpdateMaterialTransparency (material);
// material.shininess = rhinoMaterial.shine / 255.0; // material.shininess = rhinoMaterial.shine / 255.0;
if (IsBlack (material.color) && !IsWhite (rhinoMaterial.reflectionColor)) { if (IsBlack (material.color) && !IsWhite (rhinoMaterial.reflectionColor)) {
SetColor (material.color, rhinoMaterial.reflectionColor); SetColor (material.color, rhinoMaterial.reflectionColor);
} }
if (IsBlack (material.color) && !IsWhite (rhinoMaterial.transparentColor)) { if (IsBlack (material.color) && !IsWhite (rhinoMaterial.transparentColor)) {
SetColor (material.color, rhinoMaterial.transparentColor); SetColor (material.color, rhinoMaterial.transparentColor);
} }
} }
for (let i = 0; i < model.MaterialCount (); i++) { for (let i = 0; i < model.MaterialCount (); i++) {
let current = model.GetMaterial (i); let current = model.GetMaterial (i);
if (current.IsEqual (material)) { if (current.IsEqual (material)) {
return i; return i;
} }
} }
return model.AddMaterial (material); return model.AddMaterial (material);
} }
let rhinoMaterial = GetRhinoMaterial (this.rhino, rhinoObject, rhinoInstanceReferences); let rhinoMaterial = GetRhinoMaterial (this.rhino, rhinoObject, rhinoInstanceReferences);
return FindMatchingMaterial (this.model, rhinoMaterial); return FindMatchingMaterial (this.model, rhinoMaterial);
} }
} }

View File

@ -285,7 +285,7 @@ class GltfExtensions
callbacks.onSuccess (); callbacks.onSuccess ();
}); });
}).catch (() => { }).catch (() => {
callbacks.onError (); callbacks.onError ('Failed to load draco decoder.');
}); });
} else { } else {
callbacks.onSuccess (); callbacks.onSuccess ();
@ -616,8 +616,8 @@ export class ImporterGltf extends ImporterBase
this.ImportModel (gltf); this.ImportModel (gltf);
onFinish (); onFinish ();
}, },
onError : () => { onError : (message) => {
this.SetError ('Failed to load draco decoder.'); this.SetError (message);
onFinish (); onFinish ();
} }
}); });

View File

@ -51,6 +51,7 @@ export class ImporterIfc extends ImporterBase
onFinish (); onFinish ();
}); });
}).catch (() => { }).catch (() => {
this.SetError ('Failed to load web-ifc.');
onFinish (); onFinish ();
}); });
} else { } else {

View File

@ -74,6 +74,7 @@ export class ImporterThreeBase extends ImporterBase
LoadLibraries (libraries, () => { LoadLibraries (libraries, () => {
this.LoadModel (fileContent, onFinish); this.LoadModel (fileContent, onFinish);
}, () => { }, () => {
this.SetError ('Failed to load three.js loader.');
onFinish (); onFinish ();
}); });
} }