Message 1 of 9
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am getting very very slow in performance. I feel I made a mistake. How to optimize the code to active the performance.
#region Namespaces
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
#endregion
namespace Auto_Join_V3
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public static int transactionCount = 0;
public static bool accept;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
// Application app = uiapp.Application;
Document doc = uidoc.Document;
accept = false;
Autojoin_form form = new Autojoin_form();
form.ShowDialog();
if (accept)
{
SelectElementsToJoin(uidoc, doc);
}
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.ToString();
return Result.Failed;
}
}
static void SelectElementsToJoin(UIDocument uidoc, Document doc)
{
// FilteredElementCollector - First Beam Element
if (Autojoin_form.CurrentElementSelection == ElementSelection.Beam_Wall)
{
var collFirstBeamElement = GetElementCollect(doc).OfClass(typeof(FamilyInstance))
.OfCategory(BuiltInCategory.OST_StructuralFraming);
// Join With Beam
foreach (var beam in collFirstBeamElement)
{
BoundingBoxXYZ beamBoundingBox = beam.get_BoundingBox(doc.ActiveView);
Outline beamOutline = new Outline(beamBoundingBox.Min, beamBoundingBox.Max);
BoundingBoxIntersectsFilter beamBBFilter = new BoundingBoxIntersectsFilter(beamOutline);
// Wall Selection and Join With Beams
if (Autojoin_form.CurrentElementSelection == ElementSelection.Beam_Floor_Wall ||
Autojoin_form.CurrentElementSelection == ElementSelection.Beam_Wall)
{
FilteredElementCollector collWalls = GetElementCollect(doc).OfClass(typeof(Wall));
collWalls.WherePasses(beamBBFilter);
//CollWalls.WherePasses(new ElementIntersectsElementFilter(beam));
if (collWalls.GetElementCount() > 0)
{
Elementjoin(uidoc, doc, beam, collWalls, "Beam + Wall");
}
}
}
}
}
static void Elementjoin(UIDocument uidoc, Document doc,
Element FirstElem,
FilteredElementCollector Coll_of_Elems,
string transactionText)
{
if (transactionText == null || transactionText == "")
{
transactionText = "AutoJoin V3";
}
foreach (Element elem in Coll_of_Elems)
{
if (JoinGeometryUtils.AreElementsJoined(doc, FirstElem, elem))
{
if (JoinGeometryUtils.IsCuttingElementInJoin(doc, elem, FirstElem))
{
JoinGeometryUtils.SwitchJoinOrder(doc, FirstElem, elem);
}
}
else
{
transactionCount++;
transactionText = transactionCount + ". " + transactionText;
using (Transaction t = new Transaction(doc, transactionText))
{
t.Start();
FailureHandlingOptions options = t.GetFailureHandlingOptions();
WarningDiscard preproccessor = new WarningDiscard();
options.SetFailuresPreprocessor(preproccessor);
options.SetClearAfterRollback(true);
t.SetFailureHandlingOptions(options);
try
{
bool check = JoinGeometryUtils.AreElementsJoined(doc, FirstElem, elem);
if (check == false)
{
JoinGeometryUtils.JoinGeometry(doc, FirstElem, elem);
}
if (JoinGeometryUtils.IsCuttingElementInJoin(doc, elem, FirstElem))
{
JoinGeometryUtils.SwitchJoinOrder(doc, elem, FirstElem);
}
}
catch { }
t.Commit();
}
}
}
}
static FilteredElementCollector GetElementCollect(Document doc)
{
if (Autojoin_form.CurrentViewSeclection == ViewSelection.Entire_Project)
{
return new FilteredElementCollector(doc);
}
else
{
return new FilteredElementCollector(doc, doc.ActiveView.Id);
}
}
}
public class WarningDiscard : IFailuresPreprocessor
{
FailureProcessingResult IFailuresPreprocessor.PreprocessFailures(FailuresAccessor failuresAccessor)
{
String transactionName = failuresAccessor.GetTransactionName();
IList<FailureMessageAccessor> fmas = failuresAccessor.GetFailureMessages();
if (fmas.Count == 0)
{
return FailureProcessingResult.Continue;
}
bool HasErrors = false;
foreach (FailureMessageAccessor fma in fmas)
{
if (fma.GetFailureDefinitionId() == BuiltInFailures.ColumnInsideWallFailures.ColumnInsideWall
|| fma.GetFailureDefinitionId() == BuiltInFailures.JoinElementsFailures.JoiningDisjointWarn
)
{
if (fma.HasResolutions())
{
failuresAccessor.DeleteWarning(fma);
HasErrors = true;
}
}
}
if (HasErrors)
{
return FailureProcessingResult.ProceedWithRollBack;
}
return FailureProcessingResult.Continue;
}
}
}
regards,
Sudhan.
Solved! Go to Solution.
Developer Advocacy and Support +