- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear all,
I have created an application macro to load a particular family on all grid intersection - but I keep getting errors. Any help would be very much appreciated.
MACRO:
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace DDA_CSharpModule9
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("E08C2870-0CEF-46D9-BEAE-3ED8994591F0")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void LoadFamilyOnGridIntersections()
{
UIApplication uiApp = new UIApplication(this.Application);
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document doc = uiDoc.Document;
string familyPath = "C:\\Users\\dda\\XXXXXXXXXX\\Documents\\01. Revit Families\\Speciality\\TestIVCoordCube.rfa";
Family family = null;
try
{
// Load family into document if not already loaded
using (Transaction loadFamilyTransaction = new Transaction(doc, "Load Family"))
{
loadFamilyTransaction.Start();
if (!doc.LoadFamily(familyPath, out family))
{
TaskDialog.Show("Error", String.Format("Failed to load family: {0}", familyPath));
return;
}
loadFamilyTransaction.Commit();
}
if (family == null)
{
TaskDialog.Show("Error", "Family could not be loaded.");
return;
}
// Collect all gridline intersections
FilteredElementCollector gridCollector = new FilteredElementCollector(doc).OfClass(typeof(Grid));
List<Grid> grids = gridCollector.Cast<Grid>().ToList();
List<XYZ> intersections = new List<XYZ>();
for (int i = 0; i < grids.Count; i++)
{
for (int j = i + 1; j < grids.Count; j++)
{
Curve curve1 = grids[i].Curve;
Curve curve2 = grids[j].Curve;
if (curve1 != null && curve2 != null)
{
SetComparisonResult result = curve1.Intersect(curve2, out IntersectionResultArray intersectionResults);
if (result == SetComparisonResult.Overlap && intersectionResults != null)
{
foreach (IntersectionResult intersection in intersectionResults)
{
intersections.Add(intersection.XYZPoint);
}
}
}
}
}
// Place family instances at grid intersections
using (Transaction placeFamilyTransaction = new Transaction(doc, "Place Family Instances"))
{
placeFamilyTransaction.Start();
FamilySymbol familySymbol = doc.GetElement(family.GetFamilySymbolIds().First()) as FamilySymbol;
if (familySymbol != null && !familySymbol.IsActive)
{
familySymbol.Activate();
doc.Regenerate();
}
foreach (XYZ point in intersections)
{
doc.Create.NewFamilyInstance(point, familySymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
}
placeFamilyTransaction.Commit();
}
TaskDialog.Show("Success", String.Format("Family '{0}' loaded and placed at all grid intersections.", family.Name));
}
catch (Exception ex)
{
TaskDialog.Show("Error", String.Format("An error occurred: {0}", ex.Message));
}
}
}
}
Solved! Go to Solution.