Skip to content

Commit

Permalink
Update SearchAndReplace.py
Browse files Browse the repository at this point in the history
  • Loading branch information
GregValiant committed Jan 7, 2025
1 parent 349c528 commit fbe4c39
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions plugins/PostProcessingPlugin/scripts/SearchAndReplace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2017 Ghostkeeper
# The PostProcessingPlugin is released under the terms of the LGPLv3 or higher.
# 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 'Ignore StartUp G-code' and 'Ignore Ending G-code' options

Expand All @@ -23,7 +24,7 @@ def getSettingDataString(self):
"search":
{
"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",
"default_value": ""
},
Expand Down Expand Up @@ -167,30 +168,24 @@ def execute(self, data):
start_index = 2
end_index = len(data) - top_layer

# If "first_instance_only" is enabled:
# Make replacements
replaceone = False
if first_instance_only:
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):
layer = data[num]
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):
layer = data[num]
# First_instance only
if first_instance_only:
if re.search(search_regex, layer) and replaceone == False:
data[num] = re.sub(search_regex, replace_string, data[num], 1)
replaceone = True
break
if replaceone: break
return data

# 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:
for index in range(start_index, end_index, 1):
layer = data[index]
data[index] = re.sub(search_regex, replace_string, layer)
elif end_index == start_index:
layer = data[start_index]
data[start_index] = re.sub(search_regex, replace_string, layer)
return data
# All
else:
if end_index > start_index:
data[num] = re.sub(search_regex, replace_string, layer)
elif end_index == start_index:
layer = data[start_index]
data[start_index] = re.sub(search_regex, replace_string, layer)
return data

0 comments on commit fbe4c39

Please sign in to comment.