
Type: Macro · Revit API
Version: Revit 2020
There are three main ways to use this macro:
- 1.Use the .rvt file provided: WB-DividirMurosMulticapa.rvt.
- 2.With Revit closed, extract the contents of the .zip provided (editGeometry.zip) to the following path: C:\ProgramData\Autodesk\Revit\Macros\2020\Revit\AppHookup.
- 3.In an existing project, create a new C# macro module, name it “editGeometry” and paste in the attached code.
By default, Revit macros are available under Manage -> Macros -> Macro Manager.

This macro lets you split multi-layer walls into independent walls while keeping all the properties of the original* wall.
As an example, the macro shows how, by selecting a multi-layer wall, new independent walls are created from each of the layers it contains. In the example, the new wall types are named by joining the material name and the layer thickness.
The macro works very simply. It selects an existing wall, reads the properties of each of its layers, creates a new wall in the same position as the original and deletes the original.
How it works
The macro is structured in three very simple blocks:
Block 1: selecting the element and its properties.
This gathers the characteristics needed to create the new wall types later on.
To create the new wall type(s) we use the Create method, which needs the following information:
Create Method (Document, Curve, ElementId, ElementId, Double, Double, Boolean, Boolean)
The following excerpt declares the required variables and retrieves the characteristics needed for this method to work correctly.
Excerpt:
FilteredElementCollector levels = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType();
ICollection levelElem = levels.ToElementIds();
ElementId levelId = levelElem.First() as ElementId;
ICollection selIds = uiDoc.Selection.GetElementIds();
IList refs = uiDoc.Selection.PickObjects(ObjectType.Element,"Seleccione los muros que desea dividir");
foreach (Reference refe in refs)
{
Wall muro = doc.GetElement(refe) as Wall;
List ids = new List(refs.Select(r => r.ElementId));
List muroList = new List();
muroList.Add(muro);
foreach (Wall muroNuevo in muroList)
{
double alturaMuro = muroNuevo.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble(); double espesor = 0;
double capaAnt = 0;
double offsetMuro = 0;
bool flipMuro = false;
bool strMuro = false;
double medioEspesor;
double capas;
XYZ lineaInicio, lineaFin, v, w, a, b;
List listaMuros = new List();
int counter = 0;
LocationCurve muroLoc = muroNuevo.Location as LocationCurve;
Curve muroLinea = muroLoc.Curve;
lineaInicio = muroLoc.Curve.GetEndPoint( 0 );
lineaFin = muroLoc.Curve.GetEndPoint( 1 );
medioEspesor = 0.5 * muroNuevo.WallType.Width;
v = lineaFin - lineaInicio;
v = v.Normalize();
w = XYZ.BasisZ.CrossProduct( v ).Normalize();
a = lineaInicio + medioEspesor * w;
b = lineaFin + medioEspesor * w;
Line lineaExterior = Line.CreateBound(a, b);
XYZ pInicio, pFinal;
pInicio = lineaExterior.GetEndPoint(0);
pFinal = lineaExterior.GetEndPoint(1);
XYZ muroNormal = lineaExterior.Direction.Normalize();
XYZ muroDir = new XYZ(0, 0, 1);
XYZ cross = muroNormal.CrossProduct(muroDir);
CompoundStructure estructura = muroNuevo.WallType.GetCompoundStructure();
};Block 2: handling the layers and the new wall structure.
For each layer in the selected wall, a new wall type is created with that layer's characteristics (material and thickness). We also need to work out where the new walls will go.
There are many ways to do this. For this example we chose to take the exterior location line of the original wall and work out the centreline of each layer by iterating through its CompoundStructureLayer. That centreline becomes the new location of each wall type created.
Excerpt:
foreach( CompoundStructureLayer layer in estructura.GetLayers())
{
capas = estructura.GetLayerWidth(counter);
if (layer.LayerId <= 0)
{
capaAnt += (estructura.GetLayerWidth(0))/2;
espesor += estructura.GetLayerWidth(0);
}
else
{
if (layer.LayerId == 1)
{
capaAnt += capaAnt + (estructura.GetLayerWidth(counter))/2;
}
else
{
capaAnt += (estructura.GetLayerWidth(counter-1))/2+ (estructura.GetLayerWidth(counter))/2;
};
};
XYZ normal = XYZ.BasisZ.CrossProduct(muroNormal);
XYZ desplaza = normal.Multiply(-capaAnt);
XYZ ptoInicio = lineaExterior.GetEndPoint(0).Add(desplaza);
XYZ ptoFinal = lineaExterior.GetEndPoint(1).Add(desplaza);
Line lineaParalela = Line.CreateBound(ptoInicio, ptoFinal);
};Block 3: creating the new types and elements.
Once we have all the characteristics needed to create the new wall types, we run the transaction that first creates a new wall type in the project and then the element in its new location.
Excerpt:
Transaction transaction = new Transaction(doc, "Crear muro");
{
transaction.Start();
ElementId tipoMuroId = layer.MaterialId;
string layerName = doc.GetElement(tipoMuroId).Name;
double layerWidth = layer.Width;
ElementId layerMaterial = layer.MaterialId;
string muroNombre = layerName+"_"+(layerWidth *304.800).ToString()+"mm";
WallType nuevoTipo = null;
WallType tipoMuro = muroNuevo.WallType;
FilteredElementCollector col = new FilteredElementCollector(doc).OfClass(typeof(WallType));
IEnumerable wName = col.Cast().Select (e => e.Name );
foreach (WallType wt in col)
{
if(WallKind.Basic == wt.Kind)
{
if(wName.Contains(muroNombre))
{
Element ele = new FilteredElementCollector(doc).WhereElementIsElementType().ToElements().Where(o => o.Name == muroNombre).First();
nuevoTipo = ele as WallType;
break;
}
else
{
nuevoTipo = tipoMuro.Duplicate((layerName+"_"+(layerWidth *304.800).ToString()+"mm")) as WallType;
listaMuros.Add(nuevoTipo.Name);
break;
}
}
}
List layerList = new List();
layerList.Add(new CompoundStructureLayer(layer.Width, MaterialFunctionAssignment.Structure, layerMaterial));
CompoundStructure compStructure = nuevoTipo.GetCompoundStructure();
compStructure.SetLayers(layerList);
nuevoTipo.SetCompoundStructure(compStructure);
Wall muroNew = Wall.Create(doc, lineaParalela, nuevoTipo.Id, levelId,alturaMuro,offsetMuro,flipMuro,strMuro);
(muroNew.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM)).Set(alturaMuro);
transaction.Commit();EXTRA:
The new wall types are created in the same location as the layers of the compound wall, so for every independent wall created, Revit will show a pop-up warning about overlapping elements.
To avoid this pop-up, we handle the process with the following method:
void omisionAvisos (object sender, Autodesk.Revit.DB.Events.FailuresProcessingEventArgs e)
{
FailuresAccessor fa = e.GetFailuresAccessor();
IList failList = new List();
failList = fa.GetFailureMessages();
foreach (FailureMessageAccessor failure in failList)
{
fa.DeleteWarning(failure);
}
}This method must be integrated into the solution as follows:
doc.Application.FailuresProcessing += omisionAvisos;
{
//Código que provoque un aviso con ventana emergente.
}
doc.Application.FailuresProcessing -= omisionAvisos;By combining the three main blocks, we get a macro that splits multi-layer walls into independent walls using new wall types that essentially keep the properties of the original wall.
Full code
/*
* Created by SharpDevelop.
* User: Jose Valverde [Wise Build] (www.wisebuild.es)
*/
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace editGeometry
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("21AE7E06-094F-4E34-A17B-E958DD812E9F")]
public partial class ThisDocument
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void dividirMuros()
{
UIDocument uiDoc = this.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
FilteredElementCollector levels = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType();
ICollection levelElem = levels.ToElementIds();
ElementId levelId = levelElem.First() as ElementId;
ICollection selIds = uiDoc.Selection.GetElementIds();
IList refs = uiDoc.Selection.PickObjects(ObjectType.Element,"Seleccione los muros que desea dividir");
foreach (Reference refe in refs)
{
Wall muro = doc.GetElement(refe) as Wall;
List ids = new List(refs.Select(r => r.ElementId));
List muroList = new List();
muroList.Add(muro);
foreach (Wall muroNuevo in muroList)
{
double alturaMuro = muroNuevo.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble(); double espesor = 0;
double capaAnt = 0;
double offsetMuro = 0;
bool flipMuro = false;
bool strMuro = false;
double medioEspesor;
double capas;
XYZ lineaInicio, lineaFin, v, w, a, b;
List listaMuros = new List();
int counter = 0;
LocationCurve muroLoc = muroNuevo.Location as LocationCurve;
Curve muroLinea = muroLoc.Curve;
lineaInicio = muroLoc.Curve.GetEndPoint( 0 );
lineaFin = muroLoc.Curve.GetEndPoint( 1 );
medioEspesor = 0.5 * muroNuevo.WallType.Width;
v = lineaFin - lineaInicio;
v = v.Normalize();
w = XYZ.BasisZ.CrossProduct( v ).Normalize();
a = lineaInicio + medioEspesor * w;
b = lineaFin + medioEspesor * w;
Line lineaExterior = Line.CreateBound(a, b);
XYZ pInicio, pFinal;
pInicio = lineaExterior.GetEndPoint(0);
pFinal = lineaExterior.GetEndPoint(1);
XYZ muroNormal = lineaExterior.Direction.Normalize();
XYZ muroDir = new XYZ(0, 0, 1);
XYZ cross = muroNormal.CrossProduct(muroDir);
CompoundStructure estructura = muroNuevo.WallType.GetCompoundStructure();
foreach( CompoundStructureLayer layer in estructura.GetLayers())
{
capas = estructura.GetLayerWidth(counter);
if (layer.LayerId <= 0)
{
capaAnt += (estructura.GetLayerWidth(0))/2;
espesor += estructura.GetLayerWidth(0);
}
else
{
if (layer.LayerId == 1)
{
capaAnt += capaAnt + (estructura.GetLayerWidth(counter))/2;
}
else
{
capaAnt += (estructura.GetLayerWidth(counter-1))/2+ (estructura.GetLayerWidth(counter))/2;
};
};
XYZ normal = XYZ.BasisZ.CrossProduct(muroNormal);
XYZ desplaza = normal.Multiply(-capaAnt);
XYZ ptoInicio = lineaExterior.GetEndPoint(0).Add(desplaza);
XYZ ptoFinal = lineaExterior.GetEndPoint(1).Add(desplaza);
Line lineaParalela = Line.CreateBound(ptoInicio, ptoFinal);
doc.Application.FailuresProcessing += omisionAvisos;
Transaction transaction = new Transaction(doc, "Crear muro");
{
transaction.Start();
ElementId tipoMuroId = layer.MaterialId;
string layerName = doc.GetElement(tipoMuroId).Name;
double layerWidth = layer.Width;
ElementId layerMaterial = layer.MaterialId;
string muroNombre = layerName+"_"+(layerWidth *304.800).ToString()+"mm";
WallType nuevoTipo = null;
WallType tipoMuro = muroNuevo.WallType;
FilteredElementCollector col = new FilteredElementCollector(doc).OfClass(typeof(WallType));
IEnumerable wName = col.Cast().Select (e => e.Name );
foreach (WallType wt in col)
{
if(WallKind.Basic == wt.Kind)
{
if(wName.Contains(muroNombre))
{
Element ele = new FilteredElementCollector(doc).WhereElementIsElementType().ToElements().Where(o => o.Name == muroNombre).First();
nuevoTipo = ele as WallType;
break;
}
else
{
nuevoTipo = tipoMuro.Duplicate((layerName+"_"+(layerWidth *304.800).ToString()+"mm")) as WallType;
listaMuros.Add(nuevoTipo.Name);
break;
}
}
}
List layerList = new List();
layerList.Add(new CompoundStructureLayer(layer.Width, MaterialFunctionAssignment.Structure, layerMaterial));
CompoundStructure compStructure = nuevoTipo.GetCompoundStructure();
compStructure.SetLayers(layerList);
nuevoTipo.SetCompoundStructure(compStructure);
Wall muroNew = Wall.Create(doc, lineaParalela, nuevoTipo.Id, levelId,alturaMuro,offsetMuro,flipMuro,strMuro);
(muroNew.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM)).Set(alturaMuro);
transaction.Commit();
doc.Application.FailuresProcessing -= omisionAvisos;
}
counter += 1;
}
}
Transaction trDelete = new Transaction(doc, "Eliminar muro original");
{
trDelete.Start();
ICollection deletedIdSet = doc.Delete(muro.Id);
trDelete.Commit();
}
}
}
void omisionAvisos (object sender, Autodesk.Revit.DB.Events.FailuresProcessingEventArgs e)
{
FailuresAccessor fa = e.GetFailuresAccessor();
IList failList = new List();
failList = fa.GetFailureMessages();
foreach (FailureMessageAccessor failure in failList)
{
fa.DeleteWarning(failure);
}
}
}
}Files
Download RVT file
Download Zip
Download CS
Notes
This example does not take into account membrane layers in walls (thickness = 0).
* The macro provided handles some of the properties, but not every possible option has been considered. The code provided is NOT meant to be a complete solution; it is an example of one of the possible ways to achieve the stated goal.
The macro splits multi-layer walls and creates independent walls from each of their layers.


