[Transaction(TransactionMode.Manual)]
public class BreakMEPCurveCmd : IExternalCommand
{
UIDocument uIDocument = null;
Document document = null;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uIApp = commandData.Application;
uIDocument = uIApp.ActiveUIDocument;
document = uIDocument.Document;
}
}
创建外部事件实例
ExternalEventExample externalEventExample = new ExternalEventExample(commandData);
注册事件
ExternalEvent externalEvent = ExternalEvent.Create(externalEventExample);
创建窗体对象
BreakMEPCurveForm breakMEPCurveForm = new BreakMEPCurveForm(this,externalEvent);
打开窗体
breakMEPCurveForm.Show();
return Result.Succeeded;
外部事件
public class ExternalEventExample : IExternalEventHandler
{
UIDocument uIDocument = null;
Document document = null;
public ExternalEventExample(ExternalCommandData commandData)
{
UIApplication uIApp = commandData.Application;
uIDocument = uIApp.ActiveUIDocument;
document = uIDocument.Document;
}
public static int id = -1;
public void Execute(UIApplication app)
{
if (id == 0)
{
BreakMEPCurve();
}
else if (id == 1)
{
BreakMEPCurves();
}
}
public string GetName()
{
return "name";
}
一个点打断
public MEPCurve BreakMEPCurve()
{
try
{
Reference reference = uIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new MEPCurveFilter());
MEPCurve mepCurve = document.GetElement(reference) as MEPCurve;
XYZ breakPoint = reference.GlobalPoint;
MEPCurve mepCurveCopy = null;
using (Transaction trans = new Transaction(document, "tran"))
{
trans.Start();
ICollection<ElementId> ids = ElementTransformUtils.CopyElement(document, mepCurve.Id, new XYZ(0, 0, 0));
ElementId id = ids.FirstOrDefault();
mepCurveCopy = document.GetElement(id) as MEPCurve;
Curve curve = (mepCurve.Location as LocationCurve).Curve;
XYZ startPoint = curve.GetEndPoint(0);
XYZ endPoint = curve.GetEndPoint(1);
breakPoint = curve.Project(breakPoint).XYZPoint;
Line line1 = Line.CreateBound(startPoint, breakPoint);
Line line2 = Line.CreateBound(breakPoint, endPoint);
ConnectorSet connectorSets = mepCurve.ConnectorManager.Connectors;
Connector connector = null;
foreach (Connector item in connectorSets)
{
bool isBreak = false;
if (item.Id == 1 && item.IsConnected)
{
foreach (Connector item2 in item.AllRefs)
{
if (item2.Owner is FamilyInstance)
{
item.DisconnectFrom(item2);
connector = item2;
isBreak = true;
break;
}
}
}
if (isBreak)
{
break;
}
}
if (connector != null)
{
foreach (Connector item in mepCurveCopy.ConnectorManager.Connectors)
{
if (item.Id == 1)
{
item.ConnectTo(connector);
}
}
}
(mepCurve.Location as LocationCurve).Curve = line1;
(mepCurveCopy.Location as LocationCurve).Curve = line2;
trans.Commit();
}
return mepCurveCopy;
}
catch (Exception ex)
{
TaskDialog.Show("错误", ex.Message);
}
return null;
}
两个点 打断
public MEPCurve BreakMEPCurves()
{
try
{
Reference reference = uIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new MEPCurveFilter());
MEPCurve mEPCurve = document.GetElement(reference) as MEPCurve;
XYZ breakPoint1 = reference.GlobalPoint;
XYZ breakPoint2 = uIDocument.Selection.PickPoint();
MEPCurve mEPCurvecCopy1 = null;
using (Transaction transaction = new Transaction(document,"tran"))
{
transaction.Start();
ICollection<ElementId> ids1 = ElementTransformUtils.CopyElement(document, mEPCurve.Id, new XYZ(0, 0, 0));
ElementId id1 = ids1.FirstOrDefault();
mEPCurvecCopy1 = document.GetElement(id1) as MEPCurve;
Curve curve = (mEPCurve.Location as LocationCurve).Curve;
XYZ startPoint = curve.GetEndPoint(0);
XYZ endPoint = curve.GetEndPoint(1);
breakPoint1 = curve.Project(breakPoint1).XYZPoint;
breakPoint2 = curve.Project(breakPoint2).XYZPoint;
Connector connector = null;
foreach (Connector item in mEPCurve.ConnectorManager.Connectors)
{
bool isBreak = false;
if (item.Id == 1 && item.IsConnected)
{
foreach (Connector item2 in item.AllRefs)
{
if (item2.Owner is FamilyInstance)
{
item.DisconnectFrom(item2);
connector = item2;
isBreak = true;
break;
}
}
}
if (isBreak)
{
break;
}
}
if (connector != null)
{
foreach (Connector item in mEPCurvecCopy1.ConnectorManager.Connectors)
{
if (item.Id == 1)
{
item.ConnectTo(connector);
break;
}
}
}
if (startPoint.DistanceTo(breakPoint1) > startPoint.DistanceTo(breakPoint2))
{
XYZ item = breakPoint1;
breakPoint1 = breakPoint2;
breakPoint2 = item;
}
Line line1 = Line.CreateBound(startPoint, breakPoint1);
(mEPCurve.Location as LocationCurve).Curve = line1;
Line line2 = Line.CreateBound(breakPoint2, endPoint);
(mEPCurvecCopy1.Location as LocationCurve).Curve = line2;
transaction.Commit();
}
return mEPCurvecCopy1;
}
catch (Exception ex)
{
TaskDialog.Show("错误", ex.Message);
}
return null;
}
}
WPF(BreakMEPCurveForm.xaml)
public partial class BreakMEPCurveForm : Window
{
BreakMEPCurveCmd breakMEPCurveCmd = null;
ExternalEvent externalEvent = null;
public BreakMEPCurveForm(BreakMEPCurveCmd breakMEPCurveCmd,ExternalEvent externalEvent)
{
this.breakMEPCurveCmd = breakMEPCurveCmd;
this.externalEvent = externalEvent;
InitializeComponent();
this.Topmost = true;
}
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
ExternalEventExample.id = 0;
externalEvent.Raise();
}
private void RadioButton_Click_1(object sender, RoutedEventArgs e)
{
ExternalEventExample.id = 1;
externalEvent.Raise();
}
}