Not the cleanest solution but this seems to work. What I do is iterate through the stable representation "containers" until I find edges. Then i find the edge that is coincident with the plane I want to lock to.
Again, thanks for your suggestions.
var doc = uidoc.Document;
var import_in_view = new FilteredElementCollector(doc)
.OfClass(typeof(ImportInstance))
.OfType<ImportInstance>()
.FirstOrDefault();
var reference_plane = new FilteredElementCollector(doc)
.OfClass(typeof(ReferencePlane))
.OfType<ReferencePlane>()
.FirstOrDefault(plane =>
plane.Name.Equals(
"MullionOffset",
StringComparison.OrdinalIgnoreCase));
var plane_pt1 = reference_plane.FreeEnd.Project(doc.ActiveView);
var plane_pt2 = reference_plane.BubbleEnd.Project(doc.ActiveView);
var plane_line = Line.CreateBound(
plane_pt1,
plane_pt2);
var edge_references = GetGeometryReferences(
import_in_view,
doc);
var cooincident_edge_references = edge_references
.Where(edge_ref =>
edge_ref
.curve
.Intersect(plane_line)
.Equals(SetComparisonResult.Equal));
if (cooincident_edge_references.Count() > 0)
{
using (var tx = new Transaction(doc, "create curves"))
{
tx.Start();
doc.FamilyCreate.NewAlignment(
doc.ActiveView,
reference_plane.GetReference(),
cooincident_edge_references
.FirstOrDefault()
.reference);
tx.Commit();
}
}
public class EdgeReference
{
public Reference reference;
public Curve curve;
}
public HashSet<EdgeReference> GetGeometryReferences(
ImportInstance import_instance,
Document doc)
{
Options DefaultOptions = new Options();
DefaultOptions.ComputeReferences = true;
DefaultOptions.IncludeNonVisibleObjects = true;
DefaultOptions.DetailLevel = doc.ActiveView.DetailLevel;
var edgeReferences = new HashSet<EdgeReference>();
var geometry = (import_instance
.get_Geometry(DefaultOptions)
.FirstOrDefault() as GeometryInstance)
.GetSymbolGeometry();
var linear_string = geometry
.OfType<Autodesk.Revit.DB.Line>()
.FirstOrDefault()
.Reference
.ConvertToStableRepresentation(doc);
var index = 5;
var item_index = linear_string
.Split(':')[index];
var container_index = linear_string
.Split(':')[index-1];
var part_to_replace = container_index +
":" +
item_index;
//check the container line was found in for edges
for (int i = 1; i < 1000; i++)
{
var object_string = linear_string.Replace(
item_index,
i.ToString());
var object_ref = Reference.ParseFromStableRepresentation(
doc,
object_string);
var geometry_object = import_instance
.GetGeometryObjectFromReference(object_ref);
if (geometry_object is Autodesk.Revit.DB.Edge)
{
edgeReferences.Add(
new EdgeReference()
{
curve = (geometry_object as Edge).AsCurve(),
reference = object_ref
});
}
}
//check the other containers
for(int i=1; i< geometry.Count(); i++)
{
bool found_item = false;
for(int j=1; j< geometry.Count(); j++)
{
var object_string = linear_string.Replace(
part_to_replace,
i.ToString()+":"+j.ToString());
var object_ref = Reference.ParseFromStableRepresentation(
doc,
object_string);
var geometry_object = import_instance
.GetGeometryObjectFromReference(object_ref);
if (found_item && geometry_object == null)
break;
if (!found_item && geometry_object!=null )
{
found_item = true;
}
if (geometry_object is Autodesk.Revit.DB.Edge)
{
edgeReferences.Add(
new EdgeReference()
{
curve = (geometry_object as Edge).AsCurve(),
reference = object_ref
});
}
}
}
return edgeReferences;
}