DIY Material cut optimiser: Difference between revisions

From DIYWiki
Jump to navigation Jump to search
No edit summary
Line 149: Line 149:
To use in a different environment you would need to replace the Code.gs functionality with something appropriate to your new environment.
To use in a different environment you would need to replace the Code.gs functionality with something appropriate to your new environment.


[[Media:ScriptAuthorisation.png]]
 
[[Media:AppsScriptCodeEditor.png]]
[[Image:ScriptAuthorisation.png]]
[[Media:TheAppsScriptMenu.png]]
[[Image:AppsScriptCodeEditor.png]]
[[Media:ExampleWastagePage.png]]
[[Image:TheAppsScriptMenu.png]]
[[Media:ExampleOrderQuantitiesSheet.png]]
[[Image:ExampleWastagePage.png]]
[[Media:ExampleRequiredItemsList.png]]
[[Image:ExampleOrderQuantitiesSheet.png]]
[[Media:ExampleMaterialsListSheet.png]]
[[Image:ExampleRequiredItemsList.png]]
[[Media:RenamePageInSheet.png]]
[[Image:ExampleMaterialsListSheet.png]]
[[Media:CreateNewGoogleSheet.png]]
[[Image:RenamePageInSheet.png]]
[[Media:ScriptNewMenu.png]]
[[Image:CreateNewGoogleSheet.png]]
[[Media:ExecutingFirstTest.png]]
[[Image:ScriptNewMenu.png]]
[[Media:BestFitTestScript.png]]
[[Image:ExecutingFirstTest.png]]
[[Media:PermissionsRequired.png]]
[[Image:BestFitTestScript.png]]
[[Media:GoToProject.png]]
[[Image:PermissionsRequired.png]]
[[Media:AppNotVerified.png]]
[[Image:GoToProject.png]]
[[Media:ChooseAccount.png]]
[[Image:AppNotVerified.png]]
[[Image:ChooseAccount.png]]

Revision as of 20:33, 31 August 2024

What is cut optimisation?

When you need lots of parts cut to length for a larger project, you could just work out the parts you need, and then cut them as you need them from lengths of martial to hand. This works just fine, but on more involved projects you might find you are using more material that is really necessary, because you might not be making optimal use of the spare bits.

Cut optimisation is basically taking a step back, and trying to group the required cut lengths together in combinations that leave less waste. This can come into its own when you need set length parts and don't really want visible joins.

The manual approach

For small projects, you can often "see" a fairly optimal set of cut combinations. With larger projects, a spreadsheet will often let you try various "what if" style combinations until you get better solutions.

Automation

There are many tools out that that will do some of this. Many are free for use on the web if you only have limited requirements (like not many parts or not many material types etc). More comprehensive tools are usually commercial offerings (and these days "as a service" - i.e. monthly subscriptions).

DIY cut optimisation

So this article describes a "hybrid" cut optimiser that is free to use, and can be expanded to cope with large projects if desired. Hybrid because some of it is just a spreadsheet knocked up in Google Sheets, but there is some back end automation that does the clever stuff that runs behind the scenes.

You can use it to produce cut lists, and also calculate required order quantities of materials.

The code included here is released under GPL v3, so feel free to reuse or adapt as you fancy. Please include a attribution back to this page in any derivative work.

Disclaimer

This project includes software - so it won't work, and it will very likely have plenty of bugs! It was cobbled together for fun to solve a particular problem. It worked for me, but has not yet be that well tested, and it may also be somewhat "fragile" if you use differently from how it was intended!

Implementation

For this particular implementation you will need a google account (not necessarily gmail, but if you have a gmail address, then you also have google account!). Any free account will do, it does not need to be a paid for one. Once you have one of those, head over to drive.google.com This will take you to your document / file repository. Here you can create all typical office style documents like word processing files, spreadsheets, presentation etc. It runs nicely in any web browser (also true on mobile, although there are "apps" as well).

Not only does google docs make it easy to work on docs collaboratively, it also has a rich infrastructure that allows you to add code to your documents to augment and automate tasks. This can be a simple as adding a new user defined function to a spreadsheet, or can be a more complicated "mash up" with code that links out to external services, and downloads, updates, emails, automates etc, using a well designed and well documented set of libraries to handle much of the detail and hard work.

Getting started - adding materials

Click on the "New" button in google drive, and create a new blank spreadsheet. That will create an empty spreadsheet containing a single page called "Sheet1". Click on the down arrow of the tab, select "Rename" and call this tab "Materials". Add some column headings for

Material Description Standard Length Cut kerf Join Overlap

Note you can change these names, but they are referenced directly in the optimisation code, so if you change the names in the sheet, you will also need to change them there. Feel free to make the heading line look pretty with some colour and larger fonts etc.

You can now enter so stock materials. Start on row 2, and add as many as you like. Each material name needs to be unique, so if you have stock material in multiple lengths, modify the name to indicate it (e.g. "4x2 CLS 2.4m" and "4x2 CLS 3.6m"

The "Cut kerf" column indicates how much material is lost during cutting - this is typically due to the width of the blade used. This needs to be accounted for since you can't cut a 1m length of timber into two 500mm lengths!

The overlap column comes into play when you want to make "cuts" that are longer than the stock length of the material you want to cut from. So if you ask for a 5m plank when the stock is only 2.1m, the optimiser will need to use 3 separate lengths of material to achieve your final length.

Adding required items

Create a new page in the sheet using the "+" icon at the bottom left. Call this one "Required items".

Required items and the final cut lengths of material required. Add columns for

Item Name Cut From Required Length Qty

The item name gives a name to a part like "Door jamb" or "Cold water feed pipe". The Cut from field indicates what material you want it cut from - this should be one that you have added to the Materials page. The required length and Qty should hopefully be self explanatory.

Adding Required areas

Although this cut optimiser is designed for cutting linear lengths of stuff, this is a nod to the existence of another dimension! Sometimes you may want to fill in an area with multiple strips of something like shiplap planks for a shed wall, or T&G for a garden gate. Add the columns headings:

Item Name Cut From Area Type Width Height Offset Minimum

The area type is a text field, and two values are current recognised "Rectangle", and "Gable Triangle". Width and height indicate the area wanted, the Offset tells it the effective height of the material being used - so how far another length needs to be shifted from the previous one. (Note "effective", means actual distance shifted, which might be less than the material width - e.g. you might allow an overlap for cladding, and the overlap does not cover any more area).

The minimum column comes into play when a cut length wanted is actually longer than the stock available. If you specify a minimum length of material, say for example you are cladding an area of wall, where you will need to use multiple lengths of material you are probably fixing to studwork or battens, which means your shortest bit has to be long enough to span at least a pair of studs (or perhaps three, so you get at least three fixing points).

Add a page fo the cut list

Next add a page called "Cut List". Add these headings:

Material Item Remaining Length (mm) Part Name Length (mm)

Add a "Wastage" page

Material Total length used Total wasted %

Note the "%" field contains a formula "=C2/B2" - this can be copied down for multiple lines.

Add the "Order Quantities" page

Easy one this:

Material Items

Adding some code

Now we have some pages that will let you list the materials you want to to cut ("Materials"), and also specify what you want cut ("Required Items" and "Required Areas") , plus some to collect the results ("Cut List", "Wastage", and "Order Quantities"), we need something to read in the requirements and spit out the answers. For that we need some "Apps Script".

So click on the Extensions menu in your sheet, and select the "Apps Script"

That will open the apps script code editor, with a default script called "Code.gs" already created, and an example Javascript function called "myFunction". You can click on the "Untitled project" heading and change that to something more memorable!

You can also delete the definition of myFunction - we won't need it.

Now paste in the "sheet I/O and UI code" from the listing below.

Next create another script file using the "+" icon at the end of the "Files" header at the top left of the editor. Call this file "BestFitCalc.gs". Paste in the "best fit" code from below.

Hit CTRL + S to make sure both files are saved... (the editor may pop up an error if it detects some improperly constructed code).

Security

When you attache apps script code to your document, that code will run within your google account, and it can interact with that account. This includes capabilities like being able create, edit, or even delete documents in you google drive. If there is an gmail address associated with the account, and can access all your email and even send email as you. Hence you must take great care when using code that you have not written yourself to make sure it is safe to run.

Google will throw up some warnings the first time you try to run a script attached to your spreadsheet, you will need to click through several steps to verify you understand what you are doing, and also to learn exactly which permissions you are giving the script. (I will only asks for ones that it needs to carry out the tasks included in the current script - if you add more functionality to the script at a later date, it may ask again for new permissions)

First test

Click on the BestFitCalc.gs file. At the top of the editor window are buttons for "Run", "Debug", and a drop down that will list any callable functions in the file - it show show "test", the name of a text / example script at the bottom of the file.

Click "Run".

Here is where you will need to step through the permissions pages...

  1. First will be the "Authorization required" popup, click "Review permissions"
  2. Next it will ask which google account you want to use to run the script. Click on yours.
  3. You then will get a "Google hasn't verified this app" warning. You will need to avoid the "dark pattern" design, and click the small "Advanced" link.
  4. Next you need to click the "Go to...(unsafe)" link
  5. Finally you will need to Allow the script to have the permissions it is requesting. (which at present is only the capability to see, edit, create and delete google sheets spreadsheets).

(of those permissions, it only uses the ability to read and write to pages in the sheet it is attached to).

If all goes well, you should see a Cut list scroll past in the Execution log window.

If that works ok, you are ready to use the sheet to do some analysis.

Close the spreadsheet tab in your browser (it will close the script window automatically with it)

In Use

Next time you open the sheet, you will see a new menu added to the end of the tool bar - this is added by the special "onOpen" function in Code.gs, which will automatically run when you open the sheet.

To perform a cut analysis, just select the "Run best fit analysis" from that new menu.

For the geeks - how it works

The script is split into two files - Code.gs, and BestFitCalc.gs.

The first one is code that will only work in the context of google sheets - it makes use of the lavish google apps script API to read content from the sheet, which it then stuffs into the best fit calculator, and finally pushes the results from the calculator back into the spreadsheet.

The main block of functionality is in the BestFitCalc.gs file. This defines a set of Javascript classes that do all the analysis work. This code should be portable, and can run anywhere you can run Javascript. That would include in a web browser (either on the web or from a local file), or in a locally hosted Javascript engine like Node.js

To use in a different environment you would need to replace the Code.gs functionality with something appropriate to your new environment.