Replace material

Replace material

Anonymous
Not applicable
6,585 Views
37 Replies
Message 1 of 38

Replace material

Anonymous
Not applicable

Dear all,

is it possible to substitute in a selected element (mainly wall or floor) the type of material?

E.g. basic wall - material A into basic wall - material B. 

I'm wondering also if my basic wall has more than one material, is it still possible to substitute all of them with just a specific material?

Thanks in advance for your support

0 Likes
6,586 Views
37 Replies
Replies (37)
Message 21 of 38

Anonymous
Not applicable

hi @lukaskohout is there a paricular syntax to apply that code to a multiple walls selected? So far it works just if only one wall is selected, in case 2 or more walls are selected there is not materials change.

 

Thanks for your support!

0 Likes
Message 22 of 38

lukaskohout
Advocate
Advocate

If you have multiple elements selected, you have to iterate through all the walls in the selection and then execute the CompoundStructure editing code.

 

var sel = uidoc.Selection.GetElementIds();

foreach(var elId in sel)
{
    Element el = doc.GetElement(elId);
    if(el is Wall)
    {
        //implementing function for CompoundStructure
        CreateCSforWall(el as Wall);
    }
}

Or if you want to change all the wall in the project, you can use the FilteredElementCollector.

https://thebuildingcoder.typepad.com/blog/2010/10/filtered-element-collectors.html

https://www.revitapidocs.com/2020/263cf06b-98be-6f91-c4da-fb47d01688f3.htm

 

 


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 23 of 38

Anonymous
Not applicable

Thanks again @lukaskohout  for your answer,

I tried your code but it does not fit with mine, I'm sure there is something to adjust. Could you have a look into it please?

 

Capture.JPG

 

namespace Revit.SDK.Samples.CompoundStructureCreation.CS
{
    #region A Class For Create CompoundStructure
   
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    public class WallCompoundStructure : Autodesk.Revit.UI.IExternalCommand
    {
     
        UIApplication m_application;
     
        UIDocument m_document;

        public Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            m_application = commandData.Application;
            m_document = m_application.ActiveUIDocument;

            Transaction transaction = new Transaction(m_document.Document, "Create CompoundStructure for Wall");

          
            try
            {
                // Select at least a wall.
                ElementSet selectedElements = new ElementSet();
                foreach (ElementId elementId in m_document.Selection.GetElementIds())
                {
                    selectedElements.Insert(m_document.Document.GetElement(elementId));
                }
                if (selectedElements.IsEmpty)
                {
                    TaskDialog.Show("Error", "Please select one wall at least.");
                    return Autodesk.Revit.UI.Result.Cancelled;
                }

                // Create the CompoundStructure for wall.
                transaction.Start();
                if (selectedElements.IsEmpty)
                    return Result.Failed;
                //test from lukas
                foreach (Element elem in selectedElements)
                {
                    var sel = uidoc.Selection.GetElementIds();

                    foreach (var elId in sel)
                    {
                        Element el = doc.GetElement(elId);
                        if (el is Wall)
                        {
                            //implementing function for CompoundStructure
                            CreateCSforWall(el as Wall);
                        }
                    }
                }

 

0 Likes
Message 24 of 38

lukaskohout
Advocate
Advocate

This should do the trick. I am not able to test, since I do not have Windows at hand now. There is no handling of errors, you should add that.

 

 

UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uiapp.ActiveUIDocument.Document;

var selectedElementIds = uidoc.Selection.GetElementIds();
if(selectedElementIds != null)
{
    using (Transaction t = new Transaction(doc, "Editing wall types"))
    {
        t.Start();
        foreach (var id in selectedElementIds)
        {
            Element e = doc.GetElement(id);
            if (e is Wall)
            {
                CreateCSforWall(e as Wall);
            }
        }
        t.Commit();
        t.Dispose();
    }
}

 

 

.

 


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 25 of 38

Anonymous
Not applicable

thanks again @lukaskohout  for your clear message, but I still get the same error as far as I select more than one wall since the material is already in use. My aim is to change material to the all walls selected

Capture.JPG

0 Likes
Message 26 of 38

lukaskohout
Advocate
Advocate

Just create the material once, and pass it to the CreateCSforWall function. You can make it cleaner by filtering the selection ang get unique WallTypes from walls.

var selectedElementIds = uidoc.Selection.GetElementIds();
if (selectedElementIds != null)
{
    using (Transaction t = new Transaction(doc, "Editing wall types"))
    {
        t.Start();
        List<WallType> wallTypes = new List<WallType>();

        Material concrete = CreateSampleConcreteMaterial();

        foreach (ElementId id in selectedElementIds)
        {
            Element e = doc.GetElement(id);
            if (e is Wall)
            {
                Wall w = e as Wall;
                if (!wallTypes.Contains(w.WallType))
                    wallTypes.Add(w.WallType);
            }                          
        }

        foreach(WallType wt in wallTypes)
        {
            CreateCSforWall(wt, concrete);
        }

        t.Commit();
        t.Dispose();
    }
}

Updated CreateCSforWall function:

public void CreateCSforWall(WallType wallType, Material concrete)
{
    //wallType.Name = wallType.Name + "_WithNewCompoundStructure";
    CompoundStructure wallCS = wallType.GetCompoundStructure();

    // Create CompoundStructureLayers and add the materials created above to them.
    List<CompoundStructureLayer> csLayers = new List<CompoundStructureLayer>();
    CompoundStructureLayer structureLayer = new CompoundStructureLayer(0.5, MaterialFunctionAssignment.Structure, concrete.Id);
    csLayers.Add(structureLayer);

    // // Set the created layers to CompoundStructureLayer
    wallCS.SetLayers(csLayers);

    //Set which layer is used for structural analysis
    wallCS.StructuralMaterialIndex = 0;
    wallCS.SetNumberOfShellLayers(ShellLayerType.Exterior, 0);
    wallCS.SetNumberOfShellLayers(ShellLayerType.Interior, 0);

    // Set the new wall CompoundStructure to the type of wall.
    wallType.SetCompoundStructure(wallCS);
}

 


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 27 of 38

Anonymous
Not applicable

Thanks @lukaskohout  I did as you suggested and I get two errors

Capture.JPGCapture2.JPG

0 Likes
Message 28 of 38

lukaskohout
Advocate
Advocate

You did not updated the arguments of the method CreateCSforWall :

 

public void CreateCSforWall(WallType wallType, Material concrete)

 


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 29 of 38

Anonymous
Not applicable

thanks  @lukaskohout  you are completely right! Now it works when I select two walls of the same type, but I still get the error "the given value for name is already in use" when I select different walls type

0 Likes
Message 30 of 38

lukaskohout
Advocate
Advocate

All you need to do is pass one fixed materiaa to the method CreateCSforWalls. One example below, which I just tested and works fine.

 

// Getting first material of first wall type CompoundStructure.
CompoundStructure cs = wallTypes.First().GetCompoundStructure();
Material concrete = doc.GetElement(cs.GetMaterialId(0)) as Material;

// You hould be able to replace code above with your method for creating sample material
Material concrete = CreateSampleConcreteMaterial();

 

If this does not work then the problem lies within the CreateSampleConcreteMaterialMethod and I do not know the code behind it.

 

Maybe it would be easier if you could attach your VS solution.


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 31 of 38

Anonymous
Not applicable

Really appreciated you support @lukaskohout , by changing as you suggested now I got a different error when I try to select two different types of walls

Capture.JPG

0 Likes
Message 32 of 38

lukaskohout
Advocate
Advocate

Could you post your VS solution?

This code works for me. Maybe you put it before the loop when you get all the wall types from selection?


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 33 of 38

Anonymous
Not applicable
namespace Revit.SDK.Samples.CompoundStructureCreation.CS
{
   
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]

    public class WallCompoundStructure : Autodesk.Revit.UI.IExternalCommand
    {
        UIApplication uiapp1;
        UIDocument uidoc1;
        Document doc;
        public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
        {
            uiapp1 = commandData.Application;
            uidoc1 = uiapp1.ActiveUIDocument;
            doc = uiapp1.ActiveUIDocument.Document;
            var selectedElementIds = uidoc1.Selection.GetElementIds();

            if (selectedElementIds != null)
            {
                using (Transaction t = new Transaction(doc, "Editing wall types"))
                {
                    t.Start();
                    List<WallType> wallTypes = new List<WallType>();
                    CompoundStructure cs = wallTypes.First().GetCompoundStructure();
                    Material concrete = doc.GetElement(cs.GetMaterialId(0)) as Material;



                    foreach (ElementId id in selectedElementIds)
                    {
                        Element e = doc.GetElement(id);
                        if (e is Wall)
                        {
                            Wall w = e as Wall;
                            if (!wallTypes.Contains(w.WallType))
                                wallTypes.Add(w.WallType);
                        }
                    }

                    foreach (WallType wt in wallTypes)
                    {

                        CreateCSforWall(wt, concrete);
                    }

                    t.Commit();
                    t.Dispose();
                }
            }
            return Result.Succeeded;
        }


        public void CreateCSforWall(WallType wallType, Material concrete)
        {
            {

                //wallType.Name = wallType.Name + "_WithNewCompoundStructure";

                CompoundStructure wallCS = wallType.GetCompoundStructure();

                // Create CompoundStructureLayers and add the materials created above to them.
                List<CompoundStructureLayer> csLayers = new List<CompoundStructureLayer>();
                CompoundStructureLayer structureLayer = new CompoundStructureLayer(0.5, MaterialFunctionAssignment.Structure, concrete.Id);
                csLayers.Add(structureLayer);

                // // Set the created layers to CompoundStructureLayer
                wallCS.SetLayers(csLayers);

                //Set which layer is used for structural analysis
                wallCS.StructuralMaterialIndex = 0;
                wallCS.SetNumberOfShellLayers(ShellLayerType.Exterior, 0);
                wallCS.SetNumberOfShellLayers(ShellLayerType.Interior, 0);

                // Set the new wall CompoundStructure to the type of wall.
                wallType.SetCompoundStructure(wallCS);
            }
        }

        private Material GetMaterial(string name)
        {
            FilteredElementCollector collector = new FilteredElementCollector(uidoc1.Document);
            //FilteredElementCollector collector = new FilteredElementCollector(m_document.Document);
            collector.WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Materials));
            var MaterialElement = from element in collector
                                  where element.Name == name
                                  select element;

            if (MaterialElement.Count() == 0)
                return null;
            return MaterialElement.First<Element>() as Material;
        }



        private Material CreateSampleConcreteMaterial()
        {
            Material materialNew = null;
            //Try to copy an existing material.  If it is not available, create a new one.
            Material masonry_Concrete = GetMaterial("Concrete, Lightweight");
            if (masonry_Concrete != null)
            {
                materialNew = masonry_Concrete.Duplicate(masonry_Concrete.Name + "_new");
                materialNew.MaterialClass = "Concrete";
            }
            else
            {
                ElementId idNew = Material.Create(uidoc1.Document, "New Concrete Sample");
                materialNew = uidoc1.Document.GetElement(idNew) as Material;
                materialNew.Color = new Autodesk.Revit.DB.Color(130, 150, 120);
            }

            //Create a new structural asset and set properties on it.
            StructuralAsset structuralAsssetConcrete = new StructuralAsset("ConcreteStructuralAsset", Autodesk.Revit.DB.StructuralAssetClass.Concrete);
            structuralAsssetConcrete.ConcreteBendingReinforcement = .5;
            structuralAsssetConcrete.DampingRatio = .5;

            //Create a new thermal asset and set properties on it.
            ThermalAsset thermalAssetConcrete = new ThermalAsset("ConcreteThermalAsset", Autodesk.Revit.DB.ThermalMaterialType.Solid);
            thermalAssetConcrete.Porosity = 0.2;
            thermalAssetConcrete.Permeability = 0.3;
            thermalAssetConcrete.Compressibility = .5;
            thermalAssetConcrete.ThermalConductivity = .5;

            //Create PropertySets from assets and assign them to the material.
            PropertySetElement pseThermal = PropertySetElement.Create(uidoc1.Document, thermalAssetConcrete);
            PropertySetElement pseStructural = PropertySetElement.Create(uidoc1.Document, structuralAsssetConcrete);

            materialNew.SetMaterialAspectByPropertySet(MaterialAspect.Structural, pseStructural.Id);
            materialNew.SetMaterialAspectByPropertySet(MaterialAspect.Thermal, pseThermal.Id);

            return materialNew;
        }

        private FamilySymbol GetProfile(string name)
        {
            FilteredElementCollector profiles = new FilteredElementCollector(uidoc1.Document);
            profiles.OfCategory(BuiltInCategory.OST_ProfileFamilies);
            var MaterialElement = from element in profiles
                                  where element.Name == name
                                  select element;
            return MaterialElement.First<Element>() as FamilySymbol;
        }
    }
}

0 Likes
Message 34 of 38

lukaskohout
Advocate
Advocate

It is as I said above, you need to put this:

CompoundStructure cs = wallTypes.First().GetCompoundStructure();
Material concrete = doc.GetElement(cs.GetMaterialId(0)) as Material;

after this:

foreach (ElementId id in selectedElementIds)
{
   Element e = doc.GetElement(id);
   if (e is Wall)
   {
      Wall w = e as Wall;
      if (!wallTypes.Contains(w.WallType))
         wallTypes.Add(w.WallType);
   }
}

 Otherwise you are looping through empty List which throws the error you see.


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 35 of 38

Anonymous
Not applicable

perfect @lukaskohout  it works! last thing if I can still bothering you, 

Material concrete = doc.GetElement(cs.GetMaterialId(0)) as Material;

is it possible not to have the "(1)" written at the end of the material name?

0 Likes
Message 36 of 38

lukaskohout
Advocate
Advocate

I just depends on what material you get. The (1) means that you have duplicated some material and you are using it now. You can have whatever material you want asigned to the CompoundStructure.


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
Message 37 of 38

Anonymous
Not applicable

I understand @lukaskohout  but is it possible to delete it the (1) or hide it? 

0 Likes
Message 38 of 38

lukaskohout
Advocate
Advocate

You cane rename the material, or select different one.

Material.Rename = "your desired name for the material"

 

The code I posted just uses the materials you have assigned in the project wall types in selection.


Hitting Accepted Answer is a Community Contribution from me as well as from you.
======================================================================
0 Likes