Get edge vertices of a 2D polygon (counter)clockwise and export to MS SQL POLYGON or SVG

Hey people!

I had multiple building floorplans in Blender drawn as 2D planes (see the screenshot below). I needed to convert these planes to MS SQL POLYGONs so we can use the polygon data in another applications for multiple purposes and to create 3D areas out of these polygons dynamically based on an API response as well. MS SQL POLYGONs requires the edges to be sorted clockwise/counterclockwise. Each polygon represents a room or a workspace area. So I came up with an algorithm sorting the edge vertices of a 2D plane. It should work on any shape but holes are not allowed in the plane and will not be exported.

This is one of the floorplans (or room/workarea plans of a builiding):

Each area needs to be converted into a sorted textual representation of X Y coordinates like POLYGON((X1 Y1, X2 Y2, X3 Y3, …, Xn Yn)) and stored in MS SQL.

You can find the PG with the algorithm here:

All you have to do is load the model, specify the root an pass the model to the _exportPolygons method.
image

It loads a polygon (the wireframe one), extracts the sorted coordinates of the edges and draws a test mirror 3D mesh using these coordinates so one can easily spot problems with the export, if any. The MS SQL compatible POLYGON structure is written out to the console. You can use this algorithm to easily create SVG output as well.

image

If you need to reverse the order of the coordinates, just comment out this line:
image

EDIT:
Example of multiple polygons processed (the green and red small cubes are drawn just for debug purposes):

I hope this will help someone in the future :slight_smile:

:vulcan_salute:

Have a nice day!

R.

3 Likes