FreeCAD WebAssembly development snapshot
Some checks failed
FreeCAD master CI / Prepare (push) Has been cancelled
FreeCAD master CI / Pixi (push) Has been cancelled
FreeCAD master CI / Ubuntu (push) Has been cancelled
FreeCAD master CI / Windows (push) Has been cancelled
FreeCAD master CI / Lint (push) Has been cancelled
FreeCAD master CI / WrapUp (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Stale Issues / stale (push) Has been cancelled

This commit is contained in:
2026-08-20 07:00:53 +08:00
commit b5a086fda5
12718 changed files with 7841410 additions and 0 deletions

8
src/Ext/CMakeLists.txt Normal file
View File

@@ -0,0 +1,8 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# this directory is the entry point for namespace modules. Any package
# installed to a directory "freecad" which is in sys.path can be imported
# with "from freecad import package" and is checked for a init_gui.py
# module, which is imported at startup. (FreeCADGuiInit.py)
add_subdirectory(freecad)

View File

@@ -0,0 +1,50 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
EXECUTE_PROCESS(COMMAND ${Python3_EXECUTABLE} -c
"from sysconfig import get_path; print(get_path('purelib'))"
OUTPUT_VARIABLE python_libs OUTPUT_STRIP_TRAILING_WHITESPACE )
SET(PYTHON_MAIN_DIR ${python_libs})
set(NAMESPACE_DIR "${CMAKE_BINARY_DIR}/Ext/freecad")
set(NAMESPACE_INIT "${NAMESPACE_DIR}/__init__.py")
if (WIN32)
get_filename_component(FREECAD_LIBRARY_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}"
REALPATH BASE_DIR "${CMAKE_INSTALL_PREFIX}")
set( ${CMAKE_INSTALL_BINDIR})
else()
get_filename_component(FREECAD_LIBRARY_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}"
REALPATH BASE_DIR "${CMAKE_INSTALL_PREFIX}")
endif()
configure_file(__init__.py.template ${NAMESPACE_INIT})
set(EXT_FILES
freecad_doc.py
module_io.py
part.py
partdesign.py
project_utility.py
sketcher.py
UiTools.py
utils.py
utils_zip.py
)
foreach (it ${EXT_FILES})
configure_file(${it} ${NAMESPACE_DIR}/${it})
endforeach()
if (INSTALL_TO_SITEPACKAGES)
SET(SITE_PACKAGE_DIR ${PYTHON_MAIN_DIR}/freecad)
else ()
SET(SITE_PACKAGE_DIR Ext/freecad)
endif()
INSTALL(
FILES
${NAMESPACE_INIT}
${EXT_FILES}
DESTINATION
${SITE_PACKAGE_DIR}
)

View File

@@ -0,0 +1,22 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# (c) 2021 Werner Mayer LGPL
from PySide import QtUiTools
import FreeCADGui as Gui
class QUiLoader(QtUiTools.QUiLoader):
"""
This is an extension of Qt's QUiLoader to also create custom widgets
"""
def __init__(self, arg = None):
super(QUiLoader, self).__init__(arg)
self.ui = Gui.PySideUic
def createWidget(self, className, parent = None, name = ""):
widget = self.ui.createCustomWidget(className, parent, name)
if not widget:
widget = super(QUiLoader, self).createWidget(className, parent, name)
return widget

View File

@@ -0,0 +1,32 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
try:
import FreeCAD as app
except ModuleNotFoundError:
# sys.path modification has not happened yet
import sys as _sys
import os as _os
# 1. we check if an env-variable "PATH_TO_FREECAD_LIB" is already set
try:
_path_to_freecad_libdir = _os.environ["PATH_TO_FREECAD_LIBDIR"]
except KeyError:
# 2. we use the default freecad defined for this package
_path_to_freecad_libdir = "${FREECAD_LIBRARY_INSTALL_DIR}"
print("PATH_TO_FREECAD_LIBDIR not specified, using default \
FreeCAD version in {}".format("${FREECAD_LIBRARY_INSTALL_DIR}"))
_sys.path.append(_path_to_freecad_libdir) # this is the default version
import FreeCAD as app
# as this is a namespace-package we need to extend the path
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# TO NOT OVERWRITE THIS FILE, NO OTHER MODULE IS ALLOWED TO !
# PROVIDE A freecad/__init__.py FILE !
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
from pkgutil import extend_path as _extend_path
__path__ = _extend_path(__path__, __name__)

View File

@@ -0,0 +1,108 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# (c) 2024 Werner Mayer LGPL
__title__="FreeCAD Python documentation"
__author__ = "Werner Mayer"
__url__ = "https://www.freecad.org"
__doc__ = "Helper module to use pydoc"
import os, sys, pydoc, pkgutil
class FreeCADDoc(pydoc.HTMLDoc):
def index(self, dir, shadowed=None):
""" Generate an HTML index for a directory of modules."""
modpkgs = []
if shadowed is None: shadowed = {}
for importer, name, ispkg in pkgutil.iter_modules([dir]):
if name == 'Init':
continue
if name == 'InitGui':
continue
if name[-2:] == '_d':
continue
modpkgs.append((name, '', ispkg, name in shadowed))
shadowed[name] = 1
if len(modpkgs) == 0:
return None
modpkgs.sort()
contents = self.multicolumn(modpkgs, self.modpkglink)
try:
return self.bigsection(dir, '#ffffff', '#ee77aa', contents)
except Exception as e:
return self.bigsection(dir, 'pkg-content', contents)
def bltinlink(name):
return '<a href=\"%s.html\">%s</a>' % (name, name)
def getIndexOld():
pydoc.html = FreeCADDoc()
title = 'FreeCAD Python Modules Index'
heading = pydoc.html.heading('<big><big><strong>Python: Index of Modules</strong></big></big>','#ffffff', '#7799ee')
names = list(filter(lambda x: x != '__main__', sys.builtin_module_names))
contents = pydoc.html.multicolumn(names, bltinlink)
indices = ['<p>' + pydoc.html.bigsection('Built-in Modules', '#ffffff', '#ee77aa', contents)]
names = ['FreeCAD', 'FreeCADGui']
contents = pydoc.html.multicolumn(names, bltinlink)
indices.append('<p>' + pydoc.html.bigsection('Built-in FreeCAD Modules', '#ffffff', '#ee77aa', contents))
seen = {}
for dir in sys.path:
dir = os.path.realpath(dir)
ret = pydoc.html.index(dir, seen)
if ret != None:
indices.append(ret)
contents = heading + ' '.join(indices) + '''<p align=right>
<font color=\"#909090\" face=\"helvetica, arial\"><strong>
pydoc</strong> by Ka-Ping Yee &lt;ping@lfw.org&gt;</font>'''
htmldocument = pydoc.html.page(title, contents)
return htmldocument
def getIndexNew():
pydoc.html = FreeCADDoc()
title = 'FreeCAD Python Modules Index'
heading = pydoc.html.heading(
'<strong class="title">Index of Modules</strong>'
)
names = list(filter(lambda x: x != '__main__', sys.builtin_module_names))
contents = pydoc.html.multicolumn(names, bltinlink)
indices = ['<p>' + pydoc.html.bigsection('Built-in Modules', 'index', contents)]
names = ['FreeCAD', 'FreeCADGui']
contents = pydoc.html.multicolumn(names, bltinlink)
indices.append('<p>' + pydoc.html.bigsection('Built-in FreeCAD Modules', 'index', contents))
seen = {}
for dir in sys.path:
dir = os.path.realpath(dir)
ret = pydoc.html.index(dir, seen)
if ret != None:
indices.append(ret)
contents = heading + ' '.join(indices) + '''<p align=right>
<font color=\"#909090\" face=\"helvetica, arial\"><strong>
pydoc</strong> by Ka-Ping Yee &lt;ping@lfw.org&gt;</font>'''
htmldocument = pydoc.html.page(title, contents)
return htmldocument
def getIndex():
try:
return getIndexOld()
except Exception as e:
return getIndexNew()
def getPage(page):
object, name = pydoc.resolve(page)
page = pydoc.html.page(pydoc.describe(object), pydoc.html.document(object, name))
return page

View File

@@ -0,0 +1,20 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
from FreeCAD import Base
import importlib
def OpenInsertObject(importerName, objectPath, importMethod, docName = ""):
try:
importArgs = []
importKwargs = {}
importerModule = importlib.import_module(importerName)
if docName:
importArgs.append(docName)
if hasattr(importerModule, "importOptions"):
importKwargs["options"] = importerModule.importOptions(objectPath)
getattr(importerModule, importMethod)(objectPath, *importArgs, **importKwargs)
except Base.AbortIOException:
pass

3
src/Ext/freecad/part.py Normal file
View File

@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
from Part import *

View File

@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
from PartDesign import *

View File

@@ -0,0 +1,80 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# (c) 2023 Werner Mayer LGPL
__title__="Document handling module"
__author__ = "Werner Mayer"
__url__ = "https://www.freecad.org"
__doc__ = "Tools for extracting or creating project files"
import os
import xml.sax
import xml.sax.handler
import xml.sax.xmlreader
import zipfile
# SAX handler to parse the Document.xml
class DocumentHandler(xml.sax.handler.ContentHandler):
""" Parse content of Document.xml or GuiDocument.xml """
def __init__(self, dirname):
""" Init parser """
super().__init__()
self.files = []
self.dirname = dirname
def startElement(self, name, attrs):
item = attrs.get("file")
if item is not None:
self.files.append(os.path.join(self.dirname, str(item)))
def characters(self, content):
return
def endElement(self, name):
return
def extractDocument(filename, outpath):
""" Extract files from project archive """
zfile = zipfile.ZipFile(filename)
files = zfile.namelist()
for i in files:
data = zfile.read(i)
dirs = i.split("/")
if len(dirs) > 1:
dirs.pop()
curpath = outpath
for j in dirs:
curpath = curpath + "/" + j
os.mkdir(curpath)
output = open(outpath + "/"+i, 'wb')
output.write(data)
output.close()
def createDocument(filename, outpath):
""" Create project archive """
files = getFilesList(filename)
dirname = os.path.dirname(filename)
guixml = os.path.join(dirname, "GuiDocument.xml")
if os.path.exists(guixml):
files.extend(getFilesList(guixml))
compress = zipfile.ZipFile(outpath, 'w', zipfile.ZIP_DEFLATED)
for file in files:
if os.path.isfile(file):
path_in_archive = os.path.relpath(path=file, start=dirname)
compress.write(file, path_in_archive, zipfile.ZIP_DEFLATED)
compress.close()
def getFilesList(filename):
""" Determine list of files referenced in a Document.xml or GuiDocument.xml """
dirname = os.path.dirname(filename)
handler = DocumentHandler(dirname)
parser = xml.sax.make_parser()
parser.setContentHandler(handler)
parser.parse(filename)
files = []
files.append(filename)
files.extend(iter(handler.files))
return files

View File

@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
from Sketcher import *

63
src/Ext/freecad/utils.py Normal file
View File

@@ -0,0 +1,63 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2022-2023 FreeCAD Project Association *
# * Copyright (c) 2018 Gaël Écorchard <galou_breizh@yahoo.fr> *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
import os
import platform
import shutil
import FreeCAD
def get_python_exe() -> str:
"""Find Python. In preference order
A) The value of the BaseApp/Preferences/PythonConsole/PathToPythonExecutable user preference
B) The executable located in the same bin directory as FreeCAD and called "python3"
C) The executable located in the same bin directory as FreeCAD and called "python"
D) The result of a shutil search for your system's "python3" executable
E) The result of a shutil search for your system's "python" executable"""
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/PythonConsole")
python_exe = prefs.GetString("PathToPythonExecutable", "")
fc_dir = FreeCAD.getHomePath()
if not python_exe or not os.path.exists(python_exe):
python_exe = os.path.join(fc_dir, "bin", "python3")
if "Windows" in platform.system():
python_exe += ".exe"
if not python_exe or not os.path.exists(python_exe):
python_exe = os.path.join(fc_dir, "bin", "python")
if "Windows" in platform.system():
python_exe += ".exe"
if not python_exe or not os.path.exists(python_exe):
python_exe = shutil.which("python3")
if not python_exe or not os.path.exists(python_exe):
python_exe = shutil.which("python")
if not python_exe or not os.path.exists(python_exe):
return ""
python_exe = python_exe.replace("/", os.path.sep)
return python_exe

View File

@@ -0,0 +1,20 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# (c) 2024 Werner Mayer LGPL
__author__ = "Werner Mayer"
__url__ = "https://www.freecad.org"
__doc__ = "Helper module to convert zip files"
import zipfile
def rewrite(source: str, target: str):
source_zip = zipfile.ZipFile(source, "r")
target_zip = zipfile.ZipFile(target, "w")
for name in source_zip.namelist():
target_zip.writestr(name, source_zip.open(name).read())
source_zip.close()
target_zip.close()