this loop trimming is something I ran into a while back.
The solution I found was to create a list of 2d segments (mine handles arcs too), and loop through each segment to see if it hits any others. Record any hits by station and int point.
Then sort the hits by station, and look for items that hit at the same point.
Use that to trim the intersecting segments, plus remove any segments between.
You must decide what the z elevation will be, could be average of endpoints or maybe the first, just choose a method, then add z's back after the 2d analysis.
The trick here is the scaling issue. To make this fast, you must optimize the line-line, line-arc, and arc-arc intersection routines. I do this using bounding box approach, and that even requires optimization.
I cannot share the routines mentioned, but can explain the logic and even the cases to be handled.
The end result is super valuable as you can write your own alignments engine with those ingredients.