public void ImportIsFinished(GameObject go) { /* Do something */ /* show ifc properties to console for testing */ IFCVariables vars = go.GetComponentInChildren <IFCVariables>(); if (vars != null && vars.vars.Length > 0) { Debug.Log(vars.ToString()); } }
IEnumerator Import(string path, string objname) { IfcUtil util = new IfcUtil(); string name = objname; string file = path; Debug.Log(file); float projectScale = 1.0f; yield return(Ninja.JumpToUnity); if (GameObject.Find(name) != null) { Debug.Log("GameObject already exists, aborting import!"); yield return(null); } yield return(Ninja.JumpBack); Debug.Log("Parsing geometry from IFC file"); yield return(null); bool result = util.ParseIFCFile(file); if (!result) { Debug.Log("Error parsing IFC File"); yield return(null); } else { Debug.Log("Finished parsing geometry"); if (util.Latitude != 0 && util.Longitude != 0) { Debug.Log("Found georeference with coordinates:" + util.Latitude.ToString() + "," + util.Longitude.ToString()); Latitude = util.Latitude; Longitude = util.Longitude; Elevation = util.Elevation; } else { Debug.Log("Found no georeference"); } if (util.SILengthUnit != null) { Debug.Log("Project file is measured in " + util.SILengthUnit); if (util.SILengthUnit.Equals(".MILLI..METRE.")) { projectScale = 1 / 1000.0f; } else if (util.SILengthUnit.Equals(".CENTI..METRE.")) { projectScale = 1 / 100.0f; } else if (util.SILengthUnit.Equals(".DECI..METRE.")) { projectScale = 1 / 10.0f; } } yield return(null); } //okay here items = util.Geometry; // calculate dimensions Vector3 min = new Vector3(); Vector3 max = new Vector3(); bool InitMinMax = false; GetDimensions(util.ModelRoot, ref min, ref max, ref InitMinMax); Vector3 center = new Vector3(); center.x = (max.x + min.x) / 2f; center.y = (max.y + min.y) / 2f; center.z = (max.z + min.z) / 2f; center *= projectScale; float size = max.x - min.x; if (size < max.y - min.y) { size = max.y - min.y; } if (size < max.z - min.z) { size = max.z - min.z; } yield return(Ninja.JumpToUnity); go = new GameObject(); go.name = name; yield return(Ninja.JumpBack); int cnt = 0; foreach (IfcItem item in items) { if (cnt % 50 == 0) { Debug.Log("Processing mesh " + cnt + " of " + items.Count); yield return(null); } yield return(Ninja.JumpToUnity); Mesh m = new Mesh(); //process properties of ifc item meshToIfcType.Add(m, item.ifcType); if (useNamesInsteadOfTypes) { if (item.name != null && item.name.Trim().Length > 0) { m.name = item.name; } else { m.name = item.ifcType; } } else { m.name = item.ifcType; } // store additional properties List <IFCVariables.IfcVar> ifcVar = new List <IFCVariables.IfcVar>(); ifcVar.Add(new IFCVariables.IfcVar { key = "name", value = item.name }); ifcVar.Add(new IFCVariables.IfcVar { key = "type", value = item.ifcType }); ifcVar.Add(new IFCVariables.IfcVar { key = "guid", value = item.guid }); ifcVar.Add(new IFCVariables.IfcVar { key = "description", value = item.description }); meshToIfcVars.Add(m, ifcVar); yield return(Ninja.JumpBack); List <Vector3> vertices = new List <Vector3>(); for (int i = 0; i < item.verticesCount; i++) { Vector3 vec = new Vector3((item.vertices[6 * i + 0]) * projectScale, (item.vertices[6 * i + 2]) * projectScale, (item.vertices[6 * i + 1]) * projectScale); vertices.Add(vec); } Debug.Assert(item.vertices.Length == item.verticesCount * 6); yield return(Ninja.JumpToUnity); m.SetVertices(vertices); m.SetIndices(item.indicesForFaces, MeshTopology.Triangles, 0, true); // calculate UVs float scaleFactor = 0.5f; Vector2[] uvs = new Vector2[vertices.Count]; int len = m.GetIndices(0).Length; int_t[] idxs = m.GetIndices(0); yield return(Ninja.JumpBack); for (int i = 0; i < len; i = i + 3) { Vector3 v1 = vertices[idxs[i + 0]]; Vector3 v2 = vertices[idxs[i + 1]]; Vector3 v3 = vertices[idxs[i + 2]]; Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1); Quaternion rotation; if (normal == Vector3.zero) { rotation = new Quaternion(); } else { rotation = Quaternion.Inverse(Quaternion.LookRotation(normal)); } uvs[idxs[i + 0]] = (Vector2)(rotation * v1) * scaleFactor; uvs[idxs[i + 1]] = (Vector2)(rotation * v2) * scaleFactor; uvs[idxs[i + 2]] = (Vector2)(rotation * v3) * scaleFactor; } yield return(Ninja.JumpToUnity); m.SetUVs(0, new List <Vector2>(uvs)); m.RecalculateNormals(); yield return(Ninja.JumpBack); meshes.Add(m); cnt++; } cnt = 0; yield return(Ninja.JumpToUnity); foreach (Mesh m in meshes) { Material mat = initMaterial; String meshType = null; meshToIfcType.TryGetValue(m, out meshType); /* check if materials assigned, otherwise use init mat */ if (classToMat.ContainsKey(meshType)) { classToMat.TryGetValue(meshType, out mat); } GameObject child = new GameObject(m.name); child.transform.parent = go.transform; MeshFilter meshFilter = (MeshFilter)child.AddComponent(typeof(MeshFilter)); meshFilter.mesh = m; MeshRenderer renderer = child.AddComponent(typeof(MeshRenderer)) as MeshRenderer; renderer.material = mat; child.AddComponent(typeof(SerializeMesh)); // add IFC variables IFCVariables ifcVars = (IFCVariables)child.AddComponent(typeof(IFCVariables)); List <IFCVariables.IfcVar> mvar; meshToIfcVars.TryGetValue(m, out mvar); if (mvar != null) { ifcVars.vars = mvar.ToArray(); } cnt++; yield return(null); } if (util.TrueNorth != 0) { go.transform.Rotate(new Vector3(0, 1, 0), (float)util.TrueNorth * -1); } #if UNITY_EDITOR PrefabUtility.CreatePrefab("Assets/IFCGeneratedGeometry/" + name + ".prefab", go); #endif allFinished = true; // callback for external if (ImportFinished != null) { ImportFinished(go); } yield return(null); }