Casting active view

How to cast active view to 3d view in python


If you’re looking to set the active view check out this post.

When you unwrap it should enough, because python manage itself casting.
UnwrapElement( v ) where v is you Dynamo view

Iam accessing the active view directly from the python node … all i need to cast the view in the third parameter in that code line
ReferenceIntersector(filter, FindReferenceTarget.Face , “the active plan view that i need to be casted into 3d view”), i know that the python do the cast for me but error appears says that “expected View3D , got ViewPlan”

Are you sure that the active view is not a plan?
It seems so.
Maybe it was a plan? probably the script need to refresh, try changing something at beginning of script

The active view is a plan not a 3D plan but i want to cast type it to 3D .
My code is something similar to the code in the picture but in python

That means ,it is impossible to cast objects in python?

you don’t need casting for this case. Likewise, for example, LocationCurve & LocationPoint both derived from Location class. But when u code in Python it will just be like elem.Location.Point or elem.Location.Curve. Whereas in C# it would be like how you did it ( (LocationCurve) elem.Location).Curve.

again, if your error is this

“expected View3D , got ViewPlan”

the point is not casting the view ( as I told you ) but you are feeding a wrong view to the method. I know the ReferenceIntersector and everything run fine from my side.
If you need a specific help, please post your code snipped at least :slight_smile:

1 Like

yeah , i got it know that python make the casting for me , and i really was feeding a wrong view to the method . but here is a new problem , the element is a pipe , i got it’s location (so it curve based) , and i get the end point of this curve , i want to get the hitting point of the element above the end of pipe, but it gives me null however , there is an element from the same category filter above the pipe.

    import clr

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

import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView

element = UnwrapElement(IN[1])
geo = element.Location.Curve.EndPoint
vector = XYZ(0,0,1)

kk = Enum.ToObject(BuiltInCategory, IN[0].Id)
filter = ElementCategoryFilter(kk)
refi = ReferenceIntersector(filter, FindReferenceTarget.Face, view)
refc = refi.FindNearest(geo , vector)
reference = refc.GetReference();
intpoint = reference.GlobalPoint

OUT = intpoint

This is unrelated to the question posed and should really have another thread raised (just so other users with a similar question can find the right answer).

However…When doing ray casting, it is sometimes useful to bring the origin back a little. If the point and face are coincident, the ray might not intersect (since they both lie in the same plane this may return null). You could use Curve.GetPointAtParameter(0.9) and use this as your origin, this way you ensure that the origin does not lie in the plane of the face.

You should also do a little null checking to avoid warnings should an intersection not be found. Say if not refc == None: OUT = refc.GetReference().GlobalPoint otherwsie else: OUT = None.

Additionally, you should also check if the ActiveView is of type View3D. For example if doc.ActiveView.ViewType == ViewType.ThreeD: then perform intersection test, else: OUT == "Please open a 3D view to run this node".

Or you could wrap it all up in a try except and handle errors accordingly.


Thank you , that’s was very useful for me ,but i can not find GetPointAtParameter() method at revit api document

Sorry, my bad, it’s a dynamo method. I see you are purely using Revit API, so Instead use…

geo.Evaluate(0.9, True)

This will return an XYZ of 90% along that line. You can play with this value so it is a tighter fit to the endpoint, say 0.95.

Hi @eng.elfeky,

Can you mark the post @FabioDeAgostini submitted earlier as the solution as this was actually the correct solution to your original question…

It’s only so when people search for the solution to that specific question they know what was the actual solution. The rest after that is a bonus! :wink: