Reorder curves order in Rhino?

I do need to export as .dxf some circles that I do created in Rhino and I need to import this dxf files in a CAM program for drilling.

The issue is that this circles are created random and I do need a way to change the order of them according to x-y coordinates to don’t make the machine to jump from one place to other and I can’t find any command in Rhino for reordering the geometry. Please help.

Normally this is something the CAM program should handle, but if not you can only do this easily by making a script that removes the objects from Rhino, sorts them and recreates them in order. There are many examples already if you search the forum that sort based on proximity, is that what you need?

Yes, something like this. The problem is that I do have quite a lot of holes, more than 100/file, and I do need to keep in sync the Rhino drawings and the dxf files as I do need to take measurements from Rhino to program the depth of drilling into the CAM. So, in my CAM software, the circles are imported as “Geo 1”, “Geo 2”, “Geo 120”, etc.

I do created a Grasshopper definition that can select one by one every circle and measure his depth, but I can’t find a way to sort the order of the circles from the Rhino file.

I don’t see the problem, if you can sort the curves, so can you sort the data in sync, no? Not sure if you can bake curves in order, this has to be done with a script also, or anemone maybe.

edit: btw: have you checked that the order the curves are now in corresponds to the way your cam program is reading them in? otherwise it’s not sure this is solvable

Quite difficult for me to create one. Any help will be apreciated.

Hello - here’s a quick sorted export - very quick and not tested in real life - please test before relying on this -

OrderedXYExport.py (587 Bytes)

To use the Python script use RunPythonScript, or a macro:

_-RunPythonScript "Full path to py file inside double-quotes"

-Pascal

Thank you for your script. I do tested today but unfortunately it is not reordering the curves. I do exported using this script and re-imported back in Rhino. I do run my Grasshopper definition to check the order and seems to don’t be changed.

reordered.3dm (253.2 KB)

Hole_Deep_Length.gh (24.1 KB)

I also tried to replicate this definition without success:

http://storage.ning.com/topology/rest/1.0/file/get/2769308199?profile=original

Hello - yep, looks like selection order does not do it - try this one -

OrderedXYExport.py (792 Bytes)

-Pascal

3 Likes

Hole_Deep_Length_nba.gh (21.1 KB)

doesn’t this work?

3 Likes

Thank you. Much better now.

This way also work. Thank you.

Exactly what I need but this script have one issue: The TextDot objects are shown into reverse order than the order that it is exported. Instead to show from bottom to top 1,2,3 it is displaying 6,5,4,etc.

It is possible also to change the script to click into the viewport the order that will be exported? In QcadCAM I can press the “Order (VO)” buttom (red circled button from top) and after that I can click into the viewport the lines to define the order in which this lines will be mechanised.



Draw_order.3dm (148.6 KB)
Draw_order.dxf (160.0 KB)

I found that changing the count = 1 to count = 0 under rs.UnselectAllObjects() will make the TextDot objects to be displayed from 0 like QcadCAM does, and also changing X = sorted(itemList, key=itemgetter(1,2), reverse=True ) into X = sorted(itemList, key=itemgetter(1,2), reverse=False ) will reverse the order in which the lines are exported.

1 Like

ChatGPT FTW!

import rhinoscriptsyntax as rs
from operator import itemgetter, attrgetter
import Rhino
import scriptcontext as sc

def OrderedExport():
    
    ids = rs.GetObjects("Select objects to export", preselect=True)
    if not ids: return
    
    itemList = []
    for id in  ids:
        geo = rs.coercegeometry(id)
        pt = geo.GetBoundingBox(True).Center
        itemList.append( [id, pt.X, pt.Y, pt])

    X = sorted(itemList, key=itemgetter(1,2), reverse=True ) #Reverse Order
    rs.UnselectAllObjects()
    count = 0
    for x in X:
        #rs.AddTextDot(count,x[3]) #Add text dot in center of geometries (removed, see bellow)
        geo = rs.coercegeometry(x[0])
        sc.doc.Objects.Replace(x[0], geo)
        count += 1

    # create a new layer called "Dim::Dot"
    layer_name = "Dim::Dot"
    if not rs.IsLayer(layer_name):
        rs.AddLayer(layer_name)
    
    # create a new group
    group_name = "Text Dots"
    group_id = rs.AddGroup(group_name)

    # create a list to store the new text dots
    dots = []
    X = sorted(itemList, key=itemgetter(1,2))
    for i in range(len(X)-1, -1, -1):
        # V = Count from ONE = rs.AddTextDot(str(i+1), count from ZERO = rs.AddTextDot(str(i)
        dot_id = rs.AddTextDot(str(i), X[i][3])
        dots.append(dot_id)

    # add text dots to the group
    rs.AddObjectsToGroup(dots, group_id)

    # move the text dots to the "Dim::Dot" layer
    rs.ObjectLayer(dots, "Dim::Dot")
    #rs.MoveObjects(dots, "Dim")
    
    rs.SelectObjects(ids)
#    rs.Command("Export")
    
if __name__ == '__main__':OrderedExport()