// // (C) Copyright 2003-2019 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 Autodesk.Revit.Creation; using Autodesk.Revit.DB; using GeometryElement = Autodesk.Revit.DB.GeometryElement; using AppCreation = Autodesk.Revit.Creation.Application; namespace Revit.SDK.Samples.AutoJoin.CS { /// /// Tell if two geometry objects are overlapping or not. /// class Intersection { /// /// Tell if the geometry object A and B are overlapped. /// /// geometry object A /// geometry object B /// return true if A and B are overlapped, or else return false. public static bool IsOverlapped(GeometryObject geometryA, GeometryObject geometryB) { List facesOfA = new List(); List curvesOfB = new List(); GetAllFaces(geometryA, facesOfA); GetAllCurves(geometryB, curvesOfB); foreach (Face face in facesOfA) { foreach (Curve curve in curvesOfB) { if (face.Intersect(curve) == Autodesk.Revit.DB.SetComparisonResult.Overlap) { return true; } } } return false; } /// /// Get all faces of the geometry object and insert them to the list /// /// the geometry object /// the face list private static void GetAllFaces(GeometryObject geometry, List faces) { if (geometry is GeometryElement) { GetAllFaces(geometry as GeometryElement, faces); return; } if (geometry is Solid) { GetAllFaces(geometry as Solid, faces); return; } } private static void GetAllFaces(GeometryElement geoElement, List faces) { //foreach (GeometryObject geObject in geoElement.Objects) IEnumerator Objects = geoElement.GetEnumerator(); while (Objects.MoveNext()) { GeometryObject geObject = Objects.Current; GetAllFaces(geObject, faces); } } private static void GetAllFaces(Solid solid, List faces) { foreach (Face face in solid.Faces) { faces.Add(face); } } private static void GetAllCurves(GeometryObject geometry, List curves) { if (geometry is GeometryElement) { GetAllCurves(geometry as GeometryElement, curves); return; } if (geometry is Solid) { GetAllCurves(geometry as Solid, curves); return; } } private static void GetAllCurves(GeometryElement geoElement, List curves) { //foreach (GeometryObject geObject in geoElement.Objects) IEnumerator Objects = geoElement.GetEnumerator(); while (Objects.MoveNext()) { GeometryObject geObject = Objects.Current; GetAllCurves(geObject, curves); } } private static void GetAllCurves(Solid solid, List curves) { foreach (Face face in solid.Faces) { GetAllCurves(face, curves); } } private static void GetAllCurves(Face face, List curves) { foreach (EdgeArray loop in face.EdgeLoops) { foreach (Edge edge in loop) { List points = edge.Tessellate() as List; for (int ii = 0; ii + 1 < points.Count; ii++) { Line line = Line.CreateBound(points[ii], points[ii + 1]); curves.Add(line); } } } } } }