Invalid Number of References

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Every One,
I am trying to set dimension between column face and grid "My code Below here " Revit throws " an invalid number of references exception " and by Debugging I can get "2 "references in reference Array.
Thanks for advance.
public class ManholeCreationCommand : IExternalCommand
{
const double _eps = 1.0e-9;
/// <summary>
/// Check whether two real numbers are equal
/// </summary>
static public bool IsEqual(double a, double b)
{
return Math.Abs(a - b) < _eps;
}
static public bool IsParallel(XYZ a, XYZ b)
{
double angle = a.AngleTo(b);
return _eps > angle || IsEqual(angle, Math.PI);
}
static XYZ Normal(Line line)
{
XYZ p = line.GetEndPoint(0);
XYZ q = line.GetEndPoint(1);
XYZ v = q - p;
return v.CrossProduct(XYZ.BasisZ).Normalize();
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//Application Context.
UIDocument uidoc = commandData.Application.ActiveUIDocument;
var doc = uidoc.Document;
//Test for column
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Grid));
Grid grid = collector.ToElements().FirstOrDefault() as Grid;
XYZ grNormal = Normal(grid.Curve as Line);
LocationPoint pc = null;
ReferenceArray referenceArray = new ReferenceArray();
referenceArray.Clear();
referenceArray.Append(grid.Curve.Reference);
int counter = 1;
Options opt = new Options
{
ComputeReferences = true,
IncludeNonVisibleObjects = true,
View = doc.ActiveView
};
ObjectType objectType = ObjectType.Element;
Reference re = uidoc.Selection.PickObjects(objectType).FirstOrDefault();
Element e = doc.GetElement(re);
if (e.Location != null)
{
pc = e.Location as LocationPoint;
}
Face face = null;
GeometryElement geo = e.get_Geometry(opt);
foreach (GeometryObject obj in geo)
{
GeometryInstance instance = obj as GeometryInstance;
if (instance != null)
{
GeometryElement instanceObj = instance.GetInstanceGeometry();
foreach (GeometryObject geoObj in instanceObj)
{
Solid solid = geoObj as Solid;
if (solid != null)
{
FaceArray fa = solid.Faces;
foreach (Face f in fa)
{
PlanarFace pf = f as PlanarFace;
if (IsParallel(grNormal, pf.FaceNormal) && counter < 2)
{
referenceArray.Append(pf.Reference);
counter++;
}
}
}
}
}
}
List<LocationPoint> points = new List<LocationPoint>();
XYZ pcxyz = pc.Point as XYZ;
Line line = Line.CreateBound(pcxyz, grid.Curve.GetEndPoint(0));
//#region Creation Transaction Area
using (var t = new Transaction(doc))
{
doc.Create.NewDimension(doc.ActiveView, line, referenceArray);
t.Commit();
}
return Result.Succeeded;
}