// // (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; using Autodesk.Revit.ApplicationServices; namespace Revit.SDK.Samples.Openings.CS { /// /// This class which inherit from Autodesk.Revit.DB.BoundingBoxXYZ /// store the information about Max (Min) coordinate of object /// can get all the corner point coordinate and create X model line /// public class BoundingBox : BoundingBoxXYZ { /// /// define whether we have created Model Line on this BoundingBox /// private bool m_isCreated; /// /// store all the corner points in BoundingBox /// private readonly List m_points = new List(); /// /// property to get all the points /// public List Points { get { return m_points; } } /// /// property to get width of BoundingBox (short side) /// public double Width { get { double yDistance = 0; double xDistance = 0; yDistance = m_points[2].Y - m_points[1].Y; xDistance = m_points[5].X - m_points[2].X; return xDistance < yDistance ? xDistance : yDistance; } } /// /// property to get Length of BoundingBox (long side) /// public double Length { get { double yDistance = 0; double xDistance = 0; yDistance = m_points[2].Y - m_points[1].Y; xDistance = m_points[5].X - m_points[2].X; return xDistance > yDistance ? xDistance : yDistance; } } /// /// The default constructor /// /// The reference of the application in revit public BoundingBox(BoundingBoxXYZ boundBoxXYZ) { this.Min = boundBoxXYZ.Min; this.Max = boundBoxXYZ.Max; GetCorners(); } /// /// Create X model line with the BoundBox /// Create 12 lines to makeup an cube /// /// Application get from RevitAPI public void CreateLines(UIApplication app) { if (m_isCreated) { return; } //create 12 lines for (int i = 0; i < 7; i++) { NewModelLine(app, i, i + 1); } for (int i = 0; i < 5; i = i + 2) { NewModelLine(app, i, i + 3); } NewModelLine(app, 1, 6); NewModelLine(app, 0, 7); m_isCreated = true; } /// /// get all the Corner points of Cube Box via Min and Max /// private void GetCorners() { m_points.Add(this.Min); Autodesk.Revit.DB.XYZ point = new Autodesk.Revit.DB.XYZ( this.Min.X, this.Min.Y, this.Max.Z); m_points.Add(point); Autodesk.Revit.DB.XYZ point2 = new Autodesk.Revit.DB.XYZ( this.Min.X, this.Max.Y, this.Max.Z); m_points.Add(point2); Autodesk.Revit.DB.XYZ point3 = new Autodesk.Revit.DB.XYZ( this.Min.X, this.Max.Y, this.Min.Z); m_points.Add(point3); Autodesk.Revit.DB.XYZ point4 = new Autodesk.Revit.DB.XYZ( this.Max.X, this.Max.Y, this.Min.Z); m_points.Add(point4); m_points.Add(this.Max); Autodesk.Revit.DB.XYZ point5 = new Autodesk.Revit.DB.XYZ( this.Max.X, this.Min.Y, this.Max.Z); m_points.Add(point5); Autodesk.Revit.DB.XYZ point6 = new Autodesk.Revit.DB.XYZ( this.Max.X, this.Min.Y, this.Min.Z); m_points.Add(point6); } /// /// Create a Sketch Plane which pass the defined line /// the defined line must be one of BoundingBox Profile /// /// Application get from RevitAPI /// a line which sketch plane pass private SketchPlane NewSketchPlanePassLine(Line aline, UIApplication app) { //in a cube only Autodesk.Revit.DB.XYZ norm; if (aline.GetEndPoint(0).X == aline.GetEndPoint(1).X) { norm = new Autodesk.Revit.DB.XYZ(1, 0, 0); } else if (aline.GetEndPoint(0).Y == aline.GetEndPoint(1).Y) { norm = new Autodesk.Revit.DB.XYZ(0, 1, 0); } else { norm = new Autodesk.Revit.DB.XYZ(0, 0, 1); } Autodesk.Revit.DB.XYZ point = aline.GetEndPoint(0); Plane plane = Plane.CreateByNormalAndOrigin(norm, point); SketchPlane sketchPlane = SketchPlane.Create(app.ActiveUIDocument.Document, plane); return sketchPlane; } /// /// new ModelLine in BoundingBox /// /// Application get from RevitAPI /// index of point in BoundingBox acme /// index of another point in BoundingBox acme private void NewModelLine(UIApplication app, int pointIndex1, int pointIndex2) { Autodesk.Revit.DB.XYZ startP2 = m_points[pointIndex1]; Autodesk.Revit.DB.XYZ endP2 = m_points[pointIndex2]; try { Line line = Line.CreateBound(startP2, endP2); SketchPlane sketchPlane = NewSketchPlanePassLine(line, app); Line line2 = Line.CreateBound(startP2, endP2); app.ActiveUIDocument.Document.Create.NewModelCurve(line2, sketchPlane); } catch (Exception) { return; } } } }