Page 1 of 2 12 LastLast
Results 1 to 10 of 12
Like Tree18Likes

Thread: RevitAPI Docs to Python

  1. #1
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,060
    Current Local Time
    12:07 PM

    Question 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


    Quote 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 21st, 2018 at 06:06 PM.
    amoursol likes this.

  2. #2
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    05:07 PM
    Quote 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
    cellersick likes this.

  3. #3
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,060
    Current Local Time
    12:07 PM
    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

    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

  4. #4
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    05:07 PM
    Quote 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 Thumbnails Attached Thumbnails RevitAPI Docs to Python-revitforumquery_01.jpg   RevitAPI Docs to Python-revitforumquery_02.jpg   RevitAPI Docs to Python-revitforumquery_03.jpg   RevitAPI Docs to Python-revitforumquery_04.jpg   RevitAPI Docs to Python-revitforumquery_05.jpg  

    RevitAPI Docs to Python-revitforumquery_06.jpg  
    Last edited by amoursol; February 21st, 2018 at 04:25 PM.

  5. #5
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,060
    Current Local Time
    12:07 PM
    Post of the year!

  6. #6
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    05:07 PM
    Quote Originally Posted by cellophane View Post
    Post of the year!
    On that note, should probably make it a sticky at the top of the forum haha... instead of hijacking this thread
    elton williams likes this.

  7. #7
    Moderator cellophane's Avatar
    Join Date
    August 9, 2011
    Location
    Louisville, KY
    Posts
    6,060
    Current Local Time
    12:07 PM
    Quote 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.
    amoursol likes this.

  8. #8
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    05:07 PM
    Names good!

  9. #9
    Member uaifestival's Avatar
    Join Date
    May 28, 2014
    Location
    Tokyo
    Posts
    94
    Current Local Time
    01:07 AM
    It seems to me you are for teaching a class on the subject! thanks and Good job.

  10. #10
    Senior Member amoursol's Avatar
    Join Date
    November 21, 2012
    Location
    London, United Kingdom
    Posts
    612
    Current Local Time
    05:07 PM
    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"
    JWR, ScubaMan, cellophane and 2 others like this.

Page 1 of 2 12 LastLast

Similar Threads

  1. BIM 360 Glue/Field/Docs
    By Empirical objections in forum Forum Suggestions and Feedback
    Replies: 2
    Last Post: April 13th, 2016, 09:58 PM
  2. David Light: BIM360 Docs
    By David Light Blog in forum Blog Feeds
    Replies: 0
    Last Post: February 6th, 2016, 01:15 PM
  3. Replies: 0
    Last Post: December 1st, 2015, 10:45 PM
  4. Schedules and Excel Docs
    By Lox in forum Architecture and General Revit Questions
    Replies: 6
    Last Post: March 20th, 2014, 12:13 AM
  5. David Light: BIM Docs!
    By David Light Blog in forum Blog Feeds
    Replies: 0
    Last Post: April 25th, 2013, 08:15 PM

Posting Permissions

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