RevitAPI选择与某个管道相连的所有管道

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Plumbing;
using System.Collections.Generic;
using System.Linq;

namespace autoMake
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
class PipeFind : IExternalCommand
{
List<ElementId> listId = new List<ElementId>();
List<Pipe> pipes1 = new List<Pipe>(); List<ElementId> pipes1id = new List<ElementId>();
List<ElementId> owner1id = new List<ElementId>();
public ElementId[] final_conn(Document doc, Pipe pipe)
{

List<Connector> connectors = new List<Connector>();
ElementId[] pipeidarr = new ElementId[2];
pipeidarr[0] = default;pipeidarr[1] = default;
MEPCurve mep = pipe as MEPCurve;

ConnectorSetIterator connectorSetIterator = mep.ConnectorManager.Connectors.ForwardIterator();

while (connectorSetIterator.MoveNext())
{
Connector connector = connectorSetIterator.Current as Connector;
if (connector.AllRefs.Size > 0)
{
connectors.Add(connector);
//根据连接件选管道
}

}
//TaskDialog.Show("ConnNum",connectors.Count.ToString());
int k = 0;
foreach (var conn in connectors)
{

Element elem = null;
ConnectorSetIterator connectorSetIterator0 = conn.AllRefs.ForwardIterator();
while (connectorSetIterator0.MoveNext())
{
Connector connref = connectorSetIterator0.Current as Connector;
if (connref.ConnectorType == ConnectorType.Physical||connref.ConnectorType== ConnectorType.End||connref.ConnectorType==ConnectorType.Curve)
{
if (connref.Origin.IsAlmostEqualTo(conn.Origin))
{
elem = connref.Owner;
if (elem.GetType().ToString() == "Autodesk.Revit.DB.FamilyInstance")
{
owner1id.Add(elem.Id);
pipeidarr[k++] = elem.Id;
// TaskDialog.Show("conntype", connref.ConnectorType.ToString());
//TaskDialog.Show("idname", elem.Id.ToString());
}
}
}


}

}
return pipeidarr;
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;

Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "");
Element elem = doc.GetElement(refer);
listId.Add(elem.Id);
Pipe pipe = elem as Pipe;
XYZ xyz = refer.GlobalPoint;
Curve curve = (pipe.Location as LocationCurve).Curve;
XYZ project = curve.Project(xyz).XYZPoint;
/*
var connector = BackPipeNearConnectors(doc,elem);

foreach (var item in connector)
{
BackAlrefsConnectors(doc,item);//打破了两个连接管,但是两个连接管每个只链接一个管道
// GetElementAtConnector(item);
}

uidoc.Selection.SetElementIds(listId);
//uidoc.Selection.SetElementIds(pipes1id);
//TaskDialog.Show("N",connector[0].Id.ToString());
*/
FilteredElementCollector PipesE = new FilteredElementCollector(doc);
ElementClassFilter pipes = new ElementClassFilter(typeof(Pipe));
FilteredElementCollector pipes0 = PipesE.WherePasses(pipes);
int i = 0;int n2 = 0;int n1 = 0;
var op=final_conn(doc,pipe);
TaskDialog.Show("opNu", op.Length.ToString());
foreach (var it in op)
{
TaskDialog.Show("cv",op[i++].ToString());
}

foreach (var item in pipes0)
{
var pitem = item as Pipe;
var op0 = final_conn(doc, pitem);
//TaskDialog.Show("Nu", op0.Length.ToString());
//TaskDialog.Show("NuInt", op0[0].ToString());
// TaskDialog.Show("NuInt", op0[1].ToString());
//break;
foreach (var f0 in op)
{
if (f0!=default)
{
var f1 = f0.IntegerValue;
foreach (var f in op0)
{
if (f!=default)
{
if (f.IntegerValue == f1)
{
n1++;
TaskDialog.Show("fID", f.IntegerValue.ToString());
listId.Add(pitem.Id);
break;
}
}

}
}

}


}
uidoc.Selection.SetElementIds(listId);
TaskDialog.Show("Nu1", n1.ToString());
TaskDialog.Show("Nu2", n2.ToString());
return Result.Succeeded;
}

RevitAPI选择与某个管道相连的所有管道

上一篇:Windows Server 2008 系统加固


下一篇:C#析构函数(方法)