Update SearchAndReplace.py

Update SearchAndReplace.py

Changed comments.
Revised code below line 170.
Changed 'curaApp' to 'cura_app'
This commit is contained in:
GregValiant 2025-01-07 12:20:21 -05:00
parent 349c5284a9
commit be509b6696

View file

@ -1,6 +1,7 @@
# Copyright (c) 2017 Ghostkeeper # Copyright (c) 2017 Ghostkeeper
# The PostProcessingPlugin is released under the terms of the LGPLv3 or higher. # The PostProcessingPlugin is released under the terms of the LGPLv3 or higher.
# Altered by GregValiant (Greg Foresi) February, 2023. # Altered by GregValiant (Greg Foresi) February, 2023.
# Added option for "first instance only"
# Added option for a layer search with a Start Layer and an End layer. # Added option for a layer search with a Start Layer and an End layer.
# Added 'Ignore StartUp G-code' and 'Ignore Ending G-code' options # Added 'Ignore StartUp G-code' and 'Ignore Ending G-code' options
@ -23,7 +24,7 @@ class SearchAndReplace(Script):
"search": "search":
{ {
"label": "Search for:", "label": "Search for:",
"description": "All occurrences of this text (within the search range) will be replaced by the 'Replace with' text. The search string is CASE SPECIFIC so 'LAYER' is not the same as 'layer'.", "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.",
"type": "str", "type": "str",
"default_value": "" "default_value": ""
}, },
@ -95,8 +96,8 @@ class SearchAndReplace(Script):
}""" }"""
def execute(self, data): def execute(self, data):
curaApp = Application.getInstance().getGlobalContainerStack() cura_app = Application.getInstance().getGlobalContainerStack()
extruder = curaApp.extruderList extruder = cura_app.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 # 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 # 'top_layer' accounts for that
@ -117,7 +118,7 @@ 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-------------------------------------------------------------------------- #Find the raft and layer:0 indexes
raft_start_index = 0 raft_start_index = 0
layer_0_index = 0 layer_0_index = 0
start_index = 1 start_index = 1
@ -139,7 +140,7 @@ class SearchAndReplace(Script):
raft_layers = 0 raft_layers = 0
except: except:
pass pass
#Determine the actual start and end indexes of the data---------------------------------------------------- #Determine the actual start and end indexes of the data
try: try:
if not enable_layer_search: if not enable_layer_search:
if ignore_start: if ignore_start:
@ -167,29 +168,23 @@ class SearchAndReplace(Script):
start_index = 2 start_index = 2
end_index = len(data) - top_layer end_index = len(data) - top_layer
# If "first_instance_only" is enabled: # Make replacements
replaceone = False replaceone = False
if first_instance_only:
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):
layer = data[num] 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 replaceone == 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 replaceone = True
break break
if replaceone: break # All
return data else:
# For all the replacements
if not is_regex:
search_string = re.escape(search_string)
search_regex = re.compile(search_string)
if end_index > start_index: if end_index > start_index:
for index in range(start_index, end_index, 1): data[num] = re.sub(search_regex, replace_string, layer)
layer = data[index]
data[index] = re.sub(search_regex, replace_string, layer)
elif end_index == start_index: elif end_index == start_index:
layer = data[start_index] layer = data[start_index]
data[start_index] = re.sub(search_regex, replace_string, layer) data[start_index] = re.sub(search_regex, replace_string, layer)