using System.Collections.Generic; using System.Linq; using Autodesk.Revit.DB; namespace Revit.SDK.Samples.Custom2DExporter.CS { /// /// Collection of helpful utility classes. /// public class Utilities { /// /// Utility to properly append points from 1 list to another. /// /// /// public static void addTo(IList to, IList from) { int cnt = from.Count; for (int ii = 0; ii < cnt; ii++) { if (ii < cnt - 1) { XYZ p0 = from[ii]; XYZ p1 = from[ii + 1]; to.Add(p0); to.Add(p1); } } } static Plane getAppropriatePlane(View view) { Plane plane = Plane.CreateByNormalAndOrigin(view.ViewDirection, view.Origin); return plane; } /// /// Draw all lines defined by the list of (start, end) points, which are bigger than the given tolerance. /// The lines are drawn as detail lines (DetailLine). /// /// The view to draw the lines in. /// The list of (start, end) points of the lines. /// The smallest curve length allowed to be drawn. Lines smaller than this are not drawn. static void drawLines(View view, IList points, double tolerance) { Plane plane = getAppropriatePlane(view); if (plane != null) { for (int ii = 0; ii < points.Count; ii++) { UV uvStart, uvEnd; double distance = double.MaxValue; plane.Project(points[ii], out uvStart, out distance); plane.Project(points[ii + 1], out uvEnd, out distance); XYZ projectionStart; XYZ projectionEnd; projectionStart = uvStart.U * plane.XVec + uvStart.V * plane.YVec + plane.Origin; projectionEnd = uvEnd.U * plane.XVec + uvEnd.V * plane.YVec + plane.Origin; if (projectionStart.DistanceTo(projectionEnd) < tolerance) { ii++; continue; } Line geomLine = Line.CreateBound(projectionStart, projectionEnd); DetailLine line = view.Document.Create.NewDetailCurve(view, geomLine) as DetailLine; ii++; } } } /// /// Hide all elements visibe in the view. /// /// The view static void hideAllInView(View view) { FilteredElementCollector viewElems = new FilteredElementCollector(view.Document, view.Id); ICollection elementIds = viewElems.ToElementIds(); ICollection hideElemIds = new List(); foreach (ElementId id in elementIds) { if (view.Document.GetElement(id).CanBeHidden(view)) hideElemIds.Add(id); } view.HideElements(hideElemIds); } /// /// Displays the exported list of points by drawing detail lines with (start, end) defined by the points. /// All elements besides the drawn lines will be hidden. /// /// The view in which to draw lines and hide elements. /// The points which define the lines (start, end) points public static void displayExport(View view, IList points) { Document doc = view.Document; using (Transaction tran = new Transaction(view.Document, "ExportViewGeometry")) { tran.Start("Draw Exported Lines and turn off everything but Lines"); hideAllInView(view); drawLines(view, points, doc.Application.ShortCurveTolerance); tran.Commit(); } } } }