I didn’t post a script just because I believe in self education.Meaning doing the job by yourself is a better way of learning than just opening a file.
So the whole logic here is cutting a solid with another solid done in Rhinoceros.
You have this piece
You want that without self intersection
So you must cut there on the red line
I choose to cut with a solid intersection. The solid must be bigger than the piece you want to cut and having a plane at 45°. I choose a cube
So after boolean intersection I have that
I hope it is clear, I took more time to explain that just posting the file.
So try to follow the steps in Grasshopper, learn CAD modelling…
You just have to make a cube in Rhinoceros. Not a big challenge !!!