Announcement

Collapse
No announcement yet.

RevitAPI Docs to Python

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

    RevitAPI Docs to Python

    Note: Thread split from another topic since it drifted a bit. Original topic here:
    https://www.revitforum.org/dynamo-bi...ail-level.html


    Originally posted by amoursol View Post
    Hello Dhilip,

    It appears it may be possible through the Options Class and Python. If you are comfortable with Python and the Revit API, go have a look at the DetailLevel Property under Autodesk.Revit.DB.Namespace -> Options Class -> Options Properties on the revitapidocs.com
    since you brought it up, how does the stuff on Revit APIDocs translate into Python? I've stabbed blindly at Python a few times and found what I wanted on the API site but have no idea how to translate C# (or VB or VC++) into Python..
    Last edited by cellophane; February 21, 2018, 05:06 PM.
    Revit for newbies - A starting point for RFO


    chad
    BEER: Better, Efficient, Elegant, Repeatable.

    #2
    Originally posted by cellophane View Post
    since you brought it up, how does the stuff on Revit APIDocs translate into Python? I've stabbed blindly at Python a few times and found what I wanted on the API site but have no idea how to translate C# (or VB or VC++) into Python..
    Haha... yeah... therein lies the magic. It's difficult sadly. There is a bit of translation required. Effectively, you need to understand the core API calls (I.e what Revitapidocs.com has) and how to appropriate them in Python. It's rough - i'll give you that.

    Do you have any particular example you want to share? I can try to explain

    As a side note, I've annotated a little python and here is some:

    Wall Class Collector

    Code:
    #Importing external libraries
    import clr
    clr.AddReference("RevitNodes")
    import Revit
    clr.ImportExtensions(Revit.Elements)
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *
    import System
    from System.Collections.Generic import *
    
    #Defining our current Revit document as 'doc' (A nickname)
    doc = DocumentManager.Instance.CurrentDBDocument
    #Creating a Collector of a particular defined Class (Wall in this case)
    #and casting that list to Elements (i.e picking up the Elements)
    collector = FilteredElementCollector(doc).OfClass(Wall).ToElements()
    #We then simply output our list called 'Collector' by using the basic OUT
    #variable (This correlates to the output port in the  node)
    OUT = collector
    ActiveView Wall Class Collector

    Code:
    #Importing external libraries
    import clr
    clr.AddReference("RevitNodes")
    import Revit
    clr.ImportExtensions(Revit.Elements)
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *
    import System
    from System.Collections.Generic import *
    
    #Defining our current Revit document as 'doc' (A nickname)
    doc = DocumentManager.Instance.CurrentDBDocument
    #Creating a Collector of a particular defined Class (Wall in this case)
    #and only pulling those Class Elements from the open 'Active View', then
    #casting that list to Elements (i.e picking up the Elements)
    collector = FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(Wall).ToElements()
    #We then simply output our list called 'Collector' by using the basic OUT
    #variable (This correlates to the output port in the  node)
    OUT = collector
    Built In Category Collector (Walls) of ONLY instances (Not Types)

    Code:
    #Importing external libraries
    import clr
    clr.AddReference("RevitNodes")
    import Revit
    clr.ImportExtensions(Revit.Elements)
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *
    import System
    from System.Collections.Generic import *
    
    #Defining our current Revit document as 'doc' (A nickname)
    doc = DocumentManager.Instance.CurrentDBDocument
    #Creating a Collector of a particular Category of Elements (In this case
    #Walls) where all of the Elements must be 'Instances' and then casting
    #them to Elements.
    collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()
    #We then simply output our list called 'Collector' by using the basic OUT
    #variable (This correlates to the output port in the  node)
    OUT = collector
    Lambda Expression (Anonymous function definition that is not bound to an identifier)

    Code:
    #Importing external libraries
    import clr
    clr.AddReference("RevitNodes")
    import Revit
    clr.ImportExtensions(Revit.Elements)
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *
    import System
    from System.Collections.Generic import *
    
    #Defining our current Revit document as 'doc' (A nickname)
    doc = DocumentManager.Instance.CurrentDBDocument
    #Defining our Filter
    filName = IN[0]
    #Creating a Collector of a particular Category of Elements (In this case
    #Walls) where all of the Elements must be 'Instances' and then casting
    #them to Elements.
    collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()
    #We then run an anonymous Lambda function filter across our collector 
    #that uses our chosen Name as the filter
    walls = list(filter(lambda x : x.Name.Equals(filName),collector))
    #We then simply output our list called 'walls' by using the basic OUT
    #variable (This correlates to the output port in the  node)
    OUT = walls

    Sol Amour

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

    Comment


      #3
      nothing off the top of my head. I can usually open the Python in a graph/node and read through it and figure out roughly what is going on (if nothing else it is pretty easy to read Python) but trying to copy/paste and modify it to get a different result doesn't always work so well... It would be amazeballs if API docs was updated to include Python :bb:

      Looking at your Wall Class collector:
      Code:
      #Defining our current Revit document as 'doc' (A nickname)
      doc = DocumentManager.Instance.CurrentDBDocument
      
      #Creating a Collector of a particular defined Class (Wall in this case)
      #and casting that list to Elements (i.e picking up the Elements)
      
      collector = FilteredElementCollector(doc).OfClass(Wall).ToElements()
      
      #We then simply output our list called 'Collector' by using the basic OUT
      #variable (This correlates to the output port in the  node)
      
      OUT = collector
      vs APIDocs (assuming I'm even looking up the correct thing):
      C#
      public class Wall : HostObject

      Visual Basic
      Public Class Wall _
      Inherits HostObject

      Visual C++
      public ref class Wall : public HostObject
      1. Did I even look up the correct term to use?
      2. How do i go from one to the other? Especially when changing to some other category like Windows or Beams which don't have the same type of entry


      Some of it is just that my Python skills at the moment are limited to copy/paste and hack & stab blindly hoping it works (followed by a post here or on the Dynamo forum), and some is that I don't have a background in programming beyond QBasic :hide:
      Revit for newbies - A starting point for RFO


      chad
      BEER: Better, Efficient, Elegant, Repeatable.

      Comment


        #4
        Originally posted by cellophane View Post
        vs APIDocs (assuming I'm even looking up the correct thing):

        1. Did I even look up the correct term to use?
        2. How do i go from one to the other? Especially when changing to some other category like Windows or Beams which don't have the same type of entry
        This particular case is using the FilteredElementCollector class, so you should search for that rather than the Walls class. We're simply Collecting Elements by typology (Wall in this case) as follows:

        Filtered Element Collector Class as searched.



        Inside this class, we have; Members (All items pertaining to the class), Constructors (How to build one), Methods (What we can call on them) and Properties (What we can query).



        In the Members section, we choose to create one using the (doc) method only as per my Python exemplar.



        If we select this, it will give us a few examples in C++, C# and Visual Basic. None of these are Python, however we can extrapolate from this how to build in Python - most easily from the C# exemplar.

        Ignore the public keyword as python doesn't use this. It will state that to create a FilteredElemenetCollector (Pink colour) we simply need a Document (orange italics). Ignore the double casing here in C# as that language requires you to set the typology before every declaration.

        So in essence, if you simply read the pink and orange, you get: FilteredElementCollector(document) which is what we have in Python.



        We then want to run a Method over this Collector (As we don't want every single item in the Document), and for this we use the OfClass method and call it on Walls Category.



        To call a method on an object, you use dot notation, so assume you put a dot after your Collector, you simply then follow suit with the Pink / Orange methodology (Note that the colours may not always work but are a good rule of thumb).

        As such, under the C# exemplar, you'll see that to call the method, you need to use dot notation and then OfClass(type) in order to use the OfClass filter method.

        This leaves us with: FilteredElementCollector(doc).OfClass(type)

        At the base of the image above, you'll see type listed under Parameters. This tells you it needs "The Element Type" as a System Type.

        This is where it gets a little hairy, because you can't simply follow a link to access all the Class types. A pointer is at the base of the page where it says: See Also: Autodesk.Revit.DB Namespace gives us a hint. If we look here you can see what Classes are available and can browse all the classes that the DB namespace has - in theory all of which can be parsed with the class filter. You can also guess the name haha.

        After we have collected all our walls, we simply use another method called ToElements in order to get the actual Revit Elements returned.



        Following the C# example again, you'll simply need the pink section ToElements() which is called using Dot notation (Note: There are no constructors needed in this method, so we simply have empty parenthesis).

        So finally, we're left with: FilteredElementCollector(doc).OfClass(Walls).ToEle ments()

        In order to gain access to the FilteredElementCollector class, you need to import the Autodesk.Revit.DB namespace as follows:

        Code:
        clr.AddReference("RevitAPI")
        import Autodesk
        from Autodesk.Revit.DB import *
        Hope that's helpful!
        Attached Files
        Last edited by amoursol; February 21, 2018, 03:25 PM.

        Sol Amour

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

        Comment


          #5
          Post of the year! :bb:
          Revit for newbies - A starting point for RFO


          chad
          BEER: Better, Efficient, Elegant, Repeatable.

          Comment


            #6
            Originally posted by cellophane View Post
            Post of the year! :bb:
            On that note, should probably make it a sticky at the top of the forum haha... instead of hijacking this thread

            Sol Amour

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

            Comment


              #7
              Originally posted by amoursol View Post
              On that note, should probably make it a sticky at the top of the forum haha... instead of hijacking this thread
              I split it off & will sticky. I guessed on the name - let me know if you want to change it.
              Revit for newbies - A starting point for RFO


              chad
              BEER: Better, Efficient, Elegant, Repeatable.

              Comment


                #8
                Names good!

                Sol Amour

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

                Comment


                  #9
                  It seems to me you are for teaching a class on the subject! thanks and Good job.
                  If I watch...I dream, if I listen...I think, if I do ...I learn.
                  blog

                  Comment


                    #10
                    You're most welcome. I'm teaching a class on this at AU London this year! 1.5 hour lab entitled "Diving Deeper: A Beginners Look at Python in Dynamo"

                    Sol Amour

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

                    Comment

                    Related Topics

                    Collapse

                    Working...
                    X