zoukankan      html  css  js  c++  java
  • Revit Family API 添加几何实体

    先创建一个封闭曲线createProfileLShape();
    再创建实体,这里需要手工画一个参考平面
    Reference Plane
    [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class cmdCreateSolid : IExternalCommand
    {
        //创建封闭曲线
        CurveArrArray createProfileLShape(Application _rvtApp)
        {
            //
            
    // define a simple L-shape profile
            
    //
            
    //  5 tw 4
            
    //   +-+
            
    //   | | 3          h = height
            
    // d | +---+ 2
            
    //   +-----+ td
            
    //  0        1
            
    //  6  w
            
    //

            
    // sizes (hard coded for simplicity)
            
    // note: these need to match reference plane. otherwise, alignment won't work.
            
    // as an exercise, try changing those values and see how it behaves.
            
    //
            double w = Util.mmToFeet(600.0); // those are hard coded for simplicity here. in practice, you may want to find out from the references)
            double d = Util.mmToFeet(600.0);
            double tw = Util.mmToFeet(150.0); // thickness added for Lab2
            double td = Util.mmToFeet(150.0);

            // define vertices
            
    //
            const int nVerts = 6// the number of vertices

            XYZ[] pts = new XYZ[] {
        new XYZ(-w / 2.0, -d / 2.00.0),
        new XYZ(w / 2.0, -d / 2.00.0),
        new XYZ(w / 2.0, (-d / 2.0) + td, 0.0),
        new XYZ((-w / 2.0) + tw, (-d / 2.0) + td, 0.0),
        new XYZ((-w / 2.0) + tw, d / 2.00.0),
        new XYZ(-w / 2.0, d / 2.00.0),
        new XYZ(-w / 2.0, -d / 2.00.0) };  // the last one is to make the loop simple

            
    // define a loop. define individual edges and put them in a curveArray
            
    //
            CurveArray pLoop = _rvtApp.Create.NewCurveArray();
            for (int i = 0; i < nVerts; ++i)
            {
                Line line = _rvtApp.Create.NewLineBound(pts[i], pts[i + 1]);
                pLoop.Append(line);
            }

            // then, put the loop in the curveArrArray as a profile
            
    //
            CurveArrArray pProfile = _rvtApp.Create.NewCurveArrArray();
            pProfile.Append(pLoop);
            // if we come here, we have a profile now.

            return pProfile;
        }
        Extrusion CreateSolid(Application app, Document doc)
        {
            CurveArrArray pProfile = createProfileLShape(app);
            //这个参考平面模板中没有,可以切换到Front立面,自己画一个。
            ReferencePlane pRefPlane = Util.findElement(doc, typeof(ReferencePlane), "Reference Plane"as ReferencePlane;
            SketchPlane pSketchPlane = doc.FamilyCreate.NewSketchPlane(pRefPlane.Plane);
            double dHeight = Util.mmToFeet(4000);
            bool bIsSolid = true;
            Extrusion pSolid = doc.FamilyCreate.NewExtrusion(bIsSolid, pProfile, pSketchPlane, dHeight);
            return pSolid;
        }
        public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document doc = app.ActiveUIDocument.Document;

            Transaction ts = new Transaction(doc, "AddType");
            ts.Start();

            //
            FamilyManager m_familyMgr = doc.FamilyManager;

            CreateSolid(app.Application, doc);


            ts.Commit();

            return Result.Succeeded;
        }
    }
    public class Util
    {
        //Revit内部单位feet转化为mm即毫米
        public static double mmToFeet(double val) { return val / 304.8; }
        public static double feetToMm(double val) { return val * 304.8; }
        //通过类型与名称找Element
        public static Element findElement(Document _rvtDoc, Type targetType, string targetName)
        {
            // get the elements of the given type
            
    //
            FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
            collector.WherePasses(new ElementClassFilter(targetType));

            // parse the collection for the given name
            
    // using LINQ query here. 
            
    // 
            var targetElems = from element in collector where element.Name.Equals(targetName) select element;
            List<Element> elems = targetElems.ToList<Element>();

            if (elems.Count > 0)
            {  // we should have only one with the given name. 
                return elems[0];
            }

            // cannot find it.
            return null;
        }
        #region Formatting and message handlers
        public const string Caption = "Revit Family API Labs";

        /// <summary>
        
    /// MessageBox wrapper for informational message.
        
    /// </summary>
        public static void InfoMsg(string msg)
        {

            System.Diagnostics.Debug.WriteLine(msg);
            WinForm.MessageBox.Show(msg, Caption, WinForm.MessageBoxButtons.OK, WinForm.MessageBoxIcon.Information);
        }

        /// <summary>
        
    /// MessageBox wrapper for error message.
        
    /// </summary>
        public static void ErrorMsg(string msg)
        {
            WinForm.MessageBox.Show(msg, Caption, WinForm.MessageBoxButtons.OK, WinForm.MessageBoxIcon.Error);
        }
        #endregion // Formatting and message handlers
    }
    url:http://greatverve.cnblogs.com/p/revit-family-api-create-solid.html
  • 相关阅读:
    10 个最佳的网站分析方法
    网站优化:测试网站速度的8款免费工具推荐
    8 个最棒的 .NET 开发相关工具
    10 个文件和文档的比较工具
    asp.net中调用javascript自定义函数的方法(包括引入JavaScript文件)总结
    如何在ashx页面获取Session值
    java-信息安全(十一)-非对称加密算法ECC
    java-信息安全(十)-数字签名算法DSA
    java-信息安全(九)-基于DH,非对称加密,对称加密等理解HTTPS
    java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换
  • 原文地址:https://www.cnblogs.com/greatverve/p/revit-family-api-create-solid.html
Copyright © 2011-2022 走看看