All the same formatting
This commit is contained in:
+11
-14
@@ -4,22 +4,19 @@ using System.Collections.Generic;
|
||||
using xUnitRevitUtils;
|
||||
|
||||
namespace SampleLibrary {
|
||||
public sealed class DocFixture : IDisposable
|
||||
{
|
||||
public Document Doc { get; set; }
|
||||
public IList<Element> Walls { get; set; }
|
||||
public sealed class DocFixture : IDisposable {
|
||||
public Document Doc { get; set; }
|
||||
public IList<Element> Walls { get; set; }
|
||||
|
||||
|
||||
public DocFixture()
|
||||
{
|
||||
var testModel = Utils.GetTestModel("walls.rvt");
|
||||
Doc = xru.OpenDoc(testModel);
|
||||
public DocFixture() {
|
||||
var testModel = Utils.GetTestModel("walls.rvt");
|
||||
Doc = xru.OpenDoc(testModel);
|
||||
|
||||
Walls = new FilteredElementCollector(Doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
|
||||
Walls = new FilteredElementCollector(Doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
|
||||
+60
-72
@@ -1,72 +1,60 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.IFC;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
using xUnitRevitUtils;
|
||||
|
||||
namespace SampleLibrary
|
||||
{
|
||||
public class SampleTest
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether all walls in the model have a valid volume
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WallsHaveVolume()
|
||||
{
|
||||
var testModel = Utils.GetTestModel("walls.rvt");
|
||||
var doc = xru.OpenDoc(testModel);
|
||||
|
||||
var walls = new FilteredElementCollector(doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
|
||||
|
||||
foreach (var wall in walls)
|
||||
{
|
||||
var volumeParam = wall.get_Parameter(BuiltInParameter.HOST_VOLUME_COMPUTED);
|
||||
Assert.NotNull(volumeParam);
|
||||
Assert.True(volumeParam.AsDouble() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SampleFail()
|
||||
{
|
||||
#if pre2021
|
||||
var feet = UnitUtils.ConvertToInternalUnits(3000, DisplayUnitType.DUT_MILLIMETERS);
|
||||
#else
|
||||
var feet = UnitUtils.ConvertToInternalUnits(3000, UnitTypeId.Feet);
|
||||
#endif
|
||||
Assert.Equal(5, feet);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetWallGrossAreaAndRollBack()
|
||||
{
|
||||
var testModel = Utils.GetTestModel("walls.rvt");
|
||||
var doc = xru.OpenDoc(testModel);
|
||||
var walls = new FilteredElementCollector(doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
|
||||
var wall = walls[0] as Wall;
|
||||
double grossArea = 0;
|
||||
|
||||
var inserts = wall.FindInserts(true, true, true, true);
|
||||
xru.Run(() =>
|
||||
{
|
||||
using (Transaction transaction = new Transaction(doc, "Temporary - only to get gross area"))
|
||||
{
|
||||
transaction.Start();
|
||||
foreach (ElementId insertId in inserts) { doc.Delete(insertId); }
|
||||
doc.Regenerate();
|
||||
var wallFaceReference = HostObjectUtils.GetSideFaces(wall, ShellLayerType.Exterior);
|
||||
var face = doc.GetElement(wallFaceReference.First()).GetGeometryObjectFromReference(wallFaceReference.First()) as PlanarFace;
|
||||
var wallFaceEdges = face.GetEdgesAsCurveLoops();
|
||||
grossArea = ExporterIFCUtils.ComputeAreaOfCurveLoops(wallFaceEdges);
|
||||
transaction.RollBack();
|
||||
}
|
||||
}, doc).Wait();
|
||||
Assert.True(grossArea > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.IFC;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
using xUnitRevitUtils;
|
||||
|
||||
namespace SampleLibrary {
|
||||
public class SampleTest {
|
||||
/// <summary>
|
||||
/// Checks whether all walls in the model have a valid volume
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void WallsHaveVolume() {
|
||||
var testModel = Utils.GetTestModel("walls.rvt");
|
||||
var doc = xru.OpenDoc(testModel);
|
||||
|
||||
var walls = new FilteredElementCollector(doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
|
||||
|
||||
foreach (var wall in walls) {
|
||||
var volumeParam = wall.get_Parameter(BuiltInParameter.HOST_VOLUME_COMPUTED);
|
||||
Assert.NotNull(volumeParam);
|
||||
Assert.True(volumeParam.AsDouble() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SampleFail() {
|
||||
#if pre2021
|
||||
var feet = UnitUtils.ConvertToInternalUnits(3000, DisplayUnitType.DUT_MILLIMETERS);
|
||||
#else
|
||||
var feet = UnitUtils.ConvertToInternalUnits(3000, UnitTypeId.Feet);
|
||||
#endif
|
||||
Assert.Equal(5, feet);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetWallGrossAreaAndRollBack() {
|
||||
var testModel = Utils.GetTestModel("walls.rvt");
|
||||
var doc = xru.OpenDoc(testModel);
|
||||
var walls = new FilteredElementCollector(doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
|
||||
var wall = walls[0] as Wall;
|
||||
double grossArea = 0;
|
||||
|
||||
var inserts = wall.FindInserts(true, true, true, true);
|
||||
xru.Run(() => {
|
||||
using (Transaction transaction = new Transaction(doc, "Temporary - only to get gross area")) {
|
||||
transaction.Start();
|
||||
foreach (ElementId insertId in inserts) { doc.Delete(insertId); }
|
||||
doc.Regenerate();
|
||||
var wallFaceReference = HostObjectUtils.GetSideFaces(wall, ShellLayerType.Exterior);
|
||||
var face = doc.GetElement(wallFaceReference.First()).GetGeometryObjectFromReference(wallFaceReference.First()) as PlanarFace;
|
||||
var wallFaceEdges = face.GetEdgesAsCurveLoops();
|
||||
grossArea = ExporterIFCUtils.ComputeAreaOfCurveLoops(wallFaceEdges);
|
||||
transaction.RollBack();
|
||||
}
|
||||
}, doc).Wait();
|
||||
Assert.True(grossArea > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +1,61 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using xUnitRevitUtils;
|
||||
|
||||
using Autodesk.Revit.DB;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
using xUnitRevitUtils;
|
||||
|
||||
namespace SampleLibrary {
|
||||
public class TestWithFixture : IClassFixture<DocFixture>
|
||||
{
|
||||
DocFixture fixture;
|
||||
public TestWithFixture(DocFixture fixture)
|
||||
{
|
||||
this.fixture = fixture;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CountWalls()
|
||||
{
|
||||
Assert.Equal(4, fixture.Walls.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WallOffset()
|
||||
{
|
||||
var wall = fixture.Doc.GetElement(new ElementId(346573));
|
||||
var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
|
||||
|
||||
#if pre2021
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.DisplayUnitType);
|
||||
#else
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.GetUnitTypeId());
|
||||
#endif
|
||||
|
||||
Assert.Equal(2000, baseOffset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveWallsUp()
|
||||
{
|
||||
var walls = fixture.Walls.Where(x => x.Id.IntegerValue != 346573);
|
||||
|
||||
xru.RunInTransaction(() =>
|
||||
{
|
||||
foreach(var wall in walls)
|
||||
{
|
||||
var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
|
||||
|
||||
#if pre2021
|
||||
var baseOffset = UnitUtils.ConvertToInternalUnits(2000, param.DisplayUnitType);
|
||||
#else
|
||||
var baseOffset = UnitUtils.ConvertToInternalUnits(2000, param.GetUnitTypeId());
|
||||
#endif
|
||||
param.Set(baseOffset);
|
||||
}
|
||||
}, fixture.Doc)
|
||||
.Wait(); // Important! Wait for action to finish
|
||||
|
||||
foreach (var wall in walls)
|
||||
{
|
||||
var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
|
||||
#if pre2021
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.DisplayUnitType);
|
||||
#else
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.GetUnitTypeId());
|
||||
#endif
|
||||
Assert.Equal(2000, baseOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public class TestWithFixture : IClassFixture<DocFixture> {
|
||||
DocFixture fixture;
|
||||
public TestWithFixture(DocFixture fixture) {
|
||||
this.fixture = fixture;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CountWalls() {
|
||||
Assert.Equal(4, fixture.Walls.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WallOffset() {
|
||||
var wall = fixture.Doc.GetElement(new ElementId(346573));
|
||||
var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
|
||||
|
||||
#if pre2021
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.DisplayUnitType);
|
||||
#else
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.GetUnitTypeId());
|
||||
#endif
|
||||
|
||||
Assert.Equal(2000, baseOffset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveWallsUp() {
|
||||
var walls = fixture.Walls.Where(x => x.Id.IntegerValue != 346573);
|
||||
|
||||
xru.RunInTransaction(() => {
|
||||
foreach (var wall in walls) {
|
||||
var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
|
||||
|
||||
#if pre2021
|
||||
var baseOffset = UnitUtils.ConvertToInternalUnits(2000, param.DisplayUnitType);
|
||||
#else
|
||||
var baseOffset = UnitUtils.ConvertToInternalUnits(2000, param.GetUnitTypeId());
|
||||
#endif
|
||||
param.Set(baseOffset);
|
||||
}
|
||||
}, fixture.Doc)
|
||||
.Wait(); // Important! Wait for action to finish
|
||||
|
||||
foreach (var wall in walls) {
|
||||
var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
|
||||
#if pre2021
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.DisplayUnitType);
|
||||
#else
|
||||
var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.GetUnitTypeId());
|
||||
#endif
|
||||
Assert.Equal(2000, baseOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-24
@@ -1,24 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SampleLibrary
|
||||
{
|
||||
public static class Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility method to get models from local folder rather than an absolute path
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetTestModel(string filename)
|
||||
{
|
||||
var path = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "TestModels", filename);
|
||||
return path;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.IO;
|
||||
|
||||
namespace SampleLibrary {
|
||||
public static class Utils {
|
||||
/// <summary>
|
||||
/// Utility method to get models from local folder rather than an absolute path
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetTestModel(string filename) {
|
||||
var path = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "TestModels", filename);
|
||||
return path;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+28
-37
@@ -1,37 +1,28 @@
|
||||
#region Namespaces
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Autodesk.Revit.ApplicationServices;
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
#endregion
|
||||
|
||||
namespace xUnitRevit
|
||||
{
|
||||
class App : IExternalApplication
|
||||
{
|
||||
public Result OnStartup(UIControlledApplication a)
|
||||
{
|
||||
a.ControlledApplication.ApplicationInitialized += ControlledApplication_ApplicationInitialized;
|
||||
|
||||
return Result.Succeeded;
|
||||
}
|
||||
|
||||
private void ControlledApplication_ApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e)
|
||||
{
|
||||
Application app = sender as Application;
|
||||
UIApplication uiapp = new UIApplication(app);
|
||||
|
||||
Runner.ReadConfig();
|
||||
|
||||
if(Runner.Config.autoStart)
|
||||
Runner.Launch(uiapp);
|
||||
}
|
||||
|
||||
public Result OnShutdown(UIControlledApplication a)
|
||||
{
|
||||
return Result.Succeeded;
|
||||
}
|
||||
}
|
||||
}
|
||||
#region Namespaces
|
||||
using Autodesk.Revit.ApplicationServices;
|
||||
using Autodesk.Revit.UI;
|
||||
#endregion
|
||||
|
||||
namespace xUnitRevit {
|
||||
class App : IExternalApplication {
|
||||
public Result OnStartup(UIControlledApplication a) {
|
||||
a.ControlledApplication.ApplicationInitialized += ControlledApplication_ApplicationInitialized;
|
||||
|
||||
return Result.Succeeded;
|
||||
}
|
||||
|
||||
private void ControlledApplication_ApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e) {
|
||||
Application app = sender as Application;
|
||||
UIApplication uiapp = new UIApplication(app);
|
||||
|
||||
Runner.ReadConfig();
|
||||
|
||||
if (Runner.Config.autoStart)
|
||||
Runner.Launch(uiapp);
|
||||
}
|
||||
|
||||
public Result OnShutdown(UIControlledApplication a) {
|
||||
return Result.Succeeded;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
|
||||
namespace xUnitRevit
|
||||
{
|
||||
internal class CmdAvailabilityViews : IExternalCommandAvailability
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Command Availability - Views
|
||||
/// </summary>
|
||||
/// <param name="applicationData"></param>
|
||||
/// <param name="selectedCategories"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
|
||||
{
|
||||
//Can be run from any view/state
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
|
||||
namespace xUnitRevit {
|
||||
internal class CmdAvailabilityViews : IExternalCommandAvailability {
|
||||
|
||||
/// <summary>
|
||||
/// Command Availability - Views
|
||||
/// </summary>
|
||||
/// <param name="applicationData"></param>
|
||||
/// <param name="selectedCategories"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories) {
|
||||
//Can be run from any view/state
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
-31
@@ -1,31 +1,26 @@
|
||||
#region Namespaces
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
#endregion
|
||||
|
||||
namespace xUnitRevit
|
||||
{
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class Command : IExternalCommand
|
||||
{
|
||||
static object consoleLock = new object();
|
||||
static ManualResetEvent finished = new ManualResetEvent(false);
|
||||
static Result result = Result.Succeeded;
|
||||
|
||||
public Result Execute(
|
||||
ExternalCommandData commandData,
|
||||
ref string message,
|
||||
ElementSet elements)
|
||||
{
|
||||
UIApplication uiapp = commandData.Application;
|
||||
Runner.Launch(uiapp);
|
||||
return Result.Succeeded;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#region Namespaces
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using System.Threading;
|
||||
#endregion
|
||||
|
||||
namespace xUnitRevit {
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class Command : IExternalCommand {
|
||||
static object consoleLock = new object();
|
||||
static ManualResetEvent finished = new ManualResetEvent(false);
|
||||
static Result result = Result.Succeeded;
|
||||
|
||||
public Result Execute(
|
||||
ExternalCommandData commandData,
|
||||
ref string message,
|
||||
ElementSet elements) {
|
||||
UIApplication uiapp = commandData.Application;
|
||||
Runner.Launch(uiapp);
|
||||
return Result.Succeeded;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+11
-17
@@ -1,17 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace xUnitRevit
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple configuration file for lazy developers
|
||||
/// </summary>
|
||||
public class Configuration
|
||||
{
|
||||
public IList<string> StartupAssemblies { get; set; } = new List<string>();
|
||||
public bool autoStart { get; set; } = false;
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xUnitRevit {
|
||||
/// <summary>
|
||||
/// Simple configuration file for lazy developers
|
||||
/// </summary>
|
||||
public class Configuration {
|
||||
public IList<string> StartupAssemblies { get; set; } = new List<string>();
|
||||
public bool autoStart { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,38 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace xUnitRevit
|
||||
{
|
||||
/// <summary>
|
||||
/// Event invoker. Has a queue of actions that, in theory, this thing should iterate through.
|
||||
/// Required to run transactions form a non modal window.
|
||||
/// </summary>
|
||||
public class ExternalEventHandler : IExternalEventHandler
|
||||
{
|
||||
|
||||
public bool Running = false;
|
||||
public IList<Action> Queue { get; set; }
|
||||
|
||||
public ExternalEventHandler(IList<Action> queue)
|
||||
{
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
public void Execute(UIApplication app)
|
||||
{
|
||||
Debug.WriteLine("Current queue len is: " + Queue.Count);
|
||||
if (Running) return; // queue will run itself through
|
||||
|
||||
Running = true;
|
||||
|
||||
Queue[0]();
|
||||
|
||||
Queue.RemoveAt(0);
|
||||
Running = false;
|
||||
}
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return "xUnit Revit";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace xUnitRevit {
|
||||
/// <summary>
|
||||
/// Event invoker. Has a queue of actions that, in theory, this thing should iterate through.
|
||||
/// Required to run transactions form a non modal window.
|
||||
/// </summary>
|
||||
public class ExternalEventHandler : IExternalEventHandler {
|
||||
|
||||
public bool Running = false;
|
||||
public IList<Action> Queue { get; set; }
|
||||
|
||||
public ExternalEventHandler(IList<Action> queue) {
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
public void Execute(UIApplication app) {
|
||||
Debug.WriteLine("Current queue len is: " + Queue.Count);
|
||||
if (Running) return; // queue will run itself through
|
||||
|
||||
Running = true;
|
||||
|
||||
Queue[0]();
|
||||
|
||||
Queue.RemoveAt(0);
|
||||
Running = false;
|
||||
}
|
||||
|
||||
public string GetName() {
|
||||
return "xUnit Revit";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
|
||||
+51
-60
@@ -1,60 +1,51 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit.Runner.Wpf;
|
||||
using Xunit.Runner.Wpf.ViewModel;
|
||||
using xUnitRevitUtils;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.IO;
|
||||
|
||||
namespace xUnitRevit
|
||||
{
|
||||
/// <summary>
|
||||
/// Responsible for launching the xUnit WPF interface and initializing xru with Revit data
|
||||
/// </summary>
|
||||
public static class Runner
|
||||
{
|
||||
internal static Configuration Config = new Configuration();
|
||||
internal static void Launch(UIApplication uiapp)
|
||||
{
|
||||
try
|
||||
{
|
||||
var queue = new List<Action>();
|
||||
var eventHandler = ExternalEvent.Create(new ExternalEventHandler(queue));
|
||||
|
||||
xru.Initialize(uiapp, SynchronizationContext.Current, eventHandler, queue);
|
||||
|
||||
var main = new MainWindow();
|
||||
main.Title = "xUnit Revit by Speckle";
|
||||
main.MaxHeight = 800;
|
||||
|
||||
//pre-load asssemblies, if you're a lazy developer
|
||||
if (main.DataContext is MainViewModel mainViewModel)
|
||||
mainViewModel.StartupAssemblies = Config.StartupAssemblies.ToList();
|
||||
main.Show();
|
||||
}
|
||||
catch (Exception) {
|
||||
//fail silently
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ReadConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
var path = Path.Combine(dir, "config.json");
|
||||
JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer();
|
||||
var json = File.ReadAllText(path);
|
||||
Config = JavaScriptSerializer.Deserialize<Configuration>(json);
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
||||
}
|
||||
}
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Web.Script.Serialization;
|
||||
using Xunit.Runner.Wpf;
|
||||
using Xunit.Runner.Wpf.ViewModel;
|
||||
using xUnitRevitUtils;
|
||||
|
||||
namespace xUnitRevit {
|
||||
/// <summary>
|
||||
/// Responsible for launching the xUnit WPF interface and initializing xru with Revit data
|
||||
/// </summary>
|
||||
public static class Runner {
|
||||
internal static Configuration Config = new Configuration();
|
||||
internal static void Launch(UIApplication uiapp) {
|
||||
try {
|
||||
var queue = new List<Action>();
|
||||
var eventHandler = ExternalEvent.Create(new ExternalEventHandler(queue));
|
||||
|
||||
xru.Initialize(uiapp, SynchronizationContext.Current, eventHandler, queue);
|
||||
|
||||
var main = new MainWindow();
|
||||
main.Title = "xUnit Revit by Speckle";
|
||||
main.MaxHeight = 800;
|
||||
|
||||
//pre-load asssemblies, if you're a lazy developer
|
||||
if (main.DataContext is MainViewModel mainViewModel)
|
||||
mainViewModel.StartupAssemblies = Config.StartupAssemblies.ToList();
|
||||
main.Show();
|
||||
}
|
||||
catch (Exception) {
|
||||
//fail silently
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ReadConfig() {
|
||||
try {
|
||||
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
var path = Path.Combine(dir, "config.json");
|
||||
JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer();
|
||||
var json = File.ReadAllText(path);
|
||||
Config = JavaScriptSerializer.Deserialize<Configuration>(json);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+159
-188
@@ -1,188 +1,159 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace xUnitRevitUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility class with methods and properties used by the xUnit Revit plugin
|
||||
/// </summary>
|
||||
public static class xru
|
||||
{
|
||||
public static UIApplication Uiapp { get; set; }
|
||||
|
||||
private static IList<Action> Queue { get; set; }
|
||||
private static ExternalEvent EventHandler { get; set; }
|
||||
|
||||
public static SynchronizationContext UiContext { get; set; }
|
||||
|
||||
public static void Initialize(UIApplication uiapp, SynchronizationContext uiContext, ExternalEvent eventHandler, IList<Action> queue)
|
||||
{
|
||||
Uiapp = uiapp;
|
||||
UiContext = uiContext;
|
||||
EventHandler = eventHandler;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
#region utility methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected elements in the active document
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IList<Element> GetActiveSelection()
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
|
||||
if (Uiapp.ActiveUIDocument != null)
|
||||
return Uiapp.ActiveUIDocument.Selection.GetElementIds().Select(x => Uiapp.ActiveUIDocument.Document.GetElement(x)).ToList();
|
||||
return new List<Element>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Opens and activates a document if not open already
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the file to open</param>
|
||||
public static Document OpenDoc(string filePath)
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => { doc = Uiapp.OpenAndActivateDocument(filePath).Document; }, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new empty document
|
||||
/// </summary>
|
||||
/// <param name="templatePath">Path to the project template</param>
|
||||
/// <param name="filePath">Path where to save the new doc</param>
|
||||
/// <param name="overwrite">If true overwrites existing files with same name</param>
|
||||
/// <returns></returns>
|
||||
public static Document CreateNewDoc(string templatePath, string filePath, bool overwrite = true)
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (overwrite && File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x =>
|
||||
{
|
||||
//if already open, just use it
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
doc = Uiapp.Application.NewProjectDocument(templatePath);
|
||||
doc.SaveAs(filePath);
|
||||
doc.Close();
|
||||
}
|
||||
|
||||
doc = Uiapp.OpenAndActivateDocument(filePath).Document;
|
||||
}
|
||||
, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action in a Revit transaction, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <param name="transactionName">Transaction Name</param>
|
||||
/// <param name="ignoreWarnings">Enable to swallow all warnings generated by the transaction and prevent them from being raised within Revit</param>
|
||||
/// <returns></returns>
|
||||
public static Task RunInTransaction(Action action, Document doc, string transactionName = "transaction", bool ignoreWarnings = false)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Transaction transaction = new Transaction(doc, transactionName))
|
||||
{
|
||||
transaction.Start();
|
||||
|
||||
if (ignoreWarnings)
|
||||
{
|
||||
var options = transaction.GetFailureHandlingOptions();
|
||||
options.SetFailuresPreprocessor(new IgnoreAllWarnings());
|
||||
transaction.SetFailureHandlingOptions(options);
|
||||
}
|
||||
|
||||
action.Invoke();
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <returns></returns>
|
||||
public static Task Run(Action action, Document doc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A failures preprocesser that clears any failures that occur within a transaction
|
||||
/// </summary>
|
||||
internal class IgnoreAllWarnings : IFailuresPreprocessor
|
||||
{
|
||||
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
|
||||
{
|
||||
var failList = failuresAccessor.GetFailureMessages();
|
||||
|
||||
foreach (FailureMessageAccessor failure in failList)
|
||||
{
|
||||
failuresAccessor.DeleteWarning(failure);
|
||||
}
|
||||
|
||||
return FailureProcessingResult.Continue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Xunit;
|
||||
|
||||
namespace xUnitRevitUtils {
|
||||
/// <summary>
|
||||
/// Utility class with methods and properties used by the xUnit Revit plugin
|
||||
/// </summary>
|
||||
public static class xru {
|
||||
public static UIApplication Uiapp { get; set; }
|
||||
|
||||
private static IList<Action> Queue { get; set; }
|
||||
private static ExternalEvent EventHandler { get; set; }
|
||||
|
||||
public static SynchronizationContext UiContext { get; set; }
|
||||
|
||||
public static void Initialize(UIApplication uiapp, SynchronizationContext uiContext, ExternalEvent eventHandler, IList<Action> queue) {
|
||||
Uiapp = uiapp;
|
||||
UiContext = uiContext;
|
||||
EventHandler = eventHandler;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
#region utility methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected elements in the active document
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IList<Element> GetActiveSelection() {
|
||||
Assert.NotNull(Uiapp);
|
||||
|
||||
if (Uiapp.ActiveUIDocument != null)
|
||||
return Uiapp.ActiveUIDocument.Selection.GetElementIds().Select(x => Uiapp.ActiveUIDocument.Document.GetElement(x)).ToList();
|
||||
return new List<Element>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Opens and activates a document if not open already
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the file to open</param>
|
||||
public static Document OpenDoc(string filePath) {
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => { doc = Uiapp.OpenAndActivateDocument(filePath).Document; }, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new empty document
|
||||
/// </summary>
|
||||
/// <param name="templatePath">Path to the project template</param>
|
||||
/// <param name="filePath">Path where to save the new doc</param>
|
||||
/// <param name="overwrite">If true overwrites existing files with same name</param>
|
||||
/// <returns></returns>
|
||||
public static Document CreateNewDoc(string templatePath, string filePath, bool overwrite = true) {
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
if (overwrite && File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => {
|
||||
//if already open, just use it
|
||||
if (!File.Exists(filePath)) {
|
||||
doc = Uiapp.Application.NewProjectDocument(templatePath);
|
||||
doc.SaveAs(filePath);
|
||||
doc.Close();
|
||||
}
|
||||
|
||||
doc = Uiapp.OpenAndActivateDocument(filePath).Document;
|
||||
}
|
||||
, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action in a Revit transaction, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <param name="transactionName">Transaction Name</param>
|
||||
/// <param name="ignoreWarnings">Enable to swallow all warnings generated by the transaction and prevent them from being raised within Revit</param>
|
||||
/// <returns></returns>
|
||||
public static Task RunInTransaction(Action action, Document doc, string transactionName = "transaction", bool ignoreWarnings = false) {
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() => {
|
||||
try {
|
||||
using (Transaction transaction = new Transaction(doc, transactionName)) {
|
||||
transaction.Start();
|
||||
|
||||
if (ignoreWarnings) {
|
||||
var options = transaction.GetFailureHandlingOptions();
|
||||
options.SetFailuresPreprocessor(new IgnoreAllWarnings());
|
||||
transaction.SetFailureHandlingOptions(options);
|
||||
}
|
||||
|
||||
action.Invoke();
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <returns></returns>
|
||||
public static Task Run(Action action, Document doc) {
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() => {
|
||||
try {
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e) {
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A failures preprocesser that clears any failures that occur within a transaction
|
||||
/// </summary>
|
||||
internal class IgnoreAllWarnings : IFailuresPreprocessor {
|
||||
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor) {
|
||||
var failList = failuresAccessor.GetFailureMessages();
|
||||
|
||||
foreach (FailureMessageAccessor failure in failList) {
|
||||
failuresAccessor.DeleteWarning(failure);
|
||||
}
|
||||
|
||||
return FailureProcessingResult.Continue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
+155
-184
@@ -1,184 +1,155 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace xUnitRevitUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility class with methods and properties used by the xUnit Revit plugin
|
||||
/// </summary>
|
||||
public static class xru
|
||||
{
|
||||
public static UIApplication Uiapp { get; set; }
|
||||
private static IList<Action> Queue { get; set; }
|
||||
private static ExternalEvent EventHandler { get; set; }
|
||||
public static SynchronizationContext UiContext { get; set; }
|
||||
public static void Initialize(UIApplication uiapp, SynchronizationContext uiContext, ExternalEvent eventHandler, IList<Action> queue)
|
||||
{
|
||||
Uiapp = uiapp;
|
||||
UiContext = uiContext;
|
||||
EventHandler = eventHandler;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
#region utility methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected elements in the active document
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IList<Element> GetActiveSelection()
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
|
||||
if (Uiapp.ActiveUIDocument != null)
|
||||
return Uiapp.ActiveUIDocument.Selection.GetElementIds().Select(x => Uiapp.ActiveUIDocument.Document.GetElement(x)).ToList();
|
||||
return new List<Element>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Opens and activates a document if not open already
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the file to open</param>
|
||||
public static Document OpenDoc(string filePath)
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => { doc = Uiapp.OpenAndActivateDocument(filePath).Document; }, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new empty document
|
||||
/// </summary>
|
||||
/// <param name="templatePath">Path to the project template</param>
|
||||
/// <param name="filePath">Path where to save the new doc</param>
|
||||
/// <param name="overwrite">If true overwrites existing files with same name</param>
|
||||
/// <returns></returns>
|
||||
public static Document CreateNewDoc(string templatePath, string filePath, bool overwrite = true)
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (overwrite && File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x =>
|
||||
{
|
||||
//if already open, just use it
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
doc = Uiapp.Application.NewProjectDocument(templatePath);
|
||||
doc.SaveAs(filePath);
|
||||
doc.Close();
|
||||
}
|
||||
|
||||
doc = Uiapp.OpenAndActivateDocument(filePath).Document;
|
||||
}
|
||||
, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action in a Revit transaction, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <param name="transactionName">Transaction Name</param>
|
||||
/// <param name="ignoreWarnings">Enable to swallow all warnings generated by the transaction and prevent them from being raised within Revit</param>
|
||||
/// <returns></returns>
|
||||
public static Task RunInTransaction(Action action, Document doc, string transactionName = "transaction", bool ignoreWarnings = false)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Transaction transaction = new Transaction(doc, transactionName))
|
||||
{
|
||||
transaction.Start();
|
||||
|
||||
if (ignoreWarnings)
|
||||
{
|
||||
var options = transaction.GetFailureHandlingOptions();
|
||||
options.SetFailuresPreprocessor(new IgnoreAllWarnings());
|
||||
transaction.SetFailureHandlingOptions(options);
|
||||
}
|
||||
|
||||
action.Invoke();
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <returns></returns>
|
||||
public static Task Run(Action action, Document doc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A failures preprocesser that clears any failures that occur within a transaction
|
||||
/// </summary>
|
||||
internal class IgnoreAllWarnings : IFailuresPreprocessor
|
||||
{
|
||||
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
|
||||
{
|
||||
var failList = failuresAccessor.GetFailureMessages();
|
||||
|
||||
foreach (FailureMessageAccessor failure in failList)
|
||||
{
|
||||
failuresAccessor.DeleteWarning(failure);
|
||||
}
|
||||
|
||||
return FailureProcessingResult.Continue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Xunit;
|
||||
|
||||
namespace xUnitRevitUtils {
|
||||
/// <summary>
|
||||
/// Utility class with methods and properties used by the xUnit Revit plugin
|
||||
/// </summary>
|
||||
public static class xru {
|
||||
public static UIApplication Uiapp { get; set; }
|
||||
private static IList<Action> Queue { get; set; }
|
||||
private static ExternalEvent EventHandler { get; set; }
|
||||
public static SynchronizationContext UiContext { get; set; }
|
||||
public static void Initialize(UIApplication uiapp, SynchronizationContext uiContext, ExternalEvent eventHandler, IList<Action> queue) {
|
||||
Uiapp = uiapp;
|
||||
UiContext = uiContext;
|
||||
EventHandler = eventHandler;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
#region utility methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected elements in the active document
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IList<Element> GetActiveSelection() {
|
||||
Assert.NotNull(Uiapp);
|
||||
|
||||
if (Uiapp.ActiveUIDocument != null)
|
||||
return Uiapp.ActiveUIDocument.Selection.GetElementIds().Select(x => Uiapp.ActiveUIDocument.Document.GetElement(x)).ToList();
|
||||
return new List<Element>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Opens and activates a document if not open already
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the file to open</param>
|
||||
public static Document OpenDoc(string filePath) {
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => { doc = Uiapp.OpenAndActivateDocument(filePath).Document; }, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new empty document
|
||||
/// </summary>
|
||||
/// <param name="templatePath">Path to the project template</param>
|
||||
/// <param name="filePath">Path where to save the new doc</param>
|
||||
/// <param name="overwrite">If true overwrites existing files with same name</param>
|
||||
/// <returns></returns>
|
||||
public static Document CreateNewDoc(string templatePath, string filePath, bool overwrite = true) {
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
if (overwrite && File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => {
|
||||
//if already open, just use it
|
||||
if (!File.Exists(filePath)) {
|
||||
doc = Uiapp.Application.NewProjectDocument(templatePath);
|
||||
doc.SaveAs(filePath);
|
||||
doc.Close();
|
||||
}
|
||||
|
||||
doc = Uiapp.OpenAndActivateDocument(filePath).Document;
|
||||
}
|
||||
, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action in a Revit transaction, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <param name="transactionName">Transaction Name</param>
|
||||
/// <param name="ignoreWarnings">Enable to swallow all warnings generated by the transaction and prevent them from being raised within Revit</param>
|
||||
/// <returns></returns>
|
||||
public static Task RunInTransaction(Action action, Document doc, string transactionName = "transaction", bool ignoreWarnings = false) {
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() => {
|
||||
try {
|
||||
using (Transaction transaction = new Transaction(doc, transactionName)) {
|
||||
transaction.Start();
|
||||
|
||||
if (ignoreWarnings) {
|
||||
var options = transaction.GetFailureHandlingOptions();
|
||||
options.SetFailuresPreprocessor(new IgnoreAllWarnings());
|
||||
transaction.SetFailureHandlingOptions(options);
|
||||
}
|
||||
|
||||
action.Invoke();
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <returns></returns>
|
||||
public static Task Run(Action action, Document doc) {
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() => {
|
||||
try {
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e) {
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A failures preprocesser that clears any failures that occur within a transaction
|
||||
/// </summary>
|
||||
internal class IgnoreAllWarnings : IFailuresPreprocessor {
|
||||
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor) {
|
||||
var failList = failuresAccessor.GetFailureMessages();
|
||||
|
||||
foreach (FailureMessageAccessor failure in failList) {
|
||||
failuresAccessor.DeleteWarning(failure);
|
||||
}
|
||||
|
||||
return FailureProcessingResult.Continue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
+155
-184
@@ -1,184 +1,155 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace xUnitRevitUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility class with methods and properties used by the xUnit Revit plugin
|
||||
/// </summary>
|
||||
public static class xru
|
||||
{
|
||||
public static UIApplication Uiapp { get; set; }
|
||||
private static IList<Action> Queue { get; set; }
|
||||
private static ExternalEvent EventHandler { get; set; }
|
||||
public static SynchronizationContext UiContext { get; set; }
|
||||
public static void Initialize(UIApplication uiapp, SynchronizationContext uiContext, ExternalEvent eventHandler, IList<Action> queue)
|
||||
{
|
||||
Uiapp = uiapp;
|
||||
UiContext = uiContext;
|
||||
EventHandler = eventHandler;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
#region utility methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected elements in the active document
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IList<Element> GetActiveSelection()
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
|
||||
if (Uiapp.ActiveUIDocument != null)
|
||||
return Uiapp.ActiveUIDocument.Selection.GetElementIds().Select(x => Uiapp.ActiveUIDocument.Document.GetElement(x)).ToList();
|
||||
return new List<Element>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Opens and activates a document if not open already
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the file to open</param>
|
||||
public static Document OpenDoc(string filePath)
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => { doc = Uiapp.OpenAndActivateDocument(filePath).Document; }, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new empty document
|
||||
/// </summary>
|
||||
/// <param name="templatePath">Path to the project template</param>
|
||||
/// <param name="filePath">Path where to save the new doc</param>
|
||||
/// <param name="overwrite">If true overwrites existing files with same name</param>
|
||||
/// <returns></returns>
|
||||
public static Document CreateNewDoc(string templatePath, string filePath, bool overwrite = true)
|
||||
{
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (overwrite && File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x =>
|
||||
{
|
||||
//if already open, just use it
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
doc = Uiapp.Application.NewProjectDocument(templatePath);
|
||||
doc.SaveAs(filePath);
|
||||
doc.Close();
|
||||
}
|
||||
|
||||
doc = Uiapp.OpenAndActivateDocument(filePath).Document;
|
||||
}
|
||||
, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action in a Revit transaction, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <param name="transactionName">Transaction Name</param>
|
||||
/// <param name="ignoreWarnings">Enable to swallow all warnings generated by the transaction and prevent them from being raised within Revit</param>
|
||||
/// <returns></returns>
|
||||
public static Task RunInTransaction(Action action, Document doc, string transactionName = "transaction", bool ignoreWarnings = false)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Transaction transaction = new Transaction(doc, transactionName))
|
||||
{
|
||||
transaction.Start();
|
||||
|
||||
if (ignoreWarnings)
|
||||
{
|
||||
var options = transaction.GetFailureHandlingOptions();
|
||||
options.SetFailuresPreprocessor(new IgnoreAllWarnings());
|
||||
transaction.SetFailureHandlingOptions(options);
|
||||
}
|
||||
|
||||
action.Invoke();
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <returns></returns>
|
||||
public static Task Run(Action action, Document doc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A failures preprocesser that clears any failures that occur within a transaction
|
||||
/// </summary>
|
||||
internal class IgnoreAllWarnings : IFailuresPreprocessor
|
||||
{
|
||||
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
|
||||
{
|
||||
var failList = failuresAccessor.GetFailureMessages();
|
||||
|
||||
foreach (FailureMessageAccessor failure in failList)
|
||||
{
|
||||
failuresAccessor.DeleteWarning(failure);
|
||||
}
|
||||
|
||||
return FailureProcessingResult.Continue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Xunit;
|
||||
|
||||
namespace xUnitRevitUtils {
|
||||
/// <summary>
|
||||
/// Utility class with methods and properties used by the xUnit Revit plugin
|
||||
/// </summary>
|
||||
public static class xru {
|
||||
public static UIApplication Uiapp { get; set; }
|
||||
private static IList<Action> Queue { get; set; }
|
||||
private static ExternalEvent EventHandler { get; set; }
|
||||
public static SynchronizationContext UiContext { get; set; }
|
||||
public static void Initialize(UIApplication uiapp, SynchronizationContext uiContext, ExternalEvent eventHandler, IList<Action> queue) {
|
||||
Uiapp = uiapp;
|
||||
UiContext = uiContext;
|
||||
EventHandler = eventHandler;
|
||||
Queue = queue;
|
||||
}
|
||||
|
||||
#region utility methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns the selected elements in the active document
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IList<Element> GetActiveSelection() {
|
||||
Assert.NotNull(Uiapp);
|
||||
|
||||
if (Uiapp.ActiveUIDocument != null)
|
||||
return Uiapp.ActiveUIDocument.Selection.GetElementIds().Select(x => Uiapp.ActiveUIDocument.Document.GetElement(x)).ToList();
|
||||
return new List<Element>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Opens and activates a document if not open already
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the file to open</param>
|
||||
public static Document OpenDoc(string filePath) {
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => { doc = Uiapp.OpenAndActivateDocument(filePath).Document; }, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new empty document
|
||||
/// </summary>
|
||||
/// <param name="templatePath">Path to the project template</param>
|
||||
/// <param name="filePath">Path where to save the new doc</param>
|
||||
/// <param name="overwrite">If true overwrites existing files with same name</param>
|
||||
/// <returns></returns>
|
||||
public static Document CreateNewDoc(string templatePath, string filePath, bool overwrite = true) {
|
||||
Assert.NotNull(Uiapp);
|
||||
Document doc = null;
|
||||
|
||||
try {
|
||||
if (overwrite && File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//OpenAndActivateDocument only works if run from the current context
|
||||
UiContext.Send(x => {
|
||||
//if already open, just use it
|
||||
if (!File.Exists(filePath)) {
|
||||
doc = Uiapp.Application.NewProjectDocument(templatePath);
|
||||
doc.SaveAs(filePath);
|
||||
doc.Close();
|
||||
}
|
||||
|
||||
doc = Uiapp.OpenAndActivateDocument(filePath).Document;
|
||||
}
|
||||
, null);
|
||||
Assert.NotNull(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action in a Revit transaction, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <param name="transactionName">Transaction Name</param>
|
||||
/// <param name="ignoreWarnings">Enable to swallow all warnings generated by the transaction and prevent them from being raised within Revit</param>
|
||||
/// <returns></returns>
|
||||
public static Task RunInTransaction(Action action, Document doc, string transactionName = "transaction", bool ignoreWarnings = false) {
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() => {
|
||||
try {
|
||||
using (Transaction transaction = new Transaction(doc, transactionName)) {
|
||||
transaction.Start();
|
||||
|
||||
if (ignoreWarnings) {
|
||||
var options = transaction.GetFailureHandlingOptions();
|
||||
options.SetFailuresPreprocessor(new IgnoreAllWarnings());
|
||||
transaction.SetFailureHandlingOptions(options);
|
||||
}
|
||||
|
||||
action.Invoke();
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs an Action, uses TaskCompletionSource to communicate when done
|
||||
/// </summary>
|
||||
/// <param name="action">Action to run</param>
|
||||
/// <param name="doc">Revit Document</param>
|
||||
/// <returns></returns>
|
||||
public static Task Run(Action action, Document doc) {
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
Queue.Add(new Action(() => {
|
||||
try {
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception e) {
|
||||
tcs.TrySetException(e);
|
||||
}
|
||||
tcs.TrySetResult("");
|
||||
}));
|
||||
|
||||
EventHandler.Raise();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A failures preprocesser that clears any failures that occur within a transaction
|
||||
/// </summary>
|
||||
internal class IgnoreAllWarnings : IFailuresPreprocessor {
|
||||
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor) {
|
||||
var failList = failuresAccessor.GetFailureMessages();
|
||||
|
||||
foreach (FailureMessageAccessor failure in failList) {
|
||||
failuresAccessor.DeleteWarning(failure);
|
||||
}
|
||||
|
||||
return FailureProcessingResult.Continue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user