Renaming Walls Names in Revit

Renaming Walls Names in Revit

cbengaaa
Observer Observer
1,536 Views
2 Replies
Message 1 of 3

Renaming Walls Names in Revit

cbengaaa
Observer
Observer

Hello everyone, I am a Civil Engineer who just started to learn programming and RevitAPI

I may be still a bit confused about Revit hierarchy system of Elements (Would be nice if a someone refers me a good resource to understand it)

 

I've made a similar plugin for Columns and it worked [Check Photo], but when I tried to do so with Walls I get errors [Check Photo of Error]

I wrote down my code 
NOTE: All Application and Document is pre-defined and References attached

 

When I tried to debug the process the Exception appears to be in line: 20

 

Thank you in advance 

 

 

 

 

 

 TransactionGroup caseWall = new TransactionGroup(doc, "Renaming Wall Types");
 {
     caseWall.Start();

     FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
     ElementCategoryFilter wall = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
     List<Element> listofwalls = wallCollector.OfClass(typeof(Wall)).WherePasses(wall).ToList();
     List<Wall> listofwallsWalls = new List<Wall>();

     Transaction trnWall = new Transaction(doc, "Renaming Walls");
     {

         trnWall.Start();

         foreach (Element wt in listofwalls)
         {
             
             try
             {
                 if ((wt as Wall).StructuralUsage.ToString() == "Bearing" && (wt as WallType).Kind.ToString() == "Basic")
                 {
                     double width = (wt as Wall).Width * 0.30; //Convert to Meters
                     string actualName = wt.Name;
                     string newName;
                     newName = actualName + $"{(width)}";

                 }

                 else
                 {
                     gm.ShowTask($"WallId Failed: {(wt as Wall).Id}\n Wall Name Failed: {(wt as Wall).Name} ");
                 }

             }

             catch (Exception ex)
             {

                 gm.ShowTask(ex.Message);
             }

         }

         trnWall.Commit();

     }

     caseWall.Assimilate();
 }

 

 

 

 

0 Likes
Accepted solutions (1)
1,537 Views
2 Replies
Replies (2)
Message 2 of 3

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @cbengaaa 

 

    To rename the wall you have choose the WallType.Name, I have modified your code to achieve the functionality. Kindly refer the below code

 

Reference Code:

 

 TransactionGroup caseWall = new TransactionGroup(doc, "Renaming Wall Types");
            {
                caseWall.Start();

                FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
                ElementCategoryFilter wall = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
                List<Element> listofwalls = wallCollector.OfClass(typeof(Wall)).WherePasses(wall).ToList();
                List<Wall> listofwallsWalls = new List<Wall>();

                Transaction trnWall = new Transaction(doc, "Renaming Walls");
                {

                    trnWall.Start();

                    foreach (Element wt in listofwalls)
                    {
                        Wall _wall = wt as Wall; //Converting Element to Wall

                        try
                        {
                            if (_wall.StructuralUsage==StructuralWallUsage.Bearing && _wall.WallType.Kind == WallKind.Basic) //Using Enum for Checking dont't convert to string
                            {
                                double width = _wall.Width / 3.2808399; //Convert to Meters (To Convert feet to meter need to divide 3.2808399)
                                string actualName = wt.Name;
                                string newName;
                                newName = actualName + $"({width})"; //Brackets should be outside the curlybraces

                                //Set New Name to Wall Type
                                _wall.WallType.Name = newName;
                            }

                            else
                            {
                                gm.ShowTask($"WallId Failed: {(wt as Wall).Id}\n Wall Name Failed: {(wt as Wall).Name} ");
                            }

                        }

                        catch (Exception ex)
                        {

                            gm.ShowTask(ex.Message);
                        }

                    }

                    trnWall.Commit();

                }

                caseWall.Assimilate();
            }

 

 

Reference Image:

Mohamed_Arshad_0-1701319937514.png

 

Note : Use Math.Round to control the precision.

 

Links to Learn Revit API

 

https://spiderinnet.typepad.com/ 

https://thebuildingcoder.typepad.com/ 

https://www.autodesk.com/autodesk-university/class/Revit-Programming-Beginners-Easily-Access-Revit-A... 

https://www.autodesk.com/support/technical/article/caas/tsarticles/ts/7I2bC1zUr4VjJ3U31uM66K.html 

 

Video Tutorials

https://youtube.com/playlist?list=PLlyMZ5IcKcci1TvB4qM9S8J-RKp0DhVWO&si=N_G9KYKvXjA3oxLP 

 

Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 3 of 3

cbengaaa
Observer
Observer

thank you so much, you are the best!

 

 

I guess I still need a little dive into Revit API to get it correctly.