private void CreateDimensionForOrientation( Document doc, Dictionary <Orientation, List <Grid> > grids, View view, GridParameters p, Orientation orientation) { List <Grid> gridByOrientation = grids[orientation]; // Originale last points XYZ oFirst = gridByOrientation.First().Curve.GetEndPoint(0); XYZ oLast = gridByOrientation.Last().Curve.GetEndPoint(0); // Last points with offset XYZ endPointFirst = default; XYZ endPointLast = default; // Offset size as half of distance double offset = p.GetDistance( p.GetPerpendicularOrientation(orientation)) / 2; switch (orientation) { case Orientation.Vertical: endPointFirst = new XYZ(oFirst.X, oFirst.Y + offset, oFirst.Z); endPointLast = new XYZ(oLast.X, oLast.Y + offset, oLast.Z); break; case Orientation.Horizontal: endPointFirst = new XYZ(oFirst.X + offset, oFirst.Y, oFirst.Z); endPointLast = new XYZ(oLast.X + offset, oLast.Y, oLast.Z); break; } Line dimensionLine = Line.CreateBound(endPointFirst, endPointLast); ReferenceArray axesReferences = new ReferenceArray(); foreach (Grid grid in gridByOrientation) { Options options = new Options() { ComputeReferences = true, IncludeNonVisibleObjects = false, View = view }; foreach (GeometryObject geometry in grid.get_Geometry(options)) { if (geometry is Line) { Line line = geometry as Line; axesReferences.Append(line.Reference); } } } using (Transaction transaction = new Transaction(doc)) { try { transaction.Start( $"Create dimensions between axes in " + $"{orientation.ToString().ToLower()} orientation");; doc.Create.NewDimension(view, dimensionLine, axesReferences); transaction.Commit(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } }
private List <Grid> CreateAxes(Document doc, GridParameters p, Orientation orientation) { List <Grid> list = new List <Grid>(); Line line = default; Grid gridLine = default; double perpendicularSize = p.GetSize(p.GetPerpendicularOrientation(orientation)); double startPoint = p.GetStartPoint(orientation); double endPoint = p.GetEndPoint(orientation); int number = p.GetNumber(orientation); double distance = p.GetDistance(orientation); using (Transaction transaction = new Transaction(doc)) { try { // Iteration start with one (1), not zero (0)! for (int i = 1, n = number; i <= n; i++) { string name = default; double offset = default; if (distance > 0) { offset = (distance * i) - (perpendicularSize / 2); } double x0 = default; double x1 = default; double y0 = default; double y1 = default; switch (orientation) { case Orientation.Vertical: x0 = x1 = offset; y0 = startPoint; y1 = endPoint; name = "1"; break; case Orientation.Horizontal: x0 = startPoint; x1 = endPoint; y0 = y1 = offset; name = "A"; break; } transaction.Start($"Create {orientation.ToString().ToLower()} axis"); line = Line.CreateBound( new XYZ(x0, y0, 0), new XYZ(x1, y1, 0)); gridLine = Grid.Create(doc, line); // Initialize grid axes elevation double indent = GetElevationIndent(); double elevation = GetHighestElevation(doc); gridLine.SetVerticalExtents(0 - indent, indent + elevation); // Set name for first axis in orientation if (i == 1) { ElementCategoryFilter gridsFilter = new ElementCategoryFilter(BuiltInCategory.OST_Grids); FilteredElementCollector existingGrids = new FilteredElementCollector(doc) .WherePasses(gridsFilter) .WhereElementIsNotElementType(); List <string> existingGridsName = new List <string>(); foreach (Grid grid in existingGrids) { existingGridsName.Add(grid.Name); } if (existingGridsName.Contains(name)) { string prefixName; do { SetPrefixForm setPrefixForm = new SetPrefixForm(orientation.ToString()); setPrefixForm.ShowDialog(); if (setPrefixForm.DialogResult == DialogResult.Cancel) { throw new OperationCanceledException(); } prefixName = $"{setPrefixForm.prefix} {name}"; }while (existingGridsName.Contains(prefixName)); gridLine.Name = prefixName; } else { gridLine.Name = name; } } transaction.Commit(); list.Add(gridLine); } } catch (ArgumentException) { System.Windows.Forms.MessageBox.Show("The axes can't have same name"); } } return(list); }