public List <SpotDimension> CreatSpotDimensionOnFloorInLink(List <ElementId> floorids, View activeview, Document doc, RevitLinkInstance rli, ref string report) { List <SpotDimension> sds = new List <SpotDimension>(); var a = rli.GetTotalTransform(); int errnum = 0; foreach (ElementId id in floorids) { Floor floor = rli.GetLinkDocument().GetElement(id) as Floor; Options option = new Options(); option.ComputeReferences = true; option.View = activeview; option.IncludeNonVisibleObjects = false; GeometryElement geometry = floor.get_Geometry(option); if (geometry == null) { continue; } foreach (GeometryObject geomobj in geometry) { Solid gemosolid = geomobj as Solid; if (gemosolid != null) { XYZ center = rli.GetTotalTransform().OfPoint(gemosolid.ComputeCentroid()); foreach (Face geomface in gemosolid.Faces) { PlanarFace plane = geomface as PlanarFace; if (plane != null) { if (plane.FaceNormal.X == 0 && plane.FaceNormal.Y == 0 && plane.FaceNormal.Z == 1) { //get reference for creation Reference refe = geomface.Reference.CreateLinkReference(rli); //get points for creation XYZ origin = center; XYZ bend = origin.Add(new XYZ(0, 1, 0)); XYZ end = origin.Add(new XYZ(1, 1, 0)); XYZ refpt = origin; try { SpotDimension sd = doc.Create.NewSpotElevation(activeview, refe, origin, bend, end, refpt, false); sds.Add(sd); } catch (Autodesk.Revit.Exceptions.InvalidOperationException e) { errnum++; } } } } } } } report += "\n" + "在" + rli.Name + "有" + errnum.ToString() + "个楼板没有生成高程点."; return(sds); }
/// <summary> /// Create three new spot elevations on the top /// surface of a beam, at its midpoint and both /// endpoints. /// </summary> bool NewSpotElevation(Document doc) { //Document doc = ActiveDocument; // for VSTA macro version View westView = FindView(doc, "West"); if (null == westView) { Util.ErrorMsg("No view found named 'West'."); return(false); } // define the hard-coded beam element id: //ElementId instanceId = Create.NewElementId(); //instanceId.IntegerValue = 230298; ElementId instanceId = new ElementId(230298); FamilyInstance beam = doc.GetElement( instanceId) as FamilyInstance; if (null == beam) { Util.ErrorMsg("Beam 230298 not found."); return(false); } //doc.BeginTransaction(); // for VSTA macro version using (Transaction t = new Transaction(doc)) { t.Start("Create Spot Elevation"); Reference topReference = FindTopMostReference(beam); LocationCurve lCurve = beam.Location as LocationCurve; for (int i = 0; i < 3; ++i) { XYZ lCurvePnt = lCurve.Curve.Evaluate( 0.5 * i, true); XYZ bendPnt = lCurvePnt.Add( Create.NewXYZ(0, 1, 4)); XYZ endPnt = lCurvePnt.Add( Create.NewXYZ(0, 2, 4)); // NewSpotElevation arguments: // View view, Reference reference, // XYZ origin, XYZ bend, XYZ end, XYZ refPt, // bool hasLeader SpotDimension d = doc.Create.NewSpotElevation( westView, topReference, lCurvePnt, bendPnt, endPnt, lCurvePnt, true); } //doc.EndTransaction(); // for VSTA macro version t.Commit(); } return(true); }
private void CreateSpotElevation(FamilyInstance familyInstance) { //get the location curve of the family instance LocationCurve location = familyInstance.Location as LocationCurve; //instantiate a curve Curve curve = null; //check for location null if (location != null) { //get the curve from the location curve = location.Curve; } else if (familyInstance.Category.Name.Equals("Structural Columns")) //structural columns do not have location curve, but analytical model does { curve = familyInstance.GetAnalyticalModel().GetCurve(); } if (curve != null) { //get the end points and mid point from the curve XYZ startPoint = curve.GetEndPoint(0); XYZ endPoint = curve.GetEndPoint(1); XYZ midPoint = curve.Evaluate(0.5, true); //set the default point on the element to tag XYZ pointOnElementToTag = startPoint; TaskDialog td = new TaskDialog("Select Point to Tag"); td.MainInstruction = "Select the point on the curve to tag"; td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Start Point"); td.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "End Point"); td.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "Mid Point"); td.CommonButtons = TaskDialogCommonButtons.Cancel; td.DefaultButton = TaskDialogResult.Cancel; TaskDialogResult result = td.Show(); if (result == TaskDialogResult.CommandLink1) { pointOnElementToTag = startPoint; } else if (result == TaskDialogResult.CommandLink2) { pointOnElementToTag = endPoint; } else if (result == TaskDialogResult.CommandLink3) { pointOnElementToTag = midPoint; } else { TaskDialog.Show("Canceled", "The action was canceled by the user"); return; } //instantiate a transaction to add the individual tag using (Transaction trans = new Transaction(_doc, "Add Spot Elevation")) { trans.Start(); try { //get the top reference for the spot elevation Reference spotReference = FindTopMostReference(familyInstance); //set the bend and endpoints for the new spot elevation XYZ seBendPoint = pointOnElementToTag.Add(new XYZ(0, 1, 4)); XYZ seEndPoint = pointOnElementToTag.Add(new XYZ(0, 2, 4)); //add the spot elevation SpotDimension sd = _docCreator.NewSpotElevation(_uiDoc.ActiveView, spotReference, pointOnElementToTag, seBendPoint, seEndPoint, pointOnElementToTag, true); trans.Commit(); //TaskDialog.Show("Success", "The Spot elevation was added successfully"); } catch (Exception ex) { TaskDialog.Show("Error", "The following error occurred: " + ex.Message); trans.RollBack(); } } } }
private void Stream( ArrayList data, SpotDimension dim ) { data.Add( new Snoop.Data.ClassSeparator( typeof( SpotDimension ) ) ); data.Add( new Snoop.Data.Object( "Spot dimension type", dim.SpotDimensionType ) ); }
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); } }