没测试成功,留待以后研究。

[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdAddAlignment : IExternalCommand
{
    //创建对齐,把一个面对齐到参考面。
    void AddAlignment_ReferencePlane(Application app, Document doc, Extrusion pSolid, XYZ normal, string nameRefPlane)
    {
        View pViewPlan = Util.findElement(doc, typeof(ViewPlan), "Lower Ref. Level") as View;
        ReferencePlane refPlane = Util.findElement(doc, typeof(ReferencePlane), nameRefPlane) as ReferencePlane;
        PlanarFace pFace = findFace(app, pSolid, normal, refPlane);
        doc.FamilyCreate.NewAlignment(pViewPlan, refPlane.Reference, pFace.Reference);
    }
    //对齐到楼层
    void AddAlignment_Level(Document doc, Extrusion pSolid, XYZ normal, string nameLevel)
    {
        View pView = Util.findElement(doc, typeof(View), "Front") as View;
        Level pLevel = Util.findElement(doc, typeof(Level), nameLevel) as Level;
        PlanarFace pFace = Util.findFace(pSolid, normal);
        doc.FamilyCreate.NewAlignment(pView, pLevel.PlaneReference, pFace.Reference);
    }
    void AddAlignments(Application app, Document doc, Extrusion pSolid)
    {
        AddAlignment_Level(doc, pSolid, new XYZ(0.0, 0.0, 1.0), "Upper Ref Level");
        AddAlignment_Level(doc, pSolid, new XYZ(0.0, 0.0, -1.0), "Lower Ref. Level");
        AddAlignment_ReferencePlane(app, doc, pSolid, new XYZ(1.0, 0.0, 0.0), "Right");
        AddAlignment_ReferencePlane(app, doc, pSolid, new XYZ(-1.0, 0.0, 0.0), "Left");
        AddAlignment_ReferencePlane(app, doc, pSolid, new XYZ(0.0, -1.0, 0.0), "Front");
        AddAlignment_ReferencePlane(app, doc, pSolid, new XYZ(0.0, 1.0, 0.0), "Back");
        AddAlignment_ReferencePlane(app, doc, pSolid, new XYZ(1.0, 0.0, 0.0), "OffsetV");
        AddAlignment_ReferencePlane(app, doc, pSolid, new XYZ(0.0, 1.0, 0.0), "OffsetH");
    }
    // ===============================================================
    // helper function: given a solid, find a planar 
    //Extrusion实体,给一个实体,给一个方向,找到与此方向一致的面。
    // face with the given normal (version 2)
    // this is a slightly enhaced version of the previous 
    // version and checks if the face is on the given reference plane.
    // ===============================================================
    PlanarFace findFace(Application app, Extrusion pBox, XYZ normal, ReferencePlane refPlane)
    {
        // get the geometry object of the given element
        //
        Options op = new Options();
        op.ComputeReferences = true;
        GeometryObjectArray geomObjs = pBox.get_Geometry(op).Objects;         // loop through the array and find a face with the given normal
        //
        foreach (GeometryObject geomObj in geomObjs)
        {
            if (geomObj is Solid)  // solid is what we are interested in.
            {
                Solid pSolid = geomObj as Solid;
                FaceArray faces = pSolid.Faces;
                foreach (Face pFace in faces)
                {
                    PlanarFace pPlanarFace = (PlanarFace)pFace;
                    // check to see if they have same normal
                    //face.Normal是面的向量。IsAlmostEqualTo();
                    if ((pPlanarFace != null) && pPlanarFace.Normal.IsAlmostEqualTo(normal))
                    {
                        // additionally, we want to check if the face is on the reference plane
                        //还要判断面是否在参考平面上。
                        XYZ p0 = refPlane.BubbleEnd;//终点?
                        XYZ p1 = refPlane.FreeEnd;//起点?
                        Line pCurve = app.Create.NewLineBound(p0, p1);
                        if (pPlanarFace.Intersect(pCurve) == SetComparisonResult.Subset)//子集
                        {
                            return pPlanarFace; // we found the face
                        }
                    }
                }
            }             // will come back later as needed.
            //
            //else if (geomObj is Instance)
            //{
            //}
            //else if (geomObj is Curve)
            //{
            //}
            //else if (geomObj is Mesh)
            //{
            //}
        }         // if we come here, we did not find any.
        return null;
    }
    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        UIApplication app = commandData.Application;
        Document doc = app.ActiveUIDocument.Document;         Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
        ts.Start();         //
        Extrusion pSolid = CreateSolid(app.Application, doc);
        AddAlignments(app.Application, doc, pSolid);         ts.Commit();         return Result.Succeeded;
    }
    //创建封闭曲线
    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 = ; // the number of vertices         XYZ[] pts = new XYZ[] {
new XYZ(-w / 2.0, -d / 2.0, 0.0),
new XYZ(w / 2.0, -d / 2.0, 0.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.0, 0.0),
new XYZ(-w / 2.0, d / 2.0, 0.0),
new XYZ(-w / 2.0, -d / 2.0, 0.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 = ; i < nVerts; ++i)
        {
            Line line = _rvtApp.Create.NewLineBound(pts[i], pts[i + ]);
            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();
        bool bIsSolid = true;
        Extrusion pSolid = doc.FamilyCreate.NewExtrusion(bIsSolid, pProfile, pSketchPlane, dHeight);
        return pSolid;
    }
}
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 > )
        {  // we should have only one with the given name. 
            return elems[];
        }         // cannot find it.
        return null;
    }
    // =============================================================
    //   helper function: find a planar face with the given normal
    // =============================================================
    public static PlanarFace findFace(Extrusion pBox, XYZ normal)
    {
        // get the geometry object of the given element
        //
        Options op = new Options();
        op.ComputeReferences = true;
        GeometryObjectArray geomObjs = pBox.get_Geometry(op).Objects;         // loop through the array and find a face with the given normal
        //
        foreach (GeometryObject geomObj in geomObjs)
        {
            if (geomObj is Solid)  // solid is what we are interested in.
            {
                Solid pSolid = geomObj as Solid;
                FaceArray faces = pSolid.Faces;
                foreach (Face pFace in faces)
                {
                    PlanarFace pPlanarFace = (PlanarFace)pFace;
                    if ((pPlanarFace != null) && pPlanarFace.Normal.IsAlmostEqualTo(normal)) // we found the face
                    {
                        return pPlanarFace;
                    }
                }
            }
            // will come back later as needed.
            //
            //else if (geomObj is Instance)
            //{
            //}
            //else if (geomObj is Curve)
            //{
            //}
            //else if (geomObj is Mesh)
            //{
            //}
        }         // if we come here, we did not find any.
        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-add-alignment.html

Revit Family API 添加对齐的更多相关文章

  1. Revit Family API 添加类型

    FamilyManager.NewType("");添加新类型,然后设置参数,就是为新类型设置参数. [TransactionAttribute(Autodesk.Revit.At ...

  2. Revit Family API 添加参数与尺寸标注

    使用FamilyManager其他的与普通添加参数与标注没区别. [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Man ...

  3. Revit Family API 添加几何实体

    先创建一个封闭曲线createProfileLShape();再创建实体,这里需要手工画一个参考平面; ; i < nVerts; ++i)        {            Line l ...

  4. Revit Family API 添加材质参数设置可见性

    start //添加类型 void AddType(FamilyManager familyMgr, string name, double w, double d) {     FamilyType ...

  5. Revit Family API 创建参考平面

    使用API来编辑族时,使用doc.FamilyCreate.NewReferencePlane();创建参考平面. )         {  ];         }         // canno ...

  6. Revit 2015 API 的全部变化和新功能

    这里从SDK的文章中摘录出全部的API变化.主要是希望用户用搜索引擎时能找到相关信息: Major changes and renovations to the Revit API APIchange ...

  7. 工作流JBPM_day01:3-使用JBPM的API添加与执行流程

    工作流JBPM_day01:3-使用JBPM的API添加与执行流程 流程定义画完得到压缩文件--->部署流程定义-->启动流程实例-->查询我的个人任务列表-->办理任务--& ...

  8. 为IIS Host ASP.NET Web Api添加Owin Middleware

    将OWIN App部署在IIS上 要想将Owin App部署在IIS上,只添加Package:Microsoft.OWIN.Host.SystemWeb包即可.它提供了所有Owin配置,Middlew ...

  9. Bing必应地图中国API - 添加实时交通信息

    Bing必应地图中国API - 添加实时交通信息 2011-05-24 14:44:58|  分类: Bing&Google|字号 订阅     2009年4月23日,微软必应地图中国API新 ...

随机推荐

  1. 全国大学API接口分享

    1.获取省份列表: http://119.29.166.254:9090/api/provinces 返回的是省份列表,其中id很重要. 2.通过省份id查询省份城市: http://119.29.1 ...

  2. Git 使用规范流程【转】

    转自:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html 作者: 阮一峰 日期: 2015年8月 5日 团队开发中,遵循一个合理.清 ...

  3. 由time.tzname返回值引发的对str、bytes转换时编码问题实践

    Windows 10家庭中文版,Python 3.6.4, 下午复习了一下time模块,熟悉一下其中的各种时间格式的转换:时间戳浮点数.struct_tm.字符串,还算顺利. 可是,测试其中的time ...

  4. Nginx安装方式探究

    Ubuntu 16.04(阿里云ECS),Nginx 1.10.3 (Ubuntu) 本文探究两种安装方式: 1.源码安装(手动) 2.APT安装(自动) 源码安装(手动) 步骤简介: 下载.解压.. ...

  5. ***文件上传控件bootstrap-fileinput的使用和参数配置说明

    特别注意:    引入所需文件后页面刷新查看样式奇怪,浏览器提示错误等,可能是因为js.css文件的引用顺序问题,zh.js需要在fileinput.js后面引入.bootstrap最好在filein ...

  6. 前端如何在h5页面调用微信支付?

    在微信服务号开发的时候经常会遇到微信支付的功能实现,通过实际经验自己总结了一下,前端在H5页面调起微信支付有两种办法,一是利用内置对象,二是通过引用微信的js sdk,亲测都能支付成功,从写法上来看用 ...

  7. Java编程的逻辑 (19) - 接口的本质

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  8. Java编程的逻辑 (27) - 剖析包装类 (中)

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  9. 下载vc++运行库

    之前下载vc++运行库都是百度,从中关村.当下等软件网站下载,但是最近这些网站涉及到安全问题,所以从官网下载比较合适 微软官网-中文 在搜索中 搜索vc++2010/2015等,搜索结果中找到xxxx ...

  10. MySQL子查询,派生表和通用表达式

    一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...