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": "search":
{ {
"label": "Search for:", "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", "type": "str",
"default_value": "" "default_value": ""
}, },
"replace": "replace":
{ {
"label": "Replace with:", "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", "type": "str",
"default_value": "" "default_value": ""
}, },
@ -53,19 +53,19 @@ class SearchAndReplace(Script):
"search_start": "search_start":
{ {
"label": "Start S&R at Layer:", "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", "type": "int",
"default_value": 1, "default_value": 1,
"minimum_value": -6, "minimum_value": 1,
"enabled": "enable_layer_search" "enabled": "enable_layer_search"
}, },
"search_end": "search_end":
{ {
"label": "Stop S&R at end of Layer:", "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", "type": "int",
"default_value": -1, "default_value": 2,
"minimum_value": -1, "minimum_value": 1,
"enabled": "enable_layer_search" "enabled": "enable_layer_search"
}, },
"first_instance_only": "first_instance_only":
@ -99,12 +99,6 @@ class SearchAndReplace(Script):
global_stack = Application.getInstance().getGlobalContainerStack() global_stack = Application.getInstance().getGlobalContainerStack()
extruder = global_stack.extruderList extruder = global_stack.extruderList
retract_enabled = bool(extruder[0].getProperty("retraction_enable", "value")) 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") search_string = self.getSettingValueByKey("search")
replace_string = self.getSettingValueByKey("replace") replace_string = self.getSettingValueByKey("replace")
is_regex = self.getSettingValueByKey("is_regex") is_regex = self.getSettingValueByKey("is_regex")
@ -118,71 +112,52 @@ class SearchAndReplace(Script):
ignore_end = True ignore_end = True
first_instance_only = bool(self.getSettingValueByKey("first_instance_only")) first_instance_only = bool(self.getSettingValueByKey("first_instance_only"))
#Find the raft and layer:0 indexes # Account for missing layer numbers when a raft is used
raft_start_index = 0
layer_0_index = 0
start_index = 1 start_index = 1
end_index = len(data) end_index = len(data) - 1
try: data_list = [0,1]
for l_num in range(2,12,1): layer_list = [-1,0]
layer = data[l_num] lay_num = 1
if ";LAYER:-" in layer and raft_start_index == 0: for index, layer in enumerate(data):
raft_start_index = l_num if re.search(";LAYER:(-?\d+)", layer):
if ";LAYER:0" in layer: data_list.append(index)
layer_0_index = l_num layer_list.append(lay_num)
break lay_num += 1
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
else:
raft_layers = 0
except:
pass
#Determine the actual start and end indexes of the data # Get the start and end indexes within the data
try:
if not enable_layer_search: if not enable_layer_search:
if ignore_start: if ignore_start:
start_index = 2 start_index = 2
else: else:
start_index = 1 start_index = 1
if ignore_end: if ignore_end:
end_index = len(data) - top_layer end_index = data_list[len(data_list) - 1]
else: else:
end_index = len(data) # 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: elif enable_layer_search:
if start_layer < 1 and start_layer != -6: for index, num in enumerate(layer_list):
start_index = layer_0_index - raft_layers if num == start_layer:
elif start_layer == -6: start_index = data_list[index]
start_index = 2 if num == end_layer:
else: end_index = data_list[index]
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
# Make replacements # Make replacements
replaceone = False replace_one = False
if not is_regex: if not is_regex:
search_string = re.escape(search_string) search_string = re.escape(search_string)
search_regex = re.compile(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] layer = data[num]
# First_instance only # First_instance only
if 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) data[num] = re.sub(search_regex, replace_string, data[num], 1)
replaceone = True replace_one = True
break break
# All # All instances
else: else:
if end_index > start_index: if end_index > start_index:
data[num] = re.sub(search_regex, replace_string, layer) data[num] = re.sub(search_regex, replace_string, layer)