コード例 #1
0
        public void Stream(Type type)
        {
            var part = _elem as Part;

            if (type == typeof(Element) && _elem is Element element)
            {
                _data.Add(new MemberSeparatorWithOffset(nameof(PartUtils)));
                _data.Add(new Bool(nameof(PartUtils.AreElementsValidForCreateParts), PartUtils.AreElementsValidForCreateParts(element.Document, new[] { element.Id })));
                _data.Add(new Object(nameof(PartUtils.GetAssociatedPartMaker), PartUtils.GetAssociatedPartMaker(element.Document, element.Id)));
                _data.Add(new Bool(nameof(PartUtils.HasAssociatedParts), PartUtils.HasAssociatedParts(element.Document, element.Id)));
                _data.Add(new Bool(nameof(PartUtils.IsValidForCreateParts), PartUtils.IsValidForCreateParts(element.Document, new LinkElementId(element.Id))));
            }

            if (type == typeof(Part) && part != null)
            {
                _data.Add(new MemberSeparatorWithOffset(nameof(PartUtils)));
                _data.Add(new Bool(nameof(PartUtils.ArePartsValidForDivide), PartUtils.ArePartsValidForDivide(part.Document, new[] { part.Id })));
                _data.Add(new Bool(nameof(PartUtils.ArePartsValidForMerge), PartUtils.ArePartsValidForMerge(part.Document, new[] { part.Id })));
                _data.Add(new Int(nameof(PartUtils.GetChainLengthToOriginal), PartUtils.GetChainLengthToOriginal(part)));
                var isMergedPart = PartUtils.IsMergedPart(part);
                _data.Add(new Enumerable(nameof(PartUtils.GetMergedParts), isMergedPart ? PartUtils.GetMergedParts(part) : Array.Empty <ElementId>(), part.Document));
                _data.Add(new Bool(nameof(PartUtils.IsMergedPart), isMergedPart));
                _data.Add(new Bool(nameof(PartUtils.IsPartDerivedFromLink), PartUtils.IsPartDerivedFromLink(part)));

                _data.Add(new MemberSeparatorWithOffset(nameof(Part)));
                _data.Add(new String(nameof(Part.OriginalCategoryId), ((BuiltInCategory)part.OriginalCategoryId.IntegerValue).ToString()));

                var sourceElementIds = part.GetSourceElementIds().Where(e => e.HostElementId != ElementId.InvalidElementId).Select(e => e.HostElementId).ToList();
                _data.Add(new Enumerable(nameof(Part.GetSourceElementIds), sourceElementIds, part.Document));

                var sourceCategoryIds = part.GetSourceElementOriginalCategoryIds().Select(e => (BuiltInCategory)e.IntegerValue).ToList();
                _data.Add(new EnumerableAsString(nameof(Part.GetSourceElementOriginalCategoryIds), sourceCategoryIds));
            }
        }
コード例 #2
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            string input    = Interaction.InputBox("Enter the panel width in millimeters", "CLT Creator", "250", -1, -1);
            double inputnum = Convert.ToDouble(input);

            /*
             * var hi = new TaskDialog("Hi")
             * {
             *  MainContent = inputnum,
             *  MainIcon = TaskDialogIcon.TaskDialogIconInformation,
             *  CommonButtons = TaskDialogCommonButtons.Ok
             * };
             * hi.Show();
             */

            try
            {
                if (null == commandData)
                {
                    throw new ArgumentNullException("commandData");
                }

                UIApplication uiapp = commandData.Application;
                Application   app   = uiapp.Application;
                UIDocument    uidoc = uiapp.ActiveUIDocument;
                Document      doc   = uidoc.Document;

                Reference r = null;

                try
                {
                    r = uidoc.Selection.PickObject(
                        ObjectType.Element,
                        new WallSelectionFilter(),
                        "Select a wall to split into panels");
                }
                catch (Autodesk.Revit.Exceptions
                       .OperationCanceledException)
                {
                    return(Result.Cancelled);
                }

                Wall wall = (r == null || r.ElementId
                             == ElementId.InvalidElementId)
                  ? null
                  : doc.GetElement(r.ElementId) as Wall;

                if (wall == null)
                {
                    message = "Unable to retrieve wall.";
                    return(Result.Failed);
                }

                LocationCurve location
                    = wall.Location as LocationCurve;

                if (null == location)
                {
                    message = "Unable to retrieve wall location curve.";
                    return(Result.Failed);
                }

                Line line = location.Curve as Line;

                if (null == location)
                {
                    message = "Unable to retrieve wall location line.";
                    return(Result.Failed);
                }

                using (Transaction transaction = new Transaction(doc))
                {
                    transaction.Start("Building panels");

                    IList <ElementId> wallList = new List <ElementId>(1);

                    wallList.Add(r.ElementId);

                    if (PartUtils.AreElementsValidForCreateParts(
                            doc, wallList))
                    {
                        PartUtils.CreateParts(doc, wallList);

                        doc.Regenerate();

                        ICollection <ElementId> parts
                            = PartUtils.GetAssociatedParts(
                                  doc, wall.Id, false, false);

                        if (PartUtils.ArePartsValidForDivide(
                                doc, parts))
                        {
                            double divisions = line.Length / (inputnum / 25.4 / 12);

                            XYZ origin = line.Origin;

                            XYZ delta = line.Direction.Multiply(
                                inputnum / 25.4 / -12);

                            Transform shiftDelta
                                = Transform.CreateTranslation(delta);

                            // Construct a 90 degree rotation in the
                            // XY plane around the line start point

                            Transform rotation = Transform.CreateRotationAtPoint(
                                XYZ.BasisZ, 0.5 * Math.PI, origin);

                            // A vector perpendicular to the wall with
                            // length equal to twice the wall witdh

                            XYZ wallWidthVector = rotation.OfVector(
                                line.Direction.Multiply(2 * wall.Width));

                            Curve intersectionLine
                                = Line.CreateBound( // Line.CreateBound
                                      location.Curve.GetEndPoint(1) + wallWidthVector,
                                      location.Curve.GetEndPoint(1) - wallWidthVector);

                            IList <Curve> curveArray = new List <Curve>();

                            //Jeremy's Modelline Code
                            //XYZ v = 2*wallWidthVector;
                            //double dxy = Math.Abs(v.X) + Math.Abs(v.Y);
                            //XYZ w = (dxy > 1.0e-9)? XYZ.BasisZ: XYZ.BasisY;
                            //XYZ norm = v.CrossProduct(w).Normalize();
                            //Plane plane = Plane.CreateByNormalAndOrigin(norm, origin + wallWidthVector);
                            //SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
                            //Jeremy's code end

                            for (int i = 1; i < divisions; ++i)
                            {
                                intersectionLine = intersectionLine.CreateTransformed(shiftDelta);

                                //ModelCurve curve = doc.IsFamilyDocument ? doc.FamilyCreate.NewModelCurve(intersectionLine, sketchPlane) : doc.Create.NewModelCurve(intersectionLine, sketchPlane);

                                curveArray.Add(intersectionLine);
                            }

                            SketchPlane divisionSketchPlane =
                                SketchPlane.Create(doc,
                                                   Plane.CreateByNormalAndOrigin(XYZ.BasisZ, origin));

                            // An empty list of intersecting ElementIds

                            IList <ElementId> intersectionElementIds
                                = new List <ElementId>();

                            PartUtils.DivideParts(doc, parts, intersectionElementIds, curveArray, divisionSketchPlane.Id);
                        }
                        doc.ActiveView.PartsVisibility
                            = PartsVisibility.ShowPartsOnly;
                    }
                    transaction.Commit();
                }

                return(Result.Succeeded);
            }
            catch (Exception e)
            {
                message = e.Message;
                return(Result.Failed);
            }
        }