Compare commits

..

3 Commits

Author SHA1 Message Date
oguzhankoral 479bbbb040 do not run conversion on main 2025-03-28 12:15:32 +03:00
Björn Steinhagen 378438f1bc fix(revit): respect view visibility in linked models when sending (#716)
* fix(revit): sening via views with correct visibility to linked models

* handle not null

* pass path name to function instead full doc

* refactor: move common code outside

* docs: commenting on known limitations

* refactor: preprocessor directive specific stuff back in place

* refactor: oversight on common code

* fix: method name

* docs: cleanup

---------

Co-authored-by: oguzhankoral <oguzhankoral@gmail.com>
2025-03-27 14:44:11 +03:00
Adam Hathcock 07a681eda7 Avoid multiple enumeration issues when saving if we copy the list first (#713) 2025-03-26 13:14:10 +00:00
3 changed files with 51 additions and 16 deletions
@@ -3,6 +3,8 @@ using Autodesk.Revit.DB;
using Speckle.Connectors.DUI.Models.Card.SendFilter;
using Speckle.Connectors.RevitShared;
using Speckle.Connectors.RevitShared.Operations.Send.Filters;
using Speckle.Converters.RevitShared.Helpers;
using Speckle.Sdk.Common;
namespace Speckle.Connectors.Revit.HostApp;
@@ -14,9 +16,14 @@ namespace Speckle.Connectors.Revit.HostApp;
/// </summary>
public class LinkedModelHandler
{
// Dictionary to track linked model display names
private readonly RevitContext _revitContext;
public Dictionary<string, string> LinkedModelDisplayNames { get; } = new();
public LinkedModelHandler(RevitContext revitContext)
{
_revitContext = revitContext;
}
/// <summary>
/// Gets elements from a linked document based on the provided send filter.
/// This method handles the specifics of element collection but doesn't make decisions
@@ -38,6 +45,36 @@ public class LinkedModelHandler
}
return new List<Element>();
}
// send mode → Views (taken from the legacy code)
if (sendFilter is RevitViewsFilter viewFilter && viewFilter.GetView() != null)
{
RevitLinkInstance linkInstance = FindLinkInstanceForDocument(
linkedDocument.PathName,
_revitContext.UIApplication.NotNull().ActiveUIDocument.Document
);
#if REVIT2024_OR_GREATER
// revit 2024 and 2025 we can use the three-parameter constructor to get only visible elements
using var viewCollector = new FilteredElementCollector(
_revitContext.UIApplication.ActiveUIDocument.Document,
viewFilter.GetView().NotNull().Id,
linkInstance.Id
);
return viewCollector.WhereElementIsNotElementType().ToElements().ToList();
#else
// 🚨 LIMITATION: in Revit 2023 and below, we can only check if the entire linked model is visible,
// not individual elements within it. If the linked model is visible, all its elements will be included.
// constructor overload pertaining to searching and filtering visible elements from a revit link only added 2024.
if (linkInstance.IsHidden(viewFilter.GetView().NotNull()))
{
return new List<Element>(); // if the linked model is hidden, return no elements
}
// 💩 fallback to getting all elements if the linked model is visible
return GetAllElementsForLinkedModelSelection(linkedDocument);
#endif
}
// send mode → Selection
return GetAllElementsForLinkedModelSelection(linkedDocument);
}
@@ -128,4 +165,14 @@ public class LinkedModelHandler
using var collector = new FilteredElementCollector(linkedDoc);
return collector.WhereElementIsNotElementType().WhereElementIsViewIndependent().ToList();
}
private RevitLinkInstance FindLinkInstanceForDocument(string linkedDocumentPath, Document mainDocument)
{
using var collector = new FilteredElementCollector(mainDocument);
return collector
.OfClass(typeof(RevitLinkInstance))
.Cast<RevitLinkInstance>()
.FirstOrDefault(link => link.GetLinkDocument()?.PathName == linkedDocumentPath)
.NotNull();
}
}
@@ -5,7 +5,6 @@ using Speckle.Connectors.Common.Caching;
using Speckle.Connectors.Common.Conversion;
using Speckle.Connectors.Common.Extensions;
using Speckle.Connectors.Common.Operations;
using Speckle.Connectors.Common.Threading;
using Speckle.Connectors.DUI.Exceptions;
using Speckle.Connectors.Revit.HostApp;
using Speckle.Converters.Common;
@@ -23,7 +22,6 @@ public class RevitRootObjectBuilder(
IConverterSettingsStore<RevitConversionSettings> converterSettings,
ISendConversionCache sendConversionCache,
ElementUnpacker elementUnpacker,
IThreadContext threadContext,
SendCollectionManager sendCollectionManager,
ILogger<RevitRootObjectBuilder> logger,
RevitToSpeckleCacheSingleton revitToSpeckleCacheSingleton,
@@ -35,16 +33,6 @@ public class RevitRootObjectBuilder(
SendInfo sendInfo,
IProgress<CardProgress> onOperationProgressed,
CancellationToken ct = default
) =>
threadContext.RunOnMainAsync(
() => Task.FromResult(BuildSync(documentElementContexts, sendInfo, onOperationProgressed, ct))
);
private RootObjectBuilderResult BuildSync(
IReadOnlyList<DocumentToConvert> documentElementContexts,
SendInfo sendInfo,
IProgress<CardProgress> onOperationProgressed,
CancellationToken cancellationToken
)
{
var doc = converterSettings.Current.Document;
@@ -155,7 +143,7 @@ public class RevitRootObjectBuilder(
var atomicObjects = atomicObjectByDocumentAndTransform.Elements;
foreach (Element revitElement in atomicObjects)
{
cancellationToken.ThrowIfCancellationRequested();
ct.ThrowIfCancellationRequested();
string applicationId = revitElement.UniqueId;
string sourceType = revitElement.GetType().Name;
try
@@ -241,6 +229,6 @@ public class RevitRootObjectBuilder(
// NOTE: these are currently not used anywhere, we'll skip them until someone calls for it back
// rootObject[ProxyKeys.PARAMETER_DEFINITIONS] = _parameterDefinitionHandler.Definitions;
return new RootObjectBuilderResult(rootObject, results);
return Task.FromResult(new RootObjectBuilderResult(rootObject, results));
}
}
@@ -128,7 +128,7 @@ public abstract class DocumentModelStore(IJsonSerializer serializer)
}
}
protected string Serialize() => serializer.Serialize(Models);
protected string Serialize() => serializer.Serialize(Models.ToList());
// POC: this seemms more like a IModelsDeserializer?, seems disconnected from this class
protected List<ModelCard> Deserialize(string models) => serializer.Deserialize<List<ModelCard>>(models).NotNull();