Is it possible to write a script to insert a line based detail family? The intention is for the user to select two points and have the detail family inserted between them.
This is what I have so far, full disclosure I’m using ChatGPT to write the code.
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference('System')
clr.AddReference('System.Windows.Forms')
from Autodesk.Revit.DB import Transaction, FilteredElementCollector, FamilyInstance, Line
from Autodesk.Revit.UI import TaskDialog
from Autodesk.Revit.UI.Selection import ObjectSnapTypes
from System.Windows.Forms import MessageBox
# Access active Revit document
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
# Function to find an existing instance of the family and extract its symbol
def get_family_symbol_from_instances(family_name, type_name):
instances = FilteredElementCollector(doc).OfClass(FamilyInstance).ToElements()
for instance in instances:
symbol = instance.Symbol # Access the family symbol
if symbol and symbol.Family.Name == family_name and symbol.Name == type_name:
return symbol
return None
try:
# Simplified Family and Type Information
family_name = "Gyp"
type_name = "Gyp"
# Retrieve the family symbol from existing instances
family_symbol = get_family_symbol_from_instances(family_name, type_name)
# Error if no symbol is found
if family_symbol is None:
TaskDialog.Show("Error", "Family '{0}' with type '{1}' not found.".format(family_name, type_name))
__revit__.Exit()
# Activate the family symbol if inactive
if not family_symbol.IsActive:
t = Transaction(doc, "Activate Family Symbol")
t.Start()
family_symbol.Activate()
t.Commit()
# Prompt user to pick two points
p1 = uidoc.Selection.PickPoint("Select the first point", ObjectSnapTypes.Endpoints)
p2 = uidoc.Selection.PickPoint("Select the second point", ObjectSnapTypes.Endpoints)
# Create a line between the two points
detail_line = Line.CreateBound(p1, p2)
# Insert the family instance
t = Transaction(doc, "Insert Line-Based Family")
t.Start()
new_instance = doc.Create.NewFamilyInstance(detail_line, family_symbol, doc.ActiveView)
t.Commit()
# Success message
TaskDialog.Show("Success", "Inserted '{0}' with type '{1}' successfully.".format(family_name, type_name))
except Exception as e:
MessageBox.Show("Error: {0}".format(str(e)))
And just as important… What is the goal?
Line based detail families already require the user to select two points. How is this going to be different or better than what the standard Revit command offers?
In Revit I can drag and drop from the list of families.
I can Annotate >Component.
I can type DD if one is wise enough to set up shortcut keys.
I can drag and drop from windows explorer.
I can right click > create similar.
My guess is you are trying to make your own menu for selecting standard detail components. Just a guess.
And if so, you’re probably headed the wrong direction. You’ll want PromptForFamilyInstancePlacement. But just a guess based on what ChatGPT is telling me.
That’s exactly what PromptForFamilyInstancePlacement does.
The issue with the select point method is that unlike AutoCAD, there is no rubber band feature, so you can’t see the family while placing using your method. You also can’t use the spacebar to flip the family as you are placing. You want to be able to both of those things.