Files
RevitSdkSamples/SDK/Macro Samples/MacroSamples_MEP/Source/MacroSamples_MEP/Detector.cs
T
2024-04-11 13:47:20 +02:00

162 lines
5.9 KiB
C#

//
// (C) Copyright 2003-2008 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Element = Autodesk.Revit.DB.Element;
namespace CS_AvoidObstruction
{
/// <summary>
/// This class is used to detect the obstructions of a Line or a ray.
/// </summary>
class Detector
{
/// <summary>
/// Revit Document.
/// </summary>
private Document m_rvtDoc;
/// <summary>
/// Revit 3D view.
/// </summary>
private View3D? m_view3d;
/// <summary>
/// Constructor, initialize all the fields.
/// </summary>
/// <param name="rvtDoc">Revit Document</param>
public Detector(Document rvtDoc)
{
m_rvtDoc = rvtDoc;
ElementArray views = new ElementArray();
ElementFilter view3DElementFilter = new ElementClassFilter(typeof(View3D));
FilteredElementCollector collector = new FilteredElementCollector(m_rvtDoc);
collector.WherePasses(view3DElementFilter);
m_view3d = collector.ToElements()[0] as View3D;
}
/// <summary>
/// Return all the obstructions which intersect with a ray given by an origin and a direction.
/// </summary>
/// <param name="origin">Ray's origin</param>
/// <param name="dir">Ray's direction</param>
/// <returns>Obstructions intersected with the given ray</returns>
public List<ReferenceWithContext> Obstructions(XYZ origin, XYZ dir)
{
List<ReferenceWithContext> result = new List<ReferenceWithContext>();
ReferenceIntersector referenceIntersector = new ReferenceIntersector(m_view3d);
referenceIntersector.TargetType = FindReferenceTarget.Face;
var obstructionsOnUnboundLine = referenceIntersector.Find(origin, dir);
foreach (ReferenceWithContext gRef in obstructionsOnUnboundLine)
{
if (!InArray(result, gRef))
{
result.Add(gRef);
}
}
result.Sort(CompareReferences);
return result;
}
/// <summary>
/// Return all the obstructions which intersect with a bound line.
/// </summary>
/// <param name="boundLine">Bound line</param>
/// <returns>Obstructions intersected with the bound line</returns>
public List<ReferenceWithContext> Obstructions(Line boundLine)
{
List<ReferenceWithContext> result = new List<ReferenceWithContext>();
XYZ startPt = boundLine.GetEndPoint(0);
XYZ endPt = boundLine.GetEndPoint(1);
XYZ dir = (endPt.Subtract(startPt)).Normalize();
ReferenceIntersector referenceIntersector = new ReferenceIntersector(m_view3d);
referenceIntersector.TargetType = FindReferenceTarget.Face;
var obstructionsOnUnboundLine = referenceIntersector.Find(startPt, dir);
foreach (ReferenceWithContext gRef in obstructionsOnUnboundLine)
{
Reference refr = gRef.GetReference();
// Judge whether the point is in the bound line or not, if the distance between the point and line
// is Zero, then the point is in the bound line.
if (boundLine.Distance(refr.GlobalPoint) < 1e-9)
{
if (!InArray(result, gRef))
{
result.Add(gRef);
}
}
}
result.Sort(CompareReferences);
return result;
}
/// <summary>
/// Judge whether a given Reference is in a ReferenceWithContext list.
/// Give two ReferenceWithContexts, if their Proximitys and Element Ids are equal,
/// we say the two ReferenceWithContexts are equal.
/// </summary>
/// <param name="arr">ReferenceWithContext Array</param>
/// <param name="entry">ReferenceWithContext</param>
/// <returns>True of false</returns>
private bool InArray(List<ReferenceWithContext> arr, ReferenceWithContext entry)
{
foreach (ReferenceWithContext tmp in arr)
{
if (tmp.Proximity == entry.Proximity &&
tmp.GetReference().ElementId.Value == entry.GetReference().ElementId.Value)
{
return true;
}
}
return false;
}
/// <summary>
/// Used to compare two ReferenceWithContexts, just compare their Proximitys.
/// </summary>
/// <param name="a">First ReferenceWithContext to compare</param>
/// <param name="b">Second ReferenceWithContext to compare</param>
/// <returns>-1, 0, or 1</returns>
private int CompareReferences(ReferenceWithContext a, ReferenceWithContext b)
{
if (a.Proximity > b.Proximity)
{
return 1;
}
if (a.Proximity < b.Proximity)
{
return -1;
}
return 0;
}
}
}