Try this and tell me if it works as a charm:
#region Namespaces using System; using System.Text; using System.Linq; using System.Xml; using System.Reflection; using System.ComponentModel; using System.Collections; using System.Collections.Generic; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Forms; using System.IO; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Events; using Autodesk.Revit.DB.Architecture; using Autodesk.Revit.DB.Structure; using Autodesk.Revit.DB.Mechanical; using Autodesk.Revit.DB.Electrical; using Autodesk.Revit.DB.Plumbing; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.UI.Events; //using Autodesk.Revit.Collections; using Autodesk.Revit.Exceptions; using Autodesk.Revit.Utility; using RvtApplication = Autodesk.Revit.ApplicationServices.Application; using RvtDocument = Autodesk.Revit.DB.Document; using System.Diagnostics; #endregion namespace RevitAddinCS2 { [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class ExtCmd : IExternalCommand { #region Cached Variables private static ExternalCommandData _cachedCmdData; public static UIApplication CachedUiApp { get { return _cachedCmdData.Application; } } public static RvtApplication CachedApp { get { return CachedUiApp.Application; } } public static RvtDocument CachedDoc { get { return CachedUiApp.ActiveUIDocument.Document; } } #endregion #region IExternalCommand Members public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet) { _cachedCmdData = cmdData; InsertPipeCap("Cap - PVC - Sch 40"); return Result.Succeeded; } public void InsertPipeCap(string FamilyName) { FamilySymbol fS = null; FilteredElementCollector collector = new FilteredElementCollector(CachedDoc); collector.OfCategory(BuiltInCategory.OST_PipeFitting).OfClass(typeof(FamilySymbol)); FamilySymbol placeholder = collector.Cast<FamilySymbol>().Where(p => p.FamilyName == FamilyName).FirstOrDefault(); if (placeholder != null) { fS = placeholder; } Reference r = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "select a pipe"); Element el = CachedDoc.GetElement(r); Pipe pipe = el as Pipe; LocationCurve lC = pipe.Location as LocationCurve; Curve cu = lC.Curve; XYZ startPoint = cu.GetEndPoint(0); Options opt = new Options(); opt.ComputeReferences = true; opt.DetailLevel = ViewDetailLevel.Fine; GeometryElement e = pipe.get_Geometry(opt); Face normalFace = null; double dist = double.MaxValue; foreach (GeometryObject obj in e) { Solid solid = obj as Solid; if (solid != null && solid.Faces.Size > 0) { foreach (Face face in solid.Faces) { PlanarFace pf = face as PlanarFace; if (pf != null) { if (pf.Origin.DistanceTo(startPoint) < dist) { normalFace = pf; dist = pf.Origin.DistanceTo(startPoint); } } } } } Connector CapConnector = null; using (Transaction t = new Transaction(CachedDoc)) { t.Start("insert fixture"); using (SubTransaction sb = new SubTransaction(CachedDoc)) { sb.Start(); fS.Activate(); FamilyInstance fi = CachedDoc.Create.NewFamilyInstance(startPoint, fS, normalFace.ComputeNormal(new UV(0.5, 0.5)), CachedDoc.ActiveView.GenLevel, StructuralType.NonStructural); ConnectorManager cm = fi.MEPModel.ConnectorManager; ConnectorSet fCS = cm.Connectors; foreach (Connector c in fCS) { if (c != null) { CapConnector = c; break; } } sb.Commit(); } Connector PipeConnector = null; using (SubTransaction sb = new SubTransaction(CachedDoc)) { sb.Start(); XYZ fCP = CapConnector.Origin; ConnectorSet pCS = pipe.ConnectorManager.Connectors; foreach (Connector c in pCS) { if (c.Origin.DistanceTo(fCP) <= dist) { dist = c.Origin.DistanceTo(fCP); PipeConnector = c; } } CapConnector.Radius = PipeConnector.Radius; sb.Commit(); } using (SubTransaction sb = new SubTransaction(CachedDoc)) { sb.Start(); CapConnector.ConnectTo(PipeConnector); ConnectorSet cs = CapConnector.AllRefs; sb.Commit(); } t.Commit(); } } #endregion } }
If you still have problems just send me the RVT with the family that causing the problem.
If this reply satisfies your need mark it as a solution.
This is my Result: