Get CropBox location in sheet

Get CropBox location in sheet

GastonBC
Participant Participant
1,978 Views
5 Replies
Message 1 of 6

Get CropBox location in sheet

GastonBC
Participant
Participant

Hello everyone,

 

I'm trying to write a script in IronPython that creates a rectangle with detail lines, taking the shape of the CropBox of the view.

 

So far I managed to get the diagonal with the correct size but in a different place.

I can't figure out how to get the exact location of the viewport's cropbox in the sheet

 

    for vp in viewportsIdList:
#vp being each viewport in the sheet viewportFromId = doc.GetElement(vp) viewSheetId = doc.GetElement(viewportFromId.ViewId) viewScale = viewSheetId.Scale viewSheetCrop = viewSheetId.CropBox P1 = DB.XYZ(viewSheetCrop.Max.X/viewScale, viewSheetCrop.Max.Y/viewScale, 0) P2 = DB.XYZ(viewSheetCrop.Min.X/viewScale, viewSheetCrop.Min.Y/viewScale ,0) L1 = DB.Line.CreateBound(P1,P2) doc.Create.NewDetailCurve(s,L1) # So far it creates the diagonal the correct size! But not in the same place
 
Accepted solutions (1)
1,979 Views
5 Replies
Replies (5)
Message 2 of 6

TorsionTools
Contributor
Contributor

Why not use the ViewPort.GetBoxOutline() and not mess with the actual view?

Message 3 of 6

gastonASG
Explorer
Explorer

Because GetBoxOutline gets me the viewport boundary and what I need is the CropBox boundary.

 

I need to locate the cropbox in the sheet to be able to move the detail lines to the correct position

Anotación 2020-02-20 100324.png

Message 4 of 6

bhprest
Advocate
Advocate
Accepted solution

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!

 

 

 

 

 

 

Message 5 of 6

bhprest
Advocate
Advocate

Another thought:

 

I woke up this morning and realized that GetBoxOutline() may work if you turn off all of the categories that sit outside of the cropbox. I'm not at my computer right now, but it may be worth testing.

 

 

Message 6 of 6

GastonBC
Participant
Participant

This worked out great! I enabled temporary view properties and turned off all annotation categories, then disabled temporary view properties. Thanks!