From 198c2f6b5e39dcfaebd884733cf794d907766b0a Mon Sep 17 00:00:00 2001 From: Pawel Baran Date: Wed, 12 Jun 2024 13:05:03 +0200 Subject: [PATCH] #1477 finally fixed --- Revit_Core_Adapter/CRUD/Read.cs | 75 ++++++++----------------- Revit_Core_Engine/Convert/FromRevit.cs | 21 +++---- Revit_Core_Engine/Modify/Postprocess.cs | 72 +++++++++++++++++++++--- 3 files changed, 93 insertions(+), 75 deletions(-) diff --git a/Revit_Core_Adapter/CRUD/Read.cs b/Revit_Core_Adapter/CRUD/Read.cs index b21f3bfcb..9239c8289 100644 --- a/Revit_Core_Adapter/CRUD/Read.cs +++ b/Revit_Core_Adapter/CRUD/Read.cs @@ -224,23 +224,6 @@ public static List Read(Document document, Transform transform, Lis discipline = Discipline.Physical; } - Options geometryOptions = BH.Revit.Engine.Core.Create.Options(ViewDetailLevel.Fine, geometryConfig.IncludeNonVisible, false); - Options meshOptions = BH.Revit.Engine.Core.Create.Options(geometryConfig.MeshDetailLevel.ViewDetailLevel(), geometryConfig.IncludeNonVisible, false); - Options renderMeshOptions = BH.Revit.Engine.Core.Create.Options(representationConfig.DetailLevel.ViewDetailLevel(), representationConfig.IncludeNonVisible, false); - - //TODO: that does not work for multiple links! - // would rather get LinkTransforms(), find unique ones and work with these? - // NOT GOING TO WORK FOR LINKS FILTERED BY ID - //Transform linkTransform = null; - //TransformMatrix bHoMTransform = null; - //if (document.IsLinked) - //{ - // linkTransform = document.LinkTransform(); - // if (linkTransform?.IsIdentity == false) - // bHoMTransform = linkTransform.FromRevit(); - //} - - var bHoMTransform = transform.FromRevit(); if (globalRefObjects == null) globalRefObjects = new Dictionary>>(); @@ -250,89 +233,75 @@ public static List Read(Document document, Transform transform, Lis Dictionary> refObjects = globalRefObjects[document.Title]; List result = new List(); - List remainingElementIds = elementIds.ToList(); - for (int i = 0; i < remainingElementIds.Count; i++) + List remainingElementIds = new List(); + foreach (ElementId id in elementIds) { - ElementId id = remainingElementIds[i]; var existing = refObjects.GetValues(id); if (existing != null) - { - result.AddRange(existing.Select(x => x.IPostprocess(transform, settings))); - remainingElementIds.RemoveAt(i); - } + result.AddRange(existing); + else + remainingElementIds.Add(id); } // Extract panel geometry of walls, floors, slabs and roofs prior to running the converts (this is an optimisation aimed to reduce the number of view regenerations) if (!document.IsLinked) document.CachePanelGeometry(remainingElementIds, discipline, settings, refObjects); - + + Options geometryOptions = BH.Revit.Engine.Core.Create.Options(ViewDetailLevel.Fine, geometryConfig.IncludeNonVisible, false); + Options meshOptions = BH.Revit.Engine.Core.Create.Options(geometryConfig.MeshDetailLevel.ViewDetailLevel(), geometryConfig.IncludeNonVisible, false); + Options renderMeshOptions = BH.Revit.Engine.Core.Create.Options(representationConfig.DetailLevel.ViewDetailLevel(), representationConfig.IncludeNonVisible, false); + foreach (ElementId id in remainingElementIds) { Element element = document.GetElement(id); if (element == null) continue; - IEnumerable iBHoMObjects = Read(element, discipline, transform, settings, refObjects); - - if (iBHoMObjects != null && iBHoMObjects.Any()) + IEnumerable converted = Read(element, discipline, settings, refObjects); + if (converted != null) { if (pullConfig.PullMaterialTakeOff) { - foreach (IBHoMObject iBHoMObject in iBHoMObjects) + foreach (IBHoMObject obj in converted) { oM.Physical.Materials.VolumetricMaterialTakeoff takeoff = element.VolumetricMaterialTakeoff(settings, refObjects); if (takeoff != null) - iBHoMObject.Fragments.AddOrReplace(takeoff); + obj.Fragments.AddOrReplace(takeoff); } } List edges = null; if (geometryConfig.PullEdges) - { edges = element.Curves(geometryOptions, settings, true).FromRevit(); - if (bHoMTransform != null) - edges = edges.Select(x => x?.ITransform(bHoMTransform)).ToList(); - } List surfaces = null; if (geometryConfig.PullSurfaces) - { surfaces = element.Faces(geometryOptions, settings).Select(x => x.IFromRevit()).ToList(); - if (bHoMTransform != null) - surfaces = surfaces.Select(x => x?.ITransform(bHoMTransform)).ToList(); - } List meshes = null; if (geometryConfig.PullMeshes) - { meshes = element.MeshedGeometry(meshOptions, settings); - if (bHoMTransform != null) - meshes = meshes.Select(x => x?.Transform(bHoMTransform)).ToList(); - } if (geometryConfig.PullEdges || geometryConfig.PullSurfaces || geometryConfig.PullMeshes) { RevitGeometry geometry = new RevitGeometry(edges, surfaces, meshes); - foreach (IBHoMObject iBHoMObject in iBHoMObjects) + foreach (IBHoMObject obj in converted) { - iBHoMObject.Fragments.AddOrReplace(geometry); + obj.Fragments.AddOrReplace(geometry); } } if (representationConfig.PullRenderMesh) { List renderMeshes = element.RenderMeshes(renderMeshOptions, settings); - if (bHoMTransform != null) - renderMeshes = renderMeshes.Select(x => x?.Transform(bHoMTransform)).ToList(); - RevitRepresentation representation = new RevitRepresentation(renderMeshes); - foreach (IBHoMObject iBHoMObject in iBHoMObjects) + foreach (IBHoMObject obj in converted) { - iBHoMObject.Fragments.AddOrReplace(representation); + obj.Fragments.AddOrReplace(representation); } } - result.AddRange(iBHoMObjects); + result.AddRange(converted); } } @@ -340,12 +309,12 @@ public static List Read(Document document, Transform transform, Lis if (activePulls.Count(x => x) > 1) BH.Engine.Base.Compute.RecordWarning("Pull of more than one geometry/representation type has been specified in RevitPullConfig. Please consider this can be time consuming due to the amount of conversions."); - return result; + return result.Select(x => x.IPostprocess(transform, settings)).Where(x => x != null).ToList(); } /***************************************************/ - public static List Read(Element element, Discipline discipline, Transform transform, RevitSettings settings = null, Dictionary> refObjects = null) + public static List Read(Element element, Discipline discipline, RevitSettings settings = null, Dictionary> refObjects = null) { if (element == null || !element.IsValidObject) return new List(); @@ -353,7 +322,7 @@ public static List Read(Element element, Discipline discipline, Tra List result = null; try { - result = element.IFromRevit(discipline, transform, settings, refObjects); + result = element.IFromRevit(discipline, settings, refObjects); } catch (Exception exception) { diff --git a/Revit_Core_Engine/Convert/FromRevit.cs b/Revit_Core_Engine/Convert/FromRevit.cs index a33531928..66f80fe35 100644 --- a/Revit_Core_Engine/Convert/FromRevit.cs +++ b/Revit_Core_Engine/Convert/FromRevit.cs @@ -44,13 +44,12 @@ public static partial class Convert [Description("Interface method that tries to find a suitable FromRevit convert for any Revit Element.")] [Input("element", "Revit Element to be converted.")] [Input("discipline", "Engineering discipline based on the BHoM discipline classification.")] - [Input("transform", "Optional, a transform to apply to the converted object.")] [Input("settings", "Revit adapter settings to be used while performing the convert.")] [Input("refObjects", "Optional, a collection of objects already processed in the current adapter action, stored to avoid processing the same object more than once.")] [Output("fromRevit", "Resulted BHoM object converted from a Revit Element.")] - public static List IFromRevit(this Element element, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary> refObjects = null) + public static List IFromRevit(this Element element, Discipline discipline, RevitSettings settings = null, Dictionary> refObjects = null) { - return FromRevit(element as dynamic, discipline, transform, settings, refObjects); + return FromRevit(element as dynamic, discipline, settings, refObjects); } /***************************************************/ @@ -74,11 +73,10 @@ public static IGeometry IFromRevit(this Location location) [Description("Converts a Revit EnergyAnalysisDetailModel to a BHoM object based on the requested engineering discipline.")] [Input("energyAnalysisModel", "Revit EnergyAnalysisDetailModel to be converted.")] [Input("discipline", "Engineering discipline based on the BHoM discipline classification.")] - [Input("transform", "Optional, a transform to apply to the converted object.")] [Input("settings", "Revit adapter settings to be used while performing the convert.")] [Input("refObjects", "Optional, a collection of objects already processed in the current adapter action, stored to avoid processing the same object more than once.")] [Output("fromRevit", "Resulted BHoM object converted from a Revit EnergyAnalysisDetailModel.")] - public static List FromRevit(this EnergyAnalysisDetailModel energyAnalysisModel, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary> refObjects = null) + public static List FromRevit(this EnergyAnalysisDetailModel energyAnalysisModel, Discipline discipline, RevitSettings settings = null, Dictionary> refObjects = null) { if (energyAnalysisModel == null) { @@ -100,11 +98,10 @@ public static List FromRevit(this EnergyAnalysisDetailModel energyA [Description("Converts a Revit AssemblyInstance to a BHoM object based on the requested engineering discipline.")] [Input("assemblyInstance", "Revit AssemblyInstance to be converted.")] [Input("discipline", "Engineering discipline based on the BHoM discipline classification.")] - [Input("transform", "Optional, a transform to apply to the converted object. Irrelevant in case of assembly instances.")] [Input("settings", "Revit adapter settings to be used while performing the convert.")] [Input("refObjects", "Optional, a collection of objects already processed in the current adapter action, stored to avoid processing the same object more than once.")] [Output("fromRevit", "Resulted BHoM object converted from a Revit AssemblyInstance.")] - public static List FromRevit(this AssemblyInstance assemblyInstance, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary> refObjects = null) + public static List FromRevit(this AssemblyInstance assemblyInstance, Discipline discipline, RevitSettings settings = null, Dictionary> refObjects = null) { if (assemblyInstance == null) { @@ -114,7 +111,7 @@ public static List FromRevit(this AssemblyInstance assemblyInstance foreach (ElementId memberId in assemblyInstance.GetMemberIds()) { - assemblyInstance.Document.GetElement(memberId).IFromRevit(discipline, transform, settings, refObjects); + assemblyInstance.Document.GetElement(memberId).IFromRevit(discipline, settings, refObjects); } return new List { assemblyInstance.AssemblyFromRevit(settings, refObjects) }; @@ -125,11 +122,10 @@ public static List FromRevit(this AssemblyInstance assemblyInstance [Description("Converts a Revit Element to a BHoM object based on the requested engineering discipline.")] [Input("element", "Revit EnergyAnalysisDetailModel to be converted.")] [Input("discipline", "Engineering discipline based on the BHoM discipline classification.")] - [Input("transform", "Optional, a transform to apply to the converted object.")] [Input("settings", "Revit adapter settings to be used while performing the convert.")] [Input("refObjects", "Optional, a collection of objects already processed in the current adapter action, stored to avoid processing the same object more than once.")] [Output("fromRevit", "Resulted BHoM object converted from a Revit Element.")] - public static List FromRevit(this Element element, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary> refObjects = null) + public static List FromRevit(this Element element, Discipline discipline, RevitSettings settings = null, Dictionary> refObjects = null) { if (element == null) { @@ -159,12 +155,11 @@ public static List FromRevit(this Element element, Discipline disci List result = null; if (converted is IBHoMObject) - result = new List { ((IBHoMObject)converted).IPostprocess(transform, settings) }; + result = new List { ((IBHoMObject)converted) }; else if (converted is IEnumerable) { result = new List(((IEnumerable)converted) - .Where(x => x != null) - .Select(x => x.IPostprocess(transform, settings))); + .Where(x => x != null)); } return result; diff --git a/Revit_Core_Engine/Modify/Postprocess.cs b/Revit_Core_Engine/Modify/Postprocess.cs index 651c6753c..c71b2f12e 100644 --- a/Revit_Core_Engine/Modify/Postprocess.cs +++ b/Revit_Core_Engine/Modify/Postprocess.cs @@ -23,14 +23,19 @@ using Autodesk.Revit.DB; using BH.Engine.Adapters.Revit; using BH.Engine.Base; -using BH.Engine.Spatial; +using BH.Engine.Geometry; +using BH.Engine.Graphics; +using BH.oM.Adapters.Revit; using BH.oM.Adapters.Revit.Elements; using BH.oM.Adapters.Revit.Settings; using BH.oM.Base; +using BH.oM.Base.Attributes; using BH.oM.Dimensional; using BH.oM.Geometry; -using BH.oM.Base.Attributes; +using BH.oM.MEP.Fragments; +using BH.oM.Spatial.SettingOut; using System.ComponentModel; +using System.Linq; namespace BH.Revit.Engine.Core { @@ -71,7 +76,17 @@ public static IElement Postprocess(this IElement element, Transform transform, R settings = settings.DefaultIfNull(); if (transform?.IsIdentity == false) - element = element.ITransform(transform.FromRevit(), settings.DistanceTolerance); + { + TransformMatrix bhomTransform = transform.FromRevit(); + element = BH.Engine.Spatial.Modify.ITransform(element, bhomTransform, settings.DistanceTolerance); + + if (element is IBHoMObject) + { + IBHoMObject obj = (IBHoMObject)element; + obj.TransformGeometry(bhomTransform); + obj.TransformRepresentation(bhomTransform); + } + } return element; } @@ -94,7 +109,12 @@ public static IInstance Postprocess(this ModelInstance instance, Transform trans settings = settings.DefaultIfNull(); if (transform?.IsIdentity == false) - instance = instance.Transform(transform.FromRevit(), settings.DistanceTolerance) as ModelInstance; + { + TransformMatrix bhomTransform = transform.FromRevit(); + instance = instance.Transform(bhomTransform, settings.DistanceTolerance) as ModelInstance; + instance.TransformGeometry(bhomTransform); + instance.TransformRepresentation(bhomTransform); + } return instance; } @@ -118,16 +138,54 @@ public static BH.oM.Spatial.SettingOut.Level Postprocess(this BH.oM.Spatial.Sett { level = level.ShallowClone(); level.Elevation += transform.Origin.Z.ToSI(SpecTypeId.Length); + TransformMatrix bhomTransform = transform.FromRevit(); + level.TransformGeometry(bhomTransform); + level.TransformRepresentation(bhomTransform); } return level; } + /***************************************************/ + /**** Private methods ****/ + /***************************************************/ + + private static void TransformGeometry(this IBHoMObject obj, TransformMatrix transform) + { + RevitGeometry geometryFragment = obj.FindFragment(); + if (geometryFragment != null) + { + obj.Fragments.Remove(geometryFragment); + obj.Fragments.Add(new RevitGeometry + ( + geometryFragment.Edges?.Select(x => x.ITransform(transform)).ToList(), + geometryFragment.Surfaces?.Select(x => x.ITransform(transform)).ToList(), + geometryFragment.Meshes?.Select(x => x.Transform(transform)).ToList() + )); + } + } + + /***************************************************/ + + private static void TransformRepresentation(this IBHoMObject obj, TransformMatrix transform) + { + RevitRepresentation representationFragment = obj.FindFragment(); + if (representationFragment != null) + { + obj.Fragments.Remove(representationFragment); + obj.Fragments.Add(new RevitRepresentation + ( + representationFragment.RenderMeshes?.Select(x => x.Transform(transform)) + )); + } + } + + /***************************************************/ /**** Fallback methods ****/ /***************************************************/ - + private static IObject Postprocess(this IObject obj, Transform transform, RevitSettings settings) { return obj; @@ -136,7 +194,3 @@ private static IObject Postprocess(this IObject obj, Transform transform, RevitS /***************************************************/ } } - - - -