Problem with CreateNewDimension using python

Hi, I’m still new to coding so any ideas will be a huge help for me.
I’ve written code in Python with the help of Pyrevit to auto-create dimensions between the interior face of the core of walls in a project … there’s no error in the code but the created dimensions are not what you would expect one is hidden and one is not properly aligned as shown in the attached image. any ideas why?
here is the code:

# Import Revit API
import clr
import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

# Get the current document and view
doc = __revit__.ActiveUIDocument.Document
view = doc.ActiveView

# Get the walls in the active view
walls = FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()

lines = []
for wall in walls:

# Get the location line of the wall
wallCrv = wall.Location.Curve
sp = wallCrv.GetEndPoint(0)
ep = wallCrv.GetEndPoint(1)
wallDir = ep - sp
wallW = wall.Width

#get core width
wall_type = doc.GetElement(wall.GetTypeId()) # Get the wall type
compound_structure = wall_type.GetCompoundStructure() # Get the compound structure
corewidth = 0 # Initialize the variable to store the sum of core layer widths for the current wall
noncorewidth = 0

coreLayerFound = False
# Loop through each layer and check if it is a core layer
for layer_index in range(compound_structure.LayerCount):
layer_function = compound_structure.GetLayerFunction(layer_index) # Get the layer function

if layer_function == MaterialFunctionAssignment.Structure: # Check if it is a core layer
layer_width = compound_structure.GetLayerWidth(layer_index) # Get the layer width
corewidth += layer_width # Add the layer width to the corewidth variable
coreLayerFound = True
layer_width = compound_structure.GetLayerWidth(layer_index) # Get the layer width
if coreLayerFound:
noncorewidth += layer_width

off = (wallW/2) - noncorewidth
# Create a line from the face points
if wall.Flipped :
walloff = wallDir.CrossProduct(XYZ.BasisZ).Normalize().Multiply(off)
walloff = wallDir.CrossProduct(XYZ.BasisZ).Normalize().Multiply(off)

spo = sp + walloff
epo = ep + walloff
line = Line.CreateBound(epo,spo)

t = Transaction(doc, "detailLine")
Dline = doc.Create.NewDetailCurve(view,line)

pairs = []
refArray = DB.ReferenceArray()
for i in range(len(lines)):
line1 = lines[i]
dir1= line1.GeometryCurve.Direction

for j in range(i+1,len(lines)):
line2 = lines[j]
if dir1.IsAlmostEqualTo(dir2) or dir1.IsAlmostEqualTo(-dir2):

for pair in pairs:
p1 = pair[0].GeometryCurve.GetEndPoint(0)
p2 = pair[1].GeometryCurve.GetEndPoint(0)
# Get the offset vector perpendicular to the wall direction
Doff = wallDir.CrossProduct(XYZ.BasisZ).Normalize().Multiply(-off*4)
#p1 = p1 + Doff
#p2 = p2 + Doff
DimLine = DB.Line.CreateBound(p1,p2)
t = Transaction(doc, "dim")
Dime = doc.Create.NewDimension(view,DimLine,refArray)


Without the identations it’s not easy to determine the issue, I can only guess at the scope of variables,

I don’t immediatly know where the mistake happens, but if I were you I’d use some logger or print statements to output the coordinates of those references or line endpoints you’re using, just so that you’re sure that they are where you think they are.

Your code is really hard to read without the indents could you please fix that in your original post?

Other than that I have a suggestion for your code.
you forgot to negate your wall offset distance