/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">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.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>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.</returns> public virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { Document document = commandData.Application.ActiveUIDocument.Document; Autodesk.Revit.DB.View view = commandData.View; ElementId alignmentId = AlignmentSelectionFilter.SelectAlignment(document); Alignment alignment = Alignment.Get(document.GetElement(alignmentId)); TaskDialog td = new TaskDialog("Alignment and Station Label Properties"); td.MainContent = GetAlignmentProperties(alignment) + GetAlignmentHorizontalCurveTypes(alignment) + GetAlignmentStationLabelsProperties(alignment); td.Show(); return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
const double labelTextOffset = 0.005; // 5mm, defined in paper space #endregion #region InterfaceImplementation /// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">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.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>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.</returns> public virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { Document document = commandData.Application.ActiveUIDocument.Document; Autodesk.Revit.DB.View view = commandData.View; // find major and minor station arrow styles FilteredElementCollector fec = new FilteredElementCollector(document); Element majorStationLeaderArrowheadType = fec.OfClass(typeof(ElementType)) .Cast <ElementType>() .FirstOrDefault(x => x.Name.Contains(majorStationLeaderArrowheadName)); Element minorStationLeaderArrowheadType = fec.OfClass(typeof(ElementType)) .Cast <ElementType>() .FirstOrDefault(x => x.Name.Contains(minorStationLeaderArrowheadName)); if (majorStationLeaderArrowheadType == null || minorStationLeaderArrowheadType == null) { TaskDialog td = new TaskDialog("Missing arrowheads"); td.MainContent = "In Manage>Additional Settings>Arrowheads, create two styles of arrowheads, named\r\n" + "Major Station Arrowhead and Minor Station Arrowhead"; td.Show(); return(Result.Failed); } // find major and minor station label types; if not there, create them SpotDimensionType majorLabelType = fec.OfClass(typeof(SpotDimensionType)) .Cast <SpotDimensionType>() .FirstOrDefault(sdt => sdt.StyleType == DimensionStyleType.AlignmentStationLabel && sdt.Name.Contains(majorStationSetLabelTypeName)); SpotDimensionType minorLabelType = fec.OfClass(typeof(SpotDimensionType)) .Cast <SpotDimensionType>() .FirstOrDefault(sdt => sdt.StyleType == DimensionStyleType.AlignmentStationLabel && sdt.Name.Contains(minorStationSetLabelTypeName)); SpotDimensionType curvatureLabelType = fec.OfClass(typeof(SpotDimensionType)) .Cast <SpotDimensionType>() .FirstOrDefault(sdt => sdt.StyleType == DimensionStyleType.AlignmentStationLabel && sdt.Name.Contains(horizontalCurvatureChangeLabelTypeName)); using (Transaction t = new Transaction(document, "Create major station labels")) { t.Start(); if (majorLabelType == null) { // create major station label type with the given arrowhead style majorLabelType = document.GetElement(AlignmentStationLabel.CreateRecommendedTypeForSet(document)) as SpotDimensionType; majorLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_LEADER_ARROWHEAD).Set(majorStationLeaderArrowheadType.Id); majorLabelType.Name = majorStationSetLabelTypeName; } if (minorLabelType == null) { // create minor station label type with the given arrowhead style // exclude the station text, which leave only the arrowheads // make the minor station's color grey // make the text 60% of the original value, in case text is later turned on minorLabelType = document.GetElement(AlignmentStationLabel.CreateRecommendedTypeForSet(document)) as SpotDimensionType; minorLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_LEADER_ARROWHEAD).Set(minorStationLeaderArrowheadType.Id); minorLabelType.get_Parameter(BuiltInParameter.ALIGNMENT_STATION_LABEL_INCLUDE_STATION).Set(0); minorLabelType.get_Parameter(BuiltInParameter.LINE_COLOR).Set(8421504 /* 127*2^0 + 127*2^8 + 127*2^16: grey */); Parameter textSizeParam = minorLabelType.get_Parameter(BuiltInParameter.TEXT_SIZE); textSizeParam.Set(textSizeParam.AsDouble() * 0.6); minorLabelType.Name = minorStationSetLabelTypeName; } if (curvatureLabelType == null) { // create a new label type, based on the default alignment station label type, // but with some adjustments to the label contents, as described below ElementType defaultAlignmentLabelType = document.GetElement( document.GetDefaultElementTypeId(ElementTypeGroup.AlignmentStationLabelType)) as ElementType; curvatureLabelType = defaultAlignmentLabelType.Duplicate(horizontalCurvatureChangeLabelTypeName) as SpotDimensionType; curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_COORDINATE_BASE).Set(1); // "Shared" coordinate base // Label position and content curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_ROTATE_WITH_COMPONENT).Set(0); // do not rotate with component curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_TEXT_ORIENTATION).Set(0); // horizontal text curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_TEXT_LOCATION).Set(0); // text location above leader curvatureLabelType.get_Parameter(BuiltInParameter.ALIGNMENT_STATION_LABEL_INCLUDE_STATION).Set(1); // include station curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_COORDINATE_INCLUDE_ELEVATION).Set(0); // do not include elevation curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_BOT_VALUE).Set(0); // do not include bottom value curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_TOP_VALUE).Set(0); // do not include top value curvatureLabelType.get_Parameter(BuiltInParameter.ALIGNMENT_STATION_LABEL_IND_STATION).Set(""); // empty station indicator // Text curvatureLabelType.get_Parameter(BuiltInParameter.DIM_TEXT_BACKGROUND).Set(0); // nontransparent text curvatureLabelType.get_Parameter(BuiltInParameter.LINE_COLOR).Set(255 /* 255*2^0 + 0*2^8 + 0*2^16: red */); // text in red color Parameter textSizeParam = curvatureLabelType.get_Parameter(BuiltInParameter.TEXT_SIZE); textSizeParam.Set(textSizeParam.AsDouble() * 0.6); // text size 60% of default // Leader curvatureLabelType.get_Parameter(BuiltInParameter.SPOT_ELEV_LEADER_ARROWHEAD).Set(ElementId.InvalidElementId); // no leader arrowhead } t.Commit(); } // create major and minor station label sets ElementId alignmentId = AlignmentSelectionFilter.SelectAlignment(document); Alignment alignment = Alignment.Get(document.GetElement(alignmentId)); // start placement from a multiple of the major station interval // make sure to compute the multiple in the proper unit system and then convert it back to internal units for further use double labelSetsPlacementStartStation = UnitUtils.ConvertFromInternalUnits(alignment.DisplayedStartStation, UnitTypeId.StationingMeters); labelSetsPlacementStartStation = Math.Ceiling(labelSetsPlacementStartStation / majorStationInterval) * majorStationInterval; labelSetsPlacementStartStation = UnitUtils.ConvertToInternalUnits(labelSetsPlacementStartStation, UnitTypeId.StationingMeters); var majorStations = new List <double>(); using (Transaction t = new Transaction(document, "Create major station labels")) { t.Start(); AlignmentStationLabelSetOptions options = new AlignmentStationLabelSetOptions(); options.Interval = UnitUtils.ConvertToInternalUnits(majorStationInterval, UnitTypeId.StationingMeters); options.Offset = UnitUtils.ConvertToInternalUnits(labelTextOffset, UnitTypeId.StationingMeters); options.StartStation = labelSetsPlacementStartStation; options.EndStation = alignment.DisplayedEndStation; options.TypeId = majorLabelType.Id; var labels = AlignmentStationLabel.CreateSet(alignment, view, options); foreach (var label in labels) { majorStations.Add(label.Station); } t.Commit(); } using (Transaction t = new Transaction(document, "Create minor station labels")) { t.Start(); AlignmentStationLabelSetOptions options = new AlignmentStationLabelSetOptions(); options.Interval = UnitUtils.ConvertToInternalUnits(minorStationInterval, UnitTypeId.StationingMeters); // setting text offset specification can be skipped, // as in this example the minor station labels do not include any label text, only leader arrowheads options.StartStation = labelSetsPlacementStartStation; options.EndStation = labelSetsPlacementStartStation + UnitUtils.ConvertToInternalUnits(majorStationInterval, UnitTypeId.StationingMeters); options.TypeId = minorLabelType.Id; // delete the minor station labels which overlap with the major ones var labels = AlignmentStationLabel.CreateSet(alignment, view, options); foreach (var label in labels) { foreach (var majorStation in majorStations) { if (MathComparisonUtils.IsAlmostEqual(label.Station, majorStation)) { label.Element.Pinned = false; document.Delete(label.Element.Id); break; } } } t.Commit(); } if (view.ViewType == ViewType.FloorPlan || view.ViewType == ViewType.CeilingPlan || view.ViewType == ViewType.EngineeringPlan) { IList <HorizontalCurveEndpoint> curveEndpoints = alignment.GetDisplayedHorizontalCurveEndpoints(); using (TransactionGroup tg = new TransactionGroup(document, "Create horizontal curvature changes labels")) { tg.Start(); double previousStation = alignment.DisplayedStartStation; foreach (var curveEndpoint in curveEndpoints) { using (Transaction t = new Transaction(document, "Create one horizontal curvature change label")) { double thisStation = curveEndpoint.Station; // skip placing curvature labels at the start and end points of the alignment if (MathComparisonUtils.IsAlmostEqual((alignment.DisplayedStartStation), thisStation) || MathComparisonUtils.IsAlmostEqual((alignment.DisplayedEndStation), thisStation)) { continue; } t.Start(); AlignmentStationLabelOptions options = new AlignmentStationLabelOptions(thisStation); options.HasLeader = false; options.TypeId = curvatureLabelType.Id; AlignmentStationLabel label = AlignmentStationLabel.Create(alignment, view, options); // regeneration is necessary before the label's positional properties (such as Origin)L can be properly evaluated document.Regenerate(); // set the shoulder and end to coincide, creating a leader pointing along the view's up direction SpotDimension dim = label.Element as SpotDimension; XYZ leaderDirection = view.UpDirection; // compute the distance to the previous label // if the previous label is too close, flip the placement direction { var dimBBox = dim.get_BoundingBox(view); double dimOffset = Math.Abs(dimBBox.Max.X - dimBBox.Min.X); if (MathComparisonUtils.IsGreaterThanOrAlmostEqual(dimOffset, thisStation - previousStation)) { leaderDirection = leaderDirection.Negate(); } } dim.HasLeader = true; dim.LeaderHasShoulder = true; dim.LeaderShoulderPosition = dim.Origin + leaderDirection * UnitUtils.ConvertToInternalUnits(labelTextOffset, UnitTypeId.StationingMeters) * view.Scale; dim.LeaderEndPosition = dim.LeaderShoulderPosition; dim.TextPosition = dim.LeaderShoulderPosition; previousStation = thisStation; t.Commit(); } } tg.Assimilate(); } } return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }