How to know true DatumEnds.End0 is left or right in view?

How to know true DatumEnds.End0 is left or right in view?

Anonymous
Not applicable
1,132 Views
2 Replies
Message 1 of 3

How to know true DatumEnds.End0 is left or right in view?

Anonymous
Not applicable

Grid.Curve.GetEndPoint(0) does not return the same "End" with DatumEnds.End0 in view.
Therefore, how to define DatumEnds.End0 is on the left or on the right in view?

Here is my code and it not work correctly.

 

public class GridBubble : IExternalCommand
{
//get command
public bool bubbleTop = false;
public bool bubbleBottom = false;
public bool bubbleLeft = false;
public bool bubbleRight = false;


public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;

//all grids
var grids = new FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType().ToList();

// Modify document within a transaction
using (Transaction tx = new Transaction(doc))
{
tx.Start("Grid Bubble");

#region gridsX and gridsY
var gridsX = new List<Grid>();
var gridsY = new List<Grid>();

foreach (Grid grid in grids)
{
var gridCurve = grid.Curve;
var gridDirect = gridCurve.GetEndPoint(1) - gridCurve.GetEndPoint(0);

if (Math.Abs(gridDirect.X).S_IsGreater(Math.Abs(gridDirect.Y)))
{
gridsX.Add(grid);
}
else
{
gridsY.Add(grid);
}
}
#endregion

//show form to get command
var checkBubbleForm = new SettingGridBubbleForm(this);
checkBubbleForm.ShowDialog();

 

foreach (Grid grid in gridsX)
{
//gridcurve
var gridCurve = grid.Curve;

//check direction
var gridDirection = gridCurve.GetEndPoint(1) - gridCurve.GetEndPoint(0);
if (gridDirection.X < 0)
{
var auxBubbleLeft = bubbleLeft;
bubbleLeft = bubbleRight;
bubbleRight = auxBubbleLeft;
}

//left bubble
if (bubbleLeft)
{
grid.ShowBubbleInView(DatumEnds.End0, doc.ActiveView);
}
else
{
grid.HideBubbleInView(DatumEnds.End0, doc.ActiveView);
}

//right bubble
if (bubbleRight)
{
grid.ShowBubbleInView(DatumEnds.End1, doc.ActiveView);
}
else
{
grid.HideBubbleInView(DatumEnds.End1, doc.ActiveView);
}
}

tx.Commit();
}
return Result.Succeeded;
}
}

0 Likes
Accepted solutions (1)
1,133 Views
2 Replies
Replies (2)
Message 2 of 3

RPTHOMAS108
Mentor
Mentor
Accepted solution

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

Message 3 of 3

Anonymous
Not applicable

Thank you very much!

 
0 Likes