Cant place Door Legend component into Legend view with Python script

here is the code but when run have nothing happen

import clr
clr.AddReference(‘RevitAPI’)
clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.DB import *
clr.AddReference(‘RevitServices’)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System

Inputs

componentTypeIds = UnwrapElement(IN[0]) # The ElementIds of the Legend Component types
legendView = UnwrapElement(IN[1]) # The Legend view element
coordinates = UnwrapElement(IN[2]) # The XYZ coordinates for placing the Legend Components

Output

outLegendView = None # The modified Legend view element with the Legend Components placed

Start transaction

doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)

Get the appropriate Legend view

if isinstance(legendView, View):
legendView = doc.GetElement(legendView.Id)
else:
# Handle the case when the specified Legend view is not valid
pass

Set the view direction parameter to “Elevation:Front”

paramName = “View direction” # Adjust the parameter name as per your component type
paramValue = “Elevation:Front” # Adjust the parameter value as per your component type

Add the Legend Components to the Legend view

if legendView and componentTypeIds and coordinates:
if isinstance(componentTypeIds, list) or isinstance(componentTypeIds, System.Array):
for i in range(len(componentTypeIds)):
typeId = componentTypeIds[i]
if isinstance(typeId, ElementId):
# Retrieve the FamilySymbol from the ElementId
componentType = doc.GetElement(typeId)
if isinstance(componentType, FamilySymbol):
# Set the “View direction” parameter to “Elevation:Front”
if componentType.LookupParameter(paramName):
componentType.LookupParameter(paramName).Set(paramValue)

                # Create a placement point for the FamilyInstance
                position = coordinates[i] if i < len(coordinates) else XYZ.Zero
                
                # Place the existing Legend Component in the Legend view
                legendView.AddLegendComponent(componentType, position)
elif isinstance(componentTypeIds, ElementId):
    # Retrieve the FamilySymbol from the single ElementId
    componentType = doc.GetElement(componentTypeIds)
    if isinstance(componentType, FamilySymbol):
        # Set the "View direction" parameter to "Elevation:Front"
        if componentType.LookupParameter(paramName):
            componentType.LookupParameter(paramName).Set(paramValue)

        # Create a placement point for the FamilyInstance
        position = coordinates[0] if coordinates else XYZ.Zero

        # Place the existing Legend Component in the Legend view
        legendView.AddLegendComponent(componentType, position)

Commit transaction

TransactionManager.Instance.TransactionTaskDone()

Assign modified Legend view element to output

outLegendView = legendView

Return output

OUT = outLegendView, componentTypeIds

@ericke1 ,

i have not a solution i cleaned up your code as good as possible

import clr
import sys
import System

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Inputs

componentTypeIds = UnwrapElement(IN[0]) # The ElementIds of the Legend Component types
legendView = UnwrapElement(IN[1]) # The Legend view element
coordinates = UnwrapElement(IN[2]) # The XYZ coordinates for placing the Legend Components
#Output

outLegendView = None # The modified Legend view element with the Legend Components placed
transaction = Transaction(doc, "Place Legendcomponent on Legend")
transaction.Start()

#Get the appropriate Legend view

if isinstance(legendView, View):
	legendView = doc.GetElement(legendView.Id)
else:
# Handle the case when the specified Legend view is not valid
	pass
#Set the view direction parameter to “Elevation:Front”

paramName = "View direction" # Adjust the parameter name as per your component type
paramValue = "Elevation:Front" # Adjust the parameter value as per your component type
#Add the Legend Components to the Legend view

if legendView and componentTypeIds and coordinates:
if isinstance(componentTypeIds, list) or isinstance(componentTypeIds, System.Array):
for i in range(len(componentTypeIds)):
typeId = componentTypeIds[i]
if isinstance(typeId, ElementId):
# Retrieve the FamilySymbol from the ElementId
componentType = doc.GetElement(typeId)
if isinstance(componentType, FamilySymbol):
# Set the “View direction” parameter to “Elevation:Front”
if componentType.LookupParameter(paramName):
	componentType.LookupParameter(paramName).Set(paramValue)

# Create a placement point for the FamilyInstance
	position = coordinates[i] if i < len(coordinates) else XYZ.Zero
                
# Place the existing Legend Component in the Legend view
	legendView.AddLegendComponent(componentType, position)
elif isinstance(componentTypeIds, ElementId):
# Retrieve the FamilySymbol from the single ElementId
	componentType = doc.GetElement(componentTypeIds)
	if isinstance(componentType, FamilySymbol):
# Set the "View direction" parameter to "Elevation:Front"
		if componentType.LookupParameter(paramName):
			componentType.LookupParameter(paramName).Set(paramValue)
# Create a placement point for the FamilyInstance
		position = coordinates[0] if coordinates else XYZ.Zero

# Place the existing Legend Component in the Legend view
		legendView.AddLegendComponent(componentType, position)

# Commit the transaction
transaction.Commit()



outLegendView = legendView
#Return output

OUT = outLegendView, componentTypeIds

there are issues with blocks

KR

Andreas

I give up

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System

# Inputs
familyTypes = UnwrapElement(IN[0])  # The Family types (FamilySymbol instances)
legendView = UnwrapElement(IN[1])  # The Legend view element

# Output
outLegendView = None  # The modified Legend view element with the Detail Items placed

# Start transaction
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)

# Get the appropriate Legend view
if isinstance(legendView, View):
    legendView = doc.GetElement(legendView.Id)
else:
    # Handle the case when the specified Legend view is not valid
    pass

# Add the Detail Items to the Legend view
if legendView and familyTypes:
    if isinstance(familyTypes, list) or isinstance(familyTypes, System.Array):
        for familyType in familyTypes:
            if isinstance(familyType, FamilySymbol):
                # Create a FamilyInstance of the FamilySymbol
                detailItemElement = doc.Create.NewFamilyInstance(XYZ.Zero, familyType, legendView)
    elif isinstance(familyTypes, FamilySymbol):
        # Create a FamilyInstance of the FamilySymbol
        detailItemElement = doc.Create.NewFamilyInstance(XYZ.Zero, familyTypes, legendView)

# Commit transaction
TransactionManager.Instance.TransactionTaskDone()

# Assign modified Legend view element to output
outLegendView = legendView

# Return output
OUT = outLegendView, familyTypes

For this code it can place Item component to legend view i dont know why