Order Geometries C#

Hello fellow Devs.

I am automating a process of DXF file creation where I send files to a laser, which is driven by simple DXF files in layers. I have the challenge of ordering these geometries by the closes XY location from one another to prevent the machine from jumping all over the place.

Does anyone know how I can affect the ORDER in which these Geos get written to the file?


something along

        var c = curves.OrderBy(c => c.PointAtStart.DistanceTo(Point3d.Origin)).ToList();

for a quick fix.Then you can use that ordered list to populate your layers.


Thank you so much for the idea! It did not even cross my mind to use OrderBy.

Could I ask you 2 more quick questions:

  1. Do I need to delete the layer assignments in Rhino and then re-assign the curves in the order in which I want them to cut?

  2. Related to 1. Will this therefore control the order in which the curves will be written in the layer inside the DXF file? What happens if an export scheme is used? Will that affect the curve order?

Thanks again! I really appreciate the help!

Hello @AxeManGa,

I can not really go too deep on your question as I don’t know how exactly you are writing the .dxf.

Given the ordered list, you can use it to just iterate over it and bake the curve to a different layer everytime(e.g. using for(int i) and layername + i.ToString())
Since you are adding them to the doc in order of the list, you should have no problem, however, you should also remember that you might have to delete the old curves, depending on how your actual workflow is.

        var curves = new List<Curve>();

        for (int i = 0; i < curves.Count; i++)
            System.Drawing.Color color = System.Drawing.Color.FromArgb(i, 125, 125);
            int layerindex = doc.Layers.Add("my new layer " + i.ToString(), color);
            ObjectAttributes att = new ObjectAttributes() { LayerIndex = layerindex };

            doc.Objects.AddCurve(curves[i], att);
1 Like

Thank you RGR! This is a solution!