Get intersections between Walls and Ducts

Get intersections between Walls and Ducts

Anonymous
Not applicable
6,257 Views
13 Replies
Message 1 of 14

Get intersections between Walls and Ducts

Anonymous
Not applicable

Hi everyone,

I'm trying to get a List<BoundingBoxXYZ> or a List<XYZ> of every intersections between Walls and Ducts in the current Document. 

So far i'm only getting the two List<ElementsId> (walls and Ducts). 
Here is a sample : 

public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
    {
        try
        {
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
            View view = doc.ActiveView;


            FilteredElementCollector allWalls = new FilteredElementCollector(doc).OfClass(typeof(Wall));
            FilteredElementCollector allDucts = new FilteredElementCollector(doc).OfClass(typeof(Duct));

            List<ElementId> WallsBB = new List<ElementId>();
            List<ElementId> DuctsBB = new List<ElementId>();

            foreach (Element wall in allWalls)
            {
                //BoundingBoxXYZ bbWall = wall.get_BoundingBox(view);
                WallsBB.Add(wall.Id);
            }

            foreach (Element duct in allDucts)
            {
                DuctsBB.Add(duct.Id);
            }
//Here is some function i tried to use, with no result so far.
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            ExclusionFilter exclusionFilter = new ExclusionFilter(WallsBB);
            
            collector.WherePasses(exclusionFilter);

        }
        catch(Exception e)
        {
            TaskDialog.Show("e = ", e.ToString());
        }

        return Result.Succeeded;
    }

Thank you !

0 Likes
Accepted solutions (2)
6,258 Views
13 Replies
Replies (13)
Message 2 of 14

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

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


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 3 of 14

thannaingoo.api
Advocate
Advocate

I got red underline in FindWallCurve.

How should be a correction?

 

 

   Curve wallCurve = FindWallCurve(w, height);

Thanks,

Naing Oo

Message 4 of 14

tamsann
Enthusiast
Enthusiast

Do you find the solution? I get the same mistake😥

Message 5 of 14

Houba3314
Enthusiast
Enthusiast

findwallcurve is missing can you please add it

0 Likes
Message 6 of 14

Houba3314
Enthusiast
Enthusiast

Did you find any solution?

0 Likes
Message 7 of 14

Omar_Amen
Advocate
Advocate

I can notice that after getting the wall location curve at a specific height, Eng. @Mustafa.Salaheldin  didn't use it at all in this sample, 
so I think you can just remove this line

Curve wallCurve = FindWallCurve(w, height);

and the code will work fine

0 Likes
Message 8 of 14

Houba3314
Enthusiast
Enthusiast

yea i noticed that too thanks. hey quick question is it possible that i can insert an element at that intersection?

Message 9 of 14

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

@Houba3314 sorry for late reply, as @Omar_Amen said, this line I forgot to remove as this solution was part of bigger one. The idea was to get the intersection between the WallFace (not WallCurve) and the DuctCurve so that way you get set of intersection points.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 10 of 14

Mustafa.Salaheldin
Collaborator
Collaborator
My advice to you is always try to understand the logic of the procedure, if any part of the code doesn't make sense in terms of the logic context or it is not used some where else in the module, then this means you can remove it safely without disrupting the flow.

¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 11 of 14

tamsann
Enthusiast
Enthusiast

Hello, Could you give some suggestions to get intersection between wall curve and duct curve, I want to insert a fire damper at the intersection point. Thank you!!!!!

Message 12 of 14

Houba3314
Enthusiast
Enthusiast

hello again i was wondering for the intersection between floor and ducts is it the same code? .

0 Likes
Message 13 of 14

csvFC98Y
Participant
Participant

About two years ago, when i just start Revit API, i saw your code.. get it and use some parts. Now i start read "Clear Code" by RobertMatrin and  remebered your code. Understanding wondering of it. 😉

 

0 Likes
Message 14 of 14

Mustafa.Salaheldin
Collaborator
Collaborator

@csvFC98Y Thanks a lot. I feel flattered.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes