创建双跑楼梯(Revit二次开发)
今天突然想到,revit中的双跑楼梯对新手来说比较难绘制,于是写了一个小功能来达到这个目的,下面放出没有WPF的代码,如果需要添加WPF界面,请自行添加
此处需要输入的单位为mm,已经转换为英尺了
namespace PersonalTools
{[Transaction(TransactionMode.Manual)]public class CreateStairs : IExternalCommand{public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){UIApplication uiapp = commandData.Application;UIDocument uidoc = uiapp.ActiveUIDocument;Document doc = uidoc.Document;XYZ basePoint = uidoc.Selection.PickPoint(ObjectSnapTypes.Midpoints, "请选择一条边的中点作为楼梯中点");IList<Element> levelList = FilteredElement(doc, BuiltInCategory.OST_Levels, typeof(Level));//此处过滤出标高,可以在WPF中选择int riserNum = 10;//第一个楼梯的踢端数,第二个楼梯的踢面数会四舍五入算出来double stairsWidth = UnitUtils.Convert(1000, DisplayUnitType.DUT_MILLIMETERS, DisplayUnitType.DUT_DECIMAL_FEET);double stairsLength = UnitUtils.Convert(2700, DisplayUnitType.DUT_MILLIMETERS, DisplayUnitType.DUT_DECIMAL_FEET);double platformWidth = UnitUtils.Convert(1500, DisplayUnitType.DUT_MILLIMETERS, DisplayUnitType.DUT_DECIMAL_FEET);bool isOrNotChangedirection = true;//南北朝向还是东西朝向bool isOrNotStraight = false;//直线型还是折线型double rotationAngle = Math.PI / 2.0;ElementId newStairsId = CreateDoubleRunStairs(doc, doc.GetElement(new ElementId(355)) as Level, doc.GetElement(new ElementId(216991)) as Level, riserNum, stairsWidth, stairsLength, platformWidth, basePoint, isOrNotStraight);if (isOrNotChangedirection){using(Transaction trans = new Transaction(doc, "改变楼梯方向")){trans.Start();Line axis = Line.CreateBound(basePoint, basePoint.Add(XYZ.BasisZ*10));ElementTransformUtils.RotateElement(doc, newStairsId, axis, rotationAngle);trans.Commit();}}return Result.Succeeded;}private ElementId CreateDoubleRunStairs(Document document, Level levelBottom, Level levelTop, int riserNum, double width, double length, double platformWidth, XYZ basePoint, bool isOrNotStraight){ElementId newStairsId = null;using (StairsEditScope newStairsScope = new StairsEditScope(document, "新建楼梯")){newStairsId = newStairsScope.Start(levelBottom.Id, levelTop.Id);using (Transaction stairsTrans = new Transaction(document, "添加楼梯轮廓线")){stairsTrans.Start();XYZ pnt1 = new XYZ(basePoint.X, basePoint.Y, 0);XYZ pnt2 = new XYZ(length + basePoint.X, basePoint.Y, 0);XYZ pnt3 = new XYZ(basePoint.X, width + basePoint.Y, 0);XYZ pnt4 = new XYZ(length + basePoint.X, width + basePoint.Y, 0);//楼梯轮廓线/中心线IList<Curve> bdryCurves = new List<Curve>();IList<Curve> riserCurves = new List<Curve>();IList<Curve> pathCurves = new List<Curve>();//楼梯外轮廓线bdryCurves.Add(Line.CreateBound(pnt1, pnt2));bdryCurves.Add(Line.CreateBound(pnt3, pnt4));// 步数及步线位置//const int riserNum = 20;for (int ii = 0; ii <= riserNum; ii++){XYZ end0 = new XYZ(pnt1.X + length * ii / (double)riserNum, pnt1.Y, 0);XYZ end1 = new XYZ(pnt1.X + length * ii / (double)riserNum, pnt1.Y + width, 0);riserCurves.Add(Line.CreateBound(end0, end1));}//楼梯中心线XYZ pathEnd0 = (pnt1 + pnt3) / 2.0;XYZ pathEnd1 = (pnt2 + pnt4) / 2.0;pathCurves.Add(Line.CreateBound(pathEnd0, pathEnd1));// 创建楼梯1StairsRun newRun1 = StairsRun.CreateSketchedRun(document, newStairsId, levelBottom.Elevation, bdryCurves, riserCurves, pathCurves);// 楼梯2参数if (isOrNotStraight){Line locationLine = Line.CreateBound(new XYZ(platformWidth + length + basePoint.X, basePoint.Y - width / 2, newRun1.TopElevation), new XYZ(length * 2 + platformWidth + basePoint.X, basePoint.Y - width / 2, newRun1.TopElevation));StairsRun newRun2 = StairsRun.CreateStraightRun(document, newStairsId, locationLine, StairsRunJustification.Center);newRun2.ActualRunWidth = width;}else{Line locationLine = Line.CreateBound(new XYZ(length + basePoint.X, basePoint.Y - width / 2, newRun1.TopElevation), new XYZ(basePoint.X, basePoint.Y - width / 2, newRun1.TopElevation));StairsRun newRun2 = StairsRun.CreateStraightRun(document, newStairsId, locationLine, StairsRunJustification.Center);newRun2.ActualRunWidth = width;}// 楼梯平台CurveLoop landingLoop = new CurveLoop();XYZ p1 = new XYZ(length + basePoint.X, width + basePoint.Y, 0);XYZ p2 = new XYZ(length + platformWidth + basePoint.X, width + basePoint.Y, 0);XYZ p3 = new XYZ(length + platformWidth + basePoint.X, basePoint.Y - width, 0);XYZ p4 = new XYZ(length + basePoint.X, basePoint.Y - width, 0);Line curve_1 = Line.CreateBound(p1, p2);Line curve_2 = Line.CreateBound(p2, p3);Line curve_3 = Line.CreateBound(p3, p4);Line curve_4 = Line.CreateBound(p4, p1);landingLoop.Append(curve_1);landingLoop.Append(curve_2);landingLoop.Append(curve_3);landingLoop.Append(curve_4);StairsLanding newLanding = StairsLanding.CreateSketchedLanding(document, newStairsId, landingLoop, newRun1.TopElevation);stairsTrans.Commit();}// 忽略创建时失败的事务【必须有】newStairsScope.Commit(new FailuresPreprocessor());}return newStairsId;}private List<Element> FilteredElement(Document doc, BuiltInCategory category, Type eletype){FilteredElementCollector elements = new FilteredElementCollector(doc);elements.OfCategory(category).OfClass(eletype);return elements.ToList();}}public class FailuresPreprocessor : IFailuresPreprocessor{public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor){IList<FailureMessageAccessor> listFma = failuresAccessor.GetFailureMessages();if (listFma.Count == 0)return FailureProcessingResult.Continue;foreach (FailureMessageAccessor fma in listFma){if (fma.GetSeverity() == FailureSeverity.Error){if (fma.HasResolutions())failuresAccessor.ResolveFailure(fma);}if (fma.GetSeverity() == FailureSeverity.Warning){failuresAccessor.DeleteWarning(fma);}}return FailureProcessingResult.ProceedWithCommit;}}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
