Converting RebarBarType to string with Revit API

@Hi everyone,
@jacob.small

I want to set the name of my rebar container to be the RebarBarType as shown in the image below

container_name

I tried the following line, but I got as output : Autodesk.Revit.DB.Structure.RebarBarType

container_name = rebar_type.ToString()

please take a look to this part of my script (I dont know if I should load other library for this function)

import sys
import clr
import math
import System

from System.Collections.Generic import IList, List 

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

out_curv = IN[0][0].ToRevitType()
out_path = IN[0][1].ToRevitType()
iner_curv = IN[0][2].ToRevitType()
iner_path = IN[0][3].ToRevitType()

cover = IN[1]/0.3048
rebar_type = UnwrapElement(IN[2])
Hook_type = UnwrapElement(IN[3])
host= UnwrapElement(IN[4])
spacing = IN[5]/0.3048
container_name = rebar_type.ToString()

Thanks.

Going to go with a ‘teach a man to fish’ method here instead of a ‘have a fish’ so that you can grow your skill set a bit (you’re ready for it).

For now skip the last line by commenting it out, and look at what you can do with the object you have.

Two ways to do this, one is more complete than the other, and one is faster to implement on the fly.

Method 1: This is faster to implement and often less error prone, but may leave you scratching your head with some bits.

OUT = dir(yourObject)

From that output you can see every method and property you can use on that object. The stuff with two underscores proceeding and following it are worth noting specifically; these are special in that they are meant to help you work rather than providing a meaningful final output. For example, doc will give you the formal documentation for the API call (if it exists - internal and undocumented APIs won’t have this). Now you know what you can do with the object you have, so you can read the methods and properties to find something which looks appealing. Not as useful as just knowing what to do as some objects have hundreds or even thousands of calls, and some methods and properties require specialist knowledge to know how to use them; but this is still insanely useful as a tool.

Method 2: This returns more complete results, but can fail in some instances (in particular with some APIs that return complex data via reflection).

import inspect
OUT = inspect.getmembers(yourObject)

From that output you get not just the data about what you can do, but the documentation of each property and method. Basically you’re outputting the full set of data as you might see on RevitAPI docs. There are other good uses for inspect (getting the class and the class tree), so it’s useful to know if you’re not able to write the help docs by memory for the library and modules you’re using. This geeks for geeks article can help learn what you can do with it: Inspect Module in Python - GeeksforGeeks

Look those methods over and see if there is something that you can work with to get the Name of what you’re working with.

3 Likes

Hi, thank you for this useful information
Method #2 only works with Ironpython2 engine?

In method 1: Line 88 of the Name property relates to the get_Name method on line 169 and
set_Name from line 189

the Name property is not present in method 2
not in the properties of the RebarBarType class

cordially
christian.stan

1 Like

Python 3 makes the results of the inspect fail to process back to Dynamo correctly in some cases. Hence knowing all methods is good. :slight_smile:

1 Like

@jacob.small

I’m a beginner fisher and I have to learn tips and tricks to be a big fisher like you :wink:

In fact, I knew the use of the commands OUT = dir(object) and OUT = object.__doc__ which allows exploring propreties of ojects and methods for Revit nodes, but I dont knew that their uses can extend also to API.

1- I’m working with IronPython2 engine, and I noticed that the get_Name method not existing there opposite of CPython3 where there are more methods??..in this case should I use CPython3 to be able to use this method?

2- how Should I know if the item in the dropdown list is an object proprety (attribute) or a method ?
for example, in IronPython2 if I type : OUT = bar_type.CanBeCopied.__doc__ no doc is displayed, but if I type OUT = bar_type.Create.__doc__ I get the doc?

3- @christian.stan

how do you know that the name is obtained by get_Name method compared to its proprety in the line 88?
4- No way to get the RebarBarType name using AsString method in Revit API ?
Thanks.

Hello, in fact it was a question (I had forgotten the?), not an affirmation (not yet competent enough to claim things)
did you see the OUT[2] line of the displayed python script?
cordially
christian.stan

1 Like

I not figure out your question
did you mean your output nameF ?

Thanks

Wasn’t the goal of the topic the string transformation of RebarBarType?

So solved here (as it’s orange color [string] the super cool thing of Dynamo>2.16, I can’t live without it, too too good idea of ​​I don’t know who)
no, NameT out[0,1,2]
script:

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

reb_b_t=UnwrapElement(IN[0])
nameT=reb_b_t.get_Name()

OUT = nameT

cordially
christian.stan

1 Like

May be that the Name property will work as is; the get_ methods (and set_ methods) are added wrappers to help with conversion. I do recommend writing in the CPython engine or IronPython3 whenever possible, as it’ll help with forward compatibility.

Trial and error, or look it up in the official documentation, or via RevitAPIDocs.com. I usually assume a property as a first step, and if that fails move on to a method with no overloads (empty parenthesis).

No, because you haven’t accessed the name, only the object.

1 Like

Here is a solution under IronPython2 (with transform to node dynamo) (it’s not mine, you can imagine, look at this topic explanations by Mr. Poupin and Solution by Mr. Brendan Cassidy)

import clr
# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

reb_b_t=UnwrapElement(IN[0])
nameT=reb_b_t.ToDSType(False).Name
OUT = nameT

cordially
christian.stan

1 Like

@christian.stan

Yes you did it !! :+1:

Mr @c.poupin is genius , @Brendan_Cassidy and you too :star_struck:

Thanks you too for your explanations @jacob.small

3 Likes

Hi,
FYI the bug is fixed in IronPython3

more info here (use the translation button)

2 Likes

@c.poupin

IronPython3 works only in Revit 2023?

Thanks.

yes It’s works,
this is a “work in progress” DynamoTeam package

1 Like

@c.poupin

You dont understood my question, I mean can we use IronPython3 in Revit 2022 for example?

Thanks.

the Ironpython3 package can be installed on revit 2022 but in view of this message, I do not recommend it (need to be confirmed)

2 Likes