Announcement

Collapse
No announcement yet.

portability between Revit API Python and Revit Dynamo Python

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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
    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
    Revit for newbies - A starting point for RFO


    chad
    BEER: Better, Efficient, Elegant, Repeatable.

    Comment


      #3
      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 27, 2018, 07:16 PM.

      Comment


        #4
        you are way over my head at this point...
        Revit for newbies - A starting point for RFO


        chad
        BEER: Better, Efficient, Elegant, Repeatable.

        Comment


          #5
          https://www.revitforum.org/dynamo-bim/37989-portability-between-revit-api-python-revit-dynamo-python.html

          Comment


            #6
            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:
            [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]
            [COLOR=#2E2E2E]uidoc = __revit__.ActiveUIDocument[/COLOR]
            [COLOR=#2E2E2E]doc = uidoc.Document
            [/COLOR]
            [COLOR=#2E2E2E]t = Transaction(doc, 'Create Drafting View')[/COLOR]
            [COLOR=#2E2E2E]t.Start()[/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)
            [/COLOR]
            [COLOR=#2E2E2E]t.Commit()[/COLOR]
            Dynamo Version:

            Code:
            [B]import[/B] clr
            clr.[B]AddReference[/B]('RevitAPI')
            clr.[B]AddReference[/B]('RevitServices')
            [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')
                t.[B]Start[/B]()
                
                drafting_type_id = [B]get_drafting_type_id[/B]()
                drafting_view = ViewDrafting.[B]Create[/B](doc, drafting_type_id)
            
                t.[B]Commit[/B]()
            
            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 28, 2018, 10:03 PM.

            Sol Amour

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

            Comment


              #7
              Nice! I copied that post to the RevitAPI Docs to Python thread as well just to sort of lump Python stuff together.
              Revit for newbies - A starting point for RFO


              chad
              BEER: Better, Efficient, Elegant, Repeatable.

              Comment


                #8
                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

                Comment


                  #9
                  Hello Ning,

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

                  Sol Amour

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

                  Comment

                  Related Topics

                  Collapse

                  Working...
                  X