# Set Grid Extents and Bubbles

**URL:** <https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689>\
**Category:** Uncategorized\
**Created:** [February 24, 2022, 7:59pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689 "2022-02-24T19:59:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 24, 2022, 7:59pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/1 "2022-02-24T19:59:44Z")

</div>

Hello Dynamo Friends 🙂

Thanks to @c.poupin , @Nick_Boyts and @Mark.Ackerley i have a python code that [sets the grid extents](https://forum.dynamobim.com/t/change-the-curve-that-determines-the-2d-extents-of-grids-in-each-view) and i have a python code that [sets the grid bubbles](https://forum.dynamobim.com/t/python-for-loop-hide-grid-bubbles). Everything works great, but combining it in one dynamo graph does not work. After days of investigating I´m out of ideas.

First of all, the python code that sets the bubbles activates the left bubbles and if the grid is vertical it activates the upper bubbles. This works no matter in which direction the grids have been drawn.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/4/54e2a731f15aadcd169ab460ec535591a6e6d5e2.png)

Note that running the graph over and over alway gives the same result.

But now in the full graph of setting the extents and bubbles it does not work, it´s going insane. You can see that the bubbles do whatever they want and everytime i run the graph again they will change.

![ezgif.com-gif-maker](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/e/9eddee3c259030405dae06af1f604e605f0216fe.gif)

My guess is that the “Set Grids to Cropbox” code changes the grid, in particular it changes what the end and startpoint is?!

what leads me to other questions:  
When i draw a grid from left to right, the start point of the grid curve will be the one with greater x value. This is somehow reversed?!

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/f/4f68f05edaa7d3e8f79faeaae7d98b146a39bfb9.png)

So what even is DatumEnd0 and DatumEnd1, it is all confusing to me.

Dyn attached! Happy about any help 🙂  
[Set Grid and Bubbles.dyn](https://forum.dynamobim.com/uploads/short-url/mqj9HymG4zvIe8ev0vxRucmcMPN.dyn) (106.1 KB)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [February 24, 2022, 9:01pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/2 "2022-02-24T21:01:00Z")

</div>

If I run just the `Grid to Cropbox` python node the grids all get realigned to be drawn in the same direction. Is that what you’re expecting? It may be that you’re pulling the directional data before the crop changes have been committed to the project and therefore you’re doubling up on managing certain grids. You could try adding a Transaction node, but I’m guessing you might be able to just rewrite your `Grid to Cropbox` code to draw in the opposite directions (it gave me grid heads at bottom and right in my example).

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 24, 2022, 9:22pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/3 "2022-02-24T21:22:24Z")

</div>

Hello Nick 🙂

Lets have a look whats happening to two grids that are drawn in different directions:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/b/6b219ca109b1bdc0fe2efaca8501355ffec96937.png)

And at a second run:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/8/d8965fe3844043cbce2e8f8d10540a459a8faa5f.png)

We can see that the startpoint and endpoint of the curve don´t change. but the results do. This makes no sense to me. So the question is if the curve start/end are even equal to the grid start/end.

Transactions, tried that already in all possible way…

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 24, 2022, 9:25pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/4 "2022-02-24T21:25:33Z")

</div>

> [@Nick\_Boyts](#):
>
> `Grid to Cropbox` code to draw in the opposite directions (it gave me grid heads at bottom and right in my example).

very strange, this code should not set the bubbles, only the extents!  
Or do you mean the other python code? Already tried that multiple times. Doesn´t work…the graph behaves random at every run…  
The standalone “set bubbles” is in fact working…

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [February 24, 2022, 9:35pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/5 "2022-02-24T21:35:07Z")

</div>

> [@gerhard.p](#):
>
> When i draw a grid from left to right, the start point of the grid curve will be the one with greater x value. This is somehow reversed?!

In my testing, the Curve start was always at the bubble head (Grid end in my case).

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 24, 2022, 9:47pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/6 "2022-02-24T21:47:59Z")

</div>

I´m using curve start and end for my calculations.  
It seems that curve start and end do not change.

But Grid/Datum end does change, so i think the solution to my problem would be to not use the curve for calculations but the Grid/Datum. So i have to get the coordinates of DatumEnd0 and DatumEnd1 and use them for my calculations.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [February 24, 2022, 9:54pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/7 "2022-02-24T21:54:16Z")

</div>

That’s what I was going to suggest.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [February 24, 2022, 10:10pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/8 "2022-02-24T22:10:53Z")

</div>

Here’s where I’m using just the `Grid to Cropbox` node.  
 ![Revit_2022-02-24_15-57-14](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/c/dcf6a3b2f1538dbd1b8951fcb846c13cf540c8da.gif)

As for the curve direction, I think the curve is always drawn in the positive direction and that’s why they don’t change. Keep in mind the `DatumPlane` is not the same as the `Grid`. The `Grid` is only a representation of the `DatumPlane` and why there are “discrepancies” between the two. I think using `DatumEnd` will be better but you will still need to confirm left/right or top/bottom in terms of location.

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 24, 2022, 11:02pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/9 "2022-02-24T23:02:18Z")

</div>

i managed to test which end is end0 and end1 by using IsBubbleVisibleInView.

It tells me whats going on when running the “grid to cropbox” code.

The same bubble is turned off, that means the whole grid is fliped!  
This is also the proof that End0 is the End point of the grid curve. So grid curve Start/End and End0/End1 are not corresponding.

Thats at least some information. Will take me a few days to research how to get the points/coordinates of datum ends…

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/5/a5a7570cbf774a041b0afc87fbb8876ba1741627.png)

```auto

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
views = UnwrapElement(IN[0])
firstview = views[0]
elements = list()

TransactionManager.Instance.EnsureInTransaction(doc)

coll = FilteredElementCollector(doc, firstview.Id)
filt = ElementCategoryFilter(BuiltInCategory.OST_Grids)
grids = coll.WherePasses(filt).ToElements()
firstgrid = grids[0]

x = firstgrid.IsBubbleVisibleInView(DatumEnds.End0, firstview)

OUT = x

```

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 25, 2022, 11:29am UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/10 "2022-02-25T11:29:17Z")

</div>

I found the solution to this problem here:

> **[How to know true DatumEnds.End0 is left or right in view?](https://forums.autodesk.com/t5/revit-api-forum/how-to-know-true-datumends-end0-is-left-or-right-in-view/td-p/9584030)**
>
> Grid.Curve.GetEndPoint(0) does not return the same "End" with DatumEnds.End0 in view. Therefore, how to define DatumEnds.End0 is on the left or on the right in view? Here is my code and it not work correctly.   public class GridBubble :...

I have to transform the curve ends to coordinate space of view.

```auto
XYZ[] EP = new XYZ[] 
				Tx.Inverse.OfPoint(crv.GetEndPoint(0)),
				Tx.Inverse.OfPoint(crv.GetEndPoint(1))

```

Will try my luck on getting this to work in python.

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 26, 2022, 8:18pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/12 "2022-02-26T20:18:22Z")

</div>

Seems like that i got it:

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

```auto
Point = UnwrapElement(IN[1])

Tx = Transform.Identity
Tx.BasisX = view.RightDirection
Tx.BasisY = view.UpDirection
Tx.BasisZ = XYZ.BasisZ

TransPoint = Tx.Inverse.OfPoint(Point)

OUT = TransPoint

```

I´m going to try if this points are any help to my problem…

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 26, 2022, 8:50pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/13 "2022-02-26T20:50:46Z")

</div>

I´m now using the transformed endpoints for my bubble calculations, wohoo!  
The resullt is the same as before with the curve endpoints, the bubble will be at the wrong end when the grid gets fliped 😕

I think i did exactly what was suggested in the autodesk board…am i doing something wrong?  
My transformation obviousely doesnt make any difference to the points…

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/5/95d1b1e811d3615faab3704b9894910f0fcd9c55.png)

```auto
view = views[0]
grid = grids[0]
Point = UnwrapElement(IN[1])

Tx = Transform.Identity
Tx.BasisX = view.RightDirection
Tx.BasisY = view.UpDirection
Tx.BasisZ = XYZ.BasisZ

TransPoint = Tx.Inverse.OfPoint(Point)
xvalue = TransPoint.X

OUT = xvalue

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/2/92ae613f9bdc1eca23e53c39e6ecac92a6d89db7.png)

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 27, 2022, 9:37pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/14 "2022-02-27T21:37:56Z")

</div>

I found a solution that works.  
Instead of writing code to find End0 and End1, i just looked if i can take any values from the “Grid to Cropbox” code. I can!

I´m now taking points of the newGLine, they are corresponding with DatumEnds 🙂

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

Now there is only one problem left, i can only output the points of the first gridline, how can i output a list of points from all the gridlines?

```auto
startpointXYZ = newGLine.GetEndPoint(0)
endpointXYZ = newGLine.GetEndPoint(1)
startpoint = startpointXYZ.ToPoint()
endpoint = endpointXYZ.ToPoint()

OUT = lstInputViews, startpoint, endpoint

```

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/e/4e0ffc90bb295df8e1abb5268dce8e81a52d4015.png)

full code:

```auto
for activView in lstInputViews:

	#
	activView.CropBoxVisible = True
	doc.Regenerate()
	#
	cropBox = activView.CropBox 

	fecGrids = FilteredElementCollector(doc, activView.Id).OfClass(DatumPlane).ToElements()
	cutOffset = fecGrids[0].GetCurvesInView(DatumExtentType.ViewSpecific, activView)[0].GetEndPoint(0).Z
	fecGrids = [x for x in fecGrids if isinstance(x, DB.Grid)]

	outLst = []
	shpManager = activView.GetCropRegionShapeManager()
	boundLines = shpManager.GetCropShape()[0]

	if activView.ViewDirection.IsAlmostEqualTo(XYZ(0,0,1)):
		# get Current Elevation of boundLines
		currentZ = list(boundLines)[0].GetEndPoint(0).Z
		# transform boundLines CurveLoop
		tf = Transform.CreateTranslation( XYZ(0,0, cutOffset - currentZ))
		boundLines = CurveLoop.CreateViaTransform(boundLines, tf)
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
	else:
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
TransactionManager.Instance.TransactionTaskDone()

startpointXYZ = newGLine.GetEndPoint(0)
endpointXYZ = newGLine.GetEndPoint(1)
startpoint = startpointXYZ.ToPoint()
endpoint = endpointXYZ.ToPoint()

OUT = lstInputViews, startpoint, endpoint

```

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 27, 2022, 10:17pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/15 "2022-02-27T22:17:15Z")

</div>

First step, i have to make a list out of the newGLines somehow 🤔 How can i do that?  
Then i would loop through that list to get the points and output the lists of points…

Something like that?!

```auto
	outLstEnd = []
	outLstStart = []

for newGLine in fecGrids

	startpointXYZ = newGLine.GetEndPoint(0)
	endpointXYZ = newGLine.GetEndPoint(1)
	startpoint = startpointXYZ.ToPoint()
	endpoint = endpointXYZ.ToPoint()
	
	outLstEnd.append(endpoint)
	outLstStart.append(startpoint)

OUT = lstInputViews, outLstEnd, outLstStart

```

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 28, 2022, 9:39am UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/17 "2022-02-28T09:39:45Z")

</div>

Script is now working by using the new gridline points to set the grid bubbles 🙂

Thanks for the help @Nick_Boyts

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/3/53eeda8d5d24575cc14bc095f685442486e04f0d.png)

```auto
import clr
import System
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry as DSGeo
from Autodesk.DesignScript.Geometry import *

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

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

def createDatumLine(boundLines, grid):
	gridLine = None
	curveG = grid.Curve#.ToProtoType()
	vectGrid = curveG.Direction
	lstPtToLine = []
	for lineBound in boundLines:
		rayc = DB.Line.CreateUnbound(XYZ(curveG.Origin.X, curveG.Origin.Y, lineBound.GetEndPoint(0).Z) , vectGrid)
		outInterR = clr.Reference[IntersectionResultArray]()
		result = rayc.Intersect(lineBound, outInterR)
		print(result)
		if result == SetComparisonResult.Overlap:
			interResult = outInterR.Value
			lstPtToLine.append(interResult[0].XYZPoint)
	print(lstPtToLine)
	if len(lstPtToLine) == 2:
		P1 = lstPtToLine[0].ToPoint()
		P2 = lstPtToLine[1].ToPoint()
		TransXYZ1 = Geometry.Translate(P1 ,0,0,0)
		TransXYZ2 = Geometry.Translate(P2 ,0,0,0)
		TransPoint1 = TransXYZ1.ToXyz()
		TransPoint2 = TransXYZ2.ToXyz()
		gridLine = Autodesk.Revit.DB.Line.CreateBound(TransPoint1, TransPoint2)
	return gridLine
	
activView = UnwrapElement(IN[0])
lstInputViews = UnwrapElement(IN[0])
TransactionManager.Instance.EnsureInTransaction(doc)

for activView in lstInputViews:

	#
	activView.CropBoxVisible = True
	doc.Regenerate()
	#
	cropBox = activView.CropBox 

	fecGrids = FilteredElementCollector(doc, activView.Id).OfClass(DatumPlane).ToElements()
	cutOffset = fecGrids[0].GetCurvesInView(DatumExtentType.ViewSpecific, activView)[0].GetEndPoint(0).Z
	fecGrids = [x for x in fecGrids if isinstance(x, DB.Grid)]

	outLst = []
	newGLineList = []
	shpManager = activView.GetCropRegionShapeManager()
	boundLines = shpManager.GetCropShape()[0]

	if activView.ViewDirection.IsAlmostEqualTo(XYZ(0,0,1)):
		# get Current Elevation of boundLines
		currentZ = list(boundLines)[0].GetEndPoint(0).Z
		# transform boundLines CurveLoop
		tf = Transform.CreateTranslation( XYZ(0,0, cutOffset - currentZ))
		boundLines = CurveLoop.CreateViaTransform(boundLines, tf)
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			newGLineList.append(newGLine) 
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
	else:
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			newGLineList.append(newGLine)
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
TransactionManager.Instance.TransactionTaskDone()

startpointList = []
endpointList = []

for newGLine in newGLineList:
	
	startpointXYZ = newGLine.GetEndPoint(0)
	endpointXYZ = newGLine.GetEndPoint(1)
	startpoint = startpointXYZ.ToPoint()
	endpoint = endpointXYZ.ToPoint()
	startpointList.append(startpoint)
	endpointList.append(endpoint)

OUT = lstInputViews, startpointList, endpointList

```

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [February 28, 2022, 10:26pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/18 "2022-02-28T22:26:09Z")

</div>

Hello @c.poupin 🙂

By adding an output of grid angle to your code i could make the process of setting the bubbles much more efficient 😃

This is now the exact result i want:

![ezgif.com-gif-maker](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/e/1ec17ccd907ffd64298b18cf907a9fb48ceceff7.gif)

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

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

clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitServices")
import RevitServices

from RevitServices.Persistence import *
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

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

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

if isinstance(IN[1], list):
	gridList = UnwrapElement(IN[0])
	viewList = UnwrapElement(IN[1])
	angleList = UnwrapElement(IN[2])
else:
	gridList = [UnwrapElement(IN[0])]
	viewList = [UnwrapElement(IN[1])]
	angleList = [UnwrapElement(IN[2])]
	
TransactionManager.Instance.EnsureInTransaction(doc)

for grids, angles, view in zip(gridList, angleList, viewList):

	for grid, angle in zip(grids, angles):
		if 121< angle <300:	
			grid.ShowBubbleInView(DatumEnds.End1,view) 
			grid.HideBubbleInView(DatumEnds.End0,view)
		else:
			grid.ShowBubbleInView(DatumEnds.End0,view)
			grid.HideBubbleInView(DatumEnds.End1,view)
	
TransactionManager.Instance.TransactionTaskDone()

doc.Regenerate()

OUT = viewList

```

```auto
import clr
import math
import System
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry as DSGeo
from Autodesk.DesignScript.Geometry import *

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

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

def createDatumLine(boundLines, grid):
	gridLine = None
	curveG = grid.Curve#.ToProtoType()
	vectGrid = curveG.Direction
	lstPtToLine = []
	for lineBound in boundLines:
		rayc = DB.Line.CreateUnbound(XYZ(curveG.Origin.X, curveG.Origin.Y, lineBound.GetEndPoint(0).Z) , vectGrid)
		outInterR = clr.Reference[IntersectionResultArray]()
		result = rayc.Intersect(lineBound, outInterR)
		print(result)
		if result == SetComparisonResult.Overlap:
			interResult = outInterR.Value
			lstPtToLine.append(interResult[0].XYZPoint)
	print(lstPtToLine)
	if len(lstPtToLine) == 2:
		P1 = lstPtToLine[0].ToPoint()
		P2 = lstPtToLine[1].ToPoint()
		TransXYZ1 = Geometry.Translate(P1 ,0,0,0)
		TransXYZ2 = Geometry.Translate(P2 ,0,0,0)
		TransPoint1 = TransXYZ1.ToXyz()
		TransPoint2 = TransXYZ2.ToXyz()
		gridLine = Autodesk.Revit.DB.Line.CreateBound(TransPoint1, TransPoint2)
	return gridLine
	
activView = UnwrapElement(IN[0])
lstInputViews = UnwrapElement(IN[0])
TransactionManager.Instance.EnsureInTransaction(doc)

for activView in lstInputViews:

	#
	activView.CropBoxVisible = True
	doc.Regenerate()
	#
	cropBox = activView.CropBox 

	fecGrids = FilteredElementCollector(doc, activView.Id).OfClass(DatumPlane).ToElements()
	cutOffset = fecGrids[0].GetCurvesInView(DatumExtentType.ViewSpecific, activView)[0].GetEndPoint(0).Z
	fecGrids = [x for x in fecGrids if isinstance(x, DB.Grid)]

	outLst = []
	newGLineList = []
	shpManager = activView.GetCropRegionShapeManager()
	boundLines = shpManager.GetCropShape()[0]

	if activView.ViewDirection.IsAlmostEqualTo(XYZ(0,0,1)):
		# get Current Elevation of boundLines
		currentZ = list(boundLines)[0].GetEndPoint(0).Z
		# transform boundLines CurveLoop
		tf = Transform.CreateTranslation( XYZ(0,0, cutOffset - currentZ))
		boundLines = CurveLoop.CreateViaTransform(boundLines, tf)
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			newGLineList.append(newGLine) 
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
	else:
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			newGLineList.append(newGLine)
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
TransactionManager.Instance.TransactionTaskDone()

startpointList = []
endpointList = []
rotationAngleList = []

for newGLine in newGLineList:
	
	startpointXYZ = newGLine.GetEndPoint(0)
	endpointXYZ = newGLine.GetEndPoint(1)
	startpoint = startpointXYZ.ToPoint()
	endpoint = endpointXYZ.ToPoint()
	startpointList.append(startpoint)
	endpointList.append(endpoint)
	
	vector = newGLine.Direction
	rotationAngle = abs(math.degrees(vector.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ)))
	rotationAngleList.append(rotationAngle)

OUT = lstInputViews, startpointList, endpointList, rotationAngleList

```

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [March 1, 2022, 7:12am UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/19 "2022-03-01T07:12:00Z")

</div>

nice 👌 👍

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [March 1, 2022, 10:31am UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/20 "2022-03-01T10:31:42Z")

</div>

Classic, first test in real project and it does not work 😃

Getting the right angle does not work if the view has a **rotation**.

I can not use `AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ)` like that.

```auto
rotationAngle = abs(math.degrees(vector.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ)))

```

I somehow have to add **view.rightdirection** to define the plane.

edit:

```auto
rotationAngle = abs(math.degrees(vector.AngleOnPlaneTo(activView.RightDirection, XYZ.BasisZ)))

```

Seems to work like that!

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [March 1, 2022, 11:01pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/21 "2022-03-01T23:01:32Z")

</div>

Code works for multiple views but my output only works for 1 view.  
After hours of trying i ask for help again.

I don´t know how to deal with the sublists:

Working for only one view:

```auto
startpointList = []
endpointList = []
rotationAngleList = []

for newGLine in newGLineList:

	startpointXYZ = newGLine.GetEndPoint(0)
	endpointXYZ = newGLine.GetEndPoint(1)
	startpoint = startpointXYZ.ToPoint()
	endpoint = endpointXYZ.ToPoint()
	startpointList.append(startpoint)
	endpointList.append(endpoint)
	
	vector = newGLine.Direction
	rotationAngle = abs(math.degrees(vector.AngleOnPlaneTo(activView.RightDirection, XYZ.BasisZ)))
	rotationAngleList.append(rotationAngle)

OUT = lstInputViews, rotationAngleList

```

One of my failed attempts:

```auto
startpointList = []
endpointList = []
rotationAngleList = []

for newGLines in newGLineList:
	for newGLine in newGLines:

```

I got many differenht lists, for example sublists with too many items…but i can´t get the result i want 😕 Maybe i have to use this mysteriouse [x for x in y] thing i don´t understand.  
I don´t think i have to zip lists together in this case?

Happy about any help 🙂 thanks!

edit:  
The problem has to be something even before, because newGLineList ist also only a list for 1 view.

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/0/f098fcf21efae0e40e78d5af0aa544d27360f772.png)

Full code:

```auto
import clr
import math
import System
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry as DSGeo
from Autodesk.DesignScript.Geometry import *

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

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

def createDatumLine(boundLines, grid):
	gridLine = None
	curveG = grid.Curve#.ToProtoType()
	vectGrid = curveG.Direction
	lstPtToLine = []
	for lineBound in boundLines:
		rayc = DB.Line.CreateUnbound(XYZ(curveG.Origin.X, curveG.Origin.Y, lineBound.GetEndPoint(0).Z) , vectGrid)
		outInterR = clr.Reference[IntersectionResultArray]()
		result = rayc.Intersect(lineBound, outInterR)
		print(result)
		if result == SetComparisonResult.Overlap:
			interResult = outInterR.Value
			lstPtToLine.append(interResult[0].XYZPoint)
	print(lstPtToLine)
	if len(lstPtToLine) == 2:
		P1 = lstPtToLine[0].ToPoint()
		P2 = lstPtToLine[1].ToPoint()
		TransXYZ1 = Geometry.Translate(P1 ,0,0,0)
		TransXYZ2 = Geometry.Translate(P2 ,0,0,0)
		TransPoint1 = TransXYZ1.ToXyz()
		TransPoint2 = TransXYZ2.ToXyz()
		gridLine = Autodesk.Revit.DB.Line.CreateBound(TransPoint1, TransPoint2)
	return gridLine
	
activView = UnwrapElement(IN[0])
lstInputViews = UnwrapElement(IN[0])
TransactionManager.Instance.EnsureInTransaction(doc)

for activView in lstInputViews:

	#
	activView.CropBoxVisible = True
	doc.Regenerate()
	#
	cropBox = activView.CropBox 

	fecGrids = FilteredElementCollector(doc, activView.Id).OfClass(DatumPlane).ToElements()
	cutOffset = fecGrids[0].GetCurvesInView(DatumExtentType.ViewSpecific, activView)[0].GetEndPoint(0).Z
	fecGrids = [x for x in fecGrids if isinstance(x, DB.Grid)]

	outLst = []
	newGLineList = []
	shpManager = activView.GetCropRegionShapeManager()
	boundLines = shpManager.GetCropShape()[0]

	if activView.ViewDirection.IsAlmostEqualTo(XYZ(0,0,1)):
		# get Current Elevation of boundLines
		currentZ = list(boundLines)[0].GetEndPoint(0).Z
		# transform boundLines CurveLoop
		tf = Transform.CreateTranslation( XYZ(0,0, cutOffset - currentZ))
		boundLines = CurveLoop.CreateViaTransform(boundLines, tf)
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			newGLineList.append(newGLine) 
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
	else:
		for grid in fecGrids:
			outLst.append(grid.Curve.ToProtoType())
			newGLine = createDatumLine(boundLines, grid)
			newGLineList.append(newGLine)
			if newGLine is not None:
				grid.SetCurveInView(DatumExtentType.ViewSpecific, activView, newGLine)
			
TransactionManager.Instance.TransactionTaskDone()

startpointList = []
endpointList = []
rotationAngleList = []

for newGLine in newGLineList:

	startpointXYZ = newGLine.GetEndPoint(0)
	endpointXYZ = newGLine.GetEndPoint(1)
	startpoint = startpointXYZ.ToPoint()
	endpoint = endpointXYZ.ToPoint()
	startpointList.append(startpoint)
	endpointList.append(endpoint)
	
	vector = newGLine.Direction
	rotationAngle = abs(math.degrees(vector.AngleOnPlaneTo(activView.RightDirection, XYZ.BasisZ)))
	rotationAngleList.append(rotationAngle)

OUT = newGLineList

```

---

<div class="post-metadata">

**Author:** ![gerhard.p](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gerhard.p/32/147675_2.png) [@gerhard.p](https://forum.dynamobim.com/u/gerhard.p)\
**Post date:** [March 1, 2022, 11:23pm UTC](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689/22 "2022-03-01T23:23:08Z")

</div>

Another failed attemt that gives me a weird long list insted of sublists:

```auto
startpointList = []
endpointList = []
rotationAngleList = []

for activView in lstInputViews:
	for newGLine in newGLineList:

		startpointXYZ = newGLine.GetEndPoint(0)
		endpointXYZ = newGLine.GetEndPoint(1)
		startpoint = startpointXYZ.ToPoint()
		endpoint = endpointXYZ.ToPoint()
		startpointList.append(startpoint)
		endpointList.append(endpoint)
	
		vector = newGLine.Direction
		rotationAngle = abs(math.degrees(vector.AngleOnPlaneTo(activView.RightDirection, XYZ.BasisZ)))
		rotationAngleList.append(rotationAngle)

OUT = rotationAngleList

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/1/a176f53caba1e02bbab48d5a92a4a0a877cdccb3.png)

[Next page](https://forum.dynamobim.com/t/set-grid-extents-and-bubbles/73689.md?page=2)
