Workplane inversion when angle changes

I am currently inserting families on reference points in a reference plane created with a dynamo script.
The script creates a surface, divide it in point and insert panel families as a roof.

The script works perfectly in on direction, but in the other direction, it inverts (rotates) the families and they are inserted upsidedown.

I don’t know what is causing it, see the image for reference 2 crossing angles the panels are rotated 180º degrees.


Any ideas?

This is not a fix, but a workaround,
From my testes when the angle of the plane flips over the imaginary 90º mark around the origin the face of the family instance flips as well.

to work around it I am adding an input Boolean, to give the user the option of flipping the workplane.

# Enable Python support and load DesignScript library
import clr
from Autodesk.DesignScript.Geometry import *
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Place your code below this line
p = UnwrapElement(IN[0])

for e in p:
	for i in e:
		if i.IsWorkPlaneFlipped==b:
			i.IsWorkPlaneFlipped = True
			r.append("fliped " + i.Id.ToString())	
# Assign your output to the OUT variable.
OUT = p,r

This is the code of the script I am using to flip the workplane as you can see I am feeding it a nest list of FamilyIntances.

Remember I am changing nom flipped elements into flipped ones.