Create crop view by filled region (or other 2d geometry)

Hi! I’m try to crop view based on 2d element (for eg. filled region or alternativly some other like system-zones).

At the moment I get the filled region by selecting element, then I get the geometry, then curves but unfortunatelly it won’t work. I’m using overMEP to create crop view.

Hi @stecmaciej9 and welcome to the forum :wink: .there is some few thing here you will need a view and i would first try filter the surface out or the lines and get surface perimeter so you dont ahve warnings…you could try ootb remove if not node or as i do here with spring sketchlines…and guess mepover node need a flatten list…and be sure you have ironpython package 2.7 ver. 2.5 installed as well if you are in 23-24

Great! Exactly what I was looking for :slight_smile:

@sovitek once more thank you for your help!

The current solution works perfectly, but now I’m trying to expand my script to also duplicate an existing view and crop it based on filled region. I found a solution to do this for a single filled region, but now I want the script to create multiple views and crop each one based on the selected filled regions.

I think the problem lies in List.Flatten, becouse when I do that I get one list with all curves from all filled regions. Unfortunatelly I’m not sure how to manage it.

EDIT - i have chacked the script from View.SetCropBoxCurves node, i won’t live I have no idea what I’m doing but perhapse there should be some unwrapping of the IN[1]?

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

import clr
clr.AddReference("RevitNodes")
import Revit

clr.AddReference('DSCoreNodes')
import DSCore
from DSCore import *

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

# Import ToProtoType, ToRevitType geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

#The inputs to this node will be stored as a list in the IN variables.
if isinstance(IN[0], list):
	views = UnwrapElement(IN[0])
else:
	views = [UnwrapElement(IN[0])]
if isinstance(IN[1], list):
	if IN[1][0].GetType() == PolyCurve:
		curves = [PolyCurve.Curves(x) for x in IN[1]]
	elif IN[1][0].GetType() == Curve or IN[1][0].GetType() == Line:
		curves = List.OfRepeatedItem(IN[1],len(views))
	else:
		curves = IN[1]
else:
	if IN[1].GetType() == PolyCurve:
		curves = [PolyCurve.Curves(IN[1])]
	else:
		curves = [IN[1]]
	
listout = []
for view,curve in zip(views,curves):
	regionMan = view.GetCropRegionShapeManager()
	revit_curve = [c.ToRevitType() for c in curve]
	curveloop = Autodesk.Revit.DB.CurveLoop()
	for c in revit_curve:
		curveloop.Append(c)
	TransactionManager.Instance.EnsureInTransaction(doc)
	if view.CropBoxActive == False:
		view.CropBoxActive = True
		view.CropBoxVisible = True
	regionMan.SetCropShape(curveloop)
	TransactionManager.Instance.TransactionTaskDone()
	listout.append(view)


#Assign your output to the OUT variable.
OUT = listout

yeah…on multiple you will need group your loops…i think :wink:

It works! Thank you! There is still one last problem that I cannot overcome - now the curves don’t match the name of the view.
So I get the name of the newly created view from the filled regions value in “comments” parameter, for eg. R100/01, R100/02, R100/03 etc.
Due to the fact that I need to flatten the curves before I group them and convert to PolyCurve those no longer match the names of the views. So I get the view which is named R100/01 but it has a crop view from R100/03. Is there a way to somehow map / sort them?
Sorry to bother you again, I can rename one by one but it would be great if it would be possible to somehow connet the curve to the name of the view. I already tried sorting, but as far as I understand it won’t work becouse there is no key for polycurve to sort it.

EDIT @sovitek Ok I resolved that! I just needed to add levels to group curves, and flatten the list AFTER I created polycurve, not before. Now it works like a charm, thank you for your help!