// // (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; using System.Collections.Generic; using System.IO; using System.Reflection; using Autodesk.Revit; using Autodesk.Revit.Elements; using Autodesk.Revit.Geometry; using Application = Autodesk.Revit.Application; using Element = Autodesk.Revit.Element; namespace Revit.SDK.Samples.ManipulateForm.CS { /// /// Implements the Revit add-in interface IExternalCommand /// 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 IExternalCommand.Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { m_revitApp = commandData.Application; m_revitDoc = m_revitApp.ActiveDocument; // Create a loft form Form form = CreateLoft(); // Add profile to the loft form int profileIndex = AddProfile(form); string AssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); m_revitDoc.SaveAs(Path.Combine(AssemblyDirectory, "ManipulateForm.rfa")); // Move the edges on added profile MoveEdgesOnProfile(form, profileIndex); m_revitDoc.Save(); // Move the added profile MoveProfile(form, profileIndex); m_revitDoc.Save(); // Move the vertex on bottom profile MoveVertexesOnBottomProfile(form); m_revitDoc.Save(); // Add edge to the loft form Reference edgeReference = AddEdge(form); // Move the added edge XYZ offset = new XYZ(0, -40, 0); MoveSubElement(form, edgeReference, offset); m_revitDoc.Save(); // Move the vertex on added profile MoveVertexesOnAddedProfile(form, profileIndex); m_revitDoc.Save(); } catch (Exception ex) { message = ex.Message; return IExternalCommand.Result.Failed; } return IExternalCommand.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 XYZArray points = new XYZArray(); points.Append(new XYZ(-1 * length / 2, -1 * width / 2, height)); points.Append(new XYZ(length / 2, -1 * width / 2, height)); points.Append(new XYZ(length / 2, width / 2, height)); points.Append(new XYZ(-1 * length / 2, width / 2, height)); // Prepare sketch plane to create model line XYZ normal = new XYZ(0, 0, 1); XYZ origin = new XYZ(0, 0, height); Plane geometryPlane = m_revitApp.Create.NewPlane(normal, origin); SketchPlane sketchPlane = m_revitDoc.FamilyCreate.NewSketchPlane(geometryPlane); // Create model lines and get their references as the profile for (int i = 0; i < 4; i++) { XYZ startPoint = points.get_Item(i); XYZ endPoint = (i == 3 ? points.get_Item(0) : points.get_Item(i + 1)); Line line = m_revitApp.Create.NewLineBound(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 XYZ startOfTop = new XYZ(-1 * m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); XYZ startOfBottom = new 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) { XYZ offset = new 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) { XYZ startOfTop = new XYZ(-1 * m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); XYZ offset1 = new XYZ(m_profileOffset, 0, 0); XYZ offset2 = new 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 = r.GeometryObject as Line; if (line == null) { throw new Exception("Get curve reference on profile as line error."); } XYZ pnt1 = line.Evaluate(0, false); 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) { XYZ offset1 = new XYZ(-m_vertexOffsetOnBottomProfile, -m_vertexOffsetOnBottomProfile, 0); XYZ offset2 = new XYZ(m_vertexOffsetOnBottomProfile, -m_vertexOffsetOnBottomProfile, 0); XYZ startOfBottom = new XYZ(-1 * m_bottomLength / 2, -1 * m_bottomWidth / 2, m_bottomHeight); XYZ endOfBottom = new 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 = r.GeometryObject as Point; if (pnt.Coord.AlmostEqual(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) { XYZ offset = new 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 = r2.GeometryObject 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 XYZ startOfTop = new XYZ(-1 * m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); XYZ endOfTop = new XYZ(m_topLength / 2, -1 * m_topWidth / 2, m_topHeight); Edge topEdge = GetEdgeByEndPoints(form, startOfTop, endOfTop); XYZ startOfBottom = new XYZ(-1 * (m_bottomLength / 2 + m_vertexOffsetOnBottomProfile), -1 * (m_bottomWidth / 2 + m_vertexOffsetOnBottomProfile), m_bottomHeight); XYZ endOfBottom = new 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); // Get the added edge and return its reference XYZ startOfAddedEdge = startOfTop.Add(endOfTop.Subtract(startOfTop).Multiply(topParam)); 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, XYZ startPoint, 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.Geometry.Element geoElement = form.get_Geometry(geoOptions); foreach (GeometryObject geoObject in geoElement.Objects) { 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) { XYZ rpPos1 = ed.Evaluate(0); XYZ rpPos2 = ed.Evaluate(1); if ((startPoint.AlmostEqual(rpPos1) && endPoint.AlmostEqual(rpPos2)) || (startPoint.AlmostEqual(rpPos2) && endPoint.AlmostEqual(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, XYZ offset) { if (form.CanManipulateSubElement(subElemReference)) { form.MoveSubElement(subElemReference, offset); } } } }