About coloring a linked IFC file

I’m aware of this topic

but…
When i create a new Revit file without using a Template and not Link a IFC file but Open it instead, a Shared Parameter File is also created.
If i then go back to my file with a Linked IFC file in it, and add the Shared Parameters as Project Parameters, i can create Filters and Color overrides based on those Parameters (Like IfcZoneName or IfcName and others).
question is:
How to do it using Dynamo

Hi,

as part of the organization of a challenge on the french forum I had shared this as a final solution
it might help you

import clr
import sys
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
#import net library
from System import Array
from System.Collections.Generic import List, IList, Dictionary

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

def Add_IFC_Filter(lst_guid, filterName = "IFC_Filter"):
	"""
	Creates or updates an IFC filter based on the provided list of IFC type GUIDs.
	
	Parameters:
		lst_guid (list): List of IFC type GUIDs to include in the filter.
		filterName (str, optional): Name of the IFC filter. Default is "IFC_Filter".
	
	Returns:
		ParameterFilterElement: The created or updated IFC filter.
	"""
	iListBic = List[ElementId]([category.Id])
	subFilterA = List[ElementFilter]()	
	# set first filter		
	subrules = List[FilterRule]()
	for guid in lst_guid:
		subrules.Add(ParameterFilterRuleFactory.CreateEqualsRule(ElementId(BuiltInParameter.IFC_TYPE_GUID), guid, True))	
	for filterRule in subrules:
		subFilterA.Add(ElementParameterFilter(filterRule))
	subFilterA = LogicalOrFilter(subFilterA)	
	#
	filterFunc = System.Predicate[System.Object](lambda x : x.Name == filterName)
	paraFilter = List[DB.Element](FilteredElementCollector(doc).OfClass(ParameterFilterElement).ToElements()).Find(filterFunc)
	if paraFilter is not None:
		paraFilter.ClearRules()
		paraFilter.SetElementFilter(subFilterA)
	else:
		paraFilter = ParameterFilterElement.Create(doc, filterName, iListBic, subFilterA)
	return paraFilter
	
def create_Override():
	"""
	Creates an OverrideGraphicSettings object with specific surface foreground pattern and color.
	
	Returns:
		OverrideGraphicSettings: The created OverrideGraphicSettings object.
	"""
	solidFilter = System.Predicate[System.Object](lambda x : x.GetFillPattern().IsSolidFill)
	solidpatern = List[DB.Element](FilteredElementCollector(doc).OfClass(FillPatternElement).ToElements()).Find(solidFilter)
	ovg = OverrideGraphicSettings()
	ovg.SetSurfaceForegroundPatternColor(DB.Color(0,0,255))
	ovg.SetSurfaceForegroundPatternId(solidpatern.Id)
	return ovg
	

toList = lambda x : x if hasattr(x, '__iter__') else [x]

#Preparing input from dynamo to revit
lst_guid = toList(IN[0])
category = UnwrapElement(IN[1])
view = UnwrapElement(IN[2])

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

ifcfilter = Add_IFC_Filter(lst_guid)
ovg = create_Override()

if view.AreGraphicsOverridesAllowed():
	if not view.IsFilterApplied(ifcfilter.Id):
		view.AddFilter(ifcfilter.Id)
		view.SetFilterOverrides(ifcfilter.Id, ovg)

TransactionManager.Instance.TransactionTaskDone()

OUT = ifcfilter
3 Likes

Maybe asking too much, runtime <> file size?
Sorry
I had it sketch out like this btw
-read xml
-find param
-copy
write as UTF8 txt file to create a Shared Parameter file

I don’t use the Shared Parameters file, I use directly IFC parameters generated in model (when Revit linking model)
The process is as follows

from the linked model

  1. get all IFC elements by a property (e.g. “IfcName == COLUMN”)
  2. obtain their GUIDs (” Type IfcGUID ” or BuiltInParameter.IFC_TYPE_GUID )

On the host model

  1. create one or more filters with these GUIDs using BuiltInParameter.IFC_TYPE_GUID (rules construction)
  2. add filters to views
3 Likes