Modify view range

Hello guys,
I am trying to use this python script that Mostafa kindly shared long time ago but I am having a problem.
Any one can help???
Python:

import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import*
clr.AddReference(‘RevitServices’)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

views = UnwrapElement(IN[0])
ltop = UnwrapElement(IN[1])
toffset = IN[2]
lbottom = UnwrapElement(IN[3])
boffset = IN[4]
count = 0

TransactionManager.Instance.EnsureInTransaction(doc)

for v,lt,tof,lb,bof in zip(views,ltop,toffset,lbottom,boffset):

#getting the viewrange of the view
viewrange = v.GetViewRange()

#Setting top clip plane of the viewrange
#	Setting level of topclip plane
viewrange.SetLevelId(PlanViewPlane.TopClipPlane,lt.Id)
#	setting offset of top clip plane
viewrange.SetOffset(PlanViewPlane.TopClipPlane,tof)

#Setting bottom clip plane of the viewrange
#	Setting level of bottom clip plane
viewrange.SetLevelId(PlanViewPlane.BottomClipPlane,lb.Id)
#	setting offset of bottom clip plane
viewrange.SetOffset(PlanViewPlane.BottomClipPlane,bof)


#Applying the viewrange to the view
v.SetViewRange(viewrange)
count = count +1

TransactionManager.Instance.TransactionTaskDone()

OUT = ‘%d view ranges altered’ %(count)

1 Like

The error you had shown in the last post said you were getting a “ViewPlan is not iterable” error, so without seeing more, I am assuming it is because you are trying to do a for loop with only one element. Meaning, the script is expecting a list of views as an input but you are only giving one. Either add in more views or try changing the python script to this:

import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import*
clr.AddReference(‘RevitServices’)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

v = UnwrapElement(IN[0])
lt = UnwrapElement(IN[1])
tof = IN[2]
lb = UnwrapElement(IN[3])
bof = IN[4]
count = 0

TransactionManager.Instance.EnsureInTransaction(doc)

#getting the viewrange of the view
viewrange = v.GetViewRange()

#Setting top clip plane of the viewrange
#	Setting level of topclip plane
viewrange.SetLevelId(PlanViewPlane.TopClipPlane,lt.Id)
#	setting offset of top clip plane
viewrange.SetOffset(PlanViewPlane.TopClipPlane,tof)

#Setting bottom clip plane of the viewrange
#	Setting level of bottom clip plane
viewrange.SetLevelId(PlanViewPlane.BottomClipPlane,lb.Id)
#	setting offset of bottom clip plane
viewrange.SetOffset(PlanViewPlane.BottomClipPlane,bof)

#Applying the viewrange to the view
v.SetViewRange(viewrange)
count = count +1
TransactionManager.Instance.TransactionTaskDone()

OUT = ‘%d view ranges altered’ %(count)

Hello Kenny,
Thank you very much for your answer, however your script is giving me a similar problem.
I have created a list of views and used the previous graph and used yours as well and both are giving me the same problem.
IronPythonEvaluator.EvaluateIronPythonScript
operation failed.
unexpected token
See image attached…
Many thanks!!!Untitled-2

Sorry the error was due to copying and pasting. But the problem you are having is that you are not using the correct inputs. In the original post, he uses floors as input for IN[0], and levels as IN[1] and IN[3], in this way:
IN[0] = the view to change
IN[1] = level of top clip range, meaning you need a level as an input, not a number
IN[2] = offset of the top clip range, integer
IN[3] = level of bottom clip range, again needs a level input
IN[4] = offset of the bottom clip range, integer

As for the script for single views, the correct script is:

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import*
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

v = UnwrapElement(IN[0])
lt = UnwrapElement(IN[1])
tof = IN[2]
lb = UnwrapElement(IN[3])
bof = IN[4]
count = 0

TransactionManager.Instance.EnsureInTransaction(doc)

#getting the viewrange of the view
viewrange = v.GetViewRange()

#Setting top clip plane of the viewrange
#	Setting level of topclip plane
viewrange.SetLevelId(PlanViewPlane.TopClipPlane,lt.Id)
#	setting offset of top clip plane
viewrange.SetOffset(PlanViewPlane.TopClipPlane,tof)

#Setting bottom clip plane of the viewrange
#	Setting level of bottom clip plane
viewrange.SetLevelId(PlanViewPlane.BottomClipPlane,lb.Id)
#	setting offset of bottom clip plane
viewrange.SetOffset(PlanViewPlane.BottomClipPlane,bof)

#Applying the viewrange to the view
v.SetViewRange(viewrange)
count = count +1
TransactionManager.Instance.TransactionTaskDone()

OUT = '%d view ranges altered' %(count)
2 Likes

Thank you very much Kenny,
As you can tell I cannot read python, do you know how can include in the script the CUT PLANE OFFSET and VIEW DEPTH ASSOCIATED LEVEL and OFFSET???
Many thanks in advanceCapture

Hello Kenny,
Thank you very much for your help but the attached script is not working for me.

Warning attached

Edit: Sorry, let me keep working on it.

As for associated level, there is a node way and a python way. Node way would be checking parameter by name “Associated Level” but it returns a string and you would have to find the level with the string name. Python would use the GenLevel property.

I don’t know the exact command for converting Dynamo units into Revit API units but for now I have an approximation embedded into the script and it works fine. Only uses the view, top offset, and bottom offset to edit the view range.

viewrange.dyn (3.9 KB)

Edit: this one uses convert between units before the python script: viewrange2.dyn (5.1 KB)

1 Like

Hello Kenny,
Captureviewrange(Cutplaneoffset).dyn (5.7 KB)

Now works completelly fine,
I have added a new line to the python script to adjust as well the cut plane offset and seems to be working fine too.

However if I want to apply the cut plane offset based on a list, of values the python script give me an error.
Sorry for my ignorance but I have just starting getting familiar with python.

How easy is to modify the script so we can apply these changes to a list of views???

Thank you very muuuuch!!!

*Attached dyn with the new line.

1 Like

modifyViewrange.dyn (15.8 KB)

Funnily enough, after I made the last script, I made a version to work with lists but didn’t include cut plane, so I added your line to it.

This version can do a single view or list of views but it only accepts single numbers as offsets.

1 Like

Amazing Kenny!!!, you are a genious

How difficult, or is it possible to use a list for the offsets as well???

I can do it easily. Do you need an offset for view depth as well? I had it set default to 0.

Really?

Yes that would be great!!!, is it hard to do it?

modifyViewrange.dyn (15.7 KB)

Okay, this should allow for complete customization on either single view or multiple views, with single standard offset or list of offsets.

9 Likes

This is Amaaaaaazing!!!
You are a star, thank you very muuuuch for your support!!!

1 Like

Just registered to say it.
Pretty f**** amazing job by Kenny!

1 Like

Hi Kenny,

I’m trying to use it for Ceilingplans, but can’t get it working. Does it have to work, or does the python code need to change?

Hope to hear from u!

-Thomas

The same script should work, just change the codeblock next to ViewPlan from
"Revit.Elements.Views.FloorPlanView";
to
"Revit.Elements.Views.CeilingPlanView";

and then you can filter or not filter how you want. The filter part is the 2nd codeblock, where "Level"; is the word used to filter through the views. You can change it or ignore the nodes all together if you don’t want to filter.

3 Likes

Hi Kennyb6 i’m new learner study dynamo and code in python. i tried to run your code in my project, but i don’t understand why the result is this (in the picture). i think this code have to add a line code to python understand what is units of number you put in code… isn’t it? and how do i add it. i sorry to bother you, and i hope you reply to me. thank you very much
1 2 3