For 2 days long I couldn't realize why on earth your code is not working because it is totaly correct.
It is just the way Revit orders it transactions.
Here is the complete code that makes it work
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
#endregion
namespace RevitAddinCS2
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class ExtCmd : IExternalCommand
{
#region Cached Variables
private static ExternalCommandData _cachedCmdData;
public static UIApplication CachedUiApp
{
get
{
return _cachedCmdData.Application;
}
}
public static RvtApplication CachedApp
{
get
{
return CachedUiApp.Application;
}
}
public static RvtDocument CachedDoc
{
get
{
return CachedUiApp.ActiveUIDocument.Document;
}
}
#endregion
#region IExternalCommand Members
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
//TODO: add your code below.
FilteredElementCollector modelTexts = new FilteredElementCollector(CachedDoc).OfClass(typeof(ModelText));
FilteredElementCollector materials = new FilteredElementCollector(CachedDoc).OfClass(typeof(Material));
var matByName = from material in materials
where(material.Name == "Acoustic Ceiling Tile 24\" x 48\"")
select material;
ModelText modelText = (ModelText)modelTexts.FirstElement();
Material mat = (Material)matByName.FirstOrDefault();
PaintModelTextFaces(modelText, mat.Id);
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
public void PaintModelTextFaces(ModelText modelText, ElementId elementId)
{
// Before acquiring the geometry, make sure the detail level is set to 'Fine'
Options geoOptions = new Options();
geoOptions.DetailLevel = ViewDetailLevel.Fine;
// Obtain geometry for the given Wall element
GeometryElement geoElem = modelText.get_Geometry(geoOptions);
using (Transaction trans = new Transaction(CachedDoc))
{
IEnumerator<GeometryObject> geoObjectItor = geoElem.GetEnumerator();
trans.Start("PaintModelTextFaces");
while (geoObjectItor.MoveNext())
{
// need to find a solid first
Solid theSolid = geoObjectItor.Current as Solid;
if (null != theSolid)
{
foreach (Face face in theSolid.Faces)
{
using (SubTransaction st = new SubTransaction(CachedDoc))
{
st.Start();
CachedDoc.Paint(modelText.Id, face, elementId);
st.Commit();
}
}
}
}
trans.Commit();
}
}
#endregion
}
}
Please if this reply satisfies your need don't forget to mark it as an answer.
¯\_(ツ)_/¯
Let it work like a charm.
Mustafa Salaheldin


Digital Integration Manager, DuPod
Facebook |
Twitter |
LinkedIn