// // (C) Copyright 2003-2009 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.Elements; using Autodesk.Revit.Geometry; 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 Application 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 Application 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.Value.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, Application 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.ActiveDocument.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) { XYZArray points = curve.Tessellate(); 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(); XYZArray boundRect = m_opening.BoundaryRect; XYZArray 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 XYZ struct store the max and min /// coordinate of rectangular private XYZArray GetPoints(XYZArray boundRect) { XYZArray points = new XYZArray(); XYZ p1 = boundRect.get_Item(0); points.Append(p1); XYZ p2 = new XYZ(); p2.X = boundRect.get_Item(0).X; p2.Y = boundRect.get_Item(0).Y; p2.Z = boundRect.get_Item(1).Z; points.Append(p2); XYZ p3 = boundRect.get_Item(1); points.Append(p3); XYZ p4 = new XYZ(); p4.X = boundRect.get_Item(1).X; p4.Y = boundRect.get_Item(1).Y; p4.Z = boundRect.get_Item(0).Z; points.Append(p4); //make rectangle close XYZ p5 = boundRect.get_Item(0); points.Append(p5); return points; } /// /// get line from XYZArray(points) and add line to m_lines list /// /// a XYZArray contain points of the Curve private void AddLine(XYZArray points) { if (null == points || 0 == points.Size) { return; } Autodesk.Revit.Geometry.XYZ previousPoint; previousPoint = points.get_Item(0); for (int i = 1; i < points.Size; i++) { Autodesk.Revit.Geometry.XYZ point; point = points.get_Item(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; } } } }