# Sort list of points derived from Solid3d clockwise with Python

**URL:** <https://forum.dynamobim.com/t/sort-list-of-points-derived-from-solid3d-clockwise-with-python/57868>\
**Category:** Civil 3D\
**Tags:** python\
**Created:** [December 1, 2020, 12:07pm UTC](https://forum.dynamobim.com/t/sort-list-of-points-derived-from-solid3d-clockwise-with-python/57868 "2020-12-01T12:07:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stabouw](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@stabouw](https://forum.dynamobim.com/u/stabouw)\
**Post date:** [December 1, 2020, 12:07pm UTC](https://forum.dynamobim.com/t/sort-list-of-points-derived-from-solid3d-clockwise-with-python/57868/1 "2020-12-01T12:07:08Z")

</div>

Hello all,

I’m trying to create a Point3dCollection of the bottom vertices of a Solid3d. However, when I run this script:

```
      for id in boundary_id:				
             obj = t.GetObject(id, OpenMode.ForRead)
					#find half of the vertices of a solid
					if isinstance(obj, Solid3d):
						brep = Brep(obj)
						vertices = brep.Vertices
						points_XYZ = []
						for i in vertices:
							point_vertice = i.Point
							points_XYZ.append([point_vertice.X, point_vertice.Y, point_vertice.Z])
							points_XYZ.sort(key=lambda x: (x[2]))
							points = []
							for i in points_XYZ:
								point_a = Point3d(i[0],i[1],i[2])
								points.append(point_a)
																					
						pts = Point3dCollection()
						for k in points[0:(len(points)/2)]:
							pts.Add(k)
						pts.Add(points[0])
						boundary_p3dcol.append(pts)

```

I get the following result: it appears that brep.Vertices results in a unordered list of points

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/9/59f508e0670dd2d1ceba1b29f63952f065543346.png)

The order of the first list is indicated in **red** below:

 ![aa](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/f/bf307ca9446e5ff1f6479bd2cc5367cb939584c7.png)

What I want to achieve is the order marked with **blue** : the Point3Ds are sorted clockwise.  
If the point are sorted clockwise a closed polygon can later on be created for bounding a volume surface.

---

<div class="post-metadata">

**Author:** ![Paolo\_Emilio\_Serra1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/paolo_emilio_serra1/32/12902_2.png) [@Paolo\_Emilio\_Serra1](https://forum.dynamobim.com/u/Paolo_Emilio_Serra1)\
**Post date:** [December 1, 2020, 12:16pm UTC](https://forum.dynamobim.com/t/sort-list-of-points-derived-from-solid3d-clockwise-with-python/57868/2 "2020-12-01T12:16:01Z")

</div>

for k in points[:2]: pts.Add(k)  
pts.Add(points[3])  
pts.Add(points[2])

---

<div class="post-metadata">

**Author:** ![stabouw](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@stabouw](https://forum.dynamobim.com/u/stabouw)\
**Post date:** [December 1, 2020, 2:58pm UTC](https://forum.dynamobim.com/t/sort-list-of-points-derived-from-solid3d-clockwise-with-python/57868/3 "2020-12-01T14:58:39Z")

</div>

Swapping the second and thirth index works fine for rectangular solids with the script below:

```
                for id in boundary_id:
					obj = t.GetObject(id, OpenMode.ForRead)
					#find half of the vertices of a solid
					if isinstance(obj, Solid3d):
						brep = Brep(obj)
						vertices = brep.Vertices
						points_XYZ = []
						for i in vertices:
							point_vertice = i.Point
							points_XYZ.append([point_vertice.X, point_vertice.Y, point_vertice.Z])
							points_XYZ.sort(key=lambda x: (x[2]))
							points = []
							for i in points_XYZ:
								point_a = Point3d(i[0],i[1],i[2])
								points.append(point_a)
						#grab half of list, other half are top vertices
						points_filtered = []
						points_filtered.append(points[0:(len(points)/2)])
						pts = Point3dCollection()
						for k in points_filtered:
							pts.Add(k[0])
							pts.Add(k[1])
							pts.Add(k[3])
							pts.Add(k[2])
							pts.Add(k[0])
						boundary_p3dcol.append(pts)

```

This is however not applicable if the solid is not rectangular (e.g. has 5 base vertices). How can the script function regardless the amount of points in the solid?

---

<div class="post-metadata">

**Author:** ![Paolo\_Emilio\_Serra1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/paolo_emilio_serra1/32/12902_2.png) [@Paolo\_Emilio\_Serra1](https://forum.dynamobim.com/u/Paolo_Emilio_Serra1)\
**Post date:** [December 1, 2020, 4:25pm UTC](https://forum.dynamobim.com/t/sort-list-of-points-derived-from-solid3d-clockwise-with-python/57868/4 "2020-12-01T16:25:35Z")

</div>

you should look at faces and then at the edge loops then rather than immediately go for the veritces.
