For all issues regarding the Forums use, please, refer to the Forum Rules.

Our Solutions

Need professional assistance?
Consider our:

Support Offerings

 

Need to speed up your development?
Have a look at our:

Samples & Tools

 

Need some functionality extending standard OCCT capabilities?
Check out our:

Adv. Components

Related pages

Vertices of my cylinder are wrong

c r's picture
Forums: 

Hello,

I'm generating a cylinder with this:


BRepPrimAPI_MakeCylinder aMakeCylinder = new BRepPrimAPI_MakeCylinder(new gp_Ax2(new gp_Pnt(0, 0, 0), new gp_Dir(0, 0, 1)), 1, 1, 0);
TopoDS_Shape myBody = aMakeCylinder.Shape();

and then i'm extracting the vertices:

        public static List<List<gp_Pnt>> toVertices(TopoDS_Shape myBody)
        {

            List<List<gp_Pnt>> myPoints = new List<List<gp_Pnt>>();

            
            
            // we explore each face
            for (TopExp_Explorer aFaceExplorer = new TopExp_Explorer(myBody, TopAbs_ShapeEnum.TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
            {
                TopoDS_Face face = TopoDS.TopoDS.ToFace(aFaceExplorer.Current());
                


                // in each face we get all the vertex
                List<gp_Pnt> tempList = new List<gp_Pnt>();
                for (TopExp_Explorer exp = new TopExp_Explorer(face.Oriented(TopAbs_Orientation.TopAbs_FORWARD), TopAbs_ShapeEnum.TopAbs_VERTEX); exp.More(); exp.Next())
                {

                    TopoDS_Vertex vertex = TopoDS.TopoDS.ToVertex(exp.Current());
                    gp_Pnt pt = BRep_Tool.Pnt(vertex);
                    tempList.Add(pt);

                }
                myPoints.Add(tempList);
                

            }


            return myPoints;
        }

if i print the vertices of one face, i get for example:

[0.5; 0; 0.5]

[0.5; 0; 0.5]

[0.5; 0; 0.5]

[0.5; 0; 0]

[0.5; 0; 0]

[0.5; 0; 0]

[0.5; 0; 0.5]

[0.5; 0; 0]

only two differents points...what happens ??

c r's picture

Note that it works perfectly with a cube

c r's picture

Here is my solution:

so i'm still building my cylinder the same way as previously

            BRepPrimAPI_MakeCylinder aMakeCylinder = new BRepPrimAPI_MakeCylinder(new gp_Ax2(new gp_Pnt(0, 0, 0), new gp_Dir(0, 0, 1)), 1, 2, 0);
            
            TopoDS_Shape myBody = aMakeCylinder.Shape();

and i'm using FinishShape on myBody

        public List<List<gp_Pnt>> FinishShape(TopoDS_Shape input)
        {
            // we init our output array (each List<Gp_pnt> will contain a face)
            List<List<gp_Pnt>> myPoints = new List<List<gp_Pnt>>();

            // i count the number of faces but you don't really need, it's simply to remove the first face
            int faceCount = 0;
            
            // we explore each face
            for (TopExp_Explorer aFaceExplorer = new TopExp_Explorer(input, TopAbs_ShapeEnum.TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
            {
                // conversion of the explorer into a face
                TopoDS_Face face = TopoDS.TopoDS.ToFace(aFaceExplorer.Current());
                
                // creation of a temporary array that will contain a face and that we will add to our output array
                List<gp_Pnt> tempList = new List<gp_Pnt>();

                // we explore each edge
                for (TopExp_Explorer aEdgeExplorer = new TopExp_Explorer(face.Oriented(TopAbs_Orientation.TopAbs_FORWARD), TopAbs_ShapeEnum.TopAbs_EDGE); aEdgeExplorer.More(); aEdgeExplorer.Next())
                {

                    // we convert the explorer into an edge
                    TopoDS_Edge edge = TopoDS.TopoDS.ToEdge(aEdgeExplorer.Current());

                    // seems useless to build the curve ?
                    //BRepLib.BRepLib.BuildCurve3d(edge);

                    // still didn't figure out what was the purpose of thoses values
                    double first = 0, last = 1;

                    // we convert our edge into a curve
                    Geom_Curve curve = Geom_Curve.DownCast(BRep.BRep_Tool.Curve(edge, ref first, ref last));
                    
                    // sometimes fp = - infinite and lp = infinite, i guess it means that it is not a curve ?
                    double fp = curve.FirstParameter(), lp = curve.LastParameter();

                    // so i check if this is a curve or not.. i do only between -20 and 20 because it is what takes time
                    if (fp>-20 && lp<20)//curve.IsKind(Geom_BezierCurve.TypeOf()))
                    {
                        // i get "each" values between fp and lp 
                        for (double i = fp ; i < lp; i += 0.1)
                        {
                            // in fact the same face is added many time, and sometimes with many times the same point... so i verify it, u can simply do whatever you want with the data, try to remove it and draw on a paper the points you get
                            if (ContainedIn(myPoints, curve.Value(i)) || ContainedIn(tempList, curve.Value(i)))
                                break;

                            // we add our point
                            tempList.Add(curve.Value(i));
                        }

                    } else
                    {
                        // seems useless to build the edge
                        /*
                        BRepBuilderAPI_MakeEdge makeEdge = new BRepBuilderAPI.BRepBuilderAPI_MakeEdge(curve, first, last);
                        makeEdge.Build();
                        edge = makeEdge.Edge();//*/

                        // we explore the edge to get our 2 vertices
                        for (TopExp_Explorer aVertexExplorer = new TopExp_Explorer(edge, TopAbs_ShapeEnum.TopAbs_VERTEX); aVertexExplorer.More(); aVertexExplorer.Next())
                        {
                            TopoDS_Vertex vertex = TopoDS.TopoDS.ToVertex(aVertexExplorer.Current());

                            gp_Pnt pt = BRep_Tool.Pnt(vertex);

                            if (ContainedIn(myPoints, pt) || ContainedIn(tempList, pt))
                                break;

                            // we add our point that we converted from the explorer
                            tempList.Add(pt);

                        }//*/
                    }
                    

                }

                // we add our faces with their orientations
                if (tempList.Count >= 3 && faceCount>0)
                // or it is not a face //first face sucks
                {
                    orientations.Add(face.Orientation());
                    myPoints.Add(tempList.GetRange(0,tempList.Count-1)); // we add everything but the last point that is the center...
                }
                faceCount++;

            }//*/


            return AddMoreFaces(myPoints);

        }

it retrieves two faces ( top and bottom )

now it's your job to create the lateral faces (small hint, with my code you simply need to iterate through the faces and put together the vertices List1[i], List1[i+1], List2[i+1], List2[i]

it will create rectangular faces all around your cylinder

simply create triangles from those rectangles and ggwp =)

c r's picture

I'm on a good mood

this is the function to add the lateral faces, apply it on myPoints, the output of FinishShape

        public List<List<gp_Pnt>> AddMoreFaces(List<List<gp_Pnt>> list)
        {
            List<List<gp_Pnt>> newList = new List<List<gp_Pnt>>();
            if(list.Count==2)// we could also assume that you ll only give top and bottom faces
            {
                for (int i = 0; i < Math.Min(list[0].Count-1, list[1].Count-1); i++) // i think it is useless to verify that the list have the same size, but just in case...
                {
                    List<gp_Pnt> tempList = new List<gp_Pnt>();
                    tempList.Add(list[0][i]);
                    tempList.Add(list[0][i+1]);
                    tempList.Add(list[1][i+1]);
                    tempList.Add(list[1][i]);
                    newList.Add(tempList);
                    orientations.Add(TopAbs_Orientation.TopAbs_REVERSED); // it is always on that orientation as i could see for the moment
                }

            }

            // append is a custom function that does list.Append(newList) ( you need to add your new faces to the top and bottom faces =)
            return Append(list,newList);
        }

c r's picture

Edit: the way i'm buidling a cylinder is wrong, the parameter angle is set to 0, remove it