My typical disclaimer: this may not be the most optimal way to do this.
Cool problem! Here's how I would do it.
The real disconnect in your code is that the origin of the coordinates used in the cropbox are not being translated to the origin of the ViewSheet. This translation can be handled by finding the center point of the cropbox (the place equidistant between the P1 and P2 in your sample code), and referencing your points from that centerpoint. Then you can rebuild the box around the point given to you by the method ViewPort.GetBoxCenter(), which returns the point on the sheet.
However, there is a problem: GetBoxCenter() returns the center of the extents of your view port, including text and datum elements that may lay outside your cropbox. So, in order to handle this, we have to turn off those categories, make our lines, and then turn the categories back on. This will be more of an issue if your project is using View Templates, but the process will be the same. (Perhaps you could enable Temporary View Properties mode, which disables the view template, and then simply disable it later. But I'll leave the specifics of how to control this temporary visibility up to you).
Here's my sample code:
uiDoc = __revit__.ActiveUIDocument
doc = uiDoc.Document
categories = doc.Settings.Categories
# These are the categories that I tested on my views.
levelCat = categories.get_Item(BuiltInCategory.OST_Levels)
gridCat = categories.get_Item(BuiltInCategory.OST_Grids)
textCat = categories.get_Item(BuiltInCategory.OST_TextNotes)
# this is just how I selected the viewport for testing.
selection = pickObject()
for s in selection:
viewPort = doc.GetElement(s.ElementId)
view = doc.GetElement(viewPort.ViewId)
viewScale = view.Scale
cropbox = view.CropBox
P1 = XYZ(cropbox.Max.X/viewScale, cropbox.Max.Y/viewScale, 0)
P2 = XYZ(cropbox.Min.X/viewScale, cropbox.Min.Y/viewScale ,0)
# This becomes our "basepoint"
CROPMIDPOINT = (P1 + P2) / 2
# These are the vectors in relation to that basepoint.
P1M = P1 - CROPMIDPOINT
P2M = P2 - CROPMIDPOINT
t = Transaction(doc, 'Create curve')
t.Start()
# Hide the categories that skew the results of GetBoxCenter()
view.SetCategoryHidden(gridCat.Id, True)
view.SetCategoryHidden(levelCat.Id, True)
view.SetCategoryHidden(textCat.Id, True)
boxCenter = viewPort.GetBoxCenter()
# Your line, in paperspace, is just vectors that you found earlier referenced from your box center.
L1 = Line.CreateBound(boxCenter + P1M, boxCenter + P2M)
detailCurve = doc.Create.NewDetailCurve(doc.ActiveView, L1)
# Reset your visibility view.SetCategoryHidden(gridCat.Id, False)
view.SetCategoryHidden(levelCat.Id, False)
view.SetCategoryHidden(textCat.Id, False)
t.Commit()
Good luck!