// // (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 System.Text; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.ApplicationServices; using System.Drawing; using Autodesk.Revit; using System.Collections.ObjectModel; namespace Revit.SDK.Samples.Openings.CS { /// /// This class contain the data about the Opening (get from Revit) /// Such as BoundingBox, Profile Curve... /// public class OpeningInfo { private UIApplication m_revit; //Application of Revit private List m_lines = new List(); //contains lines in curve private Opening m_opening; //Opening got from Revit //OpeningProperty class which can use in PropertyGrid control private OpeningProperty m_property; private WireFrame m_sketch; //Profile information of opening private BoundingBox m_boundingBox; //BoundingBox of Opening //property /// /// Property to get and set Application of Revit /// public UIApplication Revit { get { return m_revit; } set { if (value != m_revit) m_revit = value; } } /// /// Property to get Opening store in OpeningInfo /// public Opening Opening { get { return m_opening; } } /// /// Property to get Name and Id /// eg: "Opening Cut (114389)" /// public string NameAndId { get { return String.Concat(m_opening.Name, " (", m_opening.Id.IntegerValue.ToString(), ")"); } } /// /// Property to get bool the define whether opening is Shaft Opening /// public bool IsShaft { get { if (null != m_opening.Category) { if ("Shaft Openings" == m_opening.Category.Name) return true; else { return false; } } else { return false; } } } /// /// Property to get OpeningProperty class /// which can use in PropertyGrid control /// public OpeningProperty Property { get { return m_property; } } /// /// Property to get Profile information of opening /// public WireFrame Sketch { get { return m_sketch; } } /// /// Property to get BoundingBox of Opening /// public BoundingBox BoundingBox { get { return m_boundingBox; } } /// /// The default constructor, /// get the information we want from Opening /// get OpeningProperty, BoundingBox and Profile /// /// an opening in revit /// application object public OpeningInfo(Opening opening, UIApplication app) { m_opening = opening; m_revit = app; //get OpeningProperty which can use in PropertyGrid control OpeningProperty openingProperty = new OpeningProperty(m_opening); m_property = openingProperty; //get BoundingBox of Opening BoundingBoxXYZ boxXYZ = m_opening.get_BoundingBox(m_revit.ActiveUIDocument.Document.ActiveView); BoundingBox boundingBox = new BoundingBox(boxXYZ); m_boundingBox = boundingBox; //get profile GetProfile(); } /// /// get Profile of Opening /// private void GetProfile() { CurveArray curveArray = m_opening.BoundaryCurves; if (null != curveArray) { m_lines.Clear(); foreach (Curve curve in curveArray) { List points = curve.Tessellate() as List; AddLine(points); } WireFrame wireFrameSketch = new WireFrame(new ReadOnlyCollection(m_lines)); m_sketch = wireFrameSketch; } else if (m_opening.IsRectBoundary) { //if opening profile is RectBoundary, //just can get profile info from BoundaryRect Property m_lines.Clear(); List boundRect = m_opening.BoundaryRect as List; List RectPoints = GetPoints(boundRect); AddLine(RectPoints); WireFrame wireFrameSketch = new WireFrame(new ReadOnlyCollection(m_lines)); m_sketch = wireFrameSketch; } else { m_sketch = null; } } /// /// get four corner points of a rectangular in same plane /// /// an array contain two Autodesk.Revit.DB.XYZ struct store the max and min /// coordinate of rectangular private List GetPoints(List boundRect) { List points = new List(); Autodesk.Revit.DB.XYZ p1 = boundRect[0]; points.Add(p1); Autodesk.Revit.DB.XYZ p2 = new Autodesk.Revit.DB.XYZ( boundRect[0].X, boundRect[0].Y, boundRect[1].Z); points.Add(p2); Autodesk.Revit.DB.XYZ p3 = boundRect[1]; points.Add(p3); Autodesk.Revit.DB.XYZ p4 = new Autodesk.Revit.DB.XYZ ( boundRect[1].X, boundRect[1].Y, boundRect[0].Z); points.Add(p4); //make rectangle close Autodesk.Revit.DB.XYZ p5 = boundRect[0]; points.Add(p5); return points; } /// /// get line from List(points) and add line to m_lines list /// /// a List contain points of the Curve private void AddLine(List points) { if (null == points || 0 == points.Count) { return; } Autodesk.Revit.DB.XYZ previousPoint; previousPoint = points[0]; for (int i = 1; i < points.Count; i++) { Autodesk.Revit.DB.XYZ point; point = points[i]; Line3D line = new Line3D(); Vector pointStart = new Vector(); Vector pointEnd = new Vector(); for (int j = 0; j < 3; j++) { pointStart[j] = previousPoint[j]; pointEnd[j] = point[j]; } line.StartPoint = pointStart; line.EndPoint = pointEnd; m_lines.Add(line); previousPoint = point; } } } }