Scripting for Laser Cutting

Hi all,
I am completely new to Rhino.Python (or whatever scripting tools you may suggest for the topic below), and I am trying to figure out how to do a set of things.
Mainly, I’m trying to learn how to script this stuff, but I am unfamiliar with the syntax and where to get all the info I need. Here is what I am trying to accomplish:

Background: My team and I process thousands of lasercut projects each year, and we have to check each laser file to make sure it has certain things:

  • No duplicate lines
    • This usually requires a SelDup, and if anything got selected, a Delete
  • No lines floating above or below the C-Plane
  • No lines that have a print or display color different than their layer color
  • All curves that can be joined are joined
  • Any hatch falls any of 3 specific layers
  • Any line falls on any of 5 specific layers
  • (If possible) any lines on a yellow layer are surrounded by lines on a blue layer

Extra features that I would like to learn how to script:

  • If any of these checks fail, pop up a dialog box at the end of this script that says which ones failed, how many duplicates there are, whether or not things are floating above the C-Plane, etc.
  • If those checks do fail, automatically format a string of text and copy it to the clipboard so we can paste into a chat with the customer (students) letting them know what needs to be fixed for us to process the file.
  • If the script completes without detecting any errors, allow us to put in a number (1, 2, 3, etc) that corresponds with a specific selection area that we can then print (aka send to our laser software). Also, if this would allow specific print settings to be applied at that time, that would be incredible.

Any help with similar scripts, good places to find reference material, or help working out the syntax would be incredible. I am used to scripting in C++ mostly, so python, and especially python for Rhino are a little foreign.

Thank you!

All of this is eminently possible with a little work. In a previous life, I worked in a school where we sent files to a laser constantly, sometimes as many as 100 a day per machine in crunch times. Pretty much all of what you want can be done with Python/Rhinoscriptsyntax.

The broad brush strokes:
For dupes, yes, scripting the Rhino command SelDup and then deleting the objects is the fastest way to go.

Checking to see if anything is not on the Z0 plane involves a small routine where one tries to get the curve’s plane (everything should be planar) and seeing if the Z coordinate of the plane is 0 - otherwise flag it. If the curve is not planar, flag it too of course.

Checking the objects for individually applied colors can be done with rs.ObjectColorSource(object) - if it’s not 0, the color is not by layer. Same with rs.ObjectPrintColorSource(object). Don’t know if you also need to check print width (we did), that is also available in the rs… methods.

There is a method for joining curves.

You will need to look at the methods for object layer management - rs.ObjectLayer() and the like. With that you can check which layer objects are on and change them.

Not sure what this is for actually, but if the blue curves are closed, you can check for containment with one of several methods - it can be a bit complicated depending on what you are looking for. If it’s simple lines or polylines, you can just check all of the points with PointInPlanarClosedCurve for example.

As for the extra features, all that is doable with some conditional branching and various text input and output methods.

For messages, there is rs.MessageBox, combined perhaps with a little Python line formatting. There is also rs.TextOut for opening a dialog window with text to paste elsewhere.

Below is the last version of the laser script I developed - it is different than what you are doing in that we made everyone have their geometry on one layer and have the objects colored by object - that way we could have a master file and each individual laser cutting file was a separate layer. It was executed automatically on import, automatically fixed what could be fixed and flagged what couldn’t. The students had fairly strict guides on how to set up their files for cutting.

The script automatically tried to explode blocks, delete duplicates, check colors, join curves (by color), flatten everything to the Cplane, etc. It also had an auto rotate/scale for files which were drawn for example in cm but were supposed to be in mm. And a bunch of other stuff.

LaserImport.py (15.2 KB)

Cutting file guide (only in French, sorry):
ENAC-OC-GuideDecoupe-2020_2.pdf (295.6 KB)

2 Likes

Incredible.

Thank you!
I am actually writing out pseudocode for what I think I need to get done at the moment, and I’ll figure out the syntax later.

It is also 2am for me, so I will have to read through this a little more in the coming days. I appreciate the very fast and detailed response!

1 Like

Looks good. The best way to develop this stuff is using real-world files. You will modify and expand your script over time to include more ‘special cases’ - students are very good at creating those… :stuck_out_tongue_winking_eye:

Many of the problems can be automatically fixed (like flattening stuff on the CPlane) - up to you to determine how much you want to fix silently and how much you want to flag and send back to the user.

Don’t hesitate to post more questions in here on specific operations if needed.

3 Likes

Hi Caleb,

The pseudo looks like a good start.

My advise would be to chop the functionality up in separate tasks.

eg

  • prepare incoming file
  • check and organize layers
  • check and fix curves
  • report errors

Possibly that is not always intuitive because you want to check curves both as part of preparing the file and
during and after organizing the layers.
So you might start with layer organization that includes certain curve checks.
If at some point you find yourself copy-pasting the same functional code to multiple places or modules, you best ‘refactor’ your files into modules.
Do report back and just @Willem me if you want my input at that point.

I imagine you end with a ‘main’ module ( pyton file) that runs your separate tasks in sequence.
Like a production pileline it uses other modules to perform specific tasks.

You define special functions in separate modules that can be imported in task-modules and used there.

Good luck!
-Willem