// // (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 System.Collections; using System.Windows.Forms; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Application = Autodesk.Revit.ApplicationServices.Application; namespace Revit.SDK.Samples.GridCreation.CS { /// /// Base class of all grid creation data class /// public class CreateGridsData { #region Fields /// /// The active document of Revit /// protected Autodesk.Revit.DB.Document m_revitDoc; /// /// Document Creation object to create new elements /// protected Autodesk.Revit.Creation.Document m_docCreator; /// /// Application Creation object to create new elements /// protected Autodesk.Revit.Creation.Application m_appCreator; /// /// Array list contains all grid labels in current document /// private ArrayList m_labelsList; /// /// Current display unit type /// protected ForgeTypeId m_unit; /// /// Resource manager /// protected static System.Resources.ResourceManager resManager = Properties.Resources.ResourceManager; #endregion #region Properties /// /// Current display unit type /// public ForgeTypeId Unit { get { return m_unit; } } /// /// Get array list contains all grid labels in current document /// public ArrayList LabelsList { get { return m_labelsList; } } #endregion #region Methods /// /// Constructor without display unit type /// /// Revit application /// All existing labels in Revit's document public CreateGridsData(UIApplication application, ArrayList labels) { m_revitDoc = application.ActiveUIDocument.Document; m_appCreator = application.Application.Create; m_docCreator = application.ActiveUIDocument.Document.Create; m_labelsList = labels; } /// /// Constructor with display unit type /// /// Revit application /// All existing labels in Revit's document /// Current length display unit type public CreateGridsData(UIApplication application, ArrayList labels, ForgeTypeId unit) { m_revitDoc = application.ActiveUIDocument.Document; m_appCreator = application.Application.Create; m_docCreator = application.ActiveUIDocument.Document.Create; m_labelsList = labels; m_unit = unit; } /// /// Get the line to create grid according to the specified bubble location /// /// The original selected line /// bubble location /// The line to create grid protected Line TransformLine(Line line, BubbleLocation bubLoc) { Line lineToCreate; // Create grid according to the bubble location if (bubLoc == BubbleLocation.StartPoint) { lineToCreate = line; } else { Autodesk.Revit.DB.XYZ startPoint = line.GetEndPoint(1); Autodesk.Revit.DB.XYZ endPoint = line.GetEndPoint(0); lineToCreate = NewLine(startPoint, endPoint); } return lineToCreate; } /// /// Get the arc to create grid according to the specified bubble location /// /// The original selected line /// bubble location /// The arc to create grid protected Arc TransformArc(Arc arc, BubbleLocation bubLoc) { Arc arcToCreate; if (bubLoc == BubbleLocation.StartPoint) { arcToCreate = arc; } else { // Get start point, end point of the arc and the middle point on it Autodesk.Revit.DB.XYZ startPoint = arc.GetEndPoint(0); Autodesk.Revit.DB.XYZ endPoint = arc.GetEndPoint(1); bool clockwise = (arc.Normal.Z == -1); // Get start angel and end angel of arc double startDegree = arc.GetEndParameter(0); double endDegree = arc.GetEndParameter(1); // Handle the case that the arc is clockwise if (clockwise && startDegree > 0 && endDegree > 0) { startDegree = 2 * Values.PI - startDegree; endDegree = 2 * Values.PI - endDegree; } else if (clockwise && startDegree < 0) { double temp = endDegree; endDegree = -1 * startDegree; startDegree = -1 * temp; } double sumDegree = (startDegree + endDegree) / 2; while (sumDegree > 2 * Values.PI) { sumDegree -= 2 * Values.PI; } while (sumDegree < -2 * Values.PI) { sumDegree += 2 * Values.PI; } Autodesk.Revit.DB.XYZ midPoint = new Autodesk.Revit.DB.XYZ(arc.Center.X + arc.Radius * Math.Cos(sumDegree), arc.Center.Y + arc.Radius * Math.Sin(sumDegree), 0); arcToCreate = Arc.Create(endPoint, startPoint, midPoint); } return arcToCreate; } /// /// Get the arc to create grid according to the specified bubble location /// /// Arc grid's origin /// Arc grid's radius /// Arc grid's start degree /// Arc grid's end degree /// Arc grid's Bubble location /// The expected arc to create grid protected Arc TransformArc(Autodesk.Revit.DB.XYZ origin, double radius, double startDegree, double endDegree, BubbleLocation bubLoc) { Arc arcToCreate; // Get start point and end point of the arc and the middle point on the arc Autodesk.Revit.DB.XYZ startPoint = new Autodesk.Revit.DB.XYZ(origin.X + radius * Math.Cos(startDegree), origin.Y + radius * Math.Sin(startDegree), origin.Z); Autodesk.Revit.DB.XYZ midPoint = new Autodesk.Revit.DB.XYZ(origin.X + radius * Math.Cos((startDegree + endDegree) / 2), origin.Y + radius * Math.Sin((startDegree + endDegree) / 2), origin.Z); Autodesk.Revit.DB.XYZ endPoint = new Autodesk.Revit.DB.XYZ(origin.X + radius * Math.Cos(endDegree), origin.Y + radius * Math.Sin(endDegree), origin.Z); if (bubLoc == BubbleLocation.StartPoint) { arcToCreate = Arc.Create(startPoint, endPoint, midPoint); } else { arcToCreate = Arc.Create(endPoint, startPoint, midPoint); } return arcToCreate; } /// /// Split a circle into the upper and lower parts /// /// Arc to be split /// Upper arc of the circle /// Lower arc of the circle /// bubble location protected void TransformCircle(Arc arc, ref Arc upperArc, ref Arc lowerArc, BubbleLocation bubLoc) { Autodesk.Revit.DB.XYZ center = arc.Center; double radius = arc.Radius; Autodesk.Revit.DB.XYZ XRightPoint = new Autodesk.Revit.DB.XYZ(center.X + radius, center.Y, 0); Autodesk.Revit.DB.XYZ XLeftPoint = new Autodesk.Revit.DB.XYZ(center.X - radius, center.Y, 0); Autodesk.Revit.DB.XYZ YUpperPoint = new Autodesk.Revit.DB.XYZ(center.X, center.Y + radius, 0); Autodesk.Revit.DB.XYZ YLowerPoint = new Autodesk.Revit.DB.XYZ(center.X, center.Y - radius, 0); if (bubLoc == BubbleLocation.StartPoint) { upperArc = Arc.Create(XRightPoint, XLeftPoint, YUpperPoint); lowerArc = Arc.Create(XLeftPoint, XRightPoint, YLowerPoint); } else { upperArc = Arc.Create(XLeftPoint, XRightPoint, YUpperPoint); lowerArc = Arc.Create(XRightPoint, XLeftPoint, YLowerPoint); } } /// /// Create a new bound line /// /// start point of line /// end point of line /// protected Line NewLine(Autodesk.Revit.DB.XYZ start, Autodesk.Revit.DB.XYZ end) { return Line.CreateBound(start, end); } /// /// Create a grid with a line /// /// Line to create grid /// Newly created grid protected Grid NewGrid(Line line) { return Grid.Create(m_revitDoc, line); } /// /// Create a grid with an arc /// /// Arc to create grid /// Newly created grid protected Grid NewGrid(Arc arc) { return Grid.Create(m_revitDoc, arc); } /// /// Create linear grid /// /// The linear curve to be transferred to grid /// The newly created grid /// protected Grid CreateLinearGrid(Line line) { return Grid.Create(m_revitDoc, line); } /// /// Create batch of grids with curves /// /// Curves used to create grids protected void CreateGrids(CurveArray curves) { foreach (Curve c in curves) { Line line = c as Line; Arc arc = c as Arc; if (line != null) { Grid.Create(m_revitDoc, line); } if (arc != null) { Grid.Create(m_revitDoc, arc); } } } /// /// Add curve to curve array for batch creation /// /// curve array stores all curves for batch creation /// curve to be added public static void AddCurveForBatchCreation(ref CurveArray curves, Curve curve) { curves.Append(curve); } /// /// Show a message box /// /// Message /// title of message box public static void ShowMessage(String message, String caption) { TaskDialog.Show(caption, message, TaskDialogCommonButtons.Ok); } #endregion } }