If everyone ever needs, here is the code:
try
{
IList < Reference > rList = PickDesiredGrids ( uidoc ) ;
Reference reference = PickLine ( uidoc ) ;
var refPlane = doc . GetElement ( reference . ElementId ) as ReferencePlane ;
XYZ sp = refPlane . BubbleEnd ;
XYZ ep = refPlane . FreeEnd ;
Curve c = Line . CreateBound ( sp , ep ) ;
foreach ( var item in rList )
{
using ( Transaction trans = new Transaction ( doc , "Hide Bubble" ) )
{
trans . Start ( ) ;
Grid grid = doc . GetElement ( item . ElementId ) as Grid ;
Curve gridLine = grid . Curve ;
XYZ spg = gridLine . GetEndPoint ( 0 ) ;
XYZ epg = gridLine . GetEndPoint ( 1 ) ;
SetComparisonResult scr = gridLine . Intersect ( c , out IntersectionResultArray intersectionResultArray ) ;
XYZ point = intersectionResultArray . get_Item ( 0 ) . XYZPoint ;
if ( spg . DistanceTo ( point ) > epg . DistanceTo ( point ) )
{
grid . HideBubbleInView ( DatumEnds . End0 , doc . ActiveView ) ;
}
else
{
grid . HideBubbleInView ( DatumEnds . End1 , doc . ActiveView ) ;
}
trans . Commit ( ) ;
}
}
}
catch ( Exception e )
{
TaskDialog . Show ( "o" , e . Message ) ;
}
Thanks a lot to @RPTHOMAS108 for giving me some ideas to start doing it!