diff --git a/InitGui.py b/InitGui.py index 85efb33..1fe5c26 100644 --- a/InitGui.py +++ b/InitGui.py @@ -34,6 +34,8 @@ import ksu_locator from kicadStepUpCMD import * +from utils import make_string as mk_str, make_unicode as mk_uni + ksuWBpath = os.path.dirname(ksu_locator.__file__) #sys.path.append(ksuWB + '/Gui') @@ -275,33 +277,6 @@ def Activated(self): prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") if prefs.IsEmpty(): #if prefs.GetContents() is None: - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input - def mk_uni(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.decode('utf-8') - return input - else: #py2 - if type(input) != unicode: - input = input.decode('utf-8') - return input - else: - return input - ## FreeCAD.Console.PrintError('Creating first time ksu preferences\n') #prefs.SetString('prefix3d_1',make_string(default_prefix3d)) prefs.SetInt('pcb_color',0) diff --git a/fps.py b/fps.py index 0508a38..162bcec 100644 --- a/fps.py +++ b/fps.py @@ -28,6 +28,8 @@ import math from math import radians +from utils import crc_gen, make_string, make_unicode + consolePrint = FreeCAD.Console.PrintMessage consolePrint('fp loader v'+fps_version+'\n') @@ -98,32 +100,6 @@ def makeAnno (name,bp,txt,afs): return anno # -def crc_gen_t(data): - import binascii - import re - - #data=u'Würfel' - content=re.sub(r'[^\x00-\x7F]+','_', data) - #make_unicode(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - #hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:].encode('utf-8') - #print(data +u'_'+ hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - return u'_'+ make_unicode_t(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) -## - -def make_unicode_t(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.decode('utf-8') - return input - else: #py2 - if type(input) != unicode: - input = input.decode('utf-8') - return input - else: - return input - def mkColor(*color): if len(color)==1: if isinstance(color[0],basestring): @@ -270,7 +246,6 @@ def simple_cpy (obj,lbl): # from kicadStepUptools import removesubtree, cfg_read_all -from kicadStepUptools import make_unicode, make_string import fcad_parser from fcad_parser import KicadPCB,SexpList @@ -355,7 +330,7 @@ def addfootprint(fname = None): last_pcb_path=os.path.dirname(fname) path, ftname = os.path.split(fname) ftname=os.path.splitext(ftname)[0] - ftname_sfx=crc_gen_t(make_unicode_t(ftname)) + ftname_sfx=crc_gen(make_unicode(ftname)) pg = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUp") pg.SetString("last_pcb_path", make_string(last_pcb_path)) # py3 .decode("utf-8") prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") diff --git a/kicadStepUpCMD.py b/kicadStepUpCMD.py index 2f4e91a..ab292c8 100644 --- a/kicadStepUpCMD.py +++ b/kicadStepUpCMD.py @@ -32,6 +32,8 @@ import constrainator from constrainator import add_constraints, sanitizeSkBsp +from utils import make_string as mk_str, make_string + ksuCMD_version__='2.5.7' global invisible_objs @@ -2815,19 +2817,6 @@ def Activated(self): # do something here... if FreeCADGui.Selection.getSelection(): sel=FreeCADGui.Selection.getSelection() - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input if len(sel) != 1: msg="Select one object with Shape to be colored Cloned!\n" reply = QtGui.QMessageBox.information(None,"Warning", msg) @@ -2884,19 +2873,6 @@ def Activated(self): # do something here... if FreeCADGui.Selection.getSelection(): sel=FreeCADGui.Selection.getSelection() - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input if len(sel) != 1: msg="Select one object with Shape to generate a colored Binder!\n" reply = QtGui.QMessageBox.information(None,"Warning", msg) @@ -2963,19 +2939,6 @@ def Activated(self): # do something here... if FreeCADGui.Selection.getSelection(): sel=FreeCADGui.Selection.getSelection() - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input if len(sel) != 2: msg="Select the Binder and one object with Shape to ReLink the Binder!\n" reply = QtGui.QMessageBox.information(None,"Warning", msg) @@ -3019,20 +2982,6 @@ def Activated(self): if FreeCADGui.Selection.getSelection(): sel=FreeCADGui.Selection.getSelection() doc=FreeCAD.activeDocument() - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input - ## doc.openTransaction('union') if len(sel)<1: msg="Select one or two objects with Shape to be copied!\n" @@ -3098,20 +3047,6 @@ def Activated(self): # do something here... if FreeCADGui.Selection.getSelection(): sel=FreeCADGui.Selection.getSelection() - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input - ## if len(sel)<1: msg="Select at least one object with Shape to be copied!\n" reply = QtGui.QMessageBox.information(None,"Warning", msg) @@ -3219,21 +3154,7 @@ def Activated(self): FreeCAD.Console.PrintWarning("Select ONE Part Design Next object\nor one or more objects to be copied!\n") FreeCADGui.addCommand('ksuToolsDeepCopy',ksuToolsDeepCopy()) -##### -def mk_str_u(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input -### + make_compound = False # import FreeCAD as app,FreeCADGui as gui @@ -3275,7 +3196,7 @@ def deep_copy_part(doc, part, compound='flat',suffix='(copy)'): pName= 'None' if make_compound=='compound': - compound = doc.addObject('Part::Compound', mk_str_u(part.Label)+suffix) + compound = doc.addObject('Part::Compound', mk_str(part.Label)+suffix) compound.Links = copied_subobjects if 0: o=copied_subobjects[0] @@ -3286,7 +3207,7 @@ def deep_copy_part(doc, part, compound='flat',suffix='(copy)'): compound.ViewObject.ShapeAppearance = oo.ViewObject.ShapeAppearance pName = doc.ActiveObject.Name elif make_compound=='part': - doc.addObject('App::Part',mk_str_u(part.Label)+'_') + doc.addObject('App::Part',mk_str(part.Label)+'_') #FreeCAD.Console.PrintMessage(doc.ActiveObject.Label) actobj=doc.ActiveObject for uplvlobj in actobj.InListRecursive: @@ -3352,9 +3273,9 @@ def copy_subobject(doc, o,suffix='(copy)'): copy.Shape = o.Shape #copy.Label = 'Copy of ' + o.Label if suffix=='_': - copy.Label = mk_str_u(o.Label)+suffix + copy.Label = mk_str(o.Label)+suffix else: - copy.Label = mk_str_u(o.Label)+'.'+suffix + copy.Label = mk_str(o.Label)+'.'+suffix #copy.Placement = get_recursive_inverse_placement(o).inverse() copy.Placement = o.getGlobalPlacement() @@ -3759,19 +3680,6 @@ def Activated(self): # do something here... if FreeCADGui.Selection.getSelection(): sel=FreeCADGui.Selection.getSelection() - def mk_str(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input def i_say(msg): FreeCAD.Console.PrintMessage(msg) FreeCAD.Console.PrintMessage('\n') @@ -4076,7 +3984,6 @@ def Activated(self): import _DXF_Import import os - from kicadStepUptools import make_unicode, make_string # _DXF_Import.open('D:/Temp/t4k3-DWG.DXF') prefs_ = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") last_pcb_path = prefs_.GetString("last_pcb_path") @@ -4123,7 +4030,6 @@ def Activated(self): try: import ezdxf import os - from kicadStepUptools import make_unicode, make_string # _DXF_Import.open('D:/Temp/t4k3-DWG.DXF') prefs_ = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") last_pcb_path = prefs_.GetString("last_pcb_path") @@ -4171,7 +4077,6 @@ def Activated(self): import _DXF_Import from dxf_parser import _importDXF import os - from kicadStepUptools import make_unicode, make_string prefs_ = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") last_pcb_path = prefs_.GetString("last_pcb_path") if not(prefs_.GetBool('not_native_dlg')): diff --git a/kicadStepUptools.py b/kicadStepUptools.py index 34516db..a40f413 100644 --- a/kicadStepUptools.py +++ b/kicadStepUptools.py @@ -449,6 +449,7 @@ import re import time +from utils import crc_gen, make_string, make_unicode max_recursion_limit=5000 # kSU issue#198 sys.setrecursionlimit(max_recursion_limit) @@ -1787,34 +1788,6 @@ def open(filename,insert=None): # onLoadFootprint(filename) -def make_unicode(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.decode('utf-8') - return input - else: #py2 - if type(input) != unicode: - input = input.decode('utf-8') - return input - else: - return input - -def make_string(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.encode('utf-8') - return input - else: #py2 - if type(input) == unicode: - input = input.encode('utf-8') - return input - else: - return input - def PLine(prm1,prm2): if hasattr(Part,"LineSegment"): return Part.LineSegment(prm1, prm2) @@ -6692,17 +6665,6 @@ def PullPCB(file_name=None): # print('Cancel') ## -def crc_gen(data): - import binascii - import re - - #data=u'Würfel' - content=re.sub(r'[^\x00-\x7F]+','_', data) - #make_unicode(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - #hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:].encode('utf-8') - #print(data +u'_'+ hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - return u'_'+ make_unicode(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) -## def check_lightDir(set_default=False): pg=FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View") diff --git a/makefacedxf.py b/makefacedxf.py index 16b25af..2941b65 100644 --- a/makefacedxf.py +++ b/makefacedxf.py @@ -25,6 +25,8 @@ FC_export_min_version="11670" #11670 latest JM silks_version = '1.5' +from utils import crc_gen, make_string, make_unicode + use_LinkGroups = False if 'LinkView' in dir(FreeCADGui): prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") @@ -65,32 +67,6 @@ def getFCversion(): #if FC_git_Nbr >= int(FC_export_min_version): use_AppPart=True -def crc_gen_d(data): - import binascii - import re - - #data=u'Würfel' - content=re.sub(r'[^\x00-\x7F]+','_', data) - #make_unicode(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - #hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:].encode('utf-8') - #print(data +u'_'+ hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - return u'_'+ make_unicode_d(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) -## - -def make_unicode_d(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.decode('utf-8') - return input - else: #py2 - if type(input) != unicode: - input = input.decode('utf-8') - return input - else: - return input - def find_pcb_name(): #searching for a pcb pcb_name = '' @@ -135,7 +111,6 @@ def simple_cpy (obj,lbl): import importDXF else: from dxf_parser import _importDXF -from kicadStepUptools import make_unicode, make_string def makeFaceDXF(): global copper_diffuse, silks_diffuse, use_dxf_internal @@ -170,7 +145,7 @@ def makeFaceDXF(): if len(fname) > 0: #importDXF.open(fname) last_pcb_path=os.path.dirname(fname) - ftname_sfx=crc_gen_d(make_unicode_d(filename)) + ftname_sfx=crc_gen(make_unicode(filename)) pg = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUp") pg.SetString("last_pcb_path", make_string(last_pcb_path)) # py3 .decode("utf-8") diff --git a/tracks.py b/tracks.py index c28f218..78ea256 100644 --- a/tracks.py +++ b/tracks.py @@ -22,6 +22,8 @@ from kicad_parser import makeVect, make_gr_rect, make_gr_poly, makeThickLine from fcad_parser import unquote #maui +from utils import crc_gen, make_string, make_unicode + global use_AppPart, use_Links, use_LinkGroups use_AppPart=False # False use_Links=False @@ -109,32 +111,6 @@ def recompute_active_object(): FreeCAD.ActiveDocument.ActiveObject.recompute() ## -def crc_gen_t(data): - import binascii - import re - - #data=u'Würfel' - content=re.sub(r'[^\x00-\x7F]+','_', data) - #make_unicode(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - #hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:].encode('utf-8') - #print(data +u'_'+ hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) - return u'_'+ make_unicode_t(hex(binascii.crc_hqx(content.encode('utf-8'), 0x0000))[2:]) -## - -def make_unicode_t(input): - if (sys.version_info > (3, 0)): #py3 - if isinstance(input, str): - return input - else: - input = input.decode('utf-8') - return input - else: #py2 - if type(input) != unicode: - input = input.decode('utf-8') - return input - else: - return input - def mkColor(*color): if len(color)==1: if isinstance(color[0],basestring): @@ -294,7 +270,6 @@ def simple_cpy (obj,lbl): # from kicadStepUptools import removesubtree, cfg_read_all -from kicadStepUptools import make_unicode, make_string import fcad_parser from fcad_parser import KicadPCB,SexpList @@ -334,7 +309,7 @@ def addtracks(fname = None): last_pcb_path=os.path.dirname(fname) path, ftname = os.path.split(fname) ftname=os.path.splitext(ftname)[0] - ftname_sfx=crc_gen_t(make_unicode_t(ftname)) + ftname_sfx=crc_gen(make_unicode(ftname)) pg = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUp") pg.SetString("last_pcb_path", make_string(last_pcb_path)) # py3 .decode("utf-8") prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/kicadStepUpGui") diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..9b6c8f2 --- /dev/null +++ b/utils.py @@ -0,0 +1,18 @@ +#-*- coding: utf-8 -*- +import re +import binascii + +def make_string(input): + if isinstance(input, str): + return input + return input.encode('utf-8') + +def make_unicode(input): + if isinstance(input, str): + return input + return input.decode('utf-8') + +def crc_gen(data): + # Replace non-ascii characters with '_' + content=re.sub(r'[^\x00-\x7F]+','_', data) + return u'_' + make_unicode(hex(binascii.crc_hqx(content.encode('utf-8'), 0))[2:])