- fixed PDF Tool such that now it can import more types of files including PDF files made with FlatCAM

This commit is contained in:
Marius Stanciu
2020-11-17 20:08:27 +02:00
committed by Marius
parent ced0259ba4
commit de3a23e82f
5 changed files with 89 additions and 66 deletions

View File

@@ -11,6 +11,7 @@ CHANGELOG for FlatCAM beta
- the Follow Geometry outputted by the Follow Tools is now of type multigeo which means that it can be fused with other multigeo object without much pain - the Follow Geometry outputted by the Follow Tools is now of type multigeo which means that it can be fused with other multigeo object without much pain
- Punch Gerber Tool - working on the new manual pads add feature - Punch Gerber Tool - working on the new manual pads add feature
- fixed PDF Tool such that now it can import more types of files including PDF files made with FlatCAM
16.11.2020 16.11.2020

View File

@@ -532,7 +532,8 @@ class PdfParser:
else: else:
ap_list = [int(k) for k in apertures_dict.keys()] ap_list = [int(k) for k in apertures_dict.keys()]
# perhaps it's the only aperture? and in that case we need to start from 10 # perhaps it's the only aperture? and in that case we need to start from 10
ap_list.remove(0) if 0 in ap_list and len(ap_list) == 1:
ap_list.remove(0)
if not ap_list: if not ap_list:
aperture = 10 aperture = 10
else: else:
@@ -874,7 +875,8 @@ class PdfParser:
else: else:
ap_list = [int(k) for k in apertures_dict.keys()] ap_list = [int(k) for k in apertures_dict.keys()]
# perhaps it's the only aperture? and in that case we need to start from 10 # perhaps it's the only aperture? and in that case we need to start from 10
ap_list.remove(0) if 0 in ap_list and len(ap_list) == 1:
ap_list.remove(0)
if not ap_list: if not ap_list:
aperture = 10 aperture = 10
else: else:

View File

@@ -13,15 +13,17 @@ from shapely.geometry import Point, MultiPolygon
from shapely.ops import unary_union from shapely.ops import unary_union
from copy import deepcopy from copy import deepcopy
from io import BytesIO # from io import BytesIO
#
import zlib # import zlib
import re import re
import time import time
import logging import logging
import traceback import traceback
import os import os
from pikepdf import Pdf, parse_content_stream
import gettext import gettext
import appTranslation as fcTranslate import appTranslation as fcTranslate
import builtins import builtins
@@ -128,67 +130,83 @@ class ToolPDF(AppTool):
with self.app.proc_container.new('%s...' % _("Parsing")): with self.app.proc_container.new('%s...' % _("Parsing")):
with open(filename, "rb") as f: with open(filename, "rb") as f:
pdf = f.read() # pdf = f.read()
pdf = Pdf.open(f)
stream_nr = 0 page = pdf.pages[0]
for s in re.findall(self.stream_re, pdf): decomp_file = ''
if self.app.abort_flag: for operands, command in parse_content_stream(page):
# graceful abort requested by the user line = ''
raise grace for op in operands:
try:
line += str(op) + ' '
except Exception as e:
# print(str(e), operands, command)
pass
line += str(command)
decomp_file += line + '\n'
self.pdf_decompressed[short_name] = decomp_file
stream_nr += 1 # stream_nr = 0
log.debug("PDF STREAM: %d\n" % stream_nr) # for s in re.findall(self.stream_re, pdf):
s = s.strip(b'\r\n') # if self.app.abort_flag:
# # graceful abort requested by the user
# https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations # raise grace
# def decompress(data): #
# decompressed = zlib.decompressobj( # stream_nr += 1
# -zlib.MAX_WBITS # see above # log.debug("PDF STREAM: %d\n" % stream_nr)
# ) # s = s.strip(b'\r\n')
# inflated = decompressed.decompress(data) #
# inflated += decompressed.flush() # # https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations
# return inflated # # def decompress(data):
# # decompressed = zlib.decompressobj(
# Convert 2 Bytes If Python 3 # # -zlib.MAX_WBITS # see above
def C2BIP3(string): # # )
if type(string) == bytes: # # inflated = decompressed.decompress(data)
return string # # inflated += decompressed.flush()
else: # # return inflated
return bytes([ord(x) for x in string]) #
# Convert 2 Bytes If Python 3
def inflate(data): # def C2BIP3(string):
try: # if type(string) == bytes:
return zlib.decompress(C2BIP3(data)) # return string
except Exception: # else:
if len(data) <= 10: # return bytes([ord(x) for x in string])
raise #
oDecompress = zlib.decompressobj(-zlib.MAX_WBITS) # def inflate(data):
oStringIO = BytesIO() # try:
count = 0 # return zlib.decompress(C2BIP3(data))
for byte in C2BIP3(data): # except Exception:
try: # if len(data) <= 10:
oStringIO.write(oDecompress.decompress(byte)) # raise
count += 1 # oDecompress = zlib.decompressobj(-zlib.MAX_WBITS)
except Exception: # oStringIO = BytesIO()
break # count = 0
if len(data) - count <= 2: # for byte in C2BIP3(data):
return oStringIO.getvalue() # try:
else: # oStringIO.write(oDecompress.decompress(byte))
raise # count += 1
# except Exception:
try: # break
decomp = inflate(s) # if len(data) - count <= 2:
except Exception as e: # return oStringIO.getvalue()
decomp = None # else:
log.debug("ToolPDF.open_pdf() -> inflate (decompress) -> %s" % str(e)) # raise
#
try: # try:
self.pdf_decompressed[short_name] += (decomp.decode('UTF-8') + '\r\n') # decomp = inflate(s)
except Exception: # except Exception as e:
try: # decomp = None
self.pdf_decompressed[short_name] += (decomp.decode('latin1') + '\r\n') # log.debug("ToolPDF.open_pdf() -> inflate (decompress) -> %s" % str(e))
except Exception as e: #
log.debug("ToolPDF.open_pdf() -> decoding error -> %s" % str(e)) # try:
# self.pdf_decompressed[short_name] += (decomp.decode('UTF-8') + '\r\n')
# except Exception:
# try:
# self.pdf_decompressed[short_name] += (decomp.decode('latin1') + '\r\n')
# except Exception as e:
# log.debug("ToolPDF.open_pdf() -> decoding error -> %s" % str(e))
# self.pdf_decompressed[short_name] = decomp_file
if self.pdf_decompressed[short_name] == '': if self.pdf_decompressed[short_name] == '':
self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Failed to open"), str(filename))) self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Failed to open"), str(filename)))

View File

@@ -29,3 +29,4 @@ reportlab>=3.5
svglib svglib
gdal gdal
pyserial>=3.4 pyserial>=3.4
pikepdf>=2.0

View File

@@ -45,6 +45,7 @@ sudo -H python3 -m pip install --upgrade \
pyqt5 \ pyqt5 \
reportlab \ reportlab \
svglib \ svglib \
pyserial pyserial \
pikepdf
sudo -H easy_install -U distribute sudo -H easy_install -U distribute