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

            FilteredElementCollector rooms
                = new FilteredElementCollector(doc);

            //
            // this is one way of obtaining rooms ... but see below for a better solution:
            //
            //rooms.OfClass( typeof( Room ) ); // Input type is of an element type that exists in the API, but not in Revit's native object model. Try using Autodesk.Revit.DB.Enclosure instead, and then postprocessing the results to find the elements of interest.
            //rooms.OfClass( typeof( Enclosure ) ); // this works but returns all Enclosure elements

            RoomFilter filter = new RoomFilter();

            rooms.WherePasses(filter);

            if (0 == rooms.Count())
            {
                LabUtils.InfoMsg("There are no rooms in this model.");
            }
            else
            {
                List <string> a = new List <string>();

                /*
                 * foreach( Enclosure e in rooms ) // todo: remove this
                 * {
                 * Room room = e as Room; // todo: remove this
                 * if( null != room ) // todo: remove this
                 * {
                 */

                foreach (Room room in rooms)
                {
                    string roomName   = room.Name;
                    string roomNumber = room.Number;

                    string s = "Room Id=" + room.Id.IntegerValue.ToString()
                               + " Name=" + roomName + " Number=" + roomNumber + "\n";

                    // Loop all boundaries of this room

                    //BoundarySegmentArrayArray boundaries = room.Boundary; // 2011

                    IList <IList <BoundarySegment> > boundaries     // 2012
                        = room.GetBoundarySegments(                 // 2012
                              new SpatialElementBoundaryOptions()); // 2012; passing in a null value throws an exception

                    // Check to ensure room has boundary

                    if (null != boundaries)
                    {
                        int iB = 0;
                        //foreach( BoundarySegmentArray boundary in boundaries ) // 2011
                        foreach (IList <BoundarySegment> boundary in boundaries) // 2012
                        {
                            ++iB;
                            s += "  Boundary " + iB + ":\n";
                            int iSeg = 0;
                            foreach (BoundarySegment segment in boundary)
                            {
                                ++iSeg;

                                // Segment's curve
                                Curve crv = segment.GetCurve();

                                if (crv is Line)
                                {
                                    Line line = crv as Line;
                                    XYZ  ptS  = line.GetEndPoint(0);
                                    XYZ  ptE  = line.GetEndPoint(1);
                                    s += "    Segment " + iSeg + " is a LINE: "
                                         + LabUtils.PointString(ptS) + " ; "
                                         + LabUtils.PointString(ptE) + "\n";
                                }
                                else if (crv is Arc)
                                {
                                    Arc    arc = crv as Arc;
                                    XYZ    ptS = arc.GetEndPoint(0);
                                    XYZ    ptE = arc.GetEndPoint(1);
                                    double r   = arc.Radius;
                                    s += "    Segment " + iSeg + " is an ARC:"
                                         + LabUtils.PointString(ptS) + " ; "
                                         + LabUtils.PointString(ptE) + " ; R="
                                         + LabUtils.RealString(r) + "\n";
                                }
                            }
                        }
                        a.Add(s);
                    }
                    LabUtils.InfoMsg("{0} room{1} in the model{2}", a);
                }
            }
            return(Result.Failed);
        }