Revit api 创建楼梯图元
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Autodesk.Revit.Attributes; 7 using Autodesk.Revit.DB; 8 using Autodesk.Revit.UI; 9 using Autodesk.Revit.DB.Architecture; 10 11 12 namespace CreateStairs 13 { 14 [Transaction(TransactionMode.Manual)] 15 public class Class1:IExternalCommand 16 { 17 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) 18 { 19 Document doc = commandData.Application.ActiveUIDocument.Document; 20 Transaction trans = new Transaction(doc,"new level"); 21 trans.Start(); 22 Level blvl = Level.Create(doc, 0); 23 Level tlvl = Level.Create(doc, 2); 24 trans.Commit(); 25 CreateStairs(doc, blvl, tlvl); 26 return Result.Succeeded; 27 } 28 private ElementId CreateStairs(Document document, Level levelBottom, Level levelTop) 29 { 30 ElementId newStairsId = null; 31 using (StairsEditScope newStairsScope = new StairsEditScope(document, "New Stairs")) 32 { 33 newStairsId = newStairsScope.Start(levelBottom.Id, levelTop.Id); 34 using (Transaction stairsTrans = new Transaction(document, "Add Runs and Landings to Stairs")) 35 { 36 stairsTrans.Start(); 37 38 // Create a sketched run for the stairs 39 IList<Curve> bdryCurves = new List<Curve>(); 40 IList<Curve> riserCurves = new List<Curve>(); 41 IList<Curve> pathCurves = new List<Curve>(); 42 XYZ pnt1 = new XYZ(0, 0, 0); 43 XYZ pnt2 = new XYZ(15, 0, 0); 44 XYZ pnt3 = new XYZ(0, 10, 0); 45 XYZ pnt4 = new XYZ(15, 10, 0); 46 // boundaries 47 bdryCurves.Add(Line.CreateBound(pnt1, pnt2)); 48 bdryCurves.Add(Line.CreateBound(pnt3, pnt4)); 49 // riser curves 50 const int riserNum = 20; 51 for (int ii = 0; ii <= riserNum; ii++) 52 { 53 XYZ end0 = (pnt1 + pnt2) * ii / (double)riserNum; 54 XYZ end1 = (pnt3 + pnt4) * ii / (double)riserNum; 55 XYZ end2 = new XYZ(end1.X, 10, 0); 56 riserCurves.Add(Line.CreateBound(end0, end2)); 57 } 58 59 //stairs path curves 60 XYZ pathEnd0 = (pnt1 + pnt3) / 2.0; 61 XYZ pathEnd1 = (pnt2 + pnt4) / 2.0; 62 pathCurves.Add(Line.CreateBound(pathEnd0, pathEnd1)); 63 StairsRun newRun1 = StairsRun.CreateSketchedRun(document, newStairsId, levelBottom.Elevation, bdryCurves, riserCurves, pathCurves); 64 // Add a straight run 65 Line locationLine = Line.CreateBound(new XYZ(20, -5, newRun1.TopElevation), new XYZ(35, -5, newRun1.TopElevation)); 66 StairsRun newRun2 = StairsRun.CreateStraightRun(document, newStairsId, locationLine, StairsRunJustification.Center); 67 newRun2.ActualRunWidth = 10; 68 // Add a landing between the runs 69 CurveLoop landingLoop = new CurveLoop(); 70 XYZ p1 = new XYZ(15, 10, 0); 71 XYZ p2 = new XYZ(20, 10, 0); 72 XYZ p3 = new XYZ(20, -10, 0); 73 XYZ p4 = new XYZ(15, -10, 0); 74 Line curve_1 = Line.CreateBound(p1, p2); 75 Line curve_2 = Line.CreateBound(p2, p3); 76 Line curve_3 = Line.CreateBound(p3, p4); 77 Line curve_4 = Line.CreateBound(p4, p1); 78 landingLoop.Append(curve_1); 79 landingLoop.Append(curve_2); 80 landingLoop.Append(curve_3); 81 landingLoop.Append(curve_4); 82 StairsLanding newLanding = StairsLanding.CreateSketchedLanding(document, newStairsId, landingLoop, newRun1.TopElevation); 83 stairsTrans.Commit(); 84 } 85 // A failure preprocessor is to handle possible failures during the edit mode commitment process. 86 newStairsScope.Commit(new FailuresPreprocessor());//new StairsFailurePreprocessor()); 87 } 88 return newStairsId; 89 } 90 91 } 92 public class FailuresPreprocessor : IFailuresPreprocessor 93 { 94 public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor) 95 { 96 IList<FailureMessageAccessor> listFma = failuresAccessor.GetFailureMessages(); 97 if (listFma.Count == 0) 98 return FailureProcessingResult.Continue; 99 foreach (FailureMessageAccessor fma in listFma) 100 { 101 if (fma.GetSeverity() == FailureSeverity.Error) 102 { 103 if (fma.HasResolutions()) 104 failuresAccessor.ResolveFailure(fma); 105 } 106 if (fma.GetSeverity() == FailureSeverity.Warning) 107 { 108 failuresAccessor.DeleteWarning(fma); 109 } 110 } 111 return FailureProcessingResult.ProceedWithCommit; 112 } 113 } 114 }