Hi Jeremy,
I have a single pipe, suppose pipe lenght is some 5000mm, i have to divede that pipe exactly at 1000mm, and i have to insert coupling over there.
To inset coupling it needs 2 connectors. I am trying with the below code. If i have 2 parallel pipes then its adding coupling over.
I have just edited your code, as you suggested and shared in previous link.
Could you please guide me how to do the same over single pipe. And i have to divide the pipe exactly at 1000mm.
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
Autodesk.Revit.Creation.Document creDoc = doc.Create;
// Select all pipes in the entire model.
List<Pipe> pipes = new List<Pipe>(new FilteredElementCollector(doc).OfClass(typeof(Pipe)).ToElements().Cast<Pipe>());
int n = pipes.Count;
// If there are less than two,
// there is nothing we can do.
if (2 > n)
{
message = _prompt;
return Result.Failed;
}
// If there are exactly two, pick those.
if (2 < n)
{
// Else, check for a pre-selection.
pipes.Clear();
Selection sel = uidoc.Selection;
n = sel.Elements.Size;
TaskDialog.Show("{0} pre-selected elements.","");
// If two or more model pipes were pre-
// selected, use the first two encountered.
if (1 < n)
{
foreach (Element e in sel.Elements)
{
Pipe c = e as Pipe;
if (null != c)
{
pipes.Add(c);
if (2 == pipes.Count)
{
Console.WriteLine("Found two model pipes, "+ "ignoring everything else.");
break;
}
}
}
}
// Else, prompt for an
// interactive post-selection.
if (2 != pipes.Count)
{
pipes.Clear();
try
{
Reference r = sel.PickObject(ObjectType.Element,new PipeElementSelectionFilter(),"Please pick first pipe.");
pipes.Add(doc.GetElement(r.ElementId)as Pipe);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Cancelled;
}
try
{
Reference r = sel.PickObject(ObjectType.Element,new PipeElementSelectionFilter(),"Please pick second pipe.");
pipes.Add(doc.GetElement(r.ElementId)as Pipe);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Cancelled;
}
}
}
// Extract data from the two selected pipes.
Curve c0 = (pipes[0].Location as LocationCurve).Curve;
Curve c1 = (pipes[1].Location as LocationCurve).Curve;
if (!(c0 is Line) || !(c1 is Line))
{
message = _prompt+" Expected straight pipes.";
return Result.Failed;
}
XYZ p00 = c0.GetEndPoint(0);
XYZ p01 = c0.GetEndPoint(1);
XYZ p10 = c1.GetEndPoint(0);
XYZ p11 = c1.GetEndPoint(1);
XYZ v0 = p01 - p00;
XYZ v1 = p11 - p10;
// Select the two pipe endpoints that are
// farthest apart.
XYZ p0 = p00.DistanceTo(p10) > p01.DistanceTo(p10)? p00: p01;
XYZ p1 = p10.DistanceTo(p0) > p11.DistanceTo(p0)? p10: p11;
XYZ pm = 0.5 * (p0 + p1);
XYZ v = p1 - p0;
String len = null;
len = pipes[0].get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsValueString();
TaskDialog.Show("revit", len);
XYZ z = v.CrossProduct(v1);
XYZ w = z.CrossProduct(v1).Normalize();
// Offset distance perpendicular to pipe direction
double distanceAcross = Math.Abs(v.DotProduct(w));
// Distance between endpoints parallel
// to pipe direction
double distanceAlong = Math.Abs(v.DotProduct(v1.Normalize()));
double angle = 45 * Math.PI / 180.0;
// The angle on the other side.
double angle2 = 0.5 * Math.PI - angle;
double length = distanceAcross * Math.Tan(angle2);
double halfLength = 0.5 * length;
// How long should the pipe stubs become?
double remainingPipeLength= 0.5 * (distanceAlong - length);
if (0 > v1.DotProduct(v))
{
v1.Negate();
}
v1 = v1.Normalize();
XYZ q0 = p0 + remainingPipeLength * v1;
XYZ q1 = p1 - remainingPipeLength * v1;
using (Transaction tx = new Transaction(doc))
{
tx.Start("Rolling Offset");
//// Trim or extend existing pipes
(pipes[0].Locationas LocationCurve).Curve= Line.CreateBound(p0, q0);
(pipes[1].Locationas LocationCurve).Curve=Line.CreateBound(p1, q1);
Connector pipe_start = null;
Connector pipe_end = null;
double dist = double.MaxValue;
foreach (Connector c in pipes[0].ConnectorManager.Connectors)
{
XYZ p = c.Origin;
double d = p.DistanceTo(q0);
if (d < dist)
{
dist = d;
pipe_start = c;
}
break;
}
foreach (Connector conn in pipes[1].ConnectorManager.Connectors)
{
if (conn.Origin.IsAlmostEqualTo(q1))
{
pipe_end = conn;
}
}
FamilyInstance takeoff = creDoc.NewUnionFitting(pipe2_start, pipe2_end);
tx.Commit();
}
}
return Result.Succeeded;
}
}