Skip to content

Commit 78f19fc

Browse files
committed
update: use dockablepanel only
- single dockablepanel handle all stuff
1 parent 247176e commit 78f19fc

18 files changed

+254
-597
lines changed

source/RevitDevTool/Application.cs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,61 @@
1-
using Nice3point.Revit.Toolkit.External;
2-
using RevitDevTool.Revit.Command;
3-
using RevitDevTool.Theme;
4-
using RevitDevTool.Utils;
5-
using RevitDevTool.View;
1+
using System.IO;
2+
using System.Reflection;
3+
using Autodesk.Revit.UI;
4+
using Autodesk.Revit.UI.Events;
5+
using RevitDevTool.Commands;
6+
using RevitDevTool.Extensions;
7+
using RevitDevTool.Handlers;
68

79
namespace RevitDevTool;
810

9-
[UsedImplicitly]
10-
public class Application : ExternalApplication
11+
[PublicAPI]
12+
public class Application : IExternalApplication
1113
{
12-
public const string Name = "RevitDevTool";
13-
public const string Panel = "TraceLog";
14-
15-
public override void OnStartup()
14+
public static UIControlledApplication RevitUiControlledApplication { get; private set; } = null!;
15+
public static UIApplication RevitUiApplication => new RibbonItemEventArgs().Application;
16+
public static Autodesk.Revit.ApplicationServices.Application RevitApplication => RevitUiApplication.Application;
17+
public static UIDocument? RevitActiveUiDocument => RevitUiApplication.ActiveUIDocument;
18+
public static Document? RevitActiveDocument => RevitActiveUiDocument?.Document;
19+
public static bool IsInRevitApiMode => RevitUiApplication.ActiveAddInId is not null;
20+
21+
public Result OnStartup(UIControlledApplication application)
1622
{
17-
AddButton();
18-
AddDockable();
23+
ResolveAssemblies();
24+
RevitUiControlledApplication = application;
25+
ExternalEventController.Register();
26+
AddButton(application);
27+
AddDockable(application);
28+
return Result.Succeeded;
1929
}
2030

21-
private void AddButton()
31+
public Result OnShutdown(UIControlledApplication application)
32+
{
33+
return Result.Succeeded;
34+
}
35+
36+
private static void AddDockable(UIControlledApplication application)
37+
{
38+
TraceCommand.RegisterDockablePane(application);
39+
}
40+
41+
private static void AddButton(UIControlledApplication application)
2242
{
23-
var panel = Application.CreatePanel(Panel, Name);
43+
var panel = application.CreateRibbonPanel("DevTool");
2444

2545
panel.AddPushButton<TraceCommand>("Trace\nPanel")
2646
.SetLargeImage("/RevitDevTool;component/Images/log.png")
2747
.SetLongDescription("Display trace data");
28-
panel.AddPushButton<TraceGeometryCommand>("Trace\nGeometry")
29-
.SetLargeImage("/RevitDevTool;component/Images/switch-off.png")
30-
.SetLongDescription("Trace geometries data");
31-
panel.AddPushButton<ClearTraceGeometryCommand>("Clear\nGeometry")
32-
.SetLargeImage("/RevitDevTool;component/Images/eraser.png")
33-
.SetLongDescription("Clear current document trace geometries data");
3448
}
3549

36-
private void AddDockable()
50+
private static void ResolveAssemblies()
3751
{
38-
DockablePaneRegisterUtils.Register<TraceLog>(Resource.TraceGuid, Application);
52+
var currentAssemblyPath = Assembly.GetExecutingAssembly().Location;
53+
var currentAssemblyDirectory = Path.GetDirectoryName(currentAssemblyPath);
54+
if (currentAssemblyDirectory is null) return;
55+
var assemblyFiles = Directory.GetFiles(currentAssemblyDirectory, "*.dll");
56+
foreach (var assemblyFile in assemblyFiles)
57+
{
58+
Assembly.LoadFrom(assemblyFile);
59+
}
3960
}
4061
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Autodesk.Revit.Attributes;
2+
using Autodesk.Revit.UI;
3+
using RevitDevTool.View;
4+
using DockablePaneProvider = RevitDevTool.Utils.DockablePaneProvider;
5+
6+
namespace RevitDevTool.Commands;
7+
8+
[UsedImplicitly]
9+
[Transaction(TransactionMode.Manual)]
10+
public class TraceCommand : IExternalCommand
11+
{
12+
private const string CommandName = "TraceLog";
13+
private const string Guid = "43AE2B41-0BE6-425A-B27A-724B2CE17351";
14+
15+
public static void RegisterDockablePane(UIControlledApplication application)
16+
{
17+
DockablePaneProvider
18+
.Register(application, new Guid(Guid), CommandName)
19+
.SetConfiguration(data =>
20+
{
21+
data.FrameworkElement = new TraceLog();
22+
data.InitialState = new DockablePaneState
23+
{
24+
MinimumWidth = 300,
25+
MinimumHeight = 400,
26+
DockPosition = DockPosition.Left,
27+
TabBehind = DockablePanes.BuiltInDockablePanes.PropertiesPalette
28+
};
29+
});
30+
}
31+
32+
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
33+
{
34+
try
35+
{
36+
var id = new DockablePaneId(new Guid(Guid));
37+
var dockableWindow = commandData.Application.GetDockablePane(id);
38+
if(!dockableWindow.IsShown())
39+
dockableWindow.Show();
40+
else
41+
dockableWindow.Hide();
42+
return Result.Succeeded;
43+
}
44+
catch (Exception e)
45+
{
46+
TaskDialog.Show("Error", e.ToString());
47+
return Result.Failed;
48+
}
49+
}
50+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Diagnostics;
2+
using System.Reflection;
3+
using RevitDevTool.Handlers;
4+
5+
namespace RevitDevTool.Geometry;
6+
7+
public static class TraceGeometry
8+
{
9+
public static readonly TraceListener TraceListener = new TraceGeometryListener();
10+
public static readonly Dictionary<int, List<ElementId>> DocGeometries = new();
11+
12+
private static MethodInfo? GenerateTransientDisplayMethod()
13+
{
14+
var geometryElementType = typeof(GeometryElement);
15+
var geometryElementTypeMethods =
16+
geometryElementType.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
17+
var method = geometryElementTypeMethods.FirstOrDefault(x => x.Name == "SetForTransientDisplay");
18+
return method;
19+
}
20+
21+
private static void Trace(GeometryObject geometryObject)
22+
{
23+
Trace(new List<GeometryObject> { geometryObject });
24+
}
25+
26+
private static void Trace(IEnumerable<GeometryObject> geometries)
27+
{
28+
ExternalEventController.ActionEventHandler.Raise(app =>
29+
{
30+
var method = GenerateTransientDisplayMethod();
31+
var doc = app.ActiveUIDocument.Document;
32+
var argsM = new object[4];
33+
argsM[0] = doc;
34+
argsM[1] = ElementId.InvalidElementId;
35+
argsM[2] = geometries;
36+
argsM[3] = ElementId.InvalidElementId;
37+
var transientElementId = method?.Invoke(null, argsM) as ElementId;
38+
39+
var hashKey = doc.GetHashCode();
40+
if (DocGeometries.TryGetValue(hashKey, out var value))
41+
{
42+
if (transientElementId != null)
43+
value.Add(transientElementId);
44+
}
45+
else
46+
{
47+
if (transientElementId != null)
48+
DocGeometries[hashKey] = [transientElementId];
49+
}
50+
});
51+
}
52+
53+
private class TraceGeometryListener : TraceListener
54+
{
55+
public override void Write(object? o)
56+
{
57+
if (o is GeometryObject go)
58+
{
59+
Trace(go);
60+
}
61+
if (o is IEnumerable<GeometryObject> geometries)
62+
{
63+
Trace(geometries);
64+
}
65+
66+
base.Write(o);
67+
}
68+
public override void Write(string? message)
69+
{
70+
}
71+
72+
public override void WriteLine(string? message)
73+
{
74+
}
75+
}
76+
}

source/RevitDevTool/Revit/Command/ClearTraceGeometryCommand.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

source/RevitDevTool/Revit/Command/TraceCommand.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

source/RevitDevTool/Revit/Command/TraceGeometryCommand.cs

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)