Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Floor from Wall

4 REPLIES 4
Reply
Message 1 of 5
reylorente1
996 Views, 4 Replies

Create Floor from Wall

Tengo este script,que funciona en el revit 2021.Pero al cambiar al Revit 2022,cambia el metodo de creacion del suelo.

Alguna idea de como solucionarlo?

Aqui esta el script

 

I have this script, it works in revit 2021, but when switching to revit 2022, it changes the method of creating the floor.

Any idea how to fix it?

Here is the script

 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Result result = Result.Succeeded;

            //Get UIDocument and Document
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            //Initialize wall selection filter
            WallSelectionFilter wallSelection = new WallSelectionFilter();

            //Select the walls
            IList<Reference> references = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, wallSelection);

            if (references != null && references.Count > 0)
            {
                //Initialize the curves list
                List<Curve> wallCurves = new List<Curve>();

                //Initialize the offsets list
                List<double> offsets = new List<double>();

                //Initialize the levels ids list
                List<ElementId> levelsIds = new List<ElementId>();

                foreach (var refer in references)
                {
                    Wall wall = (Wall)doc.GetElement(refer);

                    if (wall != null)
                    {
                        //Add the wall curve to the curves list
                        wallCurves.Add((wall.Location as LocationCurve).Curve);

                        //Add the wall curve to the curve list
                        offsets.Add(wall.Width / 2.0);

                        //Add the wall curve to the curves list
                        levelsIds.Add(wall.LevelId);

                    }

                    if (levelsIds.All(lvl => lvl == levelsIds[0]))
                    {
                        CurveLoop ccu = CurveLoop.Create(wallCurves);
                       
                        //Create floor curve loop
                        CurveLoop ccud = CurveLoop.CreateViaOffset(ccu, offsets,new XYZ(0, 0, 1));

                        //Initialize the curve array
                        CurveArray curveArray = new CurveArray();

                        //Append the floor curves to the curve array
                        foreach (Curve c in ccud)
                        {
                            curveArray.Append(c);
                        }

                        //Get the level to model the floor
                        Level level = doc.GetElement(levelsIds[0]) as Level;

                        string floorTypeName = "Generic 300mm";

                        //Collect the floor type to be used
                        FloorType floorType = new FilteredElementCollector(doc).OfClass(typeof(FloorType))
                            .First(e => e.Name.Equals(floorTypeName)) as FloorType;

                        using (Transaction tx = new Transaction(doc, "Wall to Floor"))
                        {
                            tx.Start();
                            try
                            {
                                //Create the floor
                                doc.Create.NewFloor(curveArray, floorType, level, false);// Revit API 2021

                                Floor.Create(doc, new List<CurveLoop> { ccud }, floorType.Id, level.Id);// Revit API 2022

                                //Commintchange to the model
                                tx.Commit();
                            }
                            catch (Exception e)
                            {
                                //Asign the error message to the user
                                message = e.Message;

                                //Roll back the model changes
                                tx.RollBack();

                                //Asign the result to failed
                                result = Result.Failed;
                            }
                        }
                    }
                    else
                    result = Result.Failed;
                }
            }
            else
            result = Result.Failed;
            return result;
        }

 

 

4 REPLIES 4
Message 2 of 5
bhprest
in reply to: reylorente1

Yes, the Document.NewFloor() methods have been deprecated in the 2022 API, and replaced with the Floor.Create() methods. 

 

You'll need to check which version of the application is running, and account for it inside your code. How you manage this ultimately depends on how you handle multi-targeting.

Message 3 of 5

Hello, I would like to ask if you still have this project folder. Could you please share it with me for reference? I am currently working on an assignment related to this topic!!

Message 4 of 5

Si claro, pero no fue una carpeta, simplemente lo vi en algún lado, y lo quise reproducir.
Aqui está la solución correcta:

 

Yes, of course, but it wasn't a folder, I simply saw it somewhere, and I wanted to play it. Here is the correct solution:

 

public Result Execute(
  ExternalCommandData commandData,
  ref string message,
  ElementSet elements)
{
    UIApplication uiapp = commandData.Application;
    UIDocument uidoc = uiapp.ActiveUIDocument;
    Application app = uiapp.Application;
    Document doc = uidoc.Document;

    Result result = Result.Succeeded;

    //Initialize wall selection filter
    WallSelectionFilter wallSelection = new WallSelectionFilter();

    //Select the walls
    IList<Reference> references = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, wallSelection);

    if (references != null && references.Count > 0)
    {
        //Initialize the curves list
        List<Curve> wallCurves = new List<Curve>();

        //Initialize the offsets list
        IList<double> offsets = new List<double>();

        //Initialize the levels ids list
        List<ElementId> levelsIds = new List<ElementId>();

        foreach (var refer in references)
        {
            Wall wall = (Wall)doc.GetElement(refer);

            if (wall != null)
            {
                //Add the wall curve to the curves list
                wallCurves.Add((wall.Location as LocationCurve).Curve);

                //Add the wall curve to the curve list
                offsets.Add(wall.Width / 2.0);

                //Add the wall curve to the curves list
                levelsIds.Add(wall.LevelId);
            }
        }
        if (levelsIds.All(lvl => lvl == levelsIds[0]))
        {
            CurveLoop ccu = CurveLoop.Create(wallCurves);

            //Create floor curve loop
            CurveLoop ccud = CurveLoop.CreateViaOffset(ccu, offsets, new XYZ(0, 0, 1));

            //Initialize the curve array
            CurveArray curveArray = new CurveArray();

            //Append the floor curves to the curve array
            foreach (Curve c in ccud)
            {
                curveArray.Append(c);
            }

            //Get the level to model the floor
            Level level = doc.GetElement(levelsIds[0]) as Level;

            string floorTypeName = "Generic 300mm";

            //Collect the floor type to be used
            FloorType floorType = new FilteredElementCollector(doc).OfClass(typeof(FloorType))
                .First(e => e.Name.Equals(floorTypeName)) as FloorType;

            using (Transaction tx = new Transaction(doc, "Wall to Floor"))
            {
                tx.Start();
                try
                {
                    //Create the floor
                    //doc.Create.NewFloor(curveArray, floorType, level, false);// Revit API 2021

                    Floor.Create(doc, new List<CurveLoop> { ccud }, floorType.Id, level.Id);// Revit API 2022

                    //Commintchange to the model
                    tx.Commit();
                }
                catch (Exception e)
                {
                    //Asign the error message to the user
                    message = e.Message;

                    //Roll back the model changes
                    tx.RollBack();

                    //Asign the result to failed
                    result = Result.Failed;
                }
            }

        }
        else
            result = Result.Failed;
    }
    else
    result = Result.Failed;
    return result;

}
 public class WallSelectionFilter : ISelectionFilter
 {
     public bool AllowElement(Element elem)
     {
         return elem is Wall;
     }

     public bool AllowReference(Reference reference, XYZ position)
     {
         return false;
     }
     
 }

 

Also in Revit 20... SDK, there is an example called, GenerateFloor. The ellipsis, depends on the year

Message 5 of 5

Thanks a lot!!!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community