Hi @EBSoares ,
the problem with the script is you translated is… that is not a runnable pyrevit script.
The execution of a revit Add-in command and a pyRevit script is very different, even if they use the same Revit API.
The good news is that wirting python and a pyRevit script is easier than writing and compiling an add-in!
What you wrote is a Class and, in order to run, it needs to be instantiated and tell what method to execute.
But this is the Revit Add-in way, revit knows that a class that implements the IExternalCommand
has the Execute
method and launches it when you click the button.
Within pyrevit, you just need to write the code as it is a sequence of instructions to execute - a script.
Or, if you’re feeling fancy and like a bit of organization like me, you can create functions to group functionality in a logical manner and then call those functions at the end of the file.
Other things to note: you won’t have access to the commandData
, but by using import pyrevit
(or better, importing its submodules) you can access the current doc
ument with fewer lines. And in this specific case, the pyrevit functions that you need already retrieve the doc by themselves.
This is completely untested, but I’d rewrite the code like this:
import os.path
from pyrevit.revit import query
from pyrevit.revit.db import create
from pyrevit.revit.db import transaction
from pyrevit import forms
def main(family_name, family_path):
if query.get_family(family_name):
# the family is already there, we exit early
return
if not os.path.exists(family_path):
forms.alert(
"Please ensure that the sample table family file '{0}' exists.".format(family_path),
title="Error",
)
return
with transaction.Transaction(name="Load Family"):
family = create.load_family(family_path)
symbol = next(s for s in family.Symbols)
place_family_symbol(symbol)
def place_family_symbol(symbol):
added_element_ids=[]
def update_added_elements_ids(sender, args):
nonlocal added_element_ids
added_element_ids = args.GetAddedElementIds()
app.DocumentChanged += update_added_elements_ids
uidoc.PromptForFamilyInstancePlacement(symbol)
app.DocumentChanged -= update_added_elements_ids
# here you can build the message and show it with form.alert(msg, warn_icon=False)
# ...
def build_family_path(family_name):
# not sure this will work, you can print(family_folder) after the following line to see what it contains
family_folder = os.path.dirname(System.Reflection.Assembly.GetExecutingAssembly().Location)
return os.path.join(family_folder, family_name) + ".rfa"
# the last thing to do is to set the arguments and call the main function
family_name = "family_api_table"
family_path = build_family_path(family_name)
main(family_name, family_path)
If I may, given you other post, I would suggest you to start with a generic python course, there are many available online;
trying to write code by copying and pasting things found online feels like tossing wheels, nuts and bolts on a floor hoping to build a car 
Umfortunately using pyrevit you have the double duty to know/understand two programming languages, since most of the actionable info around revit API is c# centered…