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

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(000);
        XYZ xyz2 
= new XYZ(10000);
        Line line1 
= Line.get_Bound(xyz1, xyz2);

        XYZ xyz3 
= new XYZ(-10-100);
        XYZ xyz4 
= new XYZ(1001000);
        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(000);
            XYZ p2 
= new XYZ(1000);
            pipe 
= document.Create.NewPipe(p1, p2, pipeType);
        }
        
return pipe;
    }
}
posted @ 2011-03-16 13:32  大气象  阅读(4776)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com