BIM: Fix Python 3.12 run time error with importers

Fixes #16137

Similar to this fix:
https://github.com/FreeCAD/FreeCAD/pull/13337#discussion_r1564806896
This commit is contained in:
Roy-043
2024-08-29 19:18:35 +02:00
committed by Chris Hennes
parent c3b92154b2
commit ea96d853cd
8 changed files with 14 additions and 33 deletions

View File

@@ -32,6 +32,7 @@ import os
import time
import tempfile
import math
from builtins import open as pyopen
import FreeCAD
import Part
@@ -54,10 +55,6 @@ __title__ = "FreeCAD IFC export"
__author__ = ("Yorik van Havre", "Jonathan Wiedemann", "Bernd Hahnebach")
__url__ = "https://www.freecad.org"
# Save the Python open function because it will be redefined
if open.__module__ in ['__builtin__', 'io']:
pyopen = open
# Templates and other definitions ****
# Specific FreeCAD <-> IFC slang translations
translationtable = {

View File

@@ -55,10 +55,6 @@ __url__ = "https://www.freecad.org"
DEBUG = False # Set to True to see debug messages. Otherwise, totally silent
ZOOMOUT = True # Set to False to not zoom extents after import
# Save the Python open function because it will be redefined
if open.__module__ in ['__builtin__', 'io']:
pyopen = open
# Templates and other definitions ****
# which IFC type must create which FreeCAD type

View File

@@ -27,6 +27,7 @@
import FreeCAD, Arch, Draft, os, sys, time, Part, DraftVecUtils, uuid, math, re
from builtins import open as pyopen
from draftutils import params
from draftutils.translate import translate
@@ -54,9 +55,6 @@ supportedIfcTypes = ["IfcSite", "IfcBuilding", "IfcBuildingStorey", "IfcBeam", "
"IfcPile", "IfcFooting", "IfcReinforcingBar", "IfcTendon"]
# TODO : shading device not supported?
if open.__module__ in ['__builtin__','io']:
pyopen = open # because we'll redefine open below
def open(filename,skip=None):
"called when freecad opens a file"
docname = os.path.splitext(os.path.basename(filename))[0]

View File

@@ -22,6 +22,7 @@
"""FreeCAD JSON exporter"""
import json
from builtins import open as pyopen
import FreeCAD
import Draft
@@ -37,10 +38,6 @@ else:
def translate(ctxt, txt): return txt
if open.__module__ in ['__builtin__','io']:
pythonopen = open
def export(exportList, filename):
"exports the given objects to a .json file"
@@ -52,7 +49,7 @@ def export(exportList, filename):
}
# Write file
outfile = pythonopen(filename, "w")
outfile = pyopen(filename, "w")
json.dump(data, outfile, separators = (',', ':'))
outfile.close()

View File

@@ -22,7 +22,7 @@
import os
import codecs
import ntpath
# import numpy as np
from builtins import open as pyopen
import FreeCAD
import Arch
@@ -50,9 +50,6 @@ else:
# and supports exporting faces with more than 3 vertices
# and supports object colors / materials
if open.__module__ in ['__builtin__','io']:
pythonopen = open
def findVert(aVertex,aList):
"finds aVertex in aList, returns index"
p = Draft.precision()
@@ -260,7 +257,7 @@ def export(exportList,filename,colors=None):
outfile.close()
FreeCAD.Console.PrintMessage(translate("Arch","Successfully written") + " " + filename + "\n")
if materials:
outfile = pythonopen(filenamemtl,"w")
outfile = pyopen(filenamemtl,"w")
outfile.write("# FreeCAD v" + ver[0] + "." + ver[1] + " build" + ver[2] + " Arch module\n")
outfile.write("# https://www.freecad.org\n")
kinds = {"AmbientColor":"Ka ","DiffuseColor":"Kd ","SpecularColor":"Ks ","EmissiveColor":"Ke ","Transparency":"Tr ","Dissolve":"d "}
@@ -306,7 +303,7 @@ def insert(filename,docname):
doc = FreeCAD.newDocument(docname)
FreeCAD.ActiveDocument = doc
with pythonopen(filename,"r",encoding="utf8") as infile:
with pyopen(filename,"r",encoding="utf8") as infile:
verts = []
facets = []
activeobject = None
@@ -327,7 +324,7 @@ def insert(filename,docname):
if line[:7] == "mtllib ":
matlib = os.path.join(os.path.dirname(filename),line[7:])
if os.path.exists(matlib):
with pythonopen(matlib,"r") as matfile:
with pyopen(matlib,"r") as matfile:
mname = None
color = None
trans = None

View File

@@ -28,6 +28,7 @@ import os
import tempfile
import xml.sax
import zipfile
from builtins import open as pyopen
import FreeCAD
import Arch
@@ -43,9 +44,6 @@ import Part
DEBUG = True
if open.__module__ in ['__builtin__','io']:
pyopen = open # because we'll redefine open below
def open(filename):
"called when freecad wants to open a file"

View File

@@ -21,12 +21,11 @@
#***************************************************************************
import os
from builtins import open as pyopen
import FreeCAD
translate = FreeCAD.Qt.translate
if open.__module__ in ['__builtin__','io']:
pythonopen = open
def open(filename):
"""opens a SHP/SHX/DBF file in a new FreeCAD document"""
@@ -142,7 +141,7 @@ def checkShapeFileLibrary():
return False
b = u.read()
fp = os.path.join(FreeCAD.getUserMacroDir(True),"shapefile.py")
f = pythonopen(fp,"wb")
f = pyopen(fp,"wb")
f.write(b)
f.close()
try:

View File

@@ -44,6 +44,7 @@ import Part
import OfflineRenderingUtils
import json
import textwrap
from builtins import open as pyopen
if FreeCAD.GuiUp:
import FreeCADGui
@@ -52,8 +53,6 @@ else:
FreeCADGui = None
def translate(ctxt, txt): return txt
if open.__module__ in ['__builtin__','io']: pythonopen = open
## @package importWebGL
# \ingroup ARCH
# \brief FreeCAD WebGL Exporter
@@ -868,7 +867,7 @@ def export( exportList, filename, colors = None, camera = None ):
html = html.replace('$data', json.dumps(data, separators=(',', ':')) ) # Shape Data
outfile = pythonopen(filename, "w")
outfile = pyopen(filename, "w")
outfile.write( html )
outfile.close()
FreeCAD.Console.PrintMessage( translate("Arch", "Successfully written") + ' ' + filename + "\n" )