Here you are, please don't forget to mark this reply as an answer
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
#endregion
namespace RevitAddinCS2
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class ExtCmd : IExternalCommand
{
#region Cached Variables
private static ExternalCommandData _cachedCmdData;
public static UIApplication CachedUiApp
{
get
{
return _cachedCmdData.Application;
}
}
public static RvtApplication CachedApp
{
get
{
return CachedUiApp.Application;
}
}
public static RvtDocument CachedDoc
{
get
{
return CachedUiApp.ActiveUIDocument.Document;
}
}
#endregion
#region IExternalCommand Members
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
//TODO: add your code below.
// Find intersections between family instances and a selected element
FilteredElementCollector WallCollector = new FilteredElementCollector(CachedDoc);
WallCollector.OfClass(typeof(Wall));
List<Wall> walls = WallCollector.Cast<Wall>().ToList();
FilteredElementCollector DuctCollector = new FilteredElementCollector(CachedDoc);
DuctCollector.OfClass(typeof(Duct));
List<Duct> ducts = DuctCollector.Cast<Duct>().ToList();
List<XYZ> points = new List<XYZ>();
foreach (Duct d in ducts)
{
foreach (Wall w in walls)
{
Curve ductCurve = FindDuctCurve(d);
double height = ductCurve.GetEndPoint(0).Z;
Curve wallCurve = FindWallCurve(w, height);
XYZ intersection = null;
List<Face> wallFaces = FindWallFace(w);
foreach (Face f in wallFaces)
{
intersection = FindFaceCurve(ductCurve, f);
if (null != intersection)
points.Add(intersection);
}
}
}
StringBuilder sb = new StringBuilder();
foreach (XYZ p in points)
{
sb.AppendLine(p.ToString());
}
TaskDialog.Show("Revit", sb.ToString());
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
//Find the wind pipe corresponding curve
public Curve FindDuctCurve(Duct duct)
{
//The wind pipe curve
IList<XYZ> list = new List<XYZ>();
ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();
while (csi.MoveNext())
{
Connector conn = csi.Current as Connector;
list.Add(conn.Origin);
}
Curve curve = Line.CreateBound(list.ElementAt(0), list.ElementAt(1)) as Curve;
curve.MakeUnbound();
return curve;
}
public List<Face> FindWallFace(Wall wall)
{
List<Face> normalFaces = new List<Face>();
Options opt = new Options();
opt.ComputeReferences = true;
opt.DetailLevel = ViewDetailLevel.Fine;
GeometryElement e = wall.get_Geometry(opt);
foreach (GeometryObject obj in e)
{
Solid solid = obj as Solid;
if (solid != null && solid.Faces.Size > 0)
{
foreach (Face face in solid.Faces)
{
PlanarFace pf = face as PlanarFace;
if (pf != null)
{
normalFaces.Add(pf);
}
}
}
}
return normalFaces;
}
public XYZ FindFaceCurve(Curve DuctCurve, Face WallFace)
{
//The intersection point
IntersectionResultArray intersectionR = new IntersectionResultArray();//Intersection point set
SetComparisonResult results;//Results of Comparison
results = WallFace.Intersect(DuctCurve, out intersectionR);
XYZ intersectionResult = null;//Intersection coordinate
if (SetComparisonResult.Disjoint != results)
{
if (intersectionR != null)
{
if (!intersectionR.IsEmpty)
{
intersectionResult = intersectionR.get_Item(0).XYZPoint;
}
}
}
return intersectionResult;
}
#endregion
}
}
¯\_(ツ)_/¯
Let it work like a charm.
Mustafa Salaheldin


Digital Integration Manager, DuPod
Facebook |
Twitter |
LinkedIn