Skip to content

Commit

Permalink
#1477 finally fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelbaran committed Jun 12, 2024
1 parent f2f832a commit 198c2f6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 75 deletions.
75 changes: 22 additions & 53 deletions Revit_Core_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,6 @@ public static List<IBHoMObject> 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<string, Dictionary<string, List<IBHoMObject>>>();

Expand All @@ -250,110 +233,96 @@ public static List<IBHoMObject> Read(Document document, Transform transform, Lis
Dictionary<string, List<IBHoMObject>> refObjects = globalRefObjects[document.Title];

List<IBHoMObject> result = new List<IBHoMObject>();
List<ElementId> remainingElementIds = elementIds.ToList();
for (int i = 0; i < remainingElementIds.Count; i++)
List<ElementId> remainingElementIds = new List<ElementId>();
foreach (ElementId id in elementIds)
{
ElementId id = remainingElementIds[i];
var existing = refObjects.GetValues<IBHoMObject>(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<IBHoMObject> iBHoMObjects = Read(element, discipline, transform, settings, refObjects);

if (iBHoMObjects != null && iBHoMObjects.Any())
IEnumerable<IBHoMObject> 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<ICurve> edges = null;
if (geometryConfig.PullEdges)
{
edges = element.Curves(geometryOptions, settings, true).FromRevit();
if (bHoMTransform != null)
edges = edges.Select(x => x?.ITransform(bHoMTransform)).ToList();
}

List<ISurface> 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<oM.Geometry.Mesh> 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<RenderMesh> 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);
}
}

bool[] activePulls = new bool[] { geometryConfig.PullEdges, geometryConfig.PullSurfaces, geometryConfig.PullMeshes, representationConfig.PullRenderMesh };
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<IBHoMObject> Read(Element element, Discipline discipline, Transform transform, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
public static List<IBHoMObject> Read(Element element, Discipline discipline, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
{
if (element == null || !element.IsValidObject)
return new List<IBHoMObject>();

List<IBHoMObject> result = null;
try
{
result = element.IFromRevit(discipline, transform, settings, refObjects);
result = element.IFromRevit(discipline, settings, refObjects);
}
catch (Exception exception)
{
Expand Down
21 changes: 8 additions & 13 deletions Revit_Core_Engine/Convert/FromRevit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IBHoMObject> IFromRevit(this Element element, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
public static List<IBHoMObject> IFromRevit(this Element element, Discipline discipline, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
{
return FromRevit(element as dynamic, discipline, transform, settings, refObjects);
return FromRevit(element as dynamic, discipline, settings, refObjects);
}

/***************************************************/
Expand All @@ -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<IBHoMObject> FromRevit(this EnergyAnalysisDetailModel energyAnalysisModel, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
public static List<IBHoMObject> FromRevit(this EnergyAnalysisDetailModel energyAnalysisModel, Discipline discipline, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
{
if (energyAnalysisModel == null)
{
Expand All @@ -100,11 +98,10 @@ public static List<IBHoMObject> 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<IBHoMObject> FromRevit(this AssemblyInstance assemblyInstance, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
public static List<IBHoMObject> FromRevit(this AssemblyInstance assemblyInstance, Discipline discipline, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
{
if (assemblyInstance == null)
{
Expand All @@ -114,7 +111,7 @@ public static List<IBHoMObject> 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<IBHoMObject> { assemblyInstance.AssemblyFromRevit(settings, refObjects) };
Expand All @@ -125,11 +122,10 @@ public static List<IBHoMObject> 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<IBHoMObject> FromRevit(this Element element, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
public static List<IBHoMObject> FromRevit(this Element element, Discipline discipline, RevitSettings settings = null, Dictionary<string, List<IBHoMObject>> refObjects = null)
{
if (element == null)
{
Expand Down Expand Up @@ -159,12 +155,11 @@ public static List<IBHoMObject> FromRevit(this Element element, Discipline disci

List<IBHoMObject> result = null;
if (converted is IBHoMObject)
result = new List<IBHoMObject> { ((IBHoMObject)converted).IPostprocess(transform, settings) };
result = new List<IBHoMObject> { ((IBHoMObject)converted) };
else if (converted is IEnumerable<IBHoMObject>)
{
result = new List<IBHoMObject>(((IEnumerable<IBHoMObject>)converted)
.Where(x => x != null)
.Select(x => x.IPostprocess(transform, settings)));
.Where(x => x != null));
}

return result;
Expand Down
72 changes: 63 additions & 9 deletions Revit_Core_Engine/Modify/Postprocess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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<RevitGeometry>();
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<RevitRepresentation>();
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;
Expand All @@ -136,7 +194,3 @@ private static IObject Postprocess(this IObject obj, Transform transform, RevitS
/***************************************************/
}
}




0 comments on commit 198c2f6

Please sign in to comment.