Paint ModelText's face via revit 2015 API

Paint ModelText's face via revit 2015 API

Anonymous
Not applicable
693 Views
5 Replies
Message 1 of 6

Paint ModelText's face via revit 2015 API

Anonymous
Not applicable

I'm trying to paint ModelText's face via the APIs and there are some erros in doc.Paint(modelText.Id, face, elementId). But I ever try to paint the FamilyInstance and Wall's face by using the same way is ok. If revit doesn't open  ModelText's Transaction document ?  Any guidance would be appreciated.

 

Thanks,

 

Nicky

 

 

 

// Paint the E_path's face
public void PaintModelTextFaces(ModelText modelText, ElementId elementId)
{
       Document doc = modelText.Document;
       Autodesk.Revit.DB.Options opt = new Options();
       Autodesk.Revit.DB.GeometryElement geomElem = modelText.get_Geometry(opt);

       foreach (GeometryObject geomObj in geomElem)
       {
               if (geomObj is Solid)
               {
                    Solid f = geomObj as Solid;
                    foreach (Face face in f.Faces)
                    {
                          using (Transaction trans = new Transaction(doc))
                          { 
                                 trans.Start("PaintModelTextFaces");
                                 doc.Paint(modelText.Id, face, elementId);
                                 trans.Commit();
                           }
                     }
              }
       }
}

 

 

 

0 Likes
694 Views
5 Replies
Replies (5)
Message 2 of 6

Mustafa.Salaheldin
Collaborator
Collaborator

For 2 days long I couldn't realize why on earth your code is not working till god shows me the way.

 

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 with bunch of kudo


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 3 of 6

Mustafa.Salaheldin
Collaborator
Collaborator

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


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 4 of 6

Mustafa.Salaheldin
Collaborator
Collaborator

Here you are the solution

 

#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
    }
}

¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 5 of 6

FrankHolidaytoiling
Advocate
Advocate

Can I make new family instances or duplicate the modeltext to place it in a project ?

Maybe i have to use this?

            ElementTransformUtils.CopyElement(CachedDoc, pickedObj.ElementId, xyz);

 

0 Likes
Message 6 of 6

FrankHolidaytoiling
Advocate
Advocate

only u need the users to keep a copy of the model text, still i will do some reclining after that one ...ahhh

0 Likes