Try transforming into coordinate space of view and the comparing XY vals.
public Result TObj73(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
if (commandData.Application.ActiveUIDocument == null)
return Result.Cancelled;
UIDocument UIDoc = commandData.Application.ActiveUIDocument;
Document Doc = UIDoc.Document;
ViewPlan AcView = UIDoc.ActiveGraphicalView as ViewPlan;
if (AcView == null)
return Result.Cancelled;
Transform Tx = Transform.Identity;
Tx.BasisX = AcView.RightDirection;
Tx.BasisY = AcView.UpDirection;
Tx.BasisZ = XYZ.BasisZ;
FilteredElementCollector FEC = new FilteredElementCollector(Doc, AcView.Id);
ElementClassFilter ECF = new ElementClassFilter(typeof(Grid));
List<Grid> Grids = FEC.WherePasses(ECF).ToElements().Cast<Grid>().ToList();
const bool ShowGridBubbleLeft = true;
const bool ShowGridBubbleTop = true;
using (Transaction Tr = new Transaction(Doc, "Line up those grid bubbles"))
{
if (Tr.Start() == TransactionStatus.Started)
{
foreach (Grid Gr in Grids)
{
Line crv = Gr.Curve as Line;
if (crv == null)
continue;
//Line curves only
XYZ[] EP = new XYZ[] {
Tx.Inverse.OfPoint(crv.GetEndPoint(0)),
Tx.Inverse.OfPoint(crv.GetEndPoint(1))
};
if (Math.Abs(EP[0].X - EP[1].X) > Math.Abs(EP[0].Y - EP[1].Y))
{
//More horizontal than vertical
if (EP[0].X > EP[1].X)
{
if (ShowGridBubbleLeft)
{
Gr.ShowBubbleInView(DatumEnds.End1, AcView);
Gr.HideBubbleInView(DatumEnds.End0, AcView);
}
else
{
Gr.ShowBubbleInView(DatumEnds.End0, AcView);
Gr.HideBubbleInView(DatumEnds.End1, AcView);
}
}
else
{
if (ShowGridBubbleLeft)
{
Gr.ShowBubbleInView(DatumEnds.End0, AcView);
Gr.HideBubbleInView(DatumEnds.End1, AcView);
}
else
{
Gr.ShowBubbleInView(DatumEnds.End1, AcView);
Gr.HideBubbleInView(DatumEnds.End0, AcView);
}
}
}
else
{
//More vertical than horizontal or perhaps 45 degrees (Dx=Dy)
if (EP[0].Y > EP[1].Y)
{
if (ShowGridBubbleTop)
{
Gr.ShowBubbleInView(DatumEnds.End1, AcView);
Gr.HideBubbleInView(DatumEnds.End0, AcView);
}
else
{
Gr.ShowBubbleInView(DatumEnds.End0, AcView);
Gr.HideBubbleInView(DatumEnds.End1, AcView);
}
}
else
{
if (ShowGridBubbleTop)
{
Gr.ShowBubbleInView(DatumEnds.End0, AcView);
Gr.HideBubbleInView(DatumEnds.End1, AcView);
}
else
{
Gr.ShowBubbleInView(DatumEnds.End1, AcView);
Gr.HideBubbleInView(DatumEnds.End0, AcView);
}
}
}
}
Tr.Commit();
}
}
return Result.Succeeded;
}
C#
Private Function TObj73(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData,
ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Result
If commandData.Application.ActiveUIDocument Is Nothing Then Return Result.Cancelled Else
Dim UIDoc As UIDocument = commandData.Application.ActiveUIDocument
Dim Doc As Document = UIDoc.Document
Dim AcView As ViewPlan = TryCast(UIDoc.ActiveGraphicalView, ViewPlan)
If AcView Is Nothing Then Return Result.Cancelled Else
Dim Tx As Transform = Transform.Identity
Tx.BasisX = AcView.RightDirection
Tx.BasisY = AcView.UpDirection
Tx.BasisZ = XYZ.BasisZ
Dim FEC As New FilteredElementCollector(Doc, AcView.Id)
Dim ECF As New ElementClassFilter(GetType(Grid))
Dim Grids As List(Of Grid) = FEC.WherePasses(ECF).ToElements.Cast(Of Grid).ToList
Const ShowGridBubbleLeft As Boolean = True
Const ShowGridBubbleTop As Boolean = True
Using Tr As New Transaction(Doc, "Line up those grid bubbles")
If Tr.Start = TransactionStatus.Started Then
For Each Gr As Grid In Grids
Dim crv As Line = TryCast(Gr.Curve, Line)
If crv Is Nothing Then Continue For Else 'Line curves only
Dim EP As XYZ() = New XYZ() {Tx.Inverse.OfPoint(crv.GetEndPoint(0)),
Tx.Inverse.OfPoint(crv.GetEndPoint(1))}
If Math.Abs(EP(0).X - EP(1).X) > Math.Abs(EP(0).Y - EP(1).Y) Then
'More horizontal than vertical
If EP(0).X > EP(1).X Then
If ShowGridBubbleLeft Then
Gr.ShowBubbleInView(DatumEnds.End1, AcView)
Gr.HideBubbleInView(DatumEnds.End0, AcView)
Else
Gr.ShowBubbleInView(DatumEnds.End0, AcView)
Gr.HideBubbleInView(DatumEnds.End1, AcView)
End If
Else
If ShowGridBubbleLeft Then
Gr.ShowBubbleInView(DatumEnds.End0, AcView)
Gr.HideBubbleInView(DatumEnds.End1, AcView)
Else
Gr.ShowBubbleInView(DatumEnds.End1, AcView)
Gr.HideBubbleInView(DatumEnds.End0, AcView)
End If
End If
Else
'More vertical than horizontal or perhaps 45 degrees (Dx=Dy)
If EP(0).Y > EP(1).Y Then
If ShowGridBubbleTop Then
Gr.ShowBubbleInView(DatumEnds.End1, AcView)
Gr.HideBubbleInView(DatumEnds.End0, AcView)
Else
Gr.ShowBubbleInView(DatumEnds.End0, AcView)
Gr.HideBubbleInView(DatumEnds.End1, AcView)
End If
Else
If ShowGridBubbleTop Then
Gr.ShowBubbleInView(DatumEnds.End0, AcView)
Gr.HideBubbleInView(DatumEnds.End1, AcView)
Else
Gr.ShowBubbleInView(DatumEnds.End1, AcView)
Gr.HideBubbleInView(DatumEnds.End0, AcView)
End If
End If
End If
Next
Tr.Commit()
End If
End Using
Return Result.Succeeded
End Function
Much easier to read VB