[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.NoCommandData)]
internal class Class8 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uIDocument = commandData.Application.ActiveUIDocument;
TwoDuctConnection(uIDocument, uIDocument.Document);
return Result.Succeeded;
}
/// <summary>
/// 两根风管连接,循环执行直到按下ESC键
/// </summary>
/// <param name="uIDocument"></param>
/// <param name="revitDoc"></param>
/// <returns></returns>
private bool TwoDuctConnection(UIDocument uIDocument, Document revitDoc)
{
try
{
IList<Element> ducks = uIDocument.Selection.PickElementsByRectangle(new DuckFilter(), "选择两跟风管");
double minDis = double.MaxValue;
Connector connector = null;
Connector connector1 = null;
foreach (Connector item in (ducks[0] as Duct).ConnectorManager.Connectors)
{
foreach (Connector item1 in (ducks[1] as Duct).ConnectorManager.Connectors)
{
double dis = item.Origin.DistanceTo(item1.Origin);
if (dis < minDis)
{
minDis = dis;
connector = item;
connector1 = item1;
}
}
}
if (connector != null && connector1 != null)
{
using (Transaction tr = new Transaction(revitDoc, "链接风管"))
{
tr.Start();
revitDoc.Create.NewElbowFitting(connector1, connector);
tr.Commit();
}
}
TwoDuctConnection(uIDocument, revitDoc);
}
catch (Exception)
{
return false;
}
return true;
}
}
/// <summary>
/// 风管过滤器
/// </summary>
public class DuckFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if (elem.Category.Name == "风管")
{
return true;
}
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
}
revit 二次开发两个风管连接
于 2024-06-21 11:45:49 首次发布