Change Level Type

Change Level Type

marcintromski
Contributor Contributor
7,571 Views
2 Replies
Message 1 of 3

Change Level Type

marcintromski
Contributor
Contributor

hello im trying to change Type of level. but my code is not working properly. Maybe someone could look at my code and tell me whats wrong? Im beginner to revit api.

 

 

 

 

Level levelToCheckBy = document.GetElementByName<Level>("Level 1");
var walls = document.GetElementsByType<Wall>()
.Where(w => !w.IsCurtain())
.Where(w =>
w.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId() == levelToCheckBy.Id &&
w.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble() > 1 &&
w.WallType.get_Parameter(BuiltInParameter.ALL_MODEL_DESCRIPTION).AsString() == "TheOne")
.ToList();


// exercise 2

List<WallType> wallTypes = new List<WallType>();
foreach (var wall in walls)
{

wallTypes.Add(wall.WallType);
}

List<WallType> newWallTypes = new FilteredElementCollector(document)
.OfCategory(BuiltInCategory.OST_Walls)
.WhereElementIsElementType()
.Cast<WallType>()
.ToList();

 

List<WallType> resultWallTypes = wallTypes.Except(newWallTypes).ToList();

List<Wall> walls2 = new List<Wall>();
List<ElementId> ids = new List<ElementId>();
foreach (WallType resultWallType in resultWallTypes)
{
ids.Add(resultWallType.Id);
walls2 = new FilteredElementCollector(document)
.OfCategory(BuiltInCategory.OST_Walls)
.WhereElementIsNotElementType()
.Cast<Wall>()
.Where(x => x.GetTypeId() == resultWallType.Id)
.ToList();


}

foreach (var wallInstance in walls2)
{
TaskDialog.Show("da", wallInstance.Name.ToString() +
" " + walls2.Count().ToString());
}

 

string levelTypeName = "CustomLevelType";
LevelType levelType = new FilteredElementCollector(document)
.OfCategory(BuiltInCategory.OST_Levels)
.WhereElementIsElementType()
.Cast<LevelType>()
.FirstOrDefault(x => x.Name == levelTypeName) as LevelType;

TaskDialog.Show("CustomLevelType", levelType.Name.ToString());
ElementId lvlElementId = levelType.Id;

TaskDialog.Show("customleveltypeID", lvlElementId.IntegerValue.ToString());


List<LevelType> existinglevelTypes = new FilteredElementCollector(document)
.OfCategory(BuiltInCategory.OST_Levels)
.WhereElementIsElementType()
.Cast<LevelType>()
.ToList();

foreach (LevelType existinglevelType in existinglevelTypes)
{
TaskDialog.Show("existinglevelType", existinglevelType.Name);


Parameter para = existinglevelType.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);

TaskDialog.Show("para", para.StorageType.ToString());
TaskDialog.Show("para", para.Definition.Name);
TaskDialog.Show("para", para.Id.ToString());
using (Transaction trans = new Transaction(document))
{
trans.Start("Set new type for level");
TaskDialog.Show("messgae", "Jestem w transakcji");
para.Set(lvlElementId);

 

trans.Commit();
}
}

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

jeremy_tammik
Alumni
Alumni
Accepted solution

Welcome to the Revit API!

 

I asked Gemini for you: "how can i use the revit api to change the type of a level?"

  

I do not think that the code that it generated for this will work as is, but it gives some additional ideas and explanations that might help understand how to proceed. 

 

It replied:

  

C#
  
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RevitAddin
{
    public class ChangeLevelType : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            // Get the selected level
            Level selectedLevel = uidoc.Selection.PickObject(ObjectType.Element, "Select a Level") as Level;
            if (selectedLevel == null)
            {
                message = "No Level selected.";
                return Result.Failed;
            }

            // Get all available level types
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.OfClass(typeof(LevelType));
            List<LevelType> levelTypes = collector.ToList<LevelType>();

            // Prompt the user to select a new level type
            TaskDialog.Show("Select Level Type", "Choose a new level type for the selected level.");
            LevelType newLevelType = null;
            while (newLevelType == null)
            {
                try
                {
                    string selectedTypeName = TaskDialog.Show("Select Level Type", "Choose a new level type:", TaskDialogCommonButtons.Yes, TaskDialogCommonButtons.No, levelTypes.Select(lt => lt.Name).ToArray());
                    if (selectedTypeName != null)
                    {
                        newLevelType = levelTypes.FirstOrDefault(lt => lt.Name == selectedTypeName);
                    }
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("Error", ex.Message);
                }
            }

            // Change the level type
            using (Transaction t = new Transaction(doc, "Change Level Type"))
            {
                t.Start();
                Parameter param = selectedLevel.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);
                param.Set(newLevelType.Id);
                t.Commit();
            }

            return Result.Succeeded;
        }
    }
}

Explanation:

  1. Get Selected Level:

    • The code starts by getting the selected level from the user using uidoc.Selection.PickObject().
  2. Get Available Level Types:

    • It then retrieves all available level types from the document using a FilteredElementCollector.
  3. Prompt User for New Type:

    • The code presents a dialog box to the user, displaying the names of all available level types.
    • The user is asked to select the desired level type.
  4. Change Level Type:

    • Inside a transaction, the code accesses the ELEM_TYPE_PARAM of the selected level.
    • It sets the value of this parameter to the Id of the newly selected LevelType.

To use this code:

  1. Create a new class library project in Visual Studio.
  2. Add a reference to the Revit API assemblies.
  3. Paste the code into the class.
  4. Implement the IExternalCommand interface.
  5. Compile the project.
  6. Load the add-in into Revit.
  7. Select a level in the Revit model.
  8. Run the command.

This will present a dialog box allowing you to select a new level type for the selected level.

Note:

  • This code assumes that the selected level can be changed to any of the available level types.
  • You may need to add additional checks or logic to handle specific scenarios or constraints.
  • This is a basic example, and you can further enhance it by adding features like filtering level types based on criteria, providing more user-friendly interfaces, etc.

I hope this helps!

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

marcintromski
Contributor
Contributor

thank you for your explanation 🙂

0 Likes