From 83111ccd8f7a6aba198577b0a065ba429e16b7f1 Mon Sep 17 00:00:00 2001 From: Yaroslav Zhmayev Date: Thu, 27 Apr 2023 21:20:58 +0300 Subject: [PATCH] some more improvements --- Directory.Build.props | 8 +- Directory.Build.targets | 6 +- src/Addin/App.cs | 5 +- ...deCave.Threejs.Revit.Exporter.Addin.csproj | 27 +++++- src/Addin/Ribbon/RibbonButton{T}.cs | 6 +- .../CodeCave.Threejs.Revit.Exporter.csproj | 32 +++---- src/Exporter/FamilyExporter/FamilyExporter.cs | 89 +++++++++++++++---- .../Helpers/ObjectSceneExportContext.cs | 11 +-- src/Exporter/Helpers/ObjectSceneExporter.cs | 7 +- 9 files changed, 127 insertions(+), 64 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 1a20fcb..6d035ae 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,15 +1,15 @@ - 8.0 + latest netstandard2.1 - netcoreapp3.1 + net6.0 true - 3.1.3 - 3.1.3 + 6.0.* + 6.0.* diff --git a/Directory.Build.targets b/Directory.Build.targets index 15984f2..1b281ab 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -33,7 +33,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -49,7 +49,7 @@ - + all runtime; build; native; contentfiles; analyzers @@ -57,7 +57,7 @@ - + all runtime; build; native; contentfiles; analyzers diff --git a/src/Addin/App.cs b/src/Addin/App.cs index 3a97ac6..be781e4 100644 --- a/src/Addin/App.cs +++ b/src/Addin/App.cs @@ -35,10 +35,7 @@ public sealed class App : IExternalApplication /// static App() { -#if WINFORMS - System.Windows.Forms.Application.EnableVisualStyles(); - System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); -#endif + } /// diff --git a/src/Addin/CodeCave.Threejs.Revit.Exporter.Addin.csproj b/src/Addin/CodeCave.Threejs.Revit.Exporter.Addin.csproj index abbc9d5..50180de 100644 --- a/src/Addin/CodeCave.Threejs.Revit.Exporter.Addin.csproj +++ b/src/Addin/CodeCave.Threejs.Revit.Exporter.Addin.csproj @@ -27,7 +27,7 @@ e.g. if you want to debug ONLY Revit 2019, put net47 below and switch to 'Debug One' configuration --> - net471 + net48 2014 @@ -98,6 +98,7 @@ + @@ -126,6 +127,11 @@ + + + + + @@ -134,12 +140,31 @@ + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + CodeCave.Threejs.Revit.Exporter.Addin Threejs.Exporter.Addin diff --git a/src/Addin/Ribbon/RibbonButton{T}.cs b/src/Addin/Ribbon/RibbonButton{T}.cs index 475366c..f0ce772 100644 --- a/src/Addin/Ribbon/RibbonButton{T}.cs +++ b/src/Addin/Ribbon/RibbonButton{T}.cs @@ -23,8 +23,12 @@ internal class RibbonButton : RibbonButton /// public RibbonButton() { + var thisNamespace = GetType().Namespace; commandAssemly = typeof(T).Assembly; - rootNameSpace = typeof(T).Assembly.ManifestModule.GetTypes().Min(t => t.Namespace); + rootNameSpace = typeof(T).Assembly.ManifestModule + .GetTypes() + .Where(t => t.Namespace is not null && t.Namespace.StartsWith(thisNamespace, StringComparison.OrdinalIgnoreCase)) + .Min(t => t.Namespace) ?? thisNamespace; } /// diff --git a/src/Exporter/CodeCave.Threejs.Revit.Exporter.csproj b/src/Exporter/CodeCave.Threejs.Revit.Exporter.csproj index b2da552..eb21cb2 100644 --- a/src/Exporter/CodeCave.Threejs.Revit.Exporter.csproj +++ b/src/Exporter/CodeCave.Threejs.Revit.Exporter.csproj @@ -13,39 +13,26 @@ 2020 | net472 2021 | net48 --> - net48;net472;net471;net461 + net48;net472;net471 - net471 + net48 true true snupkg - 0.1.4 - 0.1.4 - 0.1.4 + 0.1.7 + 0.1.7 + 0.1.7 git https://github.com/CodeCavePro/threejs-revit-exporter.git https://github.com/CodeCavePro/threejs-revit-exporter.git nuget, package, library, three.js, revit, export - - 2018 - - - 2019 - - - 2020 - - - 2021 - - true RVT$(RevitVersion) @@ -53,8 +40,7 @@ - - + @@ -90,7 +76,11 @@ - + + + + + diff --git a/src/Exporter/FamilyExporter/FamilyExporter.cs b/src/Exporter/FamilyExporter/FamilyExporter.cs index ba18a31..9889a54 100644 --- a/src/Exporter/FamilyExporter/FamilyExporter.cs +++ b/src/Exporter/FamilyExporter/FamilyExporter.cs @@ -75,11 +75,13 @@ public void ExportRfaFile(Document docWrapper, View3D view3d, string rfaFilePath if (family is null) { - using var t = new Transaction(docWrapper, $"Load the family '{familyName}'"); - t.Start(); + using var tr = new Transaction(docWrapper, $"Load the family '{familyName}'"); + tr.Start(); + if (!docWrapper.LoadFamily(rfaFilePath, out family)) throw new InvalidOperationException(); - t.Commit(); + + tr.Commit(); } foreach (var typElementId in family.GetFamilySymbolIds()) @@ -95,8 +97,8 @@ public void ExportRfaFile(Document docWrapper, View3D view3d, string rfaFilePath var familyTypeExportArgs = new FamilySymbolExportEventArgs { FamilyFilePath = rfaFilePath, Symbol = familySymbol?.Name, OutputFilePath = outputFilePath }; this?.OnSymbolExportStarted(familyTypeExportArgs); - if (string.IsNullOrWhiteSpace(outputFilePath) || File.Exists(outputFilePath)) - continue; + if (string.IsNullOrWhiteSpace(outputFilePath)) + throw new InvalidDataException(); using (var t = new Transaction(docWrapper, $"Placing family instance '{familySymbol.Name}'")) { @@ -109,6 +111,7 @@ public void ExportRfaFile(Document docWrapper, View3D view3d, string rfaFilePath familySymbol, StructuralType.NonStructural); + docWrapper.Regenerate(); t.Commit(); } @@ -128,26 +131,78 @@ public void ExportRfaFile(Document docWrapper, View3D view3d, string rfaFilePath this?.OnSymbolExportEnded(familyTypeExportArgs); - using (var t = new Transaction(docWrapper, $"Remove family symbol '{familyInstance.Name}'")) - { - t.Start(); - docWrapper.Delete(familySymbol.Id); - t.Commit(); - } + using var tr2 = new Transaction(docWrapper, $"Remove family symbol '{familyInstance.Name}'"); + tr2.Start(); + docWrapper.Delete(familySymbol.Id); + tr2.Commit(); familyInstance.Dispose(); } this?.OnExportEnded(familyExportArgs); - using (var t = new Transaction(docWrapper, $"Remove family '{family.Name}'")) + using var td = new Transaction(docWrapper, $"Remove family '{family.Name}'"); + td.Start(); + docWrapper.Delete(family.Id); + td.Commit(); + + family.Dispose(); + } + + private void HideClearances(View3D view3D) + { + var specialtyEquipmentCategory = view3D.Document.Settings.Categories.get_Item(BuiltInCategory.OST_SpecialityEquipment); + var specialtySubcats = specialtyEquipmentCategory.SubCategories.Cast().ToList(); + var clearanceSubCategories = specialtySubcats.Where(s => s.Name.ToUpperInvariant().Contains("CLEARANCE")).ToArray(); + + if (clearanceSubCategories.Any()) { - t.Start(); - docWrapper.Delete(family.Id); - t.Commit(); + using var subTransaction = new SubTransaction(view3D.Document); + subTransaction.Start(); + + try + { + foreach (Category category in clearanceSubCategories.OfType()) + { + if (category.get_AllowsVisibilityControl(view3D)) + category.set_Visible(view3D, false); + } + } + catch (Exception ex) + { + // TODO add logging + } + + subTransaction.Commit(); } - family.Dispose(); + foreach (var specialtySubCategory in specialtySubcats) + specialtySubCategory.Dispose(); + + specialtyEquipmentCategory?.Dispose(); + } + + private FamilyInstance InsertFamilySymbol(FamilySymbol familySymbol) + { + // TODO add logging + // logger.LogInformation($"Placing family instance '{familySymbol.Name}' in XYZ(0,0,0)"); + + var docWrapper = familySymbol.Family.Document; + using var subTransaction = new SubTransaction(docWrapper); + subTransaction.Start(); + + if (!familySymbol.IsActive) + familySymbol.Activate(); + + var familyInstance = docWrapper.Create.NewFamilyInstance( + XYZ.Zero, + familySymbol, + StructuralType.NonStructural + ); + + subTransaction.Commit(); + + return familyInstance; } public void ExportRvtFile(UIApplication uiapp, string projectPath) @@ -176,8 +231,8 @@ public void ExportRvtFile(UIApplication uiapp, string projectPath) { TaskDialog.Show("Error", $"Failed to export '{projectPath}'"); } - var objectSceneJson = objectScene.ToString(); + var objectSceneJson = objectScene.ToString(); var outputFilePath = Path.ChangeExtension(projectPath, ".json"); File.WriteAllText(outputFilePath, objectSceneJson); } diff --git a/src/Exporter/Helpers/ObjectSceneExportContext.cs b/src/Exporter/Helpers/ObjectSceneExportContext.cs index e160ebe..5abd2f8 100644 --- a/src/Exporter/Helpers/ObjectSceneExportContext.cs +++ b/src/Exporter/Helpers/ObjectSceneExportContext.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using Autodesk.Revit.DB; -using Autodesk.Revit.DB.Lighting; using CodeCave.Threejs.Entities; using CodeCave.Threejs.Revit.Exporter.Helpers; @@ -128,7 +127,8 @@ public void OnViewEnd(ElementId elementId) current.Element.AddChild(obj); } - outputScene.Object.AddChild(current.Element); + if (current.Element is not null) + outputScene.Object.AddChild(current.Element); } catch (Exception ex) { @@ -153,9 +153,6 @@ public RenderNodeAction OnElementBegin(ElementId elementId) if (element is null || string.IsNullOrWhiteSpace(uid)) throw new InvalidDataException(); - //if (element is Autodesk.Revit.DB.Group || element is Level) - // return RenderNodeAction.Skip; - Debug.WriteLine($"OnElementBegin: id {elementId.IntegerValue} category {element.Category?.Name} name {element.Name}"); if (outputScene.Object.HasChild(uid)) @@ -212,8 +209,8 @@ public void OnElementEnd(ElementId elementId) if (element is null || string.IsNullOrWhiteSpace(uid)) throw new InvalidDataException(); - //if (element is Autodesk.Revit.DB.Group || element is Level) - // return; + if (element is Level) // Skip levels + return; Debug.WriteLine($"OnElementEnd: id {elementId.IntegerValue} category {element.Category.Name} name {element.Name}"); diff --git a/src/Exporter/Helpers/ObjectSceneExporter.cs b/src/Exporter/Helpers/ObjectSceneExporter.cs index 8f4353a..260a5c5 100644 --- a/src/Exporter/Helpers/ObjectSceneExporter.cs +++ b/src/Exporter/Helpers/ObjectSceneExporter.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; using Autodesk.Revit.DB; using CodeCave.Threejs.Entities; @@ -67,14 +66,10 @@ public bool TryExport(View3D viewToExport, out ObjectScene outputScene, bool thr outputScene = this.context.GetResult(); return outputScene?.Object != null; } - catch (Exception) when (!throwException) + catch when (!throwException) { return false; } - catch - { - throw; - } } } }