Icon representing a recipe

Recipe: Remove Cuts (+autobkup) v1.0

created by man_the_stan

Profile


Name
Remove Cuts (+autobkup) v1.0
ID
108631
Shared with
Public
Parent
None
Children
None
Created on
April 05, 2024 at 19:26 PM UTC
Updated on
April 05, 2024 at 19:35 PM UTC
Description

Quietly removes all cuts from the selected ranges (if any segments are selected) or from the entire puzzle (if no segments are selected --at which point it will also ask for confirmation).
The total number of cuts in the puzzle is checked after each removal to ensure that the removal was successful (if it was not, or any error occurs, then this is logged in the recipe output, but the cut removal continues).

Before performing the cuts removal, the recipe with an automatically-generated name that conforms to the name scheme used by the Auto Recover recipe ("AUTOBKUP__YYYY-MM-DD_Day_HH-MM-SS[__OPTIONAL-EXTRA-DESCRIPTOR]" -- here the extra descriptor is "REMCUT").
If this save fails, a dialog will appear informing you of the failure and asking whether to continue with removing cuts anyway.

Best for


Code


-- Remove Cuts (with Auto Backup integration) v1.0 -- man_the_stan cuts = structure.GetCuts() ncuts = #cuts function RemoveCuts() local noCheckSelection = false if selection.GetCount() == 0 then local dia = dialog.CreateDialog("Remove ALL cuts (" .. ncuts .. ")?") for i = 1,ncuts do selection.SelectRange(cuts[i], cuts[i] + 1) end dia.label = dialog.AddLabel("No segments are selected.") dia.label2 = dialog.AddLabel("Remove all " .. ncuts .. " cut(s) from the entire puzzle?") dia.btn1 = dialog.AddButton("Remove all", 1) dia.btn2 = dialog.AddButton("Cancel", 0) if dialog.Show(dia) == 0 then selection.DeselectAll() return nil else selection.DeselectAll() noCheckSelection = true end end -- Removing all cuts from structure for i,idx in ipairs(cuts) do if noCheckSelection or (selection.IsSelected(idx) and selection.IsSelected(idx + 1)) then local ncuts0, tf, msg = ncuts, pcall(structure.DeleteCut, idx) cuts = structure.GetCuts() ncuts = #cuts if tf then if ncuts ~= ncuts0 - 1 then print("Removal of cut #" .. i .. " between segments " .. idx .. "-" .. idx + 1 .. " did not succeed as expected.") end else print( "Error occurred while attempting to remove #" .. i .. " between segments " .. idx .. "-" .. idx + 1 .. ":", msg) end end end return true end if ncuts > 0 then tf, msg = pcall( save.SaveSolution, os.date("AUTOBKUP__%Y-%M-%d_%a_%H-%M-%S__REMCUT") --[[@as string]] ) if tf then undo.SetUndo(false) RemoveCuts() undo.SetUndo(true) else print("Error message:", msg) dia = dialog.CreateDialog("Pre-remcut save failed") dia.label = dialog.AddLabel("Failed to save solution (see output for details). Proceed with remcut?") dia.btn1 = dialog.AddButton("Yes (!!)", 1) dia.btn2 = dialog.AddButton("No", 0) tf = dialog.Show(dia) == 1 end else print("No cuts to remove.") end

Comments