revit二次开发获取墙的几何信息

1、获取墙(标准族)的底部面积和墙的原点位置(origin),从solid里获得edge和face

using System ;
using System . Collections . Generic ;
using System . Linq ;
using System . Text ;
using System . Threading . Tasks ;
using Autodesk . Revit . UI ;
using Autodesk . Revit . DB ;
using Autodesk . Revit . UI . Selection ;
using Autodesk . Revit . ApplicationServices ;
namespace 给族添加一个参数
{
    [ Autodesk . Revit . Attributes . Transaction ( Autodesk . Revit . Attributes . TransactionMode . Manual )]
    [ Autodesk . Revit . Attributes . Journaling ( Autodesk . Revit . Attributes . JournalingMode . NoCommandData )]
    [ Autodesk . Revit . Attributes . Regeneration ( Autodesk . Revit . Attributes . RegenerationOption . Manual )]
    public class Class1 : IExternalCommand
    {
        public Result Execute ( ExternalCommandData commandData , ref string message , ElementSet elements )
        {
            Document revitDoc = commandData . Application . ActiveUIDocument . Document //取得文档
            Application revitApp = commandData . Application . Application ;             //取得应用程序
            UIDocument uiDoc = commandData . Application . ActiveUIDocument ;
            Selection sel = uiDoc . Selection ;
            Reference ref1 = sel . PickObject ( ObjectType . Element , "选择一个族实例" );
            Element elem = revitDoc . GetElement ( ref1 );
            Wall wall = elem as Wall ;
            Options opt = new Options ();
            opt . ComputeReferences = true ;
            opt . DetailLevel = ViewDetailLevel . Fine ;
            GeometryElement e = wall .get_Geometry( opt );
            foreach ( GeometryObject o in e )
            {
                Point point = o as Point ;
                Solid solid = o as Solid ;
                if ( solid != null && solid . Faces . Size >0)
                {
                    FindBottomFace ( solid );   //得到最底下的边的面积和原点坐标
                    FindEdge ( solid );        //得到12个边的长度
                   
                }
            }
          
            return Result . Succeeded ;
        }
        ///
        /// 得到最底下的边的面积和原点坐标
        ///
        /// solid ">
        ///
        Face FindBottomFace ( Solid solid )
        {
            PlanarFace pf = null ;
            foreach ( Face face in solid . Faces )
            {
                pf = face as PlanarFace ;
                if ( null != pf )
                {
                    if ( Math . Abs ( pf . FaceNormal . X ) < 0.01 && Math . Abs ( pf . FaceNormal . Y ) < 0.01 && pf . FaceNormal . Z < 0)
                    {
                        TaskDialog . Show ( "Wall Bottom Face" , "Area is " + pf . Area . ToString () + "; Origin = (" + pf . Origin . X . ToString () + "  " + pf . Origin . Y . ToString () + "  " + pf . Origin . Z . ToString () + ")" );
                        break ;
                    }
                }
            }
            return pf ;
        }
        ///
        /// /得到12个边的长度
        ///
        /// solid ">
        public void FindEdge ( Solid solid )
        {
            string strParamInfo = null ;
            foreach ( Edge e in solid . Edges )
            {
                strParamInfo += e . ApproximateLength + "\n" ;
            }
            TaskDialog . Show ( "REVIT" , strParamInfo );
        }
        
    }
}

2、可以从solid里获得的信息
  1)edges,而从edges里可以获得的信息有:点,长度,线(line)等。
  2)Faces,而从face里可以获得的信息有:面积、方向、origin等。

3、可以从族实例中获得geometry
using System ;
using System . Collections . Generic ;
using System . Linq ;
using System . Text ;
using System . Threading . Tasks ;
using Autodesk . Revit . UI ;
using Autodesk . Revit . DB ;
using Autodesk . Revit . UI . Selection ;
using Autodesk . Revit . ApplicationServices ;
namespace 从族实例中获得几何信息
{
    [ Autodesk . Revit . Attributes . Transaction ( Autodesk . Revit . Attributes . TransactionMode . Manual )]
    [ Autodesk . Revit . Attributes . Journaling ( Autodesk . Revit . Attributes . JournalingMode . NoCommandData )]
    [ Autodesk . Revit . Attributes . Regeneration ( Autodesk . Revit . Attributes . RegenerationOption . Manual )]
    public class Class1 : IExternalCommand
    {
        public Result Execute ( ExternalCommandData commandData , ref string message , ElementSet elements )
        {
            Document revitDoc = commandData . Application . ActiveUIDocument . Document //取得文档
            Application revitApp = commandData . Application . Application ;             //取得应用程序
            UIDocument uiDoc = commandData . Application . ActiveUIDocument ;
            Selection sel = uiDoc . Selection ;
            Reference ref1 = sel . PickObject ( ObjectType . Element , "选择一个族实例" );
            Element elem = revitDoc . GetElement ( ref1 );
            FamilyInstance familyInstance = elem as FamilyInstance ;
            Options opt = new Options ();
            opt . ComputeReferences = true ;
            opt . DetailLevel = ViewDetailLevel . Fine ;
            GeometryElement e = familyInstance .get_Geometry( opt );
            foreach ( GeometryObject obj in e )
            {
                if ( obj is Solid )
                {
                    Solid solid = obj as Solid ;
                    FindBottomFace ( solid );
                    FindEdge ( solid );
                }
                else if ( obj is GeometryInstance )
                {
                    GeometryInstance geoInstance = obj as GeometryInstance ;
                    GeometryElement geoElement = geoInstance . GetInstanceGeometry ();
                    foreach ( GeometryObject obj2 in geoElement )
                    {
                        if ( obj2 is Solid )
                        {
                            Solid solid2 = obj2 as Solid ;
                            if ( solid2 . Faces . Size > 0)
                                FindBottomFace ( solid2 );
                            FindEdge ( solid2 );
                            TaskDialog . Show ( "呵呵" , "在这里" );
                        }
                    }
                }
            }
            return Result . Succeeded ;
        }
        ///
        /// 得到最底下的边的面积和原点坐标
        ///
        /// solid ">
        ///
        Face FindBottomFace ( Solid solid )
        {
            PlanarFace pf = null ;
            foreach ( Face face in solid . Faces )
            {
                pf = face as PlanarFace ;
                if ( null != pf )
                {
                    if ( Math . Abs ( pf . FaceNormal . X ) < 0.01 && Math . Abs ( pf . FaceNormal . Y ) < 0.01 && pf . FaceNormal . Z < 0)
                    {
                        TaskDialog . Show ( "Wall Bottom Face" , "Area is " + pf . Area . ToString () + "; Origin = (" + pf . Origin . X . ToString () + "  " + pf . Origin . Y . ToString () + "  " + pf . Origin . Z . ToString () + ")" );
                        break ;
                    }
                }
            }
            return pf ;
        }
        ///
        /// /得到12个边的长度,并显示长度信息
        ///
        /// solid ">
        public void FindEdge ( Solid solid )
        {
            string strParamInfo = null ;
            foreach ( Edge e in solid . Edges )
            {
                strParamInfo += e . ApproximateLength + "\n" ;
            }
            TaskDialog . Show ( "REVIT" , strParamInfo );
        }
    }
}

你可能感兴趣的:(revit二次开发,revit2016,二次开发)