Donate Now Goal amount for this year: 3000 USD, Received: 1207 USD (40%)

Results 1 to 9 of 9
Like Tree6Likes
  • 1 Post By cellophane
  • 2 Post By cellophane
  • 2 Post By amoursol
  • 1 Post By cellophane

Thread: portability between Revit API Python and Revit Dynamo Python

  1. #1
    Senior Member Ning Zhou's Avatar
    Join Date
    July 20, 2011
    Posts
    850
    Current Local Time
    03:06 AM

    portability between Revit API Python and Revit Dynamo Python

    for instance, i have Revit API Python code (by Gui Talarico) to create drafting view which can run @ RevitPythonShell
    from Autodesk.Revit.DB import Transaction, Element
    from Autodesk.Revit.DB import FilteredElementCollector
    from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element
    from Autodesk.Revit.DB import ViewFamily
    uidoc = __revit__.ActiveUIDocument
    doc = uidoc.Document
    t = Transaction(doc, 'Create Drafting View')
    t.Start()
    def get_drafting_type_id():
    viewfamily_types = FilteredElementCollector(doc).OfClass(ViewFamilyTy pe)
    for i in viewfamily_types:
    if i.ViewFamily == ViewFamily.Drafting:
    return i.Id
    drafting_type_id = get_drafting_type_id()
    drafting_view = ViewDrafting.Create(doc, drafting_type_id)
    t.Commit()

    i want to "translate" to Revit Dynamo Python so i can use it within Dynamo, any standard way to do it?

  2. #2
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,104
    Current Local Time
    06:06 AM
    Don't know if it will help, but Sol put together an awesome post on this type of thing: https://www.revitforum.org/dynamo-bi...cs-python.html
    PolynineStudio likes this.

  3. #3
    Senior Member Ning Zhou's Avatar
    Join Date
    July 20, 2011
    Posts
    850
    Current Local Time
    03:06 AM
    thanks chad, yes, i do hope Sol can see my post and ...

    well, my question is not about portability between Revit API and Dynamo, it's about difference between 2 Pythons, one in Revit API another one in Dynamo Python Script node, for instance, "doc = __revit__.ActiveUIDocument.Document" from Revit API Python code, cannot be used in Dynamo Python Script code, instead "doc = DocumentManager.Instance.CurrentDBDocument" is used there.
    Last edited by Ning Zhou; April 27th, 2018 at 07:16 PM.

  4. #4
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,104
    Current Local Time
    06:06 AM
    you are way over my head at this point...
    Dave Jones and elton williams like this.

  5. #5
    Senior Member Ning Zhou's Avatar
    Join Date
    July 20, 2011
    Posts
    850
    Current Local Time
    03:06 AM

  6. #6
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    11:06 AM
    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):

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

    Code:
    import clr
    clr.AddReference('RevitAPI')
    clr.AddReference('RevitServices')
    from Autodesk.Revit.DB import *
    from RevitServices.Persistence import DocumentManager
    from RevitServices.Transactions import TransactionManager
    
    
    doc = DocumentManager.Instance.CurrentDBDocument
    
    def get_drafting_type_id():
        viewfamily_types = FilteredElementCollector(doc).OfClass(ViewFamilyType)
        for i in viewfamily_types:
            if i.ViewFamily == ViewFamily.Drafting:
                return i.Id
            
    if IN[0]:
        forceClose = TransactionManager.Instance.ForceCloseTransaction()
        t = Transaction(doc, 'Create Drafting View')
        t.Start()
        
        drafting_type_id = get_drafting_type_id()
        drafting_view = ViewDrafting.Create(doc, drafting_type_id)
    
        t.Commit()
    
    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 amoursol; April 28th, 2018 at 10:03 PM.
    Ning Zhou and JWR like this.

  7. #7
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,104
    Current Local Time
    06:06 AM
    Nice! I copied that post to the RevitAPI Docs to Python thread as well just to sort of lump Python stuff together.
    amoursol likes this.

  8. #8
    Senior Member Ning Zhou's Avatar
    Join Date
    July 20, 2011
    Posts
    850
    Current Local Time
    03:06 AM
    hi Sol, have you tried revitpythonwrapper (rpw) in RevitPythonShell? seems i cannot "import rpw" sucessfully -> IOError: System.IO.IOException: Could not add reference to assembly IronPython.Wpf, i'm using both Revit 2018.3 and Revit 2017.2

  9. #9
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    11:06 AM
    Hello Ning,

    I have not sorry... might pay to reach out to Gui Talarico about it?

Similar Threads

  1. Dynamo & Python changes?
    By Gordon Price in forum Dynamo BIM
    Replies: 2
    Last Post: February 1st, 2017, 01:41 PM
  2. Dynamo Python - display message
    By archie456 in forum Dynamo BIM
    Replies: 3
    Last Post: June 8th, 2016, 03:21 AM
  3. Dynamo and Python
    By rdon in forum Dynamo BIM
    Replies: 1
    Last Post: December 18th, 2015, 09:01 AM
  4. Replies: 0
    Last Post: October 29th, 2014, 12:45 PM
  5. Replies: 0
    Last Post: July 2nd, 2014, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •