Align grid ends

I have some gridlines in Revit.
I am trying to align the grid ends.

I’m just trying the vertical grids in the blue circle.

The Python is an updated version of the datashapes node shown.

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

from Autodesk.Revit.DB import SpecTypeId 
U1Unit = doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId()

def convunit(x):
    return UnitUtils.ConvertToInternalUnits(x, U1Unit)

def tolist(input):
    if isinstance(input, list):
        return UnwrapElement(input)
    else:
        return [UnwrapElement(input)]

if IN[0] != True:
    grids = tolist(IN[0])
else:
    grids = [UnwrapElement(i) for i in FilteredElementCollector(doc).OfClass(Grid)]

TransactionManager.Instance.EnsureInTransaction(doc)
for g in grids:
    bottom = convunit(IN[1])
    top = convunit(IN[2])

    # Ensure bottom is always lower than top
    correct_bottom = min(bottom, top)
    correct_top = max(bottom, top)

    g.SetVerticalExtents(correct_bottom, correct_top)

TransactionManager.Instance.TransactionTaskDone()

OUT = grids

It looks like it’s worked in Dynamo, but none of the grid ends have moved.

I had more success with its base class. @Alien

g = UnwrapElement(IN[0])

TransactionManager.Instance.EnsureInTransaction(doc)

g_curve = [c for c in g.GetCurvesInView(DatumExtentType.ViewSpecific, doc.ActiveView)][0]
direct = g_curve.Direction
start = g_curve.GetEndPoint(0)
end = g_curve.GetEndPoint(1)

mock_shift = 3 # an imperial value for testing, you can trim with the other grid for sure
g.SetCurveInView(DatumExtentType.ViewSpecific, doc.ActiveView, Line.CreateBound(start, end.Subtract(direct.Multiply(mock_shift))))

TransactionManager.Instance.TransactionTaskDone()
1 Like
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

def toList(input):
    """Ensures input is a list of valid Revit elements"""
    if isinstance(input, list):
        return [UnwrapElement(i) for i in input if UnwrapElement(i) is not None]
    else:
        return [UnwrapElement(input)] if UnwrapElement(input) is not None else []

# Ensure g is always a list
g = toList(IN[0])

if not g or not all(isinstance(grid, Grid) for grid in g):
    OUT = "Invalid input: Expected a list of Grid elements."
else:
    TransactionManager.Instance.EnsureInTransaction(doc)

    for grid in g:
        # Get the grid curve in the active view
        g_curve = next((c for c in grid.GetCurvesInView(DatumExtentType.ViewSpecific, doc.ActiveView)), None)

        if g_curve:
            start = g_curve.GetEndPoint(0)
            end = g_curve.GetEndPoint(1)

            # Modify only the Y-coordinates
            new_start = XYZ(start.X, start.Y + 50, start.Z) 
            new_end = XYZ(end.X, end.Y + 50, end.Z) 

            # Create the new shifted line
            new_curve = Line.CreateBound(new_start, new_end)
            grid.SetCurveInView(DatumExtentType.ViewSpecific, doc.ActiveView, new_curve)
    
    TransactionManager.Instance.TransactionTaskDone()
    OUT = g  # Output modified grids

So it works for a nice horizontal or vertical grid…

How to get it to work for an angled grid?

you should factor in the direction of the grid curve as I demonstrated. Move start and end points only along the direction (or its negation) of the curve.

Yeah, having issues calculating the movement tho…
Maybe I start a new thread if I can’t work it out soon.

aite i see you someplace else.

1 Like