Skip to content

Commit

Permalink
Use DEFLATE compression for 3MF zip
Browse files Browse the repository at this point in the history
  • Loading branch information
ansonl committed Feb 12, 2025
1 parent 4b978a3 commit 993c478
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add colored [isolines (contour lines/elevation lines)](https://en.wikipedia.org/

![map feature gcode post processing screenshot](/assets/gui_screenshot.png)

If you find this tool helpful, please leave feedback and consider supporting my development and 3D modeling with a [Printables](https://www.printables.com/@ansonl) "club membership" or [Paypal](https://paypal.me/0x80).
If you find this tool helpful, please leave feedback and consider supporting my development and 3D modeling with a [Printables](https://www.printables.com/@ansonl) "club membership" or a one-time [Paypal](https://paypal.me/0x80) contribution.

My 3D topo and other models are on [MakerWorld](https://makerworld.com/en/@ansonl) and [Printables](https://www.printables.com/@ansonl).

Expand Down
2 changes: 1 addition & 1 deletion printer-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You can also copy G-code to an SD card that you put into the printer.

#### Plate Sliced 3MF

1. Make sure your slicer project has at least 4 distinct filament colors enabled.
1. Make sure your slicer project has at least 4 distinct filament colors enabled that correspond to the color indices used by MFM.

2. Export your original project as a Plate Sliced 3MF with File > Export > **Export all plate sliced file**.

Expand Down
35 changes: 33 additions & 2 deletions src/mfm/plate_sliced.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def zipFilePointerForZip(zipPath: str, write: bool):

# Adapted from https://medium.com/dev-bits/ultimate-guide-for-working-with-i-o-streams-and-zip-archives-in-python-3-6f3cf96dca50
def processAllPlateGcodeForZipFile(inputZip: zipfile.ZipFile, out: typing.TextIO, configuration: MFMConfiguration, statusQueue: queue.Queue):
startTime = time.monotonic()
new_zip = io.BytesIO()

with zipfile.ZipFile(new_zip, 'w') as new_archive:
platesProcessed = 0
for item in inputZip.filelist:
# If you spot an existing file, create a new object
if re.match(PLATE_N, os.path.basename(item.filename)):
Expand Down Expand Up @@ -56,12 +58,41 @@ def processAllPlateGcodeForZipFile(inputZip: zipfile.ZipFile, out: typing.TextIO
# https://stackoverflow.com/questions/51801213/complexity-of-f-seek-in-python/51801243
#process(configuration=configuration, inputFP=io.TextIOWrapper(inputZip.open(zi.filename, mode='r')), outputFP=tmpGcodeFile, statusQueue=statusQueue)

new_archive.write(tmpGcodeFilename, arcname=item.filename)
if statusQueue:
sqItem = StatusQueueItem()
sqItem.statusLeft = f"{os.path.basename(item.filename)}"
sqItem.statusRight = f"Compressing"
sqItem.progress = 50
statusQueue.put(item=sqItem)

new_archive.write(tmpGcodeFilename, arcname=item.filename, compress_type=zipfile.ZIP_DEFLATED, compresslevel=9)
platesProcessed += 1
else:
if statusQueue:
sqItem = StatusQueueItem()
sqItem.statusLeft = f"{os.path.basename(item.filename)}"
sqItem.statusRight = f"Compressing"
sqItem.progress = 50
statusQueue.put(item=sqItem)

# Copy other contents as it is
new_archive.writestr(item, inputZip.read(item.filename))
# Bambu Studio only supports DEFLATE compression
new_archive.writestr(item, inputZip.read(item.filename), compress_type=zipfile.ZIP_DEFLATED, compresslevel=9)

if statusQueue:
sqItem = StatusQueueItem()
sqItem.statusLeft = f"{os.path.basename(configuration[CONFIG_INPUT_FILE])}"
sqItem.statusRight = f"Saving"
sqItem.progress = 75
statusQueue.put(item=sqItem)

out.write(new_zip.getbuffer())

if statusQueue:
sqItem = StatusQueueItem()
sqItem.statusRight = f"Completed all {platesProcessed} plate sliced gcode{'s' if platesProcessed > 0 else ''} in {str(datetime.timedelta(seconds=time.monotonic()-startTime))}s" if platesProcessed > 0 else "Did not find plate sliced gcode"
sqItem.progress = 100
statusQueue.put(item=sqItem)

def allPlateGcodeFilePointersForZipFile(z: zipfile.ZipFile) -> list[typing.TextIO]:
plateGcodeFilePointers = []
Expand Down

0 comments on commit 993c478

Please sign in to comment.