// // (C) Copyright 2003-2016 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.Linq; using System.Xml.Linq; using System.Text; using Autodesk.Revit.DB; using Autodesk.Revit.UI; namespace Revit.SDK.Samples.ProximityDetection_WallJoinControl.CS { /// /// The class that is responsible for controlling the joint of walls /// public class WallJoinControl { /// /// The singleton instance of WallJoinControl /// private static WallJoinControl Instance; /// /// revit application /// private Autodesk.Revit.ApplicationServices.Application m_app; /// /// revit document /// private Autodesk.Revit.DB.Document m_doc; /// /// Constructor /// /// Revit application /// Revit document private WallJoinControl( Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc) { m_app = app; m_doc = doc; } /// /// Get the singleton instance of WallJoinControl /// /// Revit application /// Revit document /// The singleton instance of WallJoinControl public static WallJoinControl getInstance( Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc) { if (Instance == null) { Instance = new WallJoinControl(app, doc); } return Instance; } /// /// Check every wall's joined walls by WallUtils method in initial, after disallow join, then after allow join states /// /// The walls to be checked /// The check result public XElement checkJoinedWalls(IEnumerable walls) { // create a node that place all walls. XElement wallsNode = new XElement("Walls", new XAttribute("Name", "Walls")); try { foreach (Wall wall in walls) { // create a wall node XElement wallNode = new XElement("Wall", new XAttribute("name", wall.Name), new XAttribute("Type", wall.WallType.Kind.ToString())); LocationCurve locationCurve = wall.Location as LocationCurve; if (null == locationCurve) { wallNode.Add(new XElement("Error", new XAttribute("Exception", "This wall has not a LocationCurve!"))); } else { // start XElement endNode = new XElement("Start", new XAttribute("Name", "Start")); wallNode.Add(checkWallEnd(wall, locationCurve, 0, endNode)); // end endNode = new XElement("End", new XAttribute("Name", "End")); wallNode.Add(checkWallEnd(wall, locationCurve, 1, endNode)); } wallsNode.Add(wallNode); } } catch (Exception ex) { wallsNode.Add(new XElement("Error", new XAttribute("Exception", ex.ToString()))); } // return the whole walls Node return wallsNode; } /// /// Check wall's two ends in three states /// /// The wall to be checked /// The wall's location curve /// The index indicates the start or end of this wall /// Result XML node /// The check result private XElement checkWallEnd(Wall wall, LocationCurve locationCurve, int end, XElement endnode) { // Initial state XElement stateNode = new XElement("Initial", new XAttribute("Name", "Initial")); endnode.Add(GetState(wall, locationCurve, end, stateNode)); // Disallow join WallUtils.DisallowWallJoinAtEnd(wall, end); // After DisallowWallJoinAtEnd Evoked state stateNode = new XElement("After_DisallowWallJoinAtEnd_Evoked", new XAttribute("Name", "After_DisallowWallJoinAtEnd_Evoked")); endnode.Add(GetState(wall, locationCurve, end, stateNode)); // Allow join WallUtils.AllowWallJoinAtEnd(wall, end); // After AllowWallJoinAtEnd Evoked state stateNode = new XElement("After_DisallowWallJoinAtEnd_Evoked", new XAttribute("Name", "After_DisallowWallJoinAtEnd_Evoked")); endnode.Add(GetState(wall, locationCurve, end, stateNode)); return endnode; } /// /// Get wall's one end state /// /// The wall to be checked /// The wall's location curve /// The index indicates the start or end of this wall /// Result XML node /// The check result private XElement GetState(Wall wall, LocationCurve locationCurve, int end, XElement statenode) { // Get geometry information statenode.Add(GetGeometryInfo(locationCurve, end)); // Get IsWallJoinAllowedAtEnd API method's result statenode.Add(GetIsWallJoinAllowedAtEnd(wall, end)); // Get all joined walls statenode.Add(GetJoinedWalls(locationCurve, end)); return statenode; } /// /// Get wall's one end geometry information /// /// The wall's location curve /// The index indicates the start or end of this wall /// The check result private XElement GetGeometryInfo(LocationCurve locationCurve, int end) { XElement geometryinfoNode = new XElement("GeometryInfo"); // Output LocationCurve end point coordinate XYZ endpoint = locationCurve.Curve.GetEndPoint(end); geometryinfoNode.Add(new XElement("PointCoordinate", new XAttribute("X", Math.Round(endpoint.X, 9).ToString()), new XAttribute("Y", Math.Round(endpoint.Y, 9).ToString()), new XAttribute("Z", Math.Round(endpoint.Z, 9).ToString()))); return geometryinfoNode; } /// /// Get wall's one end IsWallJoinAllowedAtEnd API method's result /// /// The wall to be checked /// The index indicates the start or end of this wall /// The check result private XElement GetIsWallJoinAllowedAtEnd(Wall wall, int end) { return new XElement("IsWallJoinAllowedAtEnd", new XAttribute("Value", WallUtils.IsWallJoinAllowedAtEnd(wall, end))); } /// /// Get wall's one end all joined walls /// /// The wall's location curve /// The index indicates the start or end of this wall /// The check result private XElement GetJoinedWalls(LocationCurve locationCurve, int end) { // retrieve joined elements ElementArray array = locationCurve.get_ElementsAtJoin(end); XElement joinedwallsNode = new XElement("JoinedWalls", new XAttribute("Count", array.Size.ToString())); // output array foreach (Element ele in array) { if (ele is Wall) { joinedwallsNode.Add(new XElement("JoinedWall", new XAttribute("Name", ele.Name))); } } return joinedwallsNode; } } }