I'm trying to duplicate the finish material from another project, and apply it to the user selected WallType in the current project. I am able to programmatically modify the WallType, however, I can't figure out how to use materials from other projects.
CompoundStructure selectedCS = selectedWall.WallType.GetCompoundStructure();
if (selectedCS == null)
return;
try
{
selectedCS.SetMaterialId(0, materialId);
selectedWall.WallType.SetCompoundStructure(selectedCS);
}
catch (Exception ex)
{
SimpleLog.Log(ex);
}
In order to get the MaterialId from another project, I've tried to duplicate a WallType using Jeremy Tammik's example. However, in this example, the focus is on duplicating the properties, and not on the CompoundStructure.
However, I thought I could still open a WallType from the other document that contains the desired material, and simply use the MaterialId of that WallType.
WallType wallType = null;
FilteredElementCollector wallTypes
= new FilteredElementCollector(docHasFamily)
.OfClass(typeof(WallType));
foreach (WallType wt in wallTypes)
{
string name = wt.Name;
if (name.Equals(wallTypeName))
{
wallType = wt;
break;
}
}
selectedCS.SetMaterialId(0, wallType.GetCompoundStructure().GetLayers()[0].MaterialId);
selectedWall.WallType.SetCompoundStructure(selectedCS);
It doesn't work. So I guess I need to figure out how I can load the material from the other document into my current document.
Maybe you can use one of the CopyElement methods of the ElementTransformUtils class on the Material object?
https://www.revitapidocs.com/2023/82e737d5-fda4-bc10-6099-88999cd51300.htm
It returns null. I'm not sure if its because I am trying to copy from another Document, or if its because I am trying to copy a Material. It doesn't make much sense to copy a Material or a WallType using the ElementTransformUtils.CopyElement() method because every overload requires an XYZ location to copy to.
ICollection<ElementId> materialCopied = ElementTransformUtils.CopyElement(docHasFamily, materialId, new XYZ());
My plan was to mimic your Adding New Materials From List method. I could manually copy the Color and the SurfacePattern of the material in the other Document. However, this only works if the SurfacePatternId already exists in the active Document I am trying to copy to.
This seems especially relevant if I want to copy the .png that is being used to set the Render Appearance of the Material.
Your method will work for many of the Material Properties, but it seems that many of the Properties that are defined by an Element can simply not be copied from another Document. I hope I am wrong.
Oh dear. Sorry about that. Yes, materials are a bit tricky, trying to reuse standard material stuff across all products, made by completely different teams... here are the possibilities that I am aware of:
I don't know how to best address your particular problem. I asked the development team for you.
Thank you very much Jeremy.
It seems I am not invited to the slack discussion:
Nope, sorry, that is an internal link for my own use. Saves me keeping notes of everything elsewhere.
Can't find what you're looking for? Ask the community or share your knowledge.