Did you test my initial code? Did it work for you?
Although Mike’s solution seems perfect with an error tolerance of 0, I still can’t understand why @aaron_rumple tested my initial code and got the expected result, while it doesn’t work for me!
Anyway, as you can see in the code below, I made some changes to @Mike.Buttery’s solution by replacing the for
loop with a while
loop for points range calculations, while keeping the main logic.
import clr
import sys
# Import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from math import isclose
doc = DocumentManager.Instance.CurrentDBDocument
# Retrieve units
units = doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId()
Lx = UnitUtils.ConvertToInternalUnits(57, units) # Total length along X
Ly = UnitUtils.ConvertToInternalUnits(63, units) # Total length along Y
# Define spaces along each axis
dx = UnitUtils.ConvertToInternalUnits(4.75, units) # Space along X
dy = UnitUtils.ConvertToInternalUnits(4.35, units) # Space along Y
# Define extension distance
e = UnitUtils.ConvertToInternalUnits(2, units)
def create_lines(lx, ly, d, e, y_axis=False):
"""Create lines along X, Y axis """
if y_axis:
lx, ly = ly, lx
n = lx // d
points_X = []
i = 0
while i <= int(n):
points_X.append(d * i - lx / 2)
i += 1
if not isclose(lx, n * d):
points_X.append(lx / 2)
points_Y = [-ly / 2 - e, ly / 2 + e]
if y_axis:
return [Line.CreateBound(*(XYZ(x, y, 0) for x in points_Y)) for y in points_X]
return [Line.CreateBound(*(XYZ(x, y, 0) for y in points_Y)) for x in points_X]
lines_x = create_lines(Lx, Ly, dx, e)
lines_y = create_lines(Lx, Ly, dy, e, y_axis=True)
OUT = [l.ToProtoType() for l in lines_x], [l.ToProtoType() for l in lines_y]
Thanks.