-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDesignAutomationHandler.cs
98 lines (92 loc) · 3.98 KB
/
DesignAutomationHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DesignAutomationFramework;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
namespace DesignAutomationHandler
{
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class DesignAutomationHandlerApp : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var app = commandData.Application.Application;
string[] files = Directory.GetFiles(app.AllUsersAddinsLocation, "*.addin");
foreach (string file in files)
{
XElement addin;
try
{
addin = XElement.Load(file);
}
catch
{
MessageBox.Show($"Malformed XML in addin: {file}", "DesignAutomationHandler");
// if the XML is malformed or not parse-able, just continue
continue;
}
IEnumerable<XElement> childList = from el in addin.Elements() select el;
foreach (XElement e in childList)
{
try
{
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFile(e.Element("Assembly").Value);
bool designAutomationBridge = false;
bool revitAPIUI = false;
foreach (System.Reflection.AssemblyName an in a.GetReferencedAssemblies())
{
string assemblyName = an.Name;
if (assemblyName == "DesignAutomationBridge")
designAutomationBridge = true;
else if (assemblyName == "RevitAPIUI")
revitAPIUI = true;
}
if (designAutomationBridge && revitAPIUI)
MessageBox.Show($"RevitAPIUI detected in DA Plugin: {e.Element("Assembly").Value}", "DesignAutomationHandler");
}
catch
{
// in case we can't open the dll for some reason, just continue
continue;
}
}
}
var doc = commandData.Application.ActiveUIDocument?.Document;
HandleDAApplication(app, doc);
return Result.Succeeded;
}
public void HandleDAApplication(Autodesk.Revit.ApplicationServices.Application app, Document doc)
{
try
{
var filename = doc?.PathName;
var currentdir = Directory.GetCurrentDirectory();
var message = string.Empty;
if (string.IsNullOrEmpty(filename))
{
message = $"No input file.\nIf you have json file for parameters, now copy it under the current folder:\n{currentdir}";
MessageBox.Show(message, "DesignAutomationHandler");
}
bool designAutomationResult = DesignAutomationBridge.SetDesignAutomationReady(app, filename);
if (designAutomationResult)
{
var resultFolder = string.IsNullOrEmpty(filename) ? currentdir : Path.GetDirectoryName(filename);
message = $"Succeed!\nFind the results at folder: {resultFolder}";
}
else
{
message = $"Failed! You may debug the addin dll.";
}
MessageBox.Show(message, "DesignAutomationHandler");
}
catch(System.Exception e)
{
MessageBox.Show(e.ToString(), "DesignAutomationHandler");
}
}
}
}