Insert line based detail family

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)))

It is possible. How did that ChatGPT go for you?

1 Like

Hi and welcome,
I mean, that is how I would start my first post, chatgpted or not. But that is just me being an ass.

Do not ask us to correct chatGPT, this is so wrong. And such a waste of time for everyone!
The imports are wrong, the rest, terrible.

What error did you get?

Elegant…#sarcasm

You need to prompt properly and know at least how to read the api docs and the code itself before anything.

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.

Or use the content bundle. (Never tried it with a line based family though)
Content bundle

Not sure what the content bundle is, will look into that.

The specific goal is to have one click pushbuttons to start the detail command with a specific family (gyp. bd., plywood, exterior sheathing, etc.)

The specific error message I am getting is that the family is not found, even though I have confirmed that it is.

PyRevitLoader - Error Family ‘Gyp’ with type ‘Gyp’ not found in the project.

Thanks for the help.

Oh, and apologies for past and future faux pas…

1 Like

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.

You could install my demo extension and have a look at the structure of folder for .content folders and have a look at the handout

1 Like

It does exactly what you are after and requires no code and no chat bot. Just reading and understanding :grin: