/***************************************************/ /**** Private methods ****/ /***************************************************/ private static string WriteBIMFile(List <IObject> objectsToWrite, string directory = null, string fileName = null, RenderMeshOptions renderMeshOptions = null) { // --------------------------------------------- // // Set-up // // --------------------------------------------- // directory = directory ?? Path.Combine("C:\\temp", "BIMFileFormat"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } fileName = fileName ?? Guid.NewGuid().ToString(); string bimFilePath = Path.Combine(directory, fileName + ".bim"); renderMeshOptions = renderMeshOptions ?? new RenderMeshOptions(); // --------------------------------------------- // // Compute representation // // --------------------------------------------- // List <Mesh> representationMeshes = new List <Mesh>(); List <Tuple <IObject, Mesh> > objsAndRepresentations = new List <Tuple <IObject, Mesh> >(); IBHoMObject bHoMObject = null; for (int i = 0; i < objectsToWrite.Count; i++) { IObject obj = objectsToWrite[i]; Mesh meshRepresentation = null; // See if there is a custom BHoM mesh representation for that BHoMObject. bHoMObject = obj as IBHoMObject; RenderMesh renderMesh = null; if (bHoMObject != null) { bHoMObject.TryGetRendermesh(out renderMesh); } if (renderMesh == null && meshRepresentation == null) { renderMesh = BH.Engine.Representation.Compute.IRenderMesh(obj, renderMeshOptions); } if (renderMesh != null) //convert to Mesh { meshRepresentation = new Mesh() { Faces = renderMesh.Faces, Vertices = renderMesh.Vertices.Select(v => new oM.Geometry.Point() { X = v.Point.X, Y = v.Point.Y, Z = v.Point.Z }).ToList() } } ; representationMeshes.Add(meshRepresentation); if (bHoMObject != null) { // Add/update the RenderMesh in the BHoMObject bHoMObject.ISetRendermesh(meshRepresentation); obj = bHoMObject; } objsAndRepresentations.Add(new Tuple <IObject, Mesh>(obj, meshRepresentation)); } // --------------------------------------------- // // File preparation // // --------------------------------------------- // BIMDataExporter exporter = new BIMDataExporter(); // Prepare default material TDR_Material defaultMat = new TDR_Material() { MaterialArray = new List <float> { 1f, 1f, 1f, 1f } }; int defaultMatIdx = exporter.AddMaterial(defaultMat.MaterialArray); // Prepare transformation matrix List <float> transfMatrix = new List <float> { 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 1, 2, 0, 0, 0, 1 }; // Prepare root node int rootNodeIdx = exporter.AddNode("root", -1, null); // Process the meshes for (int i = 0; i < objsAndRepresentations.Count; i++) { BH.oM.Geometry.Mesh m = representationMeshes[i]; Tuple <IObject, BH.oM.Geometry.Mesh> objAndRepr = objsAndRepresentations[i]; // Check if a colour has been specified in the BHoMObject's Fragment bHoMObject = objAndRepr.Item1 as IBHoMObject; int customMatIdx = defaultMatIdx; if (bHoMObject != null) { Color?colour = bHoMObject.FindFragment <ColourFragment>()?.Colour; if (colour != null) { Color col = (Color)colour; float r = (float)col.R / 255; float g = (float)col.G / 255; float b = (float)col.B / 255; float a = (float)col.A / 255; TDR_Material customMat = new TDR_Material() { MaterialArray = new List <float> { r, g, b, a } }; customMatIdx = exporter.AddMaterial(customMat.MaterialArray); } } // Convert object representation mesh to a Geometry geometry = BH.Adapter.TDrepo.Convert.MeshToGeometry(objAndRepr.Item2, customMatIdx); // Add metadata Dictionary <string, RepoVariant> metadata = new Dictionary <string, RepoVariant>(); // Serialize the object string serialisedBHoMData = BH.Engine.Serialiser.Convert.ToJson(objAndRepr.Item1); // Flatten the JSON in a Dictionary. Nested properties names are concatenated with fullstops. Dictionary <string, object> flattenedObj = BH.Engine.Adapters.TDRepo.Compute.FlattenJsonToDictionary(serialisedBHoMData); // For each entry in the flattened object, add a metadata with the value. flattenedObj.ToList().ForEach( kv => { if (kv.Value is int) { metadata.Add(kv.Key, RepoVariant.Int((int)kv.Value)); } else if (kv.Value is double) { metadata.Add(kv.Key, RepoVariant.Double((double)kv.Value)); } else if (kv.Value is bool) { metadata.Add(kv.Key, RepoVariant.Boolean((bool)kv.Value)); } else { metadata.Add(kv.Key, RepoVariant.String(kv.Value?.ToString())); } } ); metadata.Add("ZippedBHoM", RepoVariant.String(BH.Engine.Serialiser.Convert.ToZip(serialisedBHoMData))); //metadata.Add("Area", RepoVariant.Int(1)); //metadata.Add("Boolean Test", RepoVariant.Boolean(true)); //metadata.Add("Double", RepoVariant.Double(1.3242524)); // Add node to exporter. exporter.AddNode("mesh" + i, rootNodeIdx, transfMatrix, geometry, metadata); } exporter.ExportToFile(bimFilePath); return(bimFilePath); }