Esempio n. 1
0
        /***************************************************/
        /****              Private methods              ****/
        /***************************************************/

        private static XYZ CeilingPatternAlignment(this Ceiling ceiling, Material material, RevitSettings settings, out double rotation)
        {
            rotation = 0;
            if (ceiling == null || material == null)
            {
                return(null);
            }

            Document doc = ceiling.Document;

            FillPatternElement fillPatternElement;

#if (REVIT2020 || REVIT2021)
            fillPatternElement = doc.GetElement(material.SurfaceForegroundPatternId) as FillPatternElement;
#else
            fillPatternElement = doc.GetElement(material.SurfacePatternId) as FillPatternElement;
#endif

            FillPattern fp = fillPatternElement?.GetFillPattern();
            if (fp == null || fp.GridCount != 2)
            {
                return(null);
            }

            XYZ result = null;
            settings = settings.DefaultIfNull();

            Options o = new Options();
            o.ComputeReferences = true;
            Document          hostDoc       = doc.IsLinked ? doc.HostDocument() : doc;
            RevitLinkInstance linkInstance  = doc.IsLinked ? doc.LinkInstance() : null;
            Transform         linkTransform = linkInstance == null ? Transform.Identity : linkInstance.GetTotalTransform();

            foreach (GeometryObject go in ceiling.get_Geometry(o))
            {
                if (go is Solid)
                {
                    foreach (Autodesk.Revit.DB.Face f in ((Solid)go).Faces)
                    {
                        PlanarFace pf = f as PlanarFace;
                        if (pf == null)
                        {
                            continue;
                        }

                        if (1 + pf.FaceNormal.DotProduct(XYZ.BasisZ) > settings.AngleTolerance)
                        {
                            continue;
                        }

                        Reference faceRef = f.Reference;
                        if (doc.IsLinked)
                        {
                            faceRef = faceRef.CreateLinkReference(linkInstance).PrepareForLinkDimensioning(hostDoc);
                        }

                        string stableRef = faceRef.ConvertToStableRepresentation(hostDoc);

                        ReferenceArray horR = new ReferenceArray();
                        horR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/2"));
                        horR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/" + (2 + fp.GridCount * 2).ToString()));

                        ReferenceArray verR = new ReferenceArray();
                        verR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/1"));
                        verR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/" + (1 + fp.GridCount * 2).ToString()));

                        using (Transaction t = new Transaction(hostDoc, "temp dim"))
                        {
                            t.Start();
                            Dimension horDim = hostDoc.Create.NewDimension(hostDoc.ActiveView, Autodesk.Revit.DB.Line.CreateBound(XYZ.Zero, pf.XVector), horR);
                            Dimension verDim = hostDoc.Create.NewDimension(hostDoc.ActiveView, Autodesk.Revit.DB.Line.CreateBound(XYZ.Zero, pf.YVector), verR);
                            ElementTransformUtils.MoveElement(hostDoc, horDim.Id, XYZ.BasisX);
                            ElementTransformUtils.MoveElement(hostDoc, verDim.Id, XYZ.BasisX);

                            rotation  = -(horDim.Curve as Autodesk.Revit.DB.Line).Direction.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
                            rotation += linkTransform.BasisX.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
                            Transform tr = Transform.CreateRotation(XYZ.BasisZ, rotation);

                            double x = tr.Inverse.OfPoint(linkTransform.Inverse.OfPoint(horDim.Origin)).X;
                            double y = tr.Inverse.OfPoint(linkTransform.Inverse.OfPoint(verDim.Origin)).Y;
                            t.RollBack();

                            foreach (FillGrid fg in fp.GetFillGrids())
                            {
                                if (fg.Angle.ToSI(UnitType.UT_Angle) > settings.AngleTolerance)
                                {
                                    y += fg.Offset * 0.5;
                                }
                                else
                                {
                                    x += fg.Offset * 0.5;
                                }
                            }

                            result = tr.OfPoint(new XYZ(x, y, 0));
                            break;
                        }
                    }
                }
            }

            return(result);
        }