示例#1
0
        /// <summary>
        /// 根据选择的一个平面或者曲线来绘制一个面层对象
        /// </summary>
        /// <param name="uiDoc"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private Result DrawFromOneFace(UIDocument uiDoc, FaceOptions options)
        {
            Result res = Result.Failed;

            // 在Revit界面中选择表面
            Reference faceRef = SelectSurface(uiDoc, options);

            if (faceRef != null)
            {
                // 1. 选择的单元
                Element struElement = _doc.GetElement(faceRef);

                // 2. 选择的表面
                GeometryObject obj = struElement.GetGeometryObjectFromReference(faceRef);
                if (!(obj is Face))
                {
                    MessageBox.Show("未检测到选择的面!", "提示");
                    return(Result.Failed);
                }
                Face face = (Face)obj;

                // 3. 族实例对象的 变换
                Transform transform = GetElementTransform(struElement);

                // ----------------------------------------------------------------------------------------------------
                // 对不同的选择情况进行分类处理
                using (Transaction tran = new Transaction(_doc, "在选择的面上生成面层"))
                {
                    try
                    {
                        tran.Start();

                        // 根据不同的选择类型进行分类处理
                        if (face is PlanarFace)
                        {
                            PlanarFace pFace = (PlanarFace)face;

                            // 对单元中所有同方向的面均进行绘制进行特殊处理

                            if (options.IncludeSameNormal)
                            {
                                IList <DirectShape> surfaces = ExtendToFacesWithSameNormal(tran, struElement,
                                                                                           pFace, options, transform);

                                // 设置面层对象的位置
                                ElementId[] ids = surfaces.Select(r => r.Id).ToArray();
                                uiDoc.Selection.SetElementIds(ids);
                            }
                            else  // 对于一般的单元进行操作
                            {
                                DirectShape ds = ConstructSurfaceFromPlanarFace(tran, pFace, options, transform);
                                if (ds != null)
                                {
                                    // 设置面层对象的位置
                                    uiDoc.Selection.SetElementIds(new ElementId[] { ds.Id });
                                }
                            }
                        }

                        else
                        {
                            // 将一个曲面转化为多个网格平面
                            SurfaceTriangulator st = new SurfaceTriangulator(face, transform);

                            List <PlanarFace> planarFaces = st.GetMeshedFaces();

                            List <FaceTransform> faceTransforms = planarFaces.Select(r => new FaceTransform(r, Transform.Identity)).ToList();
                            //
                            DirectShape ds = ConstructSurfaceFromPlanarFaces(tran, faceTransforms, options);

                            // throw new InvalidOperationException("无法在不是平面的表面生成面层。");
                        }

                        tran.Commit();
                        return(Result.Succeeded);
                    }
                    catch (Exception ex)
                    {
                        // Utils.ShowDebugCatch(ex, "生成面层出错。");
                        tran.RollBack();
                        throw new InvalidOperationException("生成面层出错。", ex);
                        return(Result.Failed);
                    }
                }
            }
            return(res);
        }
示例#2
0
        /// <summary>
        /// 选择多个面,并将对应的多个面层绘制到一个DirectShape中
        /// </summary>
        /// <param name="uiDoc"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private Result DrawOneElemWithFaces(UIDocument uiDoc, FaceOptions options)
        {
            Result res = Result.Failed;

            // 在Revit界面中选择表面
            var faceRefs = SelectSurfaces(uiDoc, options);



            if (faceRefs != null && faceRefs.Count > 0)
            {
                // 提取每一个选择的表面,以及对应的单元的 Transform 对象
                List <FaceTransform> faceTransforms = new List <FaceTransform>();
                foreach (Reference faceRef in faceRefs)
                {
                    // 1. 选择的单元
                    Element struElement = _doc.GetElement(faceRef);

                    // 2. 选择的表面
                    GeometryObject obj = struElement.GetGeometryObjectFromReference(faceRef);
                    // 3. 族实例对象的 变换
                    Transform transform = GetElementTransform(struElement);

                    if (!(obj is PlanarFace))
                    {
                        Face face = (Face)obj;

                        // 将一个曲面转化为多个网格平面
                        SurfaceTriangulator st = new SurfaceTriangulator(face, transform);

                        List <PlanarFace> planarFaces = st.GetMeshedFaces();
                        // 一个曲面所对应的所有平面网格
                        List <FaceTransform> ft = planarFaces.Select(r => new FaceTransform(r, Transform.Identity)).ToList();

                        // 将这一个曲面所对应的三角形平面集合添加到总的集合中
                        faceTransforms.AddRange(ft);
                    }
                    else
                    {
                        PlanarFace pFace = (PlanarFace)obj;

                        faceTransforms.Add(new FaceTransform(pFace, transform));
                    }
                }

                // ----------------------------------------------------------------------------------------------------
                // 对不同的选择情况进行分类处理
                using (Transaction tran = new Transaction(_doc, "通过多个平面来生成一个面层对象"))
                {
                    try
                    {
                        tran.Start();

                        DirectShape ds = ConstructSurfaceFromPlanarFaces(tran, faceTransforms, options);

                        if (ds != null)
                        {
                            // 设置面层对象的位置
                            uiDoc.Selection.SetElementIds(new ElementId[] { ds.Id });
                        }

                        tran.Commit();
                        return(Result.Succeeded);
                    }
                    catch (Exception ex)
                    {
                        //  Utils.ShowDebugCatch(ex, "生成面层出错。");
                        tran.RollBack();

                        throw new InvalidOperationException("生成面层出错。", ex);
                        //  return Result.Failed;
                    }
                }
            }
            return(res);
        }