Doc Problems for Newby

After working in Dynamo for 8+ years i am jumping into pyrevit. Right off the bat I am having issues with something related to Document Manager. Essentially the near standard frontend of any Dynamo script where you define doc seems to be failing:

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
app=DocumentManager.Instance.CurrentUIApplication.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

av=doc.ActiveView

image

however, if I add in the pyrevit revit lines and adjust the definition of doc - it does work

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

from pyrevit import revit

#doc = DocumentManager.Instance.CurrentDBDocument
doc=revit.doc
app=DocumentManager.Instance.CurrentUIApplication.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

av=doc.ActiveView

From what I have been able to read here on the forum - this should have worked as it was - so i guess I am just curious as to is there something else i maybe doing wrong or something messed up in my pyrevit settings?

I know the pyrevit.revit stuff is there for convenience - but frankly if you are comfortable using RevitAPI docs and have lots of experience in Dynamo Python scripting - it’s actually harder to understand than just using more normal written methods (because now i have to remember two different ways to do the same thing instead of always using the same thing). i really want to avoid using the pyrevit revit methods and stick to normal Revit API (but pyrevit forms i’ll use all day :smile:)

For getting doc, uidoc and app I recommend using the pyrevit ‘revit’ library. Most of the typical Dynamo boilerplate can be left behind by using this and the ‘DB’ library.

My general pyrevit boilerplate is usually just:

from pyrevit import revit, DB, forms, script

Once you have these, you can get doc, app, uidoc using:

doc = revit.doc
app = __revit__.Application
uidoc = revit.uidoc

DB means you can avoid importing RevitAPI libraris/references. If you ever call on a class directly, simply prefix it with DB. and it will tell pyRevit this is a RevitAPI class. Methods/properties etc. require no DB as long as they act on an object pyRevit already knows is of Revit DB origin. Use this function as an example…

def createSheet(number, name, titleBlockId):
    sheet = DB.ViewSheet.Create(doc, titleBlockId)
    sheet.SheetNumber = number
    sheet.Name = name

I can get the sheet’s number without calling on DB or needing API reference before, but if I create a new sheet I call on the API class directly so I add DB. Hope that helps ease the entry to pyRevit and boilerplates!

Hi @bosborne

This is why your script don’t work.

You were not using Revit APIs, but dynamo ones.

So, since you have to change it anyway, you might as well embrace the pyrevit way :wink: