Ejemplo n.º 1
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Application app = commandData.Application.Application;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            Reference pickedReference = null;
            try
            {
                pickedReference = uidoc.Selection.PickObject(ObjectType.Element, new RebarSelectFilter() , "Pick a Rebar");
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return Result.Cancelled;
            }

            Rebar rebar = doc.GetElement(pickedReference) as Rebar;
            if (!rebar.IsRebarShapeDriven() || rebar.LayoutRule == RebarLayoutRule.Single)
            {
                message = "Singe rebar and non-shape driven rebars are not supported.";
                return Result.Failed;
            }
            double rebarDiameter = rebar.GetBendData().BarDiameter;
            RebarBarType barType = doc.GetElement(rebar.GetTypeId()) as RebarBarType;

            IList<Curve> transformedCurvesFirst = Utils.GetTransformedCenterLineCurvesAtPostition(rebar, 0);
            IList<Curve> transformedCurvesLast = Utils.GetTransformedCenterLineCurvesAtPostition(rebar, rebar.NumberOfBarPositions-1);

            XYZ direction = transformedCurvesFirst.OfType<Line>().First().Direction;

            List<XYZ> rebarInBendFirstPoints = GetPointInArc(transformedCurvesFirst, rebarDiameter);
            List<XYZ> rebarInBendLastPoints = GetPointInArc(transformedCurvesLast, rebarDiameter);

            using (Transaction t1 = new Transaction(doc, "Add rebar in bend"))
            {
                t1.Start();
                for (int i = 0; i < rebarInBendFirstPoints.Count; i++)
                {
                    Line newRebarCenterline = Line.CreateBound(rebarInBendFirstPoints[i], rebarInBendLastPoints[i]);
                    IList<Curve> rebarCurve = new List<Curve>();
                    rebarCurve.Add(newRebarCenterline);
                    Rebar.CreateFromCurves(doc, RebarStyle.Standard, barType, null, null, doc.GetElement(rebar.GetHostId()), direction, rebarCurve, RebarHookOrientation.Left, RebarHookOrientation.Left, true, false);
                }
                doc.Regenerate();
                t1.Commit();
            }
            return Result.Succeeded;
        }