import clr
import sys
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
# Import Dynamo Revit Libraries 'RevitServices' Namespace
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
# Get the current Revit document and application
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
# Input data from Dynamo
reference_assembly = UnwrapElement(IN[0]) # The reference assembly (source)
target_assembly = UnwrapElement(IN[1]) # The target assembly
# Ensure the elements are of the correct type
if not isinstance(reference_assembly, AssemblyInstance) or not isinstance(target_assembly, AssemblyInstance):
raise ValueError("Both reference_assembly and target_assembly must be of type AssemblyInstance.")
# Initialize the transaction
TransactionManager.Instance.EnsureInTransaction(doc)
# Acquire views from the reference assembly and apply to the target assembly
try:
# Call AcquireAssemblyViews directly from the AssemblyInstance class
AssemblyInstance.AcquireAssemblyViews(doc, reference_assembly.Id, target_assembly.Id)
result = "Views successfully acquired from reference assembly to target assembly."
except Exception as e:
result = f"Error: {str(e)}"
# End the transaction
TransactionManager.Instance.TransactionTaskDone()
# Output the result
OUT = result
Python.txt (1.6 KB)
I have tried to create assembly views by selection of reference assembly & target assembly but python script is not working. I tried create by below reference but failed .Please help me to create views.
@john_pierson @sovitek @solamour @staylor
What is the script returning? Was this code possibly written by AI? The AcquireAssemblyViews
method is part of the AssemblyViewUtils class, not AssemblyInstance
. This method also does not create new views, just move existing views to a new owner.
“Transfers the assembly views owned by a source assembly instance to a target sibling assembly instance of the same assembly type.”
3 Likes
@Nick_Boyts you are right I have created this python script using ChatGPT .Thank you for clarifying concept. But currently I want to create assembly views using string as like Evolvelab node AssemblyInstance.CreateViews , but it is not working . I have pasted script below can anyone modify it to create views using string.
import clr
import sys
clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
#Import Dynamo Revit Libraries ‘RevitServices’ Namespace#
clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *
from System.Collections.Generic import *
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
data = IN
view_orientations = [
AssemblyDetailViewOrientation.ElevationBack,
AssemblyDetailViewOrientation.ElevationBottom,
AssemblyDetailViewOrientation.ElevationFront,
AssemblyDetailViewOrientation.ElevationLeft,
AssemblyDetailViewOrientation.ElevationRight,
AssemblyDetailViewOrientation.ElevationTop,
AssemblyDetailViewOrientation.DetailSectionA,
AssemblyDetailViewOrientation.DetailSectionB
]
view_orientation_names = map(lambda x: x.ToString(), view_orientations)
assembly = UnwrapElement(IN[0])
view_types_to_create = IN[1]
views =
TransactionManager.Instance.EnsureInTransaction(doc)
for view_type in view_types_to_create:
view_orientation = None
try:
if view_type != ‘3D View’:
view_orientation = view_orientations[view_orientation_names.index(view_type)]
else:
pass
except:
continue
if view_type == ‘3D View’:
view_created = AssemblyViewUtils.Create3DOrthographic(doc, assembly.Id)
view_created.Name = ’ - '.join([assembly.AssemblyTypeName, view_type])
else:
view_created = AssemblyViewUtils.CreateDetailSection(doc, assembly.Id, view_orientation)
view_created.Name = ’ - '.join([assembly.AssemblyTypeName, view_type])
views.append(view_created)
TransactionManager.Instance.TransactionTaskDone()
Assign your output to the OUT variable.
OUT = views
@Nick_Boyts I have created python script using AI & it works for me creating All assembly views .It had worked for me. is there any way to create views using filter of view name? Actually I have just basic understanding about Revit API & Python. It would be helpful if you help.
Python.txt (2.5 KB)
So you have code that works by passing the view element instead of the view name and you want it to take the view name? It’s always best to post the python code rather than sharing the file so we don’t have to reproduce your exact case. If you want to use the view name then you just need to pass that through and then check each view one by one to compare names. If the names match, continue to the view creation.
You can do the comparison ahead of the python code with some pretty basic nodes, or you can use a FilteredElementCollector
to get all views in the project.