Update SearchAndReplace.py

Replaced the complex method of dealing with rafts to something simpler.

Update SearchAndReplace.py

Update
This commit is contained in:
GregValiant 2025-02-07 18:40:19 -05:00
parent 28a21cb314
commit 36141b082c

View file

@ -24,14 +24,14 @@ class SearchAndReplace(Script):
"search":
{
"label": "Search for:",
"description": "CASE SPECIFIC. 'LAYER' is not the same as 'Layer'. All occurrences of this text (within the search range) will be replaced by the 'Replace with' string.",
"description": "All occurrences of this text (within the search range) will be replaced by the 'Replace with' string. The search string is 'Case Specific' and 'Layer' is not the same as 'layer'.",
"type": "str",
"default_value": ""
},
"replace":
{
"label": "Replace with:",
"description": "The 'Search For' text will get replaced by this text. For MultiLine insertions use the newline character 'backslash plus n' as the delimiter. If your Search term ends with a 'newline' remember to add 'newline' to the end of this Replace term.",
"description": "The 'Search For' text will get replaced by this text. For MultiLine insertions use the newline character 'backslash n' as the delimiter. If your Search term ends with a 'newline' remember to add 'newline' to the end of this Replace term.",
"type": "str",
"default_value": ""
},
@ -53,19 +53,19 @@ class SearchAndReplace(Script):
"search_start":
{
"label": "Start S&R at Layer:",
"description": "Use the Cura Preview layer numbering. The Start Layer will be included. Enter '1' to start with gcode ';LAYER:0'. Enter ''-6'' to start with the first layer of a raft.",
"description": "Use the Cura Preview layer numbering.",
"type": "int",
"default_value": 1,
"minimum_value": -6,
"minimum_value": 1,
"enabled": "enable_layer_search"
},
"search_end":
{
"label": "Stop S&R at end of Layer:",
"description": "Use the Cura Preview layer numbering. Enter '-1' to search and replace to the end of the file. Enter any other layer number and the replacements will conclude at the end of that layer. If the End Layer is equal to the Start Layer then only that single layer is searched.",
"description": "Use the Cura Preview layer numbering. The replacements will conclude at the end of this layer. If the End Layer is equal to the Start Layer then only that single layer is searched.",
"type": "int",
"default_value": -1,
"minimum_value": -1,
"default_value": 2,
"minimum_value": 1,
"enabled": "enable_layer_search"
},
"first_instance_only":
@ -99,12 +99,6 @@ class SearchAndReplace(Script):
global_stack = Application.getInstance().getGlobalContainerStack()
extruder = global_stack.extruderList
retract_enabled = bool(extruder[0].getProperty("retraction_enable", "value"))
# If retractions are enabled then the CuraEngine inserts a single data item for the retraction at the end of the last layer
# 'top_layer' accounts for that
if retract_enabled:
top_layer = 2
else:
top_layer = 1
search_string = self.getSettingValueByKey("search")
replace_string = self.getSettingValueByKey("replace")
is_regex = self.getSettingValueByKey("is_regex")
@ -118,71 +112,52 @@ class SearchAndReplace(Script):
ignore_end = True
first_instance_only = bool(self.getSettingValueByKey("first_instance_only"))
#Find the raft and layer:0 indexes
raft_start_index = 0
layer_0_index = 0
# Account for missing layer numbers when a raft is used
start_index = 1
end_index = len(data)
try:
for l_num in range(2,12,1):
layer = data[l_num]
if ";LAYER:-" in layer and raft_start_index == 0:
raft_start_index = l_num
if ";LAYER:0" in layer:
layer_0_index = l_num
break
if raft_start_index == 0:
raft_start_index = layer_0_index
raft_layers = 0
elif raft_start_index < layer_0_index:
raft_layers = layer_0_index - raft_start_index
end_index = len(data) - 1
data_list = [0,1]
layer_list = [-1,0]
lay_num = 1
for index, layer in enumerate(data):
if re.search(";LAYER:(-?\d+)", layer):
data_list.append(index)
layer_list.append(lay_num)
lay_num += 1
# Get the start and end indexes within the data
if not enable_layer_search:
if ignore_start:
start_index = 2
else:
raft_layers = 0
except:
pass
#Determine the actual start and end indexes of the data
try:
if not enable_layer_search:
if ignore_start:
start_index = 2
else:
start_index = 1
if ignore_end:
end_index = len(data) - top_layer
else:
end_index = len(data)
elif enable_layer_search:
if start_layer < 1 and start_layer != -6:
start_index = layer_0_index - raft_layers
elif start_layer == -6:
start_index = 2
else:
start_index = raft_start_index + start_layer - 1
if end_layer == -1:
end_index = len(data) - top_layer
else:
end_index = raft_start_index + int(end_layer)
if end_index > len(data) - 1: end_index = len(data) - 1 #For possible user input error
if int(end_index) < int(start_index): end_index = start_index #For possible user input error
except:
start_index = 2
end_index = len(data) - top_layer
start_index = 1
if ignore_end:
end_index = data_list[len(data_list) - 1]
else:
# Account for the extra data item when retraction is enabled
end_index = data_list[len(data_list) - 1] + (2 if retract_enabled else 1)
elif enable_layer_search:
for index, num in enumerate(layer_list):
if num == start_layer:
start_index = data_list[index]
if num == end_layer:
end_index = data_list[index]
# Make replacements
replaceone = False
replace_one = False
if not is_regex:
search_string = re.escape(search_string)
search_regex = re.compile(search_string)
for num in range(start_index, end_index, 1):
for num in range(start_index, end_index + 1, 1):
layer = data[num]
# First_instance only
if first_instance_only:
if re.search(search_regex, layer) and replaceone == False:
if re.search(search_regex, layer) and replace_one == False:
data[num] = re.sub(search_regex, replace_string, data[num], 1)
replaceone = True
replace_one = True
break
# All
# All instances
else:
if end_index > start_index:
data[num] = re.sub(search_regex, replace_string, layer)