No announcement yet.

RevitAPI Docs to Python

This is a sticky topic.
  • Filter
  • Time
  • Show
Clear All
new posts

    Super happy they record the presentations. Cannot wait to watch it. Thanks Sol


      ModNote from cellophane: Copied from this thread:

      Hello all,

      The primary difference between Python (standard) and Dynamo's implementation is that Dynamo uses IronPython - and an older version at that. A lot of functionality from Python is ported through - but the primary focus of IronPython is to allow for the .NET framework to be imported into the Dynamo environment.

      In terms of portability, there are a few Dynamo centric parts you need to implement. Predominantly these are surrounding imports.

      RevitPythonShell version (Note needs to be indented properly to work):

      [COLOR=#2E2E2E]from Autodesk.Revit.DB import Transaction, Element[/COLOR]
      [COLOR=#2E2E2E]from Autodesk.Revit.DB import FilteredElementCollector[/COLOR]
      [COLOR=#2E2E2E]from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element[/COLOR]
      [COLOR=#2E2E2E]from Autodesk.Revit.DB import ViewFamily
      [COLOR=#2E2E2E]uidoc = __revit__.ActiveUIDocument[/COLOR]
      [COLOR=#2E2E2E]doc = uidoc.Document
      [COLOR=#2E2E2E]t = Transaction(doc, 'Create Drafting View')[/COLOR]
      [COLOR=#2E2E2E]def get_drafting_type_id():[/COLOR]
      [COLOR=#2E2E2E]    viewfamily_types = FilteredElementCollector(doc).OfClass(ViewFamilyTy pe)[/COLOR]
      [COLOR=#2E2E2E]    for i in viewfamily_types:[/COLOR]
      [COLOR=#2E2E2E]        if i.ViewFamily == ViewFamily.Drafting:[/COLOR]
      [COLOR=#2E2E2E]            return i.Id[/COLOR]
      [COLOR=#2E2E2E]drafting_type_id = get_drafting_type_id()[/COLOR]
      [COLOR=#2E2E2E]drafting_view = ViewDrafting.Create(doc, drafting_type_id)
      Dynamo Version:

      [B]import[/B] clr
      [B]from[/B] Autodesk.Revit.DB [B]import[/B] *
      [B]from[/B] RevitServices.Persistence [B]import[/B] DocumentManager
      [B]from[/B] RevitServices.Transactions [B]import[/B] TransactionManager
      doc = DocumentManager.Instance.CurrentDBDocument
      [I]def[/I] [B]get_drafting_type_id[/B]():
          viewfamily_types = [B]FilteredElementCollector[/B](doc).[B]OfClass[/B](ViewFamilyType)
          [B]for[/B] i [B]in[/B] viewfamily_types:
              [B]if[/B] i.ViewFamily == ViewFamily.Drafting:
                  return i.Id
      [B]if[/B] IN[0]:
          forceClose = TransactionManager.Instance.[B]ForceCloseTransaction[/B]()
          t = [B]Transaction[/B](doc, 'Create Drafting View')
          drafting_type_id = [B]get_drafting_type_id[/B]()
          drafting_view = ViewDrafting.[B]Create[/B](doc, drafting_type_id)
      OUT = drafting_view
      The variances are as follows:

      1. Importing. You'll have to append paths to the CLR (Common Language Runtime) module. This is an IronPython thing. Any module not hard-coded with a path inside of the Dynamo Python IDE (i.e ProtoGeometry) will need to be appended here. If you are using normal Python modules inside of Dynamo, you'll need to append the system path too**.
      2. The way we pull the Document and Transactions is typically different. It's via the RevitServices manager. You can pull the other way as stipulated in your original code - but typically it's done as I have in my redux of the code.
      3. Dynamo interacts with Transactions differently than RevitPythonShell. So you'll have to run a 'ForceCloseTransaction' call before creating a new one if you wish to take this path (Named transactions). The reason for this is that Revit can get stuck inside a Transaction and Dynamo won't execute. Otherwise, you simply call 'DocumentManager.Instance.EnsureInTransaction(doc)' at the start and 'DocumentManager.Transaction.TransactionTaskDone()' at the end - this is the safer path hence most Python code inside of Dynamo using it.
      4. You'll need to put a boolean check in this kind of script - or you'll run into Recursion issues. So I've put a simple switch in - toggle true and it will create, toggle false and it won't. Every time you 'touch' the Python code, it will re-execute. This is either by changing the code inside, or flicking the boolean, or toggling input port amount.
      5. You'll also need an OUT port - of which you can push anything out.

      I hope this helps!

      ** If you wish to append the System path, you do so as follows:

      import sys
      sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
      import csv
      import math
      Last edited by cellophane; April 30, 2018, 01:49 PM.

      Sol Amour

      Architectural Explorer, Digital warrior, Affectual adventurer and Curious Human Being
      Portfolio Website @ Cargo Collective


      Related Topics