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
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.
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.
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.
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()