阀门法兰
/*以下为拾取阀门和法兰,将他们对齐
Reference refer1 = uiDoc.Selection.PickObject(ObjectType.Element);
Reference refer2 = uiDoc.Selection.PickObject(ObjectType.Element);
Element elem1 = doc.GetElement(refer1);
Element elem2 = doc.GetElement(refer2);
Familyinstance instance1 = elem1 as FamilyInstance;
FamilyInstance instance2 = elem2 as FamilyInstance;
ConnectorSet connSet1 = instance1.MEPModel.ConnectorManager.Connectors;
ConnectorSet connSet2 = instance2.MEPModel.ConnectorManager.Connectors;
double mindistance = double.MaxValue;
Connector connector1 = null;
Connector connector2 = null;
foreach (Connector conn1 in connSet1)
{
foreach (Connector conn2 in connSet2)
{
var dis = conn1.Origin.DistanceTo(conn2.Origin);
if (dis < minDistance)
{
minDistance = dis;
connector1 = conn1;
connector2 = conn2;
}
}
}
if (connector1 != null && connector2 != null)
{
using (transaction trans = new Transaction(doc))
{
trans.Start("Connector two connectors");
//删除中间管段
if (connector1.IsConnected && connector2.IsConnected)
{
ConnectorSet refs1 = connector1.AllRefs;
ConnectorSet refs2 = connector2.AllRefs;
foreach (Connector refCon1 in refs1)
{
foreach (Connector refCon2 in refs2)
{
if (refCon1.Owner != null
&& refCon2.Owner != null
&& refCon1.Owner.Id == refCon2.Owner.Id)
{
TaskDialog.Show("Information", refCon1.Owner.Name + ":" + refCon2.Owner.Name);
doc.Delete(refCon1.Owner.Id);
}
}
}
}
//物理连接
XYZ director = new XYZ(connector1.Origin.X - connector2.Origin.X,
connector1.Origin.Y - connector2.Origin.Y,
connector1.Origin.Z - connector2.Origin.Z);
instance2.Location.Move(director);
//电气连接
connector1.ConnectTo(connector2);
trans.Commit();
}
}