Trimming curves(lines) by elements(surfaces, boundingboxs or solids) - Geometry - Dynamo (dynamobim.com)
I followed your solution and got the results but still there are small things left