public static ClonedType CloneType(this IBHoMObject sourceRevitObject, string newName) { if (sourceRevitObject == null) { BH.Engine.Reflection.Compute.RecordError("It is impossible to clone a null Revit object."); return null; } RevitIdentifiers identifiers = sourceRevitObject.FindFragment<RevitIdentifiers>(); if (identifiers == null) { BH.Engine.Reflection.Compute.RecordError("The input object is not a valid pulled Revit element."); return null; } ClonedType result = new ClonedType { SourceTypeId = identifiers.FamilyTypeId, Name = newName }; if (identifiers.ElementId == identifiers.FamilyTypeId) { RevitParametersToPush parametersToPush = sourceRevitObject.FindFragment<RevitParametersToPush>(); if (parametersToPush != null) { BH.Engine.Reflection.Compute.RecordWarning("Parameters to push have been cloned from the source Revit type object."); result.AddFragment(parametersToPush.DeepClone()); } } else BH.Engine.Reflection.Compute.RecordWarning("The input object is a pulled Revit element, its type has been cloned."); return result; }
private static ConditionResult VerifyCondition(List <object> objects, HasId idCondition) { ConditionResult result = new ConditionResult() { Condition = idCondition }; foreach (var obj in objects) { IBHoMObject bhomObj = obj as IBHoMObject; if (bhomObj == null || idCondition.Ids.Contains(bhomObj.FindFragment <IAdapterId>()?.Id)) { result.PassedObjects.Add(obj); result.Pattern.Add(true); } else { result.FailedObjects.Add(obj); result.FailInfo.Add($"{(string.IsNullOrWhiteSpace(idCondition.Clause) ? "" : idCondition.Clause + " failed: ")}" + $"does not have the requested id."); result.Pattern.Add(false); } } return(result); }
private static string GetIdFromFragment(this IBHoMObject obj, Type fragmentType, string fragmentIdProperty) { if (!typeof(IFragment).IsAssignableFrom(fragmentType)) { BH.Engine.Reflection.Compute.RecordError("The specified Type must be a fragment Type."); return(null); } // Check on fragmentIdProperty if (string.IsNullOrWhiteSpace(fragmentIdProperty)) { BH.Engine.Reflection.Compute.RecordError($"Invalid {nameof(fragmentIdProperty)} provided."); return(null); } IFragment idFragm = obj.FindFragment <IFragment>(fragmentType); if (idFragm == null) { BH.Engine.Reflection.Compute.RecordWarning($"Object of type {obj.GetType()}, guid {obj.BHoM_Guid} does not contain a fragment of the provided Fragment type {fragmentType}."); return(null); } object value = BH.Engine.Reflection.Query.PropertyValue(idFragm, fragmentIdProperty); if (value == null) { BH.Engine.Reflection.Compute.RecordWarning($"The retrieved fragment for an object of type {obj.GetType()}, guid {obj.BHoM_Guid} has not any value under the property named {fragmentIdProperty}."); return(null); } return(value.ToString()); }
public static RevisionFragment RevisionFragment(this IBHoMObject obj) { if (obj == null) { BH.Engine.Reflection.Compute.RecordError("Cannot query the revision fragment from a null object."); return(null); } return(obj.FindFragment <RevisionFragment>()); }
public static bool TryGetRendermesh(this IBHoMObject bHoMObject, out RenderMesh renderMesh) { renderMesh = null; if (bHoMObject == null) { return(false); } renderMesh = bHoMObject.FindFragment <RenderMesh>(typeof(RenderMesh)); if (renderMesh != null) { return(true); } else { return(false); } }
private static List <IBHoMObject> WithNonNullPersistentAdapterId(this IEnumerable <object> objects, out List <object> reminder) { List <IBHoMObject> output = new List <IBHoMObject>(); reminder = new List <object>(); foreach (var obj in objects) { IBHoMObject ibhomobject = obj as IBHoMObject; if (ibhomobject != null && ibhomobject.FindFragment <IPersistentAdapterId>()?.PersistentId != null) { output.Add(ibhomobject); } else { reminder.Add(obj); } } return(output); }
private static void RepresentationFragment(this Links component, Dataset dataset, ViewConfig viewConfig) { BHoMGroup <IBHoMObject> entityGroup = (BHoMGroup <IBHoMObject>)dataset.Data.Find(x => x.Name == "Entities"); List <IBHoMObject> entities = entityGroup.Elements; BHoMGroup <IBHoMObject> relationGroup = (BHoMGroup <IBHoMObject>)dataset.Data.Find(x => x.Name == "Relations"); List <IBHoMObject> relations = relationGroup.Elements; Gradient gradient = Create.Gradient(); List <object> colourNames = new List <object>(); if (component.Colour.Contains("Source.")) { colourNames = Convert.ToDataList(entities.PropertyValue(component.Colour.Replace("Source.", ""))); } if (component.Colour.Contains("Target.")) { colourNames = Convert.ToDataList(entities.PropertyValue(component.Colour.Replace("Target.", ""))); } else { colourNames = Convert.ToDataList(relations.PropertyValue(component.Colour)); } IScale colourScale = Create.IScale(colourNames, gradient.Markers.Values.Cast <object>().ToList()); foreach (IBHoMObject obj in relations) { IBHoMObject start = entities.Find(ent => ent.BHoM_Guid.Equals(obj.PropertyValue(component.Start))); IBHoMObject end = entities.Find(ent => ent.BHoM_Guid.Equals(obj.PropertyValue(component.End))); EntityRepresentation startFrag = start.FindFragment <EntityRepresentation>(); EntityRepresentation endFrag = end.FindFragment <EntityRepresentation>(); RelationRepresentation representation = new RelationRepresentation(); Line line = Geometry.Create.Line(startFrag.OutgoingRelationPoint, endFrag.IncomingRelationPoint); representation.Curves.Add(line); representation.Curves.AddRange(component.Marker.IMarker(line.End, line.Direction())); representation.TextDirection = line.Direction(); representation.TextPosition = line.IPointAtLength(line.Length() / 3); if (component.Text.Contains("Source.")) { representation.Text = start.PropertyValue(component.Text.Replace("Source.", "")).ToString(); } else if (component.Text.Contains("Target.")) { representation.Text = end.PropertyValue(component.Text.Replace("Target.", "")).ToString(); } else { representation.Text = obj.PropertyValue(component.Text).ToString(); } if (component.Colour.Contains("Source.")) { representation.Colour = Convert.ColourFromObject(colourScale.IScale(start.PropertyValue(component.Colour.Replace("Source.", "")))); } else if (component.Colour.Contains("Target.")) { representation.Colour = Convert.ColourFromObject(colourScale.IScale(end.PropertyValue(component.Colour.Replace("Target.", "")))); } else { representation.Colour = Convert.ColourFromObject(colourScale.IScale(obj.PropertyValue(component.Colour))); } obj.Fragments.AddOrReplace(representation); } }
/***************************************************/ /**** 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); }
public static HashFragment GetHashFragment(this IBHoMObject obj) { return(obj.FindFragment <HashFragment>()); }