Grids Off-Axis

Grids Off-Axis

Dale.Bartlett
Collaborator Collaborator
3,958 Views
8 Replies
Message 1 of 9

Grids Off-Axis

Dale.Bartlett
Collaborator
Collaborator

I have models with Grids having off-axis warnings. Snooping the Grid.Lines shows direction vectors that are microscopically off horizontal/vertical. I have been unsuccessful in changing the XYZ .Direction (read only). Just to prove I tried unsuccessfully, even if it is daft:

// get the current grid location
LocationCurve location = grid.Location as LocationCurve;
// get the points
XYZ pt1 = location.Curve.GetEndPoint(0);
XYZ pt2 = location.Curve.GetEndPoint(1);
// make x values equal
pt2 = pt2.Add(new XYZ(pt1.X, pt2.Y, pt2.Z));
// create a new LineBound
//Line line = createApp.NewLineBound( // 2013 
Line newGridLine = Line.CreateBound(pt1, pt2);
// update the grid curve
location.Curve = newGridLine;

 

This was a Building Coder comment from 4 years ago:

Hello Jeremy,
This is the closest snippet of a conversation I could find related to modifying existing Grids.
I am trying to modify a Grid line by changing the underlying curve (line or arc) or setting the grid to a newly defined curve. Is this even possible?
-Nate
Dear Nate,
Sorry, currently that does not seem possible. I added a note of your request to the existing wish list item SPR #151715 [API functionality request: to change Grid's start/end point coordinates via API].
Cheers, Jeremy  

 

So, is it now possible to adjust the direction vector, or the start/end points to resolve the off-axis error? 




______________
Yes, I'm Satoshi.
0 Likes
Accepted solutions (1)
3,959 Views
8 Replies
Replies (8)
Message 2 of 9

jeremytammik
Autodesk
Autodesk

Dear Dale,

 

Thank you for your query.

 

Did you notice the blog post last week on how to modify grid curve end points?

 

http://thebuildingcoder.typepad.com/blog/2017/05/sdk-update-rvtsamples-and-modifying-grid-end-point....

 

That looks as if it might be exactly what you need.

 

I hope this helps.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 9

Dale.Bartlett
Collaborator
Collaborator

Indeed Jeremy, I did miss that one, and it is precisely what is required. When it is run on a vertical grid, it works as expected. I modified your code so that the start and end points are given the same X value (i.e. truly vertical) to remove the microscopic "off-axis". It fails here:

// "The curve is unbound or not coincident with the original one of the datum plane"

grid.SetCurveInView(DatumExtentType.Model, view, newLine);

 

To be clear, if my code is run on a vertical grid (i.e. not really effecting a change, but still functionally the same), it completes without error. 

 

The main change I made to your code is:

XYZ newStart = new XYZ(start.X, start.Y, start.Z);

XYZ newEnd = new XYZ(start.X, end.Y, end.Z);

 

A test file with faulty vertical grids can be found here:

Off-Axis Test File

 

For completeness, here is my code:

// only fixes vertical or horizontal - IsVertical
        public static Result FixGridAxisByPick(UIDocument uidoc, bool IsVertical, out string pstrMsg)
        {
            Document doc = uidoc.Document;
            Selection sel = uidoc.Selection;
            Autodesk.Revit.DB.View view = doc.ActiveView;
            string lstrMsg = "Off-Axis Error Results:";

            ISelectionFilter f = new JtElementsOfClassSelectionFilter<Grid>();
            Reference elemRef = sel.PickObject(ObjectType.Element, f, "Pick a grid");
            Grid grid = doc.GetElement(elemRef) as Grid;

            IList<Curve> gridCurves = grid.GetCurvesInView(DatumExtentType.Model, view);

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Modify Grid Endpoints");

                foreach (Curve c in gridCurves)
                {
                    XYZ start = c.GetEndPoint(0);
                    XYZ end = c.GetEndPoint(1);

                    MessageBox.Show("start: "
                        + start.ToString()
                        + Environment.NewLine
                        + " end: "
                        + end.ToString());

                    //XYZ newStart = start + 10 * XYZ.BasisY;
                    //XYZ newEnd = end - 10 * XYZ.BasisY;
                    XYZ newStart = new XYZ(start.X, start.Y, start.Z);
                    XYZ newEnd = new XYZ(start.X, end.Y, end.Z);

                    MessageBox.Show("newStart: "
                        + newStart.ToString()
                        + Environment.NewLine
                        + " newEnd: "
                        + newEnd.ToString());

                    Line newLine = Line.CreateBound(newStart, newEnd);

                    // *** fails on this line ***
                    // "The curve is unbound or not coincident with the original one of the datum plane"
                    grid.SetCurveInView(DatumExtentType.Model, view, newLine);
                }
                tx.Commit();
            }
            pstrMsg = lstrMsg;
            return Result.Succeeded;
        }

I am not asking you to debug for me, but I am not able to interpret the exception message. Thanks for any advice. Dale 

 




______________
Yes, I'm Satoshi.
0 Likes
Message 4 of 9

FAIR59
Advisor
Advisor

the exception message basically says that your new curve doesn't lie on the old curve. Meaning you can't change the direction this way.

The solution is to rotate the Grid with ElementTransformUtils.RotateElement().

 

0 Likes
Message 5 of 9

Dale.Bartlett
Collaborator
Collaborator

Hi,

That would be a reasonable solution. I suspect that the error is not from user input, but a result of Revit's accuracy limitations and rounding issues. In my case the variation is microscopic, so determining the relative rotation increment, rather than being able to set the absolute angle will just push the rounding error further out. This is the off-axis grid:

GridOff-AxisError.PNG

The highlighted value should be 1.0 but this Direction property is read-only.

Thanks for the reply.

Dale




______________
Yes, I'm Satoshi.
0 Likes
Message 6 of 9

FAIR59
Advisor
Advisor
Accepted solution

It is maybe a rounding issue, but Revit gives you a warning in the Warning-Review list, so it would be prudent to correct the issue.

 

            Grid grid = doc.GetElement(sel.GetElementIds().FirstOrDefault()) as Grid;
            XYZ direction = grid.Curve.GetEndPoint(1).Subtract(grid.Curve.GetEndPoint(0)).Normalize();
            double distance2hor = direction.DotProduct(XYZ.BasisY);
            double distance2vert = direction.DotProduct(XYZ.BasisX);
            double angle = 0;
// maybe use another criterium then <0.0001 if (Math.Abs(distance2hor) < 0.0001) { XYZ vector = direction.X < 0 ? direction.Negate() : direction; angle = Math.Asin(-vector.Y); } if (Math.Abs(distance2vert) < 0.0001) { XYZ vector = direction.Y < 0 ? direction.Negate() : direction; angle = Math.Asin(vector.X); } if (angle.CompareTo(0) != 0) { using (Transaction t = new Transaction(doc, "correctGrid")) { t.Start(); ElementTransformUtils.RotateElement(doc, grid.Id, Line.CreateBound(grid.Curve.GetEndPoint(0), grid.Curve.GetEndPoint(0).Add(XYZ.BasisZ)), angle); t.Commit(); } }
Message 7 of 9

Dale.Bartlett
Collaborator
Collaborator

Many thanks, that has indeed resolved the error. Dale




______________
Yes, I'm Satoshi.
0 Likes
Message 8 of 9

jeremytammik
Autodesk
Autodesk

Thank you very much @FAIR59

 

Added to The Building Coder samples:

 

https://github.com/jeremytammik/the_building_coder_samples/compare/2018.0.133.0...2018.0.133.1

 

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 9 of 9

jeremytammik
Autodesk
Autodesk

For future reference, I published a summary of this discussion here:

 

http://thebuildingcoder.typepad.com/blog/2017/06/aligning-a-slightly-off-axis-grid.html

 

Thank you again, Fair 59!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes