Revit提供特殊的类和集合来完成这些操作,积累一下。

    /// <summary>
    /// Utility method for getting the intersection between two lines.
    /// </summary>
    /// <param name="line1">The first line.</param>
    /// <param name="line2">The second line.</param>
    /// <returns>The intersection point.</returns>
    /// <exception cref="InvalidOperationException">Thrown when an intersection can't be found.</exception>
    private Autodesk.Revit.DB.XYZ GetIntersection(Line line1, Line line2)
    {
        IntersectionResultArray results;//交点数组
        Autodesk.Revit.DB.SetComparisonResult result = line1.Intersect(line2, out results);

if (result != Autodesk.Revit.DB.SetComparisonResult.Overlap)//重叠,没有重叠就是平行
            throw new InvalidOperationException("Input lines did not intersect.");

if (results == null || results.Size != 1)//没有交点或者交点不是1个
            throw new InvalidOperationException("Could not extract intersection point for lines.");

IntersectionResult iResult = results.get_Item(0);//取得交点
        Autodesk.Revit.DB.XYZ intersectionPoint = iResult.XYZPoint;//取得交点坐标

return intersectionPoint;
    }

实例:CreateTruss

//[Transaction(TransactionMode.Automatic)]
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
//[Journaling(JournalingMode.NoCommandData)]
public class NewPipeCommand : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;
        //CreateNewPipe(doc);

//线段交点
        XYZ xyz1 = new XYZ(0, 0, 0);
        XYZ xyz2 = new XYZ(100, 0, 0);
        Line line1 = Line.get_Bound(xyz1, xyz2);

XYZ xyz3 = new XYZ(-10, -10, 0);
        XYZ xyz4 = new XYZ(100, 100, 0);
        Line line2 = Line.get_Bound(xyz3, xyz4);
        XYZ xyz = null;
        //XYZ xyz = IntersectionPoint(line1, line2);

//MessageBox.Show(xyz.X + "," + xyz.Y + "," + xyz.Z);

//直线交点,按说应该相交在(0,0,0)但却是(2.8,0,0)
        Curve curve1 = Line.get_Bound(xyz1, xyz2) as Curve;
        Curve curve2 = Line.get_Bound(xyz3, xyz4) as Curve;
        curve1.MakeUnbound();//unbound绑定
        curve2.MakeUnbound();
        xyz = IntersectionPoint(curve1, curve2);

MessageBox.Show(xyz.X + "," + xyz.Y + "," + xyz.Z);

return Result.Succeeded;
    }
    //取得两条线段的交点,直线延长线上的相交不算。
    public XYZ IntersectionPoint(Line line1, Line line2)
    {
        IntersectionResultArray intersectionR = new IntersectionResultArray();
        SetComparisonResult comparisonR;
        comparisonR = line1.Intersect(line2, out intersectionR);
        XYZ intersectionResult = null;
        if (SetComparisonResult.Disjoint != comparisonR)//Disjoint不交
        {
            if (!intersectionR.IsEmpty)//两条直线如果重复为一条直线,这里也为空
            {
                intersectionResult = intersectionR.get_Item(0).XYZPoint;
            }
        }
        return intersectionResult;
    }
    //取得两条直线的交点
    public XYZ IntersectionPoint(Curve curve1, Curve curve2)
    {
        IntersectionResultArray intersectionR = new IntersectionResultArray();
        SetComparisonResult comparisonR;
        comparisonR = curve1.Intersect(curve2, out intersectionR);
        XYZ intersectionResult = null;
        if (SetComparisonResult.Disjoint != comparisonR)//Disjoint不交
        {
            if (!intersectionR.IsEmpty)//两条直线如果重复为一条直线,这里也为空
            {
                intersectionResult = intersectionR.get_Item(0).XYZPoint;
            }
        }
        return intersectionResult;
    }

public Pipe CreateNewPipe(Document document)
    {
        FilteredElementCollector collector = new FilteredElementCollector(document);
        collector.OfClass(typeof(PipeType));
        PipeType pipeType = collector.FirstElement() as PipeType;

Pipe pipe = null;
        if (null != pipeType)
        {
            //create pipe between 2 points
            XYZ p1 = new XYZ(0, 0, 0);
            XYZ p2 = new XYZ(10, 0, 0);
            pipe = document.Create.NewPipe(p1, p2, pipeType);
        }
        return pipe;
    }
}

Revit 2011 二次开发之“取得两条直线的交点”相关推荐

  1. Java黑皮书课后题第8章:*8.31(几何:交点)编写一个方法,返回两条直线的交点。四个点存放在4*2的二维数组points中。编写一个程序,提示用户输入4个点,并显示交点

    *8.31(几何:交点)编写一个方法,返回两条直线的交点.四个点存放在4*2的二维数组points中.编写一个程序,提示用户输入4个点,并显示交点 题目 题目描述 破题 代码 本题运行实例 题目 题目 ...

  2. 两条直线求交点c语言,C§ 3.3.1两条直线的交点坐标(5页)-原创力文档

    § 3.1两条直线的交点坐标 学习目标 1.掌握判断两直线相交的方法:会求两直线交点坐标: 2.体会判断两直线相交中的数形结合思想. 学习过程 一.课前准备: (预习教材P112~ P114,找出疑 ...

  3. java两条直线交点_求两条直线的交点坐标

    看UdaCity机器学习大纲,发现入门班中有一条:编写算法计算一组直线或平面的交点. 遂从最简单的直角坐标系两条直线的交点开始, 直线1的方程解析式: 2x-y=0; 直线2的方程解析式: 4x-5y ...

  4. JAVA计算两条直线的交点(判断两条线段相交)

    //计算两条直线的交点 //输入四个点,前两个点确定线段1,后两个点确定线段2 //输出交点坐标 public class Main{public static void main(String ar ...

  5. opencv求解两条直线的交点

    假设现在有一个点集,需要拟合出最能够表达点集轮廓的几条直线,并求直线之间的交点. 从点集中拟合直线可以采用的方法:随机抽样一致性(RANSAC),霍夫变换(though transform) 思路1 ...

  6. 求空间中两条直线的交点

    求空间中两条直线的交点 求交点坐标的问题完全可以用数学的方法求得结果的表达公式来解决啊 设P1(x1,y1,z1),P2(x2,y2,z2) L2:P3(x3,y3,z3),P4(x4,y4,z4) ...

  7. 用矢量变换的方法求两条直线的交点

    求两条直线的交点,最常见的写法是列出两条直线的方程,联立求解.     但这种办法的弊端很大:     1 )算法是坐标系相关的,要考虑直线是水平还是垂直,写出很多判断条件,增加了程序的不稳定性    ...

  8. 两条直线的交点 c语言,计算两条直线的交点(C#)

    PS:从其他地方看到的源码是有问题的.下面是修正后的 /// /// 计算两条直线的交点 /// /// L1的点1坐标 /// L1的点2坐标 /// L2的点1坐标 /// L2的点2坐标 /// ...

  9. C#CAD二次开发学习 两条直线求交点的方法IntersectWith的一点整理

    求两条直线交点时可以使用IntersectWith方法,但对于我这种初学者有点摸不清该怎么使用. 直接上代码吧 [CommandMethod("GTest")]public sta ...

最新文章

  1. hdu 2028 Lowest Common Multiple Plus
  2. 紧急求助!配置SMTP插件出错,SMTP connect() failed
  3. js进阶 12-8 如何知道鼠标和键盘当前操作的是哪个键
  4. linux java socket编程_linux下java程序与C语言程序通过SOCKET通信的简单例子
  5. 吐血整理!10 个机器学习教程汇总,爱可可推荐!
  6. 使用 QT 时遇到的问题及解决办法
  7. 图片适应窗口_在word中插入图片,如何避免失真模糊?实用文档建议收藏
  8. oracle的dtime,Oracle时间函数
  9. 《魔鬼搭讪学》读书笔记
  10. 基于simulink的PN码同步的性能仿真
  11. android 移动国家代码,Android-imsi-MCC-MNC-国家码
  12. 一些方便的LaTex在线编辑工具
  13. 【产品功能】弹性网卡支持私网多IP
  14. php的strtoupper,关于PHP的strtoupper函数
  15. 0.进校的第一张Excel表:“住宿分布表” ——《Excel“智能化”之路》 系列文章
  16. 部署Openstack-train版
  17. html中p元素添加超链接_如何在HTML中添加超链接
  18. 是谁用Python弹奏一曲东风破
  19. C++ 类的静态成员及静态成员函数
  20. Docker安全性(一)——Docker容器真的安全吗?

热门文章

  1. 超级账本 --- ReadWriteSet的逻辑结构
  2. 记录:成功配置 centos + nginx + .net core 2.0
  3. Spring处理跨域请求
  4. spring之初识IocAop
  5. windows/linuxjdk安装,jdk1.6升级到1.7
  6. mysql update case when和where之间的注意事项
  7. Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
  8. VM中的Linux安装jdk和tomcat
  9. [CTO札记]Disney也进军数字出版市场了
  10. 【辨异】inverse, reverse, converse