Generating curve from tubular shape

Or maybe something like this can be scripted: