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: 

DirectShape from floor faces

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
desdinova
1693 Views, 4 Replies

DirectShape from floor faces

desdinova
Advocate
Advocate

 

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;

            IList<Reference> references = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Face);

            IList<IList<IList<XYZ>>> all = new List<IList<IList<XYZ>>>();

            foreach (Reference reference in references)
            {
                Face face = doc.GetElement(reference).GetGeometryObjectFromReference(reference) as Face;

                IList<IList<XYZ>> loopVertices = new List<IList<XYZ>>();

                PlanarFace planarFace = face as PlanarFace;

                if(!planarFace.FaceNormal.IsAlmostEqualTo(XYZ.BasisZ))
                {
                    EdgeArrayArray edgeArrayArray = planarFace.EdgeLoops;

                    foreach (EdgeArray ea in edgeArrayArray)
                    {
                        IList<XYZ> loops = new List<XYZ>();

                        foreach (Edge e in ea)
                        {
                            Curve c = e.AsCurve();
                            loops.Add(c.GetEndPoint(0));
                        }

                        loopVertices.Add(loops);
                    }
                }
                all.Add(loopVertices);
            }

            CreateTessellatedShape(doc, all, ElementId.InvalidElementId);

            return Result.Succeeded;
        }
public void CreateTessellatedShape(Document doc, IList<IList<IList<XYZ>>> all, ElementId materialId)
        {
            TessellatedShapeBuilder builder = new TessellatedShapeBuilder();

            builder.OpenConnectedFaceSet(true);

            foreach (IList<IList<XYZ>> loopVertices in all)
            {
                TessellatedFace tessellatedFace = new TessellatedFace(loopVertices, materialId);

                if (builder.DoesFaceHaveEnoughLoopsAndVertices(tessellatedFace))
                {
                    builder.AddFace(tessellatedFace);
                }
            }

            builder.CloseConnectedFaceSet();
            builder.Target = TessellatedShapeBuilderTarget.AnyGeometry;
            builder.Fallback = TessellatedShapeBuilderFallback.Mesh;
            builder.Build();

            TessellatedShapeBuilderResult result = builder.GetBuildResult();

            using (Transaction t = new Transaction(doc, "Create tessellated direct shape"))
            {
                t.Start();
                DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_Floors));
                ds.ApplicationId = "Application id";
                ds.ApplicationDataId = "Geometry object id";
                ds.SetShape(result.GetGeometricalObjects());
                t.Commit();
            }
        }

 

Hello friends,

I want to create one direct shape from selected floor faces

The bottom face of floor is ok but side faces are not created properly 

How can I solve this

Thanks in advance..

0 Likes

DirectShape from floor faces

 

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;

            IList<Reference> references = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Face);

            IList<IList<IList<XYZ>>> all = new List<IList<IList<XYZ>>>();

            foreach (Reference reference in references)
            {
                Face face = doc.GetElement(reference).GetGeometryObjectFromReference(reference) as Face;

                IList<IList<XYZ>> loopVertices = new List<IList<XYZ>>();

                PlanarFace planarFace = face as PlanarFace;

                if(!planarFace.FaceNormal.IsAlmostEqualTo(XYZ.BasisZ))
                {
                    EdgeArrayArray edgeArrayArray = planarFace.EdgeLoops;

                    foreach (EdgeArray ea in edgeArrayArray)
                    {
                        IList<XYZ> loops = new List<XYZ>();

                        foreach (Edge e in ea)
                        {
                            Curve c = e.AsCurve();
                            loops.Add(c.GetEndPoint(0));
                        }

                        loopVertices.Add(loops);
                    }
                }
                all.Add(loopVertices);
            }

            CreateTessellatedShape(doc, all, ElementId.InvalidElementId);

            return Result.Succeeded;
        }
public void CreateTessellatedShape(Document doc, IList<IList<IList<XYZ>>> all, ElementId materialId)
        {
            TessellatedShapeBuilder builder = new TessellatedShapeBuilder();

            builder.OpenConnectedFaceSet(true);

            foreach (IList<IList<XYZ>> loopVertices in all)
            {
                TessellatedFace tessellatedFace = new TessellatedFace(loopVertices, materialId);

                if (builder.DoesFaceHaveEnoughLoopsAndVertices(tessellatedFace))
                {
                    builder.AddFace(tessellatedFace);
                }
            }

            builder.CloseConnectedFaceSet();
            builder.Target = TessellatedShapeBuilderTarget.AnyGeometry;
            builder.Fallback = TessellatedShapeBuilderFallback.Mesh;
            builder.Build();

            TessellatedShapeBuilderResult result = builder.GetBuildResult();

            using (Transaction t = new Transaction(doc, "Create tessellated direct shape"))
            {
                t.Start();
                DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_Floors));
                ds.ApplicationId = "Application id";
                ds.ApplicationDataId = "Geometry object id";
                ds.SetShape(result.GetGeometricalObjects());
                t.Commit();
            }
        }

 

Hello friends,

I want to create one direct shape from selected floor faces

The bottom face of floor is ok but side faces are not created properly 

How can I solve this

Thanks in advance..

4 REPLIES 4
Message 2 of 5
Revitalizer
in reply to: desdinova

Revitalizer
Advisor
Advisor
Accepted solution

Hi,

 

replace

 

Curve c = e.AsCurve();

 

by

 

Curve c = e.AsCurveFollowingFace();

 

I think the reason for the error is that you just get duplicate vertices by using e.AsCurve() since the direction of the edges is not as you expect.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Hi,

 

replace

 

Curve c = e.AsCurve();

 

by

 

Curve c = e.AsCurveFollowingFace();

 

I think the reason for the error is that you just get duplicate vertices by using e.AsCurve() since the direction of the edges is not as you expect.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 3 of 5
Revitalizer
in reply to: desdinova

Revitalizer
Advisor
Advisor

Hi,

 

see also here:

http://thebuildingcoder.typepad.com/blog/2010/01/face-edges.html

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Hi,

 

see also here:

http://thebuildingcoder.typepad.com/blog/2010/01/face-edges.html

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 4 of 5
desdinova
in reply to: Revitalizer

desdinova
Advocate
Advocate

Thanks a lot Revitalizer,

It is working...

 

0 Likes

Thanks a lot Revitalizer,

It is working...

 

Message 5 of 5
jeremytammik
in reply to: desdinova

jeremytammik
Autodesk
Autodesk
Accepted solution

If you want to create direct shapes from anything at all, you might want to check out the flattener:

 

http://thebuildingcoder.typepad.com/blog/2015/11/flatten-all-elements-to-directshape.html

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

If you want to create direct shapes from anything at all, you might want to check out the flattener:

 

http://thebuildingcoder.typepad.com/blog/2015/11/flatten-all-elements-to-directshape.html

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

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

Post to forums  

Autodesk Design & Make Report