Get an up-to-date material preview image file path
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I thought the new Revit 2018.1 API dedicated to materials would help me solve a quite old issue, but it seems that it is not the case.
I want to retrieve (then show) the image preview of a material. It is the image at the top of the rendering tab (or asset) in the material editor.
From what I have understood, this image is updated/regenerated :
- if not created yet, when you open the rendering tab for the first time
- when you change one of the rendering properties
The image location path seems to be stored in the AssetProperty called "thumbnail". It can be :
- relative, if it points to a preset material image in the ADSK library. For instance "Mats\WallPaint\Presets\t_Paint-057.png".
- absolute. For instance it can point to the "temp" folder when you just regenerated the file. In that case the path is like "materialthumbnails_pid_NNN\XXX.png" or "MaterialRapidRT_PIDNNNNN\XXX.png"
So I write this piece of code to get the wanted path :
AppearanceAssetElement aae = _document.GetElement(mat.AppearanceAssetId) as AppearanceAssetElement; if (null != aae) { Asset asset = aae.GetRenderingAsset(); if (null != asset) { if (asset[SchemaCommon.Thumbnail] is AssetPropertyString ap) { string path = ap.Value; if (path.StartsWith("Mats") || path.StartsWith("Maps")) // Revit default { path = path.Insert(0, @"C:\Program Files (x86)\Common Files\Autodesk Shared\Materials\2018\assetlibrary_base.fbm\"); } else if (path.StartsWith("material", true, System.Globalization.CultureInfo.CurrentCulture)) { path = path.Insert(0, @"%tmp%\"); } if (!string.IsNullOrEmpty(path) && File.Exists(path)) { Bitmap bmp_representation = new Bitmap(Image.FromFile(path), size); } } } }
This works fine for some materials, but for most of them the retrieved path does not exist. Why ? I think it is because the image has not been regenerated in the current Revit session. So ideally I would like the API to have a function that updates a material preview image, so that the corresponding path points to an existing and up-to-date PNG file.
I also tried to first edit the rendering asset (without making any changes), in the hope that would update the preview image, but no success : for nearly all the materials, the preview image is the UnifiedBitmap default PNG.
using (Transaction trans = new Transaction(_document, "Fake transaction")) { trans.Start(); using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(_document)) { Asset editableAsset = editScope.Start(mat.AppearanceAssetId); editScope.Commit(true); } trans.Commit(); }
Thanks for your ideas/suggestions.
Maxime