// // (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; using System.Collections.Generic; using System.IO; using System.Reflection; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Application = Autodesk.Revit.ApplicationServices.Application; using Element = Autodesk.Revit.DB.Element; namespace Revit.SDK.Samples.ManipulateForm.CS { /// /// Implements the Revit add-in interface IExternalCommand /// [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)] [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)] public class Command : IExternalCommand { /// /// Revit document /// Document m_revitDoc; /// /// Revit application /// Application m_revitApp; /// /// Rectangle length of bottom profile /// double m_bottomLength = 200; /// /// Rectangle width of bottom profile /// double m_bottomWidth = 120; /// /// Height of bottom profile /// double m_bottomHeight = 0; /// /// Rectangle length of top profile /// double m_topLength = 140; /// /// Rectangle width of top profile /// double m_topWidth = 60; /// /// Height of top profile /// double m_topHeight = 40; /// /// offset of profile /// double m_profileOffset = 10; /// /// offset of vertex on bottom profile /// double m_vertexOffsetOnBottomProfile = 20; /// /// offset of vertex on middle profile /// double m_vertexOffsetOnMiddleProfile = 10; /// /// Used for double compare /// const double Epsilon = 0.000001; /// /// Implement this method as an external command for Revit. /// /// An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view. /// A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command. /// A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation. /// Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation. public virtual Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData , ref string message, Autodesk.Revit.DB.ElementSet elements) { m_revitApp = commandData.Application.Application; m_revitDoc = commandData.Application.ActiveUIDocument.Document; Transaction transaction = new Transaction(m_revitDoc, "ManipulateForm"); try { transaction.Start(); // Create a loft form Form form = CreateLoft(); m_revitDoc.Regenerate(); // Add profile to the loft form int profileIndex = AddProfile(form); m_revitDoc.Regenerate(); // Move the edges on added profile MoveEdgesOnProfile(form, profileIndex); m_revitDoc.Regenerate(); // Move the added profile MoveProfile(form, profileIndex); m_revitDoc.Regenerate(); // Move the vertex on bottom profile MoveVertexesOnBottomProfile(form); m_revitDoc.Regenerate(); // Add edge to the loft form Reference edgeReference = AddEdge(form); m_revitDoc.Regenerate(); // Move the added edge Autodesk.Revit.DB.XYZ offset = new Autodesk.Revit.DB.XYZ(0, -40, 0); MoveSubElement(form, edgeReference, offset); m_revitDoc.Regenerate(); // Move the vertex on added profile MoveVertexesOnAddedProfile(form, profileIndex); m_revitDoc.Regenerate(); transaction.Commit(); } catch (Exception ex) { message = ex.Message; transaction.RollBack(); return Autodesk.Revit.UI.Result.Failed; } return Autodesk.Revit.UI.Result.Succeeded; } /// /// Create a loft form /// /// Created loft form private Form CreateLoft() { // Prepare profiles for loft creation ReferenceArrayArray profiles = new ReferenceArrayArray(); ReferenceArray bottomProfile = new ReferenceArray(); bottomProfile = CreateProfile(m_bottomLength, m_bottomWidth, m_bottomHeight); profiles.Append(bottomProfile); ReferenceArray topProfile = new ReferenceArray(); topProfile = CreateProfile(m_topLength, m_topWidth, m_topHeight); profiles.Append(topProfile); // return the created loft form return m_revitDoc.FamilyCreate.NewLoftForm(true, profiles); } /// /// Create a rectangle profile with provided length, width and height /// /// Length of the rectangle /// Width of the rectangle /// Height of the profile /// The created profile private ReferenceArray CreateProfile(double length, double width, double height) { ReferenceArray profile = new ReferenceArray(); // Prepare points to create lines List points = new List(); points.Add(new Autodesk.Revit.DB.XYZ(-1 * length / 2, -1 * width / 2, height)); points.Add(new Autodesk.Revit.DB.XYZ(length / 2, -1 * width / 2, height)); points.Add(new Autodesk.Revit.DB.XYZ(length / 2, width / 2, height)); points.Add(new Autodesk.Revit.DB.XYZ(-1 * length / 2, width / 2, height)); // Prepare sketch plane to create model line Autodesk.Revit.DB.XYZ normal = new Autodesk.Revit.DB.XYZ(0, 0, 1); Autodesk.Revit.DB.XYZ origin = new Autodesk.Revit.DB.XYZ(0, 0, height); Plane geometryPlane = Plane.CreateByNormalAndOrigin(normal, origin); SketchPlane sketchPlane = SketchPlane.Create(m_revitDoc, geometryPlane); // Create model lines and get their references as the profile for (int i = 0; i < 4; i++) { Autodesk.Revit.DB.XYZ startPoint = points[i]; Autodesk.Revit.DB.XYZ endPoint = (i == 3 ? points[0] : points[i + 1]); Line line = Line.CreateBound(startPoint, endPoint); ModelCurve modelLine = m_revitDoc.FamilyCreate.NewModelCurve(line, sketchPlane); profile.Append(modelLine.GeometryCurve.Reference); } return profile; } /// /// Add profile to the loft form /// /// The loft form to be added edge /// Index of the added profile private int AddProfile(Form form) { // Get a connecting edge from the form Autodesk.Revit.DB.XYZ startOfTop = new Autodesk.Revit.DB.XYZ(-1 * m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); Autodesk.Revit.DB.XYZ startOfBottom = new Autodesk.Revit.DB.XYZ(-1 * m_bottomLength / 2, -1 * m_bottomWidth / 2, m_bottomHeight); Edge connectingEdge = GetEdgeByEndPoints(form, startOfTop, startOfBottom); // Add an profile with specific parameters double param = 0.5; return form.AddProfile(connectingEdge.Reference, param); } /// /// Move the profile /// /// The form contains the edge /// Index of the profile to be moved private void MoveProfile(Form form, int profileIndex) { Autodesk.Revit.DB.XYZ offset = new Autodesk.Revit.DB.XYZ(0, 0, 5); if (form.CanManipulateProfile(profileIndex)) { form.MoveProfile(profileIndex, offset); } } /// /// Move the edges on profile /// /// The form contains the edge /// Index of the profile to be moved private void MoveEdgesOnProfile(Form form, int profileIndex) { Autodesk.Revit.DB.XYZ startOfTop = new Autodesk.Revit.DB.XYZ(-1 * m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); Autodesk.Revit.DB.XYZ offset1 = new Autodesk.Revit.DB.XYZ(m_profileOffset, 0, 0); Autodesk.Revit.DB.XYZ offset2 = new Autodesk.Revit.DB.XYZ(-m_profileOffset, 0, 0); Reference r1 = null; Reference r2 = null; ReferenceArray ra = form.get_CurveLoopReferencesOnProfile(profileIndex, 0); foreach (Reference r in ra) { Line line = form.GetGeometryObjectFromReference(r) as Line; if (line == null) { throw new Exception("Get curve reference on profile as line error."); } Autodesk.Revit.DB.XYZ pnt1 = line.Evaluate(0, false); Autodesk.Revit.DB.XYZ pnt2 = line.Evaluate(1, false); if (Math.Abs(pnt1.X - pnt2.X) < Epsilon) { if (pnt1.X < startOfTop.X) { r1 = r; } else { r2 = r; } } } if ((r1 == null) || (r2 == null)) { throw new Exception("Get line on profile error."); } MoveSubElement(form, r1, offset1); MoveSubElement(form, r2, offset2); } /// /// Move the form vertexes /// /// The form contains the vertexes private void MoveVertexesOnBottomProfile(Form form) { Autodesk.Revit.DB.XYZ offset1 = new Autodesk.Revit.DB.XYZ(-m_vertexOffsetOnBottomProfile, -m_vertexOffsetOnBottomProfile, 0); Autodesk.Revit.DB.XYZ offset2 = new Autodesk.Revit.DB.XYZ(m_vertexOffsetOnBottomProfile, -m_vertexOffsetOnBottomProfile, 0); Autodesk.Revit.DB.XYZ startOfBottom = new Autodesk.Revit.DB.XYZ(-1 * m_bottomLength / 2, -1 * m_bottomWidth / 2, m_bottomHeight); Autodesk.Revit.DB.XYZ endOfBottom = new Autodesk.Revit.DB.XYZ(m_bottomLength / 2, -1 * m_bottomWidth / 2, m_bottomHeight); Edge bottomEdge = GetEdgeByEndPoints(form, startOfBottom, endOfBottom); ReferenceArray pntsRef = form.GetControlPoints(bottomEdge.Reference); Reference r1 = null; Reference r2 = null; foreach (Reference r in pntsRef) { Point pnt = form.GetGeometryObjectFromReference(r) as Point; if (pnt.Coord.IsAlmostEqualTo(startOfBottom)) { r1 = r; } else { r2 = r; } } MoveSubElement(form, r1, offset1); MoveSubElement(form, r2, offset2); } /// /// Move the form vertexes on added profile /// /// The form contains the vertexes /// Index of added profile private void MoveVertexesOnAddedProfile(Form form, int profileIndex) { Autodesk.Revit.DB.XYZ offset = new Autodesk.Revit.DB.XYZ(0, m_vertexOffsetOnMiddleProfile, 0); ReferenceArray ra = form.get_CurveLoopReferencesOnProfile(profileIndex, 0); foreach (Reference r in ra) { ReferenceArray ra2 = form.GetControlPoints(r); foreach (Reference r2 in ra2) { Point vertex = form.GetGeometryObjectFromReference(r2) as Point; if (Math.Abs(vertex.Coord.X) < Epsilon) { MoveSubElement(form, r2, offset); break; } } } } /// /// Add edge to the loft form /// /// The loft form to be added edge /// Reference of the added edge private Reference AddEdge(Form form) { // Get two specific edges from the form Autodesk.Revit.DB.XYZ startOfTop = new Autodesk.Revit.DB.XYZ(-1 * m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); Autodesk.Revit.DB.XYZ endOfTop = new Autodesk.Revit.DB.XYZ(m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); Edge topEdge = GetEdgeByEndPoints(form, startOfTop, endOfTop); Autodesk.Revit.DB.XYZ startOfBottom = new Autodesk.Revit.DB.XYZ(-1 * (m_bottomLength / 2 + m_vertexOffsetOnBottomProfile), -1 * (m_bottomWidth / 2 + m_vertexOffsetOnBottomProfile), m_bottomHeight); Autodesk.Revit.DB.XYZ endOfBottom = new Autodesk.Revit.DB.XYZ((m_bottomLength / 2 + m_vertexOffsetOnBottomProfile), -1 * (m_bottomWidth / 2 + m_vertexOffsetOnBottomProfile), m_bottomHeight); Edge bottomEdge = GetEdgeByEndPoints(form, startOfBottom, endOfBottom); // Add an edge between the two edges with specific parameters double topParam = 0.5; double bottomParam = 0.5; form.AddEdge(topEdge.Reference, topParam, bottomEdge.Reference, bottomParam); m_revitDoc.Regenerate(); // Get the added edge and return its reference Autodesk.Revit.DB.XYZ startOfAddedEdge = startOfTop.Add(endOfTop.Subtract(startOfTop).Multiply(topParam)); Autodesk.Revit.DB.XYZ endOfAddedEdge = startOfBottom.Add(endOfBottom.Subtract(startOfBottom).Multiply(bottomParam)); return GetEdgeByEndPoints(form, startOfAddedEdge, endOfAddedEdge).Reference; } /// /// Get an edge from the form by its endpoints /// /// The form contains the edge /// Start point of the edge /// End point of the edge /// The edge found private Edge GetEdgeByEndPoints(Form form, Autodesk.Revit.DB.XYZ startPoint, Autodesk.Revit.DB.XYZ endPoint) { Edge edge = null; // Get all edges of the form EdgeArray edges = null; Options geoOptions = m_revitApp.Create.NewGeometryOptions(); geoOptions.ComputeReferences = true; Autodesk.Revit.DB.GeometryElement geoElement = form.get_Geometry(geoOptions); //foreach (GeometryObject geoObject in geoElement.Objects) IEnumerator Objects = geoElement.GetEnumerator(); while (Objects.MoveNext()) { GeometryObject geoObject = Objects.Current; Solid solid = geoObject as Solid; if (null == solid) continue; edges = solid.Edges; } // Traverse the edges and look for the edge with the right endpoints foreach (Edge ed in edges) { Autodesk.Revit.DB.XYZ rpPos1 = ed.Evaluate(0); Autodesk.Revit.DB.XYZ rpPos2 = ed.Evaluate(1); if ((startPoint.IsAlmostEqualTo(rpPos1) && endPoint.IsAlmostEqualTo(rpPos2)) || (startPoint.IsAlmostEqualTo(rpPos2) && endPoint.IsAlmostEqualTo(rpPos1))) { edge = ed; break; } } return edge; } /// /// Move the sub element /// /// The form contains the sub element /// Reference of the sub element to be moved /// offset to be moved private void MoveSubElement(Form form, Reference subElemReference, Autodesk.Revit.DB.XYZ offset) { if (form.CanManipulateSubElement(subElemReference)) { form.MoveSubElement(subElemReference, offset); } } } }