# Crop Region by curve

**URL:** <https://forum.dynamobim.com/t/crop-region-by-curve/4607>\
**Category:** Developers\
**Tags:** revit\
**Created:** [June 19, 2016, 1:49pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607 "2016-06-19T13:49:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![raber95](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@raber95](https://forum.dynamobim.com/u/raber95)\
**Post date:** [June 19, 2016, 1:49pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/1 "2016-06-19T13:49:06Z")

</div>

Hey everyone  
i 'm looking for a solution to have a Crop Region view by curves , someone have any idea to deal with that  
Thx

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [June 19, 2016, 4:55pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/2 "2016-06-19T16:55:18Z")

</div>

Hello raber95,

You can try something like this:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/e/e766596bc630881dc9e33e1f5e7c8e351f21a272.png)

```
import clr

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#prepare input
view = UnwrapElement(IN[0])
curves = IN[1]

#Create curveloop
loop = CurveLoop()
for c in curves:
	loop.Append(c.ToRevitType())
	
#Set cropregion in Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
view.CropBoxActive = True
view.GetCropRegionShapeManager().SetCropShape(loop)
TransactionManager.Instance.TransactionTaskDone()
OUT = view

```

![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/c/cb5ba8eb03806ddebbd7193486846ed910f8860e.png)

---

<div class="post-metadata">

**Author:** ![Kulkul](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kulkul/32/49801_2.png) [@Kulkul](https://forum.dynamobim.com/u/Kulkul)\
**Post date:** [June 19, 2016, 5:58pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/3 "2016-06-19T17:58:02Z")

</div>

@raber95 @Einar_Raknes @Mostafa_El_Ayoubi Are you looking to crop one view or multiple views:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/4/46becfc770e874515d7d5ff29e9289c5d92d2cdf.gif)

---

<div class="post-metadata">

**Author:** ![raber95](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@raber95](https://forum.dynamobim.com/u/raber95)\
**Post date:** [June 19, 2016, 6:38pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/4 "2016-06-19T18:38:04Z")

</div>

thank you for your reply  
i’m looking to use room boundary to crop region

@Einar_Raknes i’ve this messssage when i use your script

" File “”, line 26, in   
AttributeError: ‘NoneType’ object has no attribute ‘ToRevitType’

@Kulkul yes, i’m looking for thing like this

🙂

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [June 19, 2016, 6:52pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/5 "2016-06-19T18:52:16Z")

</div>

Could you please paste a screenshot of your graph? You have both a view and curves attached to the python node?

See this link for crop regions from room:

> **[Room Plans, Non-Rectangular View Ports in Revit 2014 and Python - dp Stuff](http://dp-stuff.org/room-plans-non-rectangular-viewports-revit-2014/)**
>
> This post adds Non Rectangular Viewports from Revit 2014 and new Revit 2014 API to the equation of how to batch-create Room Plans with Python.

---

<div class="post-metadata">

**Author:** ![Kulkul](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kulkul/32/49801_2.png) [@Kulkul](https://forum.dynamobim.com/u/Kulkul)\
**Post date:** [June 19, 2016, 6:56pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/6 "2016-06-19T18:56:04Z")

</div>

@raber95 And also which Revit version your using?

---

<div class="post-metadata">

**Author:** ![raber95](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@raber95](https://forum.dynamobim.com/u/raber95)\
**Post date:** [June 19, 2016, 7:09pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/7 "2016-06-19T19:09:51Z")

</div>

hey there is the screenshot

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/a/a3309bb60c3a9c39d0ce077eb40a420204f692e1.png)

i’m using revit 2016  
thx

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [June 19, 2016, 8:31pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/8 "2016-06-19T20:31:51Z")

</div>

I’m sorry but that’s not going to work. The python script can only take one view and crop it to match one set of curves. If you are looking for a view for each room it would be better to create new ones in the script.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/5/59419b0b535fc9c5b691536a8b6584eb5fdceedc.png)

If you wrap the python node in a custom node, you can make it work for several views and rooms. (two lists with same length)

---

<div class="post-metadata">

**Author:** ![raber95](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@raber95](https://forum.dynamobim.com/u/raber95)\
**Post date:** [June 19, 2016, 9:27pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/9 "2016-06-19T21:27:27Z")

</div>

hey Einar , thx for your answer  
i found a script C# in autodesk site to deal with what i’m looking for :

public void CropAroundRoom(Room room, View view)  
{  
if (view != null)  
{  
IList\<IList\<Autodesk.Revit.DB.BoundarySegment\>\> segments = room.GetBoundarySegments(new SpatialElementBoundaryOptions());

```
    if (null != segments) //the room may not be bound
    {
        foreach (IList<Autodesk.Revit.DB.BoundarySegment> segmentList in segments)
        {
            CurveLoop loop = new CurveLoop();
            foreach (Autodesk.Revit.DB.BoundarySegment boundarySegment in segmentList)
            {
                loop.Append(boundarySegment.GetCurve());
            }

            ViewCropRegionShapeManager vcrShapeMgr = view.GetCropRegionShapeManager();
            vcrShapeMgr.SetCropShape(loop);
            break; // if more than one set of boundary segments for room, crop around the first one
        }
    }
}

```

}

any idea to traduce that in python

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [June 20, 2016, 9:50am UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/10 "2016-06-20T09:50:21Z")

</div>

try this: [http://dp-stuff.org/room-plans-non-rectangular-viewports-revit-2014/](http://dp-stuff.org/room-plans-non-rectangular-viewports-revit-2014/)  
 ![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/2X/4/4eee7900db0f4fa56d7d8e1d0572b3895f4ce4d8.png)

```
#dp Stuff | http://dp-stuff.org | createRoomPlanViews
#this Revit Python Script batch generates Room Plan Views
#before running the script select the rooms to generate Plan Views for
 
import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
 
doc = DocumentManager.Instance.CurrentDBDocument
 
collector = FilteredElementCollector(doc).OfClass(clr.GetClrType(ViewFamilyType))
collectorVP = FilteredElementCollector(doc).OfClass(clr.GetClrType(ViewPlan)).WhereElementIsNotElementType()
vtId = 0

expandVal = 0
 
#selected room elements
selEls = UnwrapElement(IN[0])
 
#get elementId of FloorPlan viewFamilyType
for el in collector:
	if el.ViewFamily == ViewFamily.FloorPlan:
		vtId = el.Id
		break
 
TransactionManager.Instance.EnsureInTransaction(doc)
for rm in selEls:
 
	if int(BuiltInCategory.OST_Rooms) == rm.Category.Id.IntegerValue:		
		rmbox = rm.get_BoundingBox(None)
		print rmbox
		minPt = XYZ(rmbox.Min.X-expandVal, rmbox.Min.Y-expandVal, 0)
		maxPt = XYZ(rmbox.Max.X+expandVal, rmbox.Max.Y+expandVal, 0)
		rmbox.Min = minPt
		rmbox.Max = maxPt
		lev = rm.Level
 
		#create a Plan View for the room
		vp = ViewPlan.Create(doc, vtId, lev.Id)
		print vp.Name		
 
		testName = rm.GetParameters('Name')[0].AsString() + ' ' + str(rm.GetParameters('Number')[0].AsString())
 
		for el in collectorVP:
			if el.Name == testName:
				testName = testName + ' (1)'
				break
 
		vp.Name = testName
 
		#2014 custom crop view boundary block
		opt = SpatialElementBoundaryOptions()
		rmBoundarySegListList = rm.GetBoundarySegments(opt)		
		bsList = 0
		crvList = list()
		#get first BoundarySegment Loop
		for bsL in rmBoundarySegListList:
			bsList = bsL
			break
		#create a Curve Loop and make sure they are all lines
		#if a segment is not a line then we tesselate it
		for bs in bsList:
			if bs.GetCurve() is clr.GetClrType(Line):
				crvList.append(bs.GetCurve())
			else:
				#we tesselate the curve
				pts = bs.GetCurve().Tessellate()
				i = -1
				for pt in pts:
					i+=1
					if i < len(pts)-1:
						#create and add linear segments to the future CropBoundary loop
						crvList.append(Line.CreateBound(pts[i], pts[i+1]))	
					myLoop = CurveLoop.Create(crvList)
		#assign CurveLoop to the CropBox of the view
		vp.CropBoxActive = True
		cropManager = vp.GetCropRegionShapeManager()
		if cropManager.IsCropRegionShapeValid(myLoop):					
			cropManager.SetCropShape(myLoop)
		else:
			print 'Not Allowed to manage Crop for ' + vp.Name + ' :-(' 
			vp.CropBox = rmbox
		#END 2014 custom crop view boundary block
		#you can fancy this up by assigning a particular view template etc
 
TransactionManager.Instance.TransactionTaskDone()

OUT = 0
```

---

<div class="post-metadata">

**Author:** ![raber95](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@raber95](https://forum.dynamobim.com/u/raber95)\
**Post date:** [June 20, 2016, 11:27am UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/11 "2016-06-20T11:27:58Z")

</div>

Thank you for your efforts , i have this message when i paste the code in python node :

Avertissement:IronPythonEvaluator.EvaluateIronPythonScript l’opération a échoué.  
File “”, line 31  
if el.ViewFamily == ViewFamily.FloorPlan:

```
^

```

SyntaxError: expected an indented block

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [June 20, 2016, 11:59am UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/12 "2016-06-20T11:59:38Z")

</div>

OK, try this file then: [roomViews.dyn](https://forum.dynamobim.com/uploads/short-url/xmEAjKL7gaTXNVUb0ryNQCdpakK.dyn) (4.5 KB)

(I think I have fixed the codeblock aswell)

---

<div class="post-metadata">

**Author:** ![raber95](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@raber95](https://forum.dynamobim.com/u/raber95)\
**Post date:** [June 20, 2016, 12:31pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/13 "2016-06-20T12:31:58Z")

</div>

Yeah thx man it work 🙂  
can i controle the offset of boudary with this code

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [June 20, 2016, 1:12pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/14 "2016-06-20T13:12:30Z")

</div>

Try to set expandVal to something else than 0 or maybe  
expandVal = IN[1] and add another port to the python node

---

<div class="post-metadata">

**Author:** ![jup](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@jup](https://forum.dynamobim.com/u/jup)\
**Post date:** [September 27, 2016, 12:50pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/15 "2016-09-27T12:50:09Z")

</div>

Hi @Einar_Raknes

I am trying to modify this script to work with multiple views and multiple curves. I’ve spotted your comment that if this was wrapped in a custom node, this would be possible. I’ve been learning a bit of Python (mostly on a high level) not really dynamo specific. - but hopefully not too far off.

I’ve had a stab at modifying the python script to place everything within a for loop, but I can’t get it quite worked out. Could you give me a bit of advice, or if it would be easier wrapped in a custom node, what that would entail.

My fumblings are shown below:

```
import clr

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#prepare input
views = UnwrapElement(IN[0])
curves = IN[1]
curvelist =[]
viewlist = []

#Create curveloop
TransactionManager.Instance.EnsureInTransaction(doc)

for curve,view in curves, views:
	loop = CurveLoop()
	for c in curves:
		loop.Append(c)

#Set cropregion in Transaction

	view.CropBoxActive = True
	view.GetCropRegionShapeManager().SetCropRegionShape(loop)
	viewlist.append(view)
	TransactionManager.Instance.TransactionTaskDone()

OUT = view

```

I’m getting the error as shown …

this came out of the discussion [Find Bounding Boxes of Multiple Scope Boxes](https://forum.dynamobim.com/t/find-bounding-boxes-of-multiple-scope-boxes/6374/6)

Cheers

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/f/fbb3b244ccc9919574045f63db37368196967fb0.png)

---

<div class="post-metadata">

**Author:** ![jup](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@jup](https://forum.dynamobim.com/u/jup)\
**Post date:** [September 27, 2016, 12:51pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/16 "2016-09-27T12:51:03Z")

</div>

It all seems so simple when you read it, until you come write it!

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [September 27, 2016, 7:35pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/17 "2016-09-27T19:35:06Z")

</div>

Maybe you can try to follow this example:

> [@Dividing parts with dynamo](https://forum.dynamobim.com/t/dividing-parts-with-dynamo/5388/7):
>
> @jostein_olsen Thank you, I do not have a [nice blog](http://jbdynamo.blogspot.no/) like your self, and I thought I might share a little something while trying to learn for my self. ##How to make this work for multiple walls? A limitation with the above python script is that it will only work for a single wall element, and that is not very efficient. We have two options to make it work for a list of walls: Wrap the python script in a custom node, be sure to define the input correctly as explained by @Dimitar_Venkov in [t…](https://forum.dynamobim.com/t/how-to-handle-variable-input-list-length-nesting-in-custom-nodes-python/3192/5)

---

<div class="post-metadata">

**Author:** ![jup](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@jup](https://forum.dynamobim.com/u/jup)\
**Post date:** [September 29, 2016, 4:41pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/18 "2016-09-29T16:41:57Z")

</div>

CRACKED IT ! My first successfully working Python API node.

I have modified the code and it working! @Einar_Raknes

I was walking home and had a light bulb-esque moment.

I transposed the graph, to produce a combined list with both the view and curve per item, then iterated through the originl script per item.

I was thinking this method could apply to any definition which will work only once, but for which you want to iterate multiple times.

Anyway enough chat, here’s the code…

```
import clr

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#prepare input
combinedlist = IN[0]
outputlist = []

for i in combinedlist:
	view = UnwrapElement(i[0])
	curves = i[1]

	#Create curveloop
	loop = CurveLoop()
	for c in curves:
		loop.Append(c.ToRevitType())

	#Set cropregion in Transaction
	TransactionManager.Instance.EnsureInTransaction(doc)
	view.CropBoxActive = True
	view.GetCropRegionShapeManager().SetCropShape(loop)
outputlist.append(view)
TransactionManager.Instance.TransactionTaskDone()

OUT = outputlist
```

---

<div class="post-metadata">

**Author:** ![jup](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@jup](https://forum.dynamobim.com/u/jup)\
**Post date:** [September 29, 2016, 4:44pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/19 "2016-09-29T16:44:59Z")

</div>

However it hasn’t solved the main problem. I am struggling to place views on sheets, i was trying to find n alternate way to crop the views to see if they would pack onto the sheets, but alas I have the same error.

I have noticed this has also been recently commented on in the following thread.

> [@Issue placing views on sheets](https://forum.dynamobim.com/t/issue-placing-views-on-sheets/6368):
>
> [image] Dynamo is giving me a issue when I try to place a cropped view (trough scope box) into a sheet. It’s say that the views is too big to be placed inside the sheet. I think that dynamo is computing the whole floor view, not the cropped one. Any suggestions? Thanks!

Previously i had the same error, it seemed the view was too large for the sheet. However now, even the numbers are smaller so i’m a bit confused …

![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/d/d8e23e36be86c66fbf1d55b709823604d2b47266.png)

---

<div class="post-metadata">

**Author:** ![Einar\_Raknes](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/einar_raknes/32/8566_2.png) [@Einar\_Raknes](https://forum.dynamobim.com/u/Einar_Raknes)\
**Post date:** [September 29, 2016, 6:46pm UTC](https://forum.dynamobim.com/t/crop-region-by-curve/4607/20 "2016-09-29T18:46:46Z")

</div>

Nice that you got it to work!

If you wanted to combine the lists in python you could have used [zip()](https://docs.python.org/2/library/functions.html#zip)

```
for view,curves in zip(views,curvelist):

	#Create curveloop
	loop = CurveLoop()
	for c in curves:
		loop.Append(c.ToRevitType())

	#Set cropregion in Transaction
	TransactionManager.Instance.EnsureInTransaction(doc)
	view.CropBoxActive = True
	view.GetCropRegionShapeManager().SetCropShape(loop)
outputlist.append(view)
TransactionManager.Instance.TransactionTaskDone()

OUT = outputlist
```

[Next page](https://forum.dynamobim.com/t/crop-region-by-curve/4607.md?page=2)
