Unicode fixes for Python3

This commit is contained in:
cclauss
2019-01-04 12:18:25 +01:00
committed by wmayer
parent 0453ee71e6
commit e3545c8c06
12 changed files with 108 additions and 101 deletions

View File

@@ -21,7 +21,9 @@
#* *
#***************************************************************************
import FreeCAD,Draft,math,DraftVecUtils,ArchCommands,sys
import six
import FreeCAD,Draft,math,DraftVecUtils,ArchCommands
from FreeCAD import Vector
if FreeCAD.GuiUp:
import FreeCADGui
@@ -544,7 +546,7 @@ class _ViewProviderAxis:
tx = coin.SoAsciiText()
tx.justification = coin.SoText2.LEFT
t = vobj.Object.Labels[i]
if sys.version_info.major < 3 and isinstance(t,unicode):
if six.PY2 and isinstance(t,six.text_type):
t = t.encode("utf8")
tx.string.setValue(t)
if hasattr(vobj,"FontSize"):
@@ -577,7 +579,7 @@ class _ViewProviderAxis:
('C',100),('XC',90),('L',50),('XL',40),
('X',10),('IX',9),('V',5),('IV',4),('I',1))
if hasattr(vobj.Object,"CustomNumber") and vobj.Object.CustomNumber:
if sys.version_info.major < 3:
if six.PY2:
return vobj.Object.CustomNumber.encode("utf8")
else:
return vobj.Object.CustomNumber

View File

@@ -27,6 +27,8 @@ __title__ = "FreeCAD IFC importer - Enhanced ifcopenshell-only version"
__author__ = "Yorik van Havre","Jonathan Wiedemann"
__url__ = "http://www.freecadweb.org"
import six
import os,time,tempfile,uuid,FreeCAD,Part,Draft,Arch,math,DraftVecUtils,sys
from DraftGeomUtils import vec
@@ -121,12 +123,9 @@ def decode(filename,utf=False):
"turns unicodes into strings"
if (sys.version_info.major < 3) and isinstance(filename,unicode):
if six.PY2 and isinstance(filename,six.text_type):
# workaround since ifcopenshell currently can't handle unicode filenames
if utf:
encoding = "utf8"
else:
encoding = sys.getfilesystemencoding()
encoding = "utf8" if utf else sys.getfilesystemencoding()
filename = filename.encode(encoding)
return filename
@@ -321,7 +320,7 @@ def explore(filename=None):
t = "Entity #" + str(argvalue.id()) + ": " + str(argvalue.is_a())
elif isinstance(argvalue,list):
t = ""
elif (sys.version_info.major < 3) and (isinstance(argvalue,str) or isinstance(argvalue,unicode)):
elif six.PY2 and isinstance(argvalue,six.string_types):
t = argvalue.encode("latin1")
else:
t = str(argvalue)
@@ -573,7 +572,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
name = str(ptype[3:])
if product.Name:
name = product.Name
if sys.version_info.major < 3:
if six.PY2:
name = name.encode("utf8")
if PREFIX_NUMBERS: name = "ID" + str(pid) + " " + name
obj = None
@@ -942,7 +941,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
if l.is_a("IfcPropertySingleValue"):
if DEBUG:
print("property name",l.Name,type(l.Name))
if sys.version_info.major < 3:
if six.PY2:
catname = catname.encode("utf8")
lname = lname.encode("utf8")
ifc_spreadsheet.set(str('A'+str(n)), catname)
@@ -954,7 +953,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
#print("l.NominalValue.Unit",l.NominalValue.Unit,type(l.NominalValue.Unit))
ifc_spreadsheet.set(str('C'+str(n)), l.NominalValue.is_a())
if l.NominalValue.is_a() in ['IfcLabel','IfcText','IfcIdentifier','IfcDescriptiveMeasure']:
if sys.version_info.major < 3:
if six.PY2:
ifc_spreadsheet.set(str('D'+str(n)), "'" + str(l.NominalValue.wrappedValue.encode("utf8")))
else:
ifc_spreadsheet.set(str('D'+str(n)), "'" + str(l.NominalValue.wrappedValue))
@@ -972,18 +971,18 @@ def insert(filename,docname,skip=[],only=[],root=None):
d = obj.IfcProperties
for pset in properties[pid].keys():
psetname = ifcfile[pset].Name
if sys.version_info.major < 3:
if six.PY2:
psetname = psetname.encode("utf8")
for prop in properties[pid][pset]:
e = ifcfile[prop]
pname = e.Name
if sys.version_info.major < 3:
if six.PY2:
pname = pname.encode("utf8")
if e.is_a("IfcPropertySingleValue"):
ptype = e.NominalValue.is_a()
if ptype in ['IfcLabel','IfcText','IfcIdentifier','IfcDescriptiveMeasure']:
pvalue = e.NominalValue.wrappedValue
if sys.version_info.major < 3:
if six.py2:
pvalue = pvalue.encode("utf8")
else:
pvalue = str(e.NominalValue.wrappedValue)
@@ -1125,7 +1124,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
else:
if DEBUG: print("no group name specified for entity: #", ifcfile[host].id(), ", entity type is used!")
grp_name = ifcfile[host].is_a() + "_" + str(ifcfile[host].id())
if sys.version_info.major < 3:
if six.PY2:
grp_name = grp_name.encode("utf8")
grp = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup",grp_name)
grp.Label = grp_name
@@ -1266,7 +1265,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
name = "Grid"
if annotation.Name:
name = annotation.Name
if sys.version_info.major < 3:
if six.PY2:
name = name.encode("utf8")
if PREFIX_NUMBERS:
name = "ID" + str(aid) + " " + name
@@ -1276,7 +1275,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
name = "Annotation"
if annotation.Name:
name = annotation.Name
if sys.version_info.major < 3:
if six.PY2:
name = name.encode("utf8")
if "annotation" not in name.lower():
name = "Annotation " + name
@@ -1323,7 +1322,7 @@ def insert(filename,docname,skip=[],only=[],root=None):
name = "Material"
if material.Name:
name = material.Name
if sys.version_info.major < 3:
if six.PY2:
name = name.encode("utf8")
if MERGE_MATERIALS and (name in fcmats.keys()):
mat = fcmats[name]
@@ -1589,7 +1588,7 @@ def export(exportList,filename):
template = template.replace("IfcOpenShell","IfcOpenShell "+ifcopenshell.version)
templatefilehandle,templatefile = tempfile.mkstemp(suffix=".ifc")
of = pyopen(templatefile,"w")
if sys.version_info.major < 3:
if six.PY2:
template = template.encode("utf8")
of.write(template)
of.close()
@@ -1656,10 +1655,10 @@ def export(exportList,filename):
# getting generic data
name = obj.Label
if sys.version_info.major < 3:
if six.PY2:
name = name.encode("utf8")
description = obj.Description if hasattr(obj,"Description") else ""
if sys.version_info.major < 3:
if six.PY2:
description = description.encode("utf8")
# getting uid
@@ -1841,7 +1840,7 @@ def export(exportList,filename):
r2,p2,c2 = getRepresentation(ifcfile,context,o)
if DEBUG: print(" adding ",c2," : ",o.Label)
l = o.Label
if sys.version_info.major < 3:
if six.PY2:
l = l.encode("utf8")
prod2 = ifcfile.createIfcBuildingElementProxy(ifcopenshell.guid.compress(uuid.uuid1().hex),history,l,None,None,p2,r2,None,"ELEMENT")
subproducts[o.Name] = prod2
@@ -1861,7 +1860,7 @@ def export(exportList,filename):
r2,p2,c2 = getRepresentation(ifcfile,context,o,subtraction=True)
if DEBUG: print(" subtracting ",c2," : ",o.Label)
l = o.Label
if sys.version_info.major < 3:
if six.PY2:
l = l.encode("utf8")
prod2 = ifcfile.createIfcOpeningElement(ifcopenshell.guid.compress(uuid.uuid1().hex),history,l,None,None,p2,r2,None)
subproducts[o.Name] = prod2
@@ -1900,7 +1899,7 @@ def export(exportList,filename):
#if DEBUG: print(" property ",key," : ",pvalue.encode("utf8"), " (", str(ptype), ") in ",pset)
if ptype in ["IfcLabel","IfcText","IfcIdentifier",'IfcDescriptiveMeasure']:
if sys.version_info.major < 3:
if six.PY2:
pvalue = pvalue.encode("utf8")
elif ptype == "IfcBoolean":
if pvalue == ".T.":
@@ -1916,7 +1915,7 @@ def export(exportList,filename):
try:
pvalue = FreeCAD.Units.Quantity(pvalue).Value
except:
if sys.version_info.major < 3:
if six.PY2:
pvalue = pvalue.encode("utf8")
if DEBUG:print(" warning: unable to export property as numeric value:",key,pvalue)
p = ifcbin.createIfcPropertySingleValue(str(key),str(ptype),pvalue)
@@ -1943,11 +1942,11 @@ def export(exportList,filename):
val = sheet.get('D'+str(n))
else:
val = ''
if sys.version_info.major < 3 and isinstance(key, unicode):
if six.py2 and isinstance(key, six.text_type):
key = key.encode("utf8")
else:
key = str(key)
if sys.version_info.major < 3 and isinstance(tp, unicode):
if six.PY2 and isinstance(tp, six.text_type):
tp = tp.encode("utf8")
else:
tp = str(tp)
@@ -2010,7 +2009,7 @@ def export(exportList,filename):
val = val.strip('"')
#if DEBUG: print(" property ",key," : ",val.encode("utf8"), " (", str(tp), ")")
if tp in ["IfcLabel","IfcText","IfcIdentifier",'IfcDescriptiveMeasure']:
if sys.version_info.major < 3:
if six.PY2:
val = val.encode("utf8")
elif tp == "IfcBoolean":
if val == ".T.":
@@ -2244,7 +2243,7 @@ def export(exportList,filename):
relobjs.append(subproducts[o.Name])
if relobjs:
l = m.Label
if sys.version_info.major < 3:
if six.PY2:
l = l.encode("utf8")
mat = ifcfile.createIfcMaterial(l)
materials[m.Label] = mat
@@ -2299,7 +2298,7 @@ def export(exportList,filename):
pos = ifcbin.createIfcCartesianPoint((l.x,l.y,l.z))
tpl = ifcbin.createIfcAxis2Placement3D(pos,None,None)
s = ";".join(anno.LabelText)
if sys.version_info.major < 3:
if six.PY2:
s = s.encode("utf8")
txt = ifcfile.createIfcTextLiteral(s,tpl,"LEFT")
reps = [txt]
@@ -2308,7 +2307,7 @@ def export(exportList,filename):
pos = ifcbin.createIfcCartesianPoint((l.x,l.y,l.z))
tpl = ifcbin.createIfcAxis2Placement3D(pos,None,None)
s = ";".join(anno.Text)
if sys.version_info.major < 3:
if six.PY2:
s = s.encode("utf8")
txt = ifcfile.createIfcTextLiteral(s,tpl,"LEFT")
reps = [txt]
@@ -2334,7 +2333,7 @@ def export(exportList,filename):
shp = ifcfile.createIfcShapeRepresentation(context,'Annotation','Annotation2D',reps)
rep = ifcfile.createIfcProductDefinitionShape(None,None,[shp])
l = anno.Label
if sys.version_info.major < 3:
if six.PY2:
l = l.encode("utf8")
ann = ifcfile.createIfcAnnotation(ifcopenshell.guid.compress(uuid.uuid1().hex),history,l,'',None,gpl,rep)
annos[anno.Name] = ann
@@ -2370,7 +2369,7 @@ def export(exportList,filename):
swallowed.append(annos[o])
if children:
name = FreeCAD.ActiveDocument.getObject(g[0]).Label
if sys.version_info.major < 3:
if six.PY2:
name = name.encode("utf8")
grp = ifcfile.createIfcGroup(ifcopenshell.guid.compress(uuid.uuid1().hex),history,name,'',None)
products[g[0]] = grp
@@ -2426,7 +2425,7 @@ def buildAddress(obj,ifcfile):
t = obj.City or None
r = obj.Region or None
c = obj.Country or None
if sys.version_info.major < 3:
if six.PY2:
if a:
a = a.encode("utf8")
if p:
@@ -2969,7 +2968,7 @@ def getRepresentation(ifcfile,context,obj,forcebrep=False,subtraction=False,tess
if hasattr(obj,"Material"):
if obj.Material:
m = obj.Material.Label
if sys.version_info.major < 3:
if six.PY2:
m = m.encode("utf8")
psa = ifcbin.createIfcPresentationStyleAssignment(m,rgb[0],rgb[1],rgb[2])
surfstyles[key] = psa