Ungrouping & Purging All Families in a Model

revit
#8

They are, and I didn’t really expect to have work done for me, was quite genuinely wondering whether it’s the type of thing it would do or not. Thanks for your help though!

#9

OK, this topic could possibly go further, so here is a way to get started:


Group.UngroupElements is from Rhythm or Clockwork.
Please use with caution and do some preliminary tests (don’t run in automatic or several times and use Undo in Revit if needed)

#10

Oooo. Thank you for this, will see how I get on and report back!

1 Like
#11

"Usually, proceeding step by step leads to results"
Usually not. Thus, your next post about ungrouping is a good example - it works only with elements in the model.
The original topic is much trickier - how to process families and work inside them as we do in models. Look fwd to gurus.

#12

@Nikolay_Gerasimov1
https://forum.dynamobim.com/faq

1 Like
#13

Here is a start on how to open, ungroup and save the families. I 'm not sure if it’s possible to reload the documentes with Dynamo.

WIP-open family and ungroup.dyn (6.3 KB)

import clr

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 *

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

#Preparing input from dynamo to revit
elements = UnwrapElement(IN[0])
path = IN[1]

for e in elements:
	famdoc = doc.EditFamily(e)
	fec = FilteredElementCollector(famdoc).OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType()
	if fec.FirstElement() is not None:
		groups = fec.ToElements()
		TransactionManager.Instance.EnsureInTransaction(famdoc)
		[g.UngroupMembers() for g in groups]
		TransactionManager.Instance.ForceCloseTransaction()
		
		famdoc.SaveAs(path +"\\" + e.Name + ".rfa")
		
OUT = 0
2 Likes
#14

You can use IFamilyLoadOptions Interface in Revit API http://www.revitapidocs.com/2017/d447ed92-74e1-2125-dd0a-38a5ae85ce53.htm

1 Like
#15

Yes, you have to use that as an argument in one of the overloads of the LoadFamily Method. It requires that you create a custom class and maybe utilize StrongBox type to handle the out attributes in that class’ methods. That shouldn’t be any problem, but I had some doubt about the transactions, but it can probably be handled with ForceCloseTransaction() in the Transaction manager.

Here is a C# example:

#16

I found some time to take a look at the reloading:

The IFamilyLoadOptions implemented in python:

class FamilyOption(IFamilyLoadOptions):
	def OnFamilyFound(self,familyInUse,overwriteParameterValues):
		overwriteParameterValues = True
		return True
		
	def OnSharedFamilyFound(self, sharedFamily, familyInUse, FamilySource, overwriteParameterValues):
		return True

Here is the call of the LoadFamily method call on the familydocument:

famdoc.LoadFamily(doc, FamilyOption())

@luisa Do you want to test the script? It doesn’t purge anything, because that has to be done manually. There is no API call to the purge command in Revit.

OpenAndUngroupFamilies.dyn (6.1 KB)

For anyone that might be interested,.the complete python script:

import clr

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 *

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

class FamilyOption(IFamilyLoadOptions):
	def OnFamilyFound(self,familyInUse,overwriteParameterValues):
		overwriteParameterValues = True
		return True
		
	def OnSharedFamilyFound(self, sharedFamily, familyInUse, FamilySource, overwriteParameterValues):
		return True
		
#Preparing input from dynamo to revit
families = UnwrapElement(IN[0])

for f in families:
	famdoc = doc.EditFamily(f)
	fec = FilteredElementCollector(famdoc).OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType()
	if fec.FirstElement() is not None:
		groups = fec.ToElements()

		TransactionManager.Instance.EnsureInTransaction(famdoc)
		[g.UngroupMembers() for g in groups]
		TransactionManager.Instance.TransactionTaskDone()
		TransactionManager.Instance.ForceCloseTransaction()
		
		TransactionManager.Instance.EnsureInTransaction(doc)
		famdoc.LoadFamily(doc, FamilyOption())
		TransactionManager.Instance.TransactionTaskDone()
		TransactionManager.Instance.ForceCloseTransaction()
		
		#famdoc.SaveAs(path +"\\" + e.Name + ".rfa")
OUT = 0
2 Likes
Setting the builtin parameter accessible thru the family editor?
#17

@Einar_Raknes definitely but am snowed under with other work at the moment! I will report back once I find the time to dedicate to this. Many thanks!

#18

There was a similar question on how to edit multiple families quite a while ago, that was answered here:

It might give you some more examples to work with, but you might need to dig the old forum for the full-rez images.

1 Like
#19

This is what I wanted to know. Thx.

#20

You can still purge e.g. unused families with dynamo: http://whatrevitwants.blogspot.no/2015/11/purge-component-families-using.html

#21

It would also be interesting to get some feedback over this:

#22

Hi @Dimitar_Venkov, How can I access to the old forum? Thanks

#23
3 Likes
#24

@Einar_Raknes I tried this definition you had written some time ago… I don’t get how to fix this issue with this line

for e in elements:
_ famdoc = doc.EditFamily(e)_
_ fec = FilteredElementCollector(famdoc).OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType()_

he prints out this error out of the definition:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. _
Traceback (most recent call last):
_ File “”, line 31, in

Exception: The document is currently modifiable! Close the transaction before calling EditFamily.

#25

Dynamo is not the best tool to edit different Revit documents, but you can try to add TransactionManager.Instance.ForceCloseTransaction() before the for loop.

#26

Thank you. If it still doesn’t, Should I unload the different documents I have linked in the project or what?

#27

Nope. Encountered the same issue, what shall I do? I tried to unload the other links in the project but no luck with that

dynamo_colston_01

I tried also to use your old version (the one without the reloading step) and neither that worked, gave me this

Exception: The model could not be saved: (COleException 0x80030002)