Snap to nearest right angle breaks connectivity, "Family is connected.." error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone,
I am trying to create an add-in that would allow me to have a selected pipe snap to the nearest 90 degree point. Presently, the pipes appear to be snapping into place, based on the nearest base(right) angle and the connector adjusts graphically, but not without breaking connectivity and displaying a “Family is connected in a network and can no longer keep the connectivity” error.
Manually adjusting a pipe by dragging one end in the Revit UI does not cause any problems and so there is something else I am missing. Any input would be appreciated.
Thank you,
Charlton
CODE:
usingAutodesk.Revit.UI;
usingAutodesk.Revit.DB;
usingAutodesk.Revit.UI.Selection;
usingAutodesk.Revit.Attributes;
usingAutodesk.Revit.DB.Plumbing;
usingSystem.Collections.Generic;
usingSystem;
usingSystem.Linq;
namespaceVerticalPipes
{
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
publicclassVerticalPipes : IExternalCommand
{
privateconstdoubleANGLE_TOLERANCE=Math.PI/4;
//private const double ANGLE_TOLERANCE = 9999;
publicResultExecute(ExternalCommandDatacommandData, refstringmessage, ElementSetelements)
{
UIApplicationuiApp=commandData.Application;
Documentdoc=uiApp.ActiveUIDocument.Document;
UIDocumentuidoc=newUIDocument(doc);
Selectionchoices=uidoc.Selection;
IEnumerable<Element>SelectedElements=null;
if (!choices.Elements.Cast<Element>().Any(elem=> (elemisPipe) ||
(elem.Category.Name.Contains("Pipe"))))
{
try
{
SelectedElements=choices.PickObjects(ObjectType.Element, newElementIsPipeSelectionFilter(),"Select Pipes to rotate").Select(a=>doc.GetElement(a.ElementId));
}
catch(Autodesk.Revit.Exceptions.OperationCanceledExceptione)
{
returnAutodesk.Revit.UI.Result.Cancelled;
}
}
else
SelectedElements=choices.GetElementIds().Select(a=>doc.GetElement(a));
if (SelectedElements==null) returnAutodesk.Revit.UI.Result.Cancelled;;
Transactiontrans=newTransaction(doc);
trans.Start("Align");
foreach (ElementeleinSelectedElements)
{
LocationCurvepipeLocation=ele.LocationasLocationCurve;
if (pipeLocation==null)
{
continue;
}
else
{
varpoint1=pipeLocation.Curve.GetEndPoint(0);
XYZpoint2=pipeLocation.Curve.GetEndPoint(1);
varangle=Math.Atan2(point2.Y-point1.Y, point2.X-point1.X);
double []baseAngle=newdouble[]{0, Math.PI/2, Math.PI, Math.PI*1.5, Math.PI*2};
doubledistance=0;
doubleminAngle=double.MaxValue;
intminIndex=0;
for(inti=0; i<5; i++){
distance=Math.Abs(angle-baseAngle[i]);
if (distance<minAngle)
{
minAngle=distance;
minIndex=i;
}
}
if (minAngle<ANGLE_TOLERANCE)
{
varpoint=newXYZ(point1.X+pipeLocation.Curve.Length*Math.Cos(baseAngle[minIndex]), point1.Y+pipeLocation.Curve.Length*Math.Sin(baseAngle[minIndex]), point2.Z);
pipeLocation.Curve=Line.CreateBound(point1, point);
}
}
}
trans.Commit();
returnResult.Succeeded;
}
}
}
namespaceVerticalPipes
{
classElementIsPipeSelectionFilter : Autodesk.Revit.UI.Selection.ISelectionFilter
{
publicboolAllowElement(Autodesk.Revit.DB.Elementelem)
{
return ((elemisPipe) ||
(elem.Category.Name.Contains("Pipe")));
}
publicboolAllowReference(Autodesk.Revit.DB.Referencereference, Autodesk.Revit.DB.XYZposition)
{
returnfalse;
}
}
}