Export and Import Family Type Images in the Family Editor

Wanted to share this graph that is able to export an image of each Type in the Family Editor to a folder and then set the exported image to the Type Image for the same type.

  1. Pick a Folder to Export the Images
  2. Make sure all of the types have the correct parameter settings.
  3. Run!

Python:

#Sean Page, 2023
import clr
import os

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB 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.Elements)

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

doc = DocumentManager.Instance.CurrentDBDocument
results = []

types = doc.FamilyManager.Types
grp = TransactionGroup(doc)
grp.Start("Export Views")
for type in types:
	t = Transaction(doc)
	t.Start("Export Type")
	doc.FamilyManager.CurrentType = type
	t.Commit()
	opts = ImageExportOptions()
	opts.ExportRange = ExportRange.CurrentView
	opts.HLRandWFViewsFileType = ImageFileType.PNG
	opts.ShadowViewsFileType = ImageFileType.PNG
	opts.FitDirection = FitDirectionType.Vertical
	opts.ZoomType = ZoomFitType.Zoom
	opts.PixelSize = 400
	opts.ImageResolution = ImageResolution.DPI_150
	opts.FilePath = os.path.join(IN[0],type.Name)
	results.append(type.Name)
	doc.ExportImage(opts)
	t = Transaction(doc)
	t.Start("add image")
	for param in doc.FamilyManager.Parameters:
		if param.Definition.Name == "Type Image":
			#MessageBox.Show(opts.FilePath);
			imgOpts = ImageTypeOptions(opts.FilePath +".png",False,ImageTypeSource.Import)
			img = ImageType.Create(doc, imgOpts)
			doc.FamilyManager.Set(param, img.Id)
			break
	t.Commit();
grp.Assimilate();

OUT = results

Export Type Images.dyn (5.7 KB)

6 Likes

Hello. Thanks for sharing this script. The name of the file is the name of the type (80 x 210 cm.png) but,… How can the name of the family be added to the name? (Puerta de 1 hoja-80 x 210 cm.png). Thanks. Regards

1 Like

Yes, we can use the Title property of the Document to append the Family name, assuming it has been saved at least one time.

thanks very interesting. I am wondering how to do the reverse, there is a family type parameter builtin called Type Image where there is an image loaded in the parameter of the family, I want to extract it to a folder instead of inserting (setting) it in the parameter type Image.

So, this gets a little harder to do, but here is a way to start:

#Sean Page, 2024
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB 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

#Preparing input from dynamo to revit
if isinstance(IN[0], list):
	element = UnwrapElement(IN[0])
else:
	element = [UnwrapElement(IN[0])]

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
results = []
#Get all Family Parameters
params = doc.FamilyManager.Parameters
#Iterate Parameters
for param in params:
        #Images are stored as ElementId
        if param.StorageType == StorageType.ElementId:
                #Get the element from the parameter
                elem = doc.GetElement(doc.FamilyManager.CurrentType.AsElementId(param))
                #Check to see if the element category is Raster Image
                if elem.Category.BuiltInCategory == BuiltInCategory.OST_RasterImages:
                    #use the GetImage method to return the bitmap
                    results.append(elem.GetImage())
TransactionManager.Instance.TransactionTaskDone()

OUT = results

3 Likes

maybe ending with something like that?

import clr

# Add reference to System.Drawing for image manipulation
clr.AddReference("System.Drawing")
from System.Drawing import Image
from System.Drawing.Imaging import ImageFormat
import System

# Directory to save images
outputDir = "C:\\path\\to\\save\\images"
if not System.IO.Directory.Exists(outputDir):
    System.IO.Directory.CreateDirectory(outputDir)

# Assuming `results` contains System.Drawing.Bitmap objects
for idx, bitmap in enumerate(results):
    # Construct a file path using str.format method
    filePath = System.IO.Path.Combine(outputDir, "image_{0}.png".format(idx))
    
    # Save the bitmap to file
    bitmap.Save(filePath, ImageFormat.Png)


2 Likes

How can I export images of each type of wall in the project?

Get the Wall type then GetPreviewImage().

https://thebuildingcoder.typepad.com/blog/2010/05/get-type-id-and-preview-image.html

can you be specific

Here is what’s inside the python just to sequence the image names for saving.

#Sean Page, 2024
import clr

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

path = IN[0]
rng = IN[1]

OUT = [path + " " + str(i)+".png" for i in range(len(rng))]

test image 2

1 Like

I want it to show in full (including doors if any)

Ok, then it’s probably easier for you to show us what you’ve done and we can try to help from there.