Python - Elevation Crop

I working on my Python understanding.
I am trying to use room to generate Plan and Elevation Views.
I have success with

  1. extracting the room boundary’s to set the crop of a plan view
  2. placing an elevation marker in the center point.
  3. create elevation view for the marker.

Where I’m getting stuck is setting the crop of the elevations.
I thought it might be straight forward, by setting the elevation View CropBox to be the room Bounding Box.

It is not. I’ve been all over the web looking to solve this, but stumped.

import clr

# import RevitNodes
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# import Revit Services 
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# import system.
import System
from System.Collections.Generic import *

# get the current Revit document. 
doc = DocumentManager.Instance.CurrentDBDocument

#collect all levels
levels = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements()
#selecte frist level in list
level = levels[0]

#repeat for rooms
#note delete unclosed rooms as selecting an unclosed room will cause errors with the curveloop for plan view
rooms = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).ToElements()
room = rooms[0]

#collect View Types.
viewTypes = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#string Type Name for plan.  -Note error may cause if elevation and plan have same type name
planview ="Floor Plan"
planViewType = [ viewType for viewType in viewTypes if viewType.LookupParameter("Type Name").AsString()==planview]
planViewType=planViewType[0]



TransactionManager.Instance.EnsureInTransaction(doc)

#create view plan view
viewPlan = ViewPlan.Create(doc,viewType.Id,level.Id)

#Extract Room Boundary curves the make into curve loop to use for crop.
options = SpatialElementBoundaryOptions()
boundloc = AreaVolumeSettings.GetAreaVolumeSettings(doc).GetSpatialElementBoundaryLocation(SpatialElementType.Room)
options.SpatialElementBoundaryLocation = boundloc
curves = []
try:
	for boundarylist in room.GetBoundarySegments(options):
		for boundary in boundarylist:
			curves.append(boundary.GetCurve())
except: pass

roomBoundaryCurveLoop = CurveLoop.Create(curves)


#Turn on Crop view and apply crop using room boundary.
viewPlan.CropBoxActive = True
viewPlan.GetCropRegionShapeManager().SetCropShape(roomBoundaryCurveLoop)


#get mid point of room bounidng box to use for inserting elevation marker.
roomBoundingBox=room.get_BoundingBox(None)
roomMidPoint=(roomBoundingBox.Min + roomBoundingBox.Max)/2


eleView ="10mm Circle"
eleViewType = [ viewType for viewType in viewTypes if viewType.LookupParameter("Type Name").AsString()==eleView]
eleViewType=eleViewType[0]

#Place New Elevation Marker in Ceter Of room
eleMarker = ElevationMarker.CreateElevationMarker(doc,eleViewType.Id,roomMidPoint ,20)
eleView= eleMarker.CreateElevation(doc,viewPlan.Id,0)


#Attempting to use RoomBoundingBox to set crop for elevation?
cropBbox=BoundingBoxXYZ()
cropBbox.Min=roomBoundingBox.Min
cropBbox.Max=roomBoundingBox.Max

eleView.CropBoxActive = True

eleView.CropBox = cropBbox

TransactionManager.Instance.TransactionTaskDone()

OUT=level,room,viewType,viewPlan,eleViewType,roomBoundingBox,roomMidPoint

image

Hello,
try to use the ViewCropRegionShapeManager of the elevation view and set it with this method

an example here with a wall (need to adapt it to your case)

Hi @c.poupin,

Thanks for the very helpful links above and code within!

The example you gave works exactly as expected but I am having trouble replicating for elevations that have already been created.

For some reason I am getting the following warning:

This makes no sense to me because as far as I can tell you have no issues in line 58 of your code while calling the crop region shape manager on a section view…

My code is as follows:

import clr
import sys
import math
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

elevation = IN[0]
crv_loop = IN[1]

def ds_to_rvt_CRV(joined_curves):
	polycurvDS = DS.PolyCurve.ByJoinedCurves(joined_curves)
	lstCurv = List[Curve]([x.ToRevitType() for x in polycurvDS.Curves()])
	curvLoop = CurveLoop.Create(lstCurv)
	return curvLoop

rvt_crvs = ds_to_rvt_CRV(crv_loop)

TransactionManager.Instance.EnsureInTransaction(doc)

crManager = elevation.GetCropRegionShapeManager()
crManager.SetCropShape(rvt_crvs)

TransactionManager.Instance.TransactionTaskDone()


OUT = "why arent you working"

Many thanks in advance for any light you can shed!

Hello,

probably because your ‘elevation’ object is not exposed to Revit API, need to unwrap it :wink:
unwrap-chihuahua

5 Likes

Ahhhhhhhhhh of course - One of the many things I must remember while embarking on my API coding journey!

Thanks for the help (love the GIF haha)