//风管对齐
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdDuctAlign : IExternalCommand
{
private XYZ GetIntersection(Line line1, Line line2)
{
IntersectionResultArray results;
SetComparisonResult result
= line1.Intersect(line2, out results);
//SetComparisonResult.Subset
//SetComparisonResult.Superset
if (SetComparisonResult.Overlap == result)//没有交点,可能是平行,也可以是延长线相交。
throw new InvalidOperationException(
"Overlap");
if (SetComparisonResult.Disjoint == result)//不相交
throw new InvalidOperationException(
"Disjoint");
if (SetComparisonResult.Superset == result)//包含,子集
throw new InvalidOperationException(
"Superset");
if (SetComparisonResult.Subset == result)//交集
throw new InvalidOperationException(
"Subset");
if (results == null || results.Size != 1)
throw new InvalidOperationException(
"Could not extract line intersection point.");
IntersectionResult iResult
= results.get_Item(0);
return iResult.XYZPoint;
}
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Selection sel = app.ActiveUIDocument.Selection;
SplitButtonData splitButtonData = new SplitButtonData("", "");
PushButton pbtn = new PushButton();
RibbonPanel rpanel = new RibbonPanel();
Transaction ts = new Transaction(doc, "revit");
ts.Start();
IList<Reference> refDucts = sel.PickObjects(ObjectType.Element, "duct");
Duct duct1 = doc.GetElement(refDucts.ElementAt(0)) as Duct;
Duct duct2 = doc.GetElement(refDucts.ElementAt(1)) as Duct;
LocationCurve lCurve1 = duct1.Location as LocationCurve;
LocationCurve lCurve2 = duct1.Location as LocationCurve;
XYZ xyz11 = lCurve1.Curve.get_EndPoint(0);
XYZ xyz12 = lCurve1.Curve.get_EndPoint(1);
XYZ xyz21 = lCurve2.Curve.get_EndPoint(0);
XYZ xyz22 = lCurve2.Curve.get_EndPoint(1);
//判断线段相交关系
Line line1 = Line.get_Bound(xyz11, xyz12);
Line line2 = Line.get_Bound(xyz21, xyz22);
GetIntersection(line1, line2);
#region 风管移动
//转化到平面
XYZ xyz1 = new XYZ(xyz11.X, xyz11.Y, 0);
XYZ xyz2 = new XYZ(xyz12.X, xyz12.Y, 0);
XYZ xyz3 = new XYZ(xyz21.X, xyz21.Y, 0);
XYZ xyz4 = new XYZ(xyz22.X, xyz22.Y, 0);
//找到与直线垂直的向量
XYZ vec1 = xyz2 - xyz1;
XYZ zVec = new XYZ(0, 0, 1);
XYZ nVec = vec1.CrossProduct(zVec).Normalize();//两条线相交的面对应的向量
TaskDialog.Show("vec", nVec.CrossProduct(zVec).Normalize().ToString());
lCurve2.Move(nVec);
#endregion
ts.Commit();
return Result.Succeeded;
}
}
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdDuctAlign : IExternalCommand
{
private XYZ GetIntersection(Line line1, Line line2)
{
IntersectionResultArray results;
SetComparisonResult result
= line1.Intersect(line2, out results);
//SetComparisonResult.Subset
//SetComparisonResult.Superset
if (SetComparisonResult.Overlap == result)//没有交点,可能是平行,也可以是延长线相交。
throw new InvalidOperationException(
"Overlap");
if (SetComparisonResult.Disjoint == result)//不相交
throw new InvalidOperationException(
"Disjoint");
if (SetComparisonResult.Superset == result)//包含,子集
throw new InvalidOperationException(
"Superset");
if (SetComparisonResult.Subset == result)//交集
throw new InvalidOperationException(
"Subset");
if (results == null || results.Size != 1)
throw new InvalidOperationException(
"Could not extract line intersection point.");
IntersectionResult iResult
= results.get_Item(0);
return iResult.XYZPoint;
}
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Selection sel = app.ActiveUIDocument.Selection;
SplitButtonData splitButtonData = new SplitButtonData("", "");
PushButton pbtn = new PushButton();
RibbonPanel rpanel = new RibbonPanel();
Transaction ts = new Transaction(doc, "revit");
ts.Start();
IList<Reference> refDucts = sel.PickObjects(ObjectType.Element, "duct");
Duct duct1 = doc.GetElement(refDucts.ElementAt(0)) as Duct;
Duct duct2 = doc.GetElement(refDucts.ElementAt(1)) as Duct;
LocationCurve lCurve1 = duct1.Location as LocationCurve;
LocationCurve lCurve2 = duct1.Location as LocationCurve;
XYZ xyz11 = lCurve1.Curve.get_EndPoint(0);
XYZ xyz12 = lCurve1.Curve.get_EndPoint(1);
XYZ xyz21 = lCurve2.Curve.get_EndPoint(0);
XYZ xyz22 = lCurve2.Curve.get_EndPoint(1);
//判断线段相交关系
Line line1 = Line.get_Bound(xyz11, xyz12);
Line line2 = Line.get_Bound(xyz21, xyz22);
GetIntersection(line1, line2);
#region 风管移动
//转化到平面
XYZ xyz1 = new XYZ(xyz11.X, xyz11.Y, 0);
XYZ xyz2 = new XYZ(xyz12.X, xyz12.Y, 0);
XYZ xyz3 = new XYZ(xyz21.X, xyz21.Y, 0);
XYZ xyz4 = new XYZ(xyz22.X, xyz22.Y, 0);
//找到与直线垂直的向量
XYZ vec1 = xyz2 - xyz1;
XYZ zVec = new XYZ(0, 0, 1);
XYZ nVec = vec1.CrossProduct(zVec).Normalize();//两条线相交的面对应的向量
TaskDialog.Show("vec", nVec.CrossProduct(zVec).Normalize().ToString());
lCurve2.Move(nVec);
#endregion
ts.Commit();
return Result.Succeeded;
}
}
参考:
http://revit.haotui.com/thread-171-1-32.html
http://revit.haotui.com/thread-489-1-23.html