public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;

            uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;

            doc = uidoc.Document;
            sel = uidoc.Selection;
            Reference      rf        = sel.PickObject(ObjectType.Face, "Select Face");
            Element        ele       = doc.GetElement(rf);
            GeometryObject geoobject = ele.GetGeometryObjectFromReference(rf);
            PlanarFace     face      = geoobject as PlanarFace;
            Plane          plane     = face.Faceby3pointPlane();
            XYZ            direction = face.ComputeNormal(UV.Zero);

            using (Transaction tran = new Transaction(doc, "Set view by face"))
            {
                tran.Start();
                SketchPlane skt    = SketchPlane.Create(doc, plane);
                View3D      view3d = doc.ActiveView as View3D;
                view3d.OrientTo(plane.Normal);
                doc.ActiveView.SketchPlane = skt;
                uidoc.RefreshActiveView();
                uidoc.ShowElements(ele.Id);
                tran.Commit();
            }
            return(Result.Succeeded);
        }
Пример #2
0
        /// <summary>
        /// 建立新的3D视图,并将其剖面框设为选择房间的大小
        /// create a new 3D view and change the section box fix the selected room
        /// </summary>
        /// <returns></returns>
        public View3D Create3DView()
        {
            using (Transaction tran = new Transaction(DocSet.doc))
            {
                var collector       = new FilteredElementCollector(DocSet.doc).OfClass(typeof(ViewFamilyType));
                var viewFamilyTypes = from elem in collector
                                      let type = elem as ViewFamilyType
                                                 where type.ViewFamily == ViewFamily.ThreeDimensional
                                                 select type;

                tran.Start("Create A new 3Dview");
                View3D view = View3D.CreateIsometric(DocSet.doc, viewFamilyTypes.First().Id);

                //设定一个新的截面框
                BoundingBoxXYZ boxXyz = new BoundingBoxXYZ();
                XYZ            max    = DocSet.selRoom.get_BoundingBox(view).Max;
                XYZ            min    = DocSet.selRoom.get_BoundingBox(view).Min;
                boxXyz.Max = new XYZ(max.X + 2, max.Y + 2, max.Z);
                boxXyz.Min = new XYZ(min.X - 2, min.Y - 2, min.Z - 0.5);

                view.OrientTo(new XYZ(1, 1, -1));//将3D视角转到左上
                view.SetSectionBox(boxXyz);

                string name = "ISO ";
                name         += _SoANumber;
                view.ViewName = name;
                var par = view.get_Parameter(BuiltInParameter.VIEW_DESCRIPTION).Set(DocSet.selRoom.Name + " - ISOMETRIC");
                tran.Commit();
                //view.SetOrientation(new ViewOrientation3D )

                return(view);
            }
        }