The FreeCAD source code/zh-cn

FreeCAD的源代码通过Git管理，是公开的，是开放的，是可获得的，声明于LGPL许可协议之下. 任何人都可以复制、下载、阅读、分析、分发和改动. 如果你改了代码，还想看到它被吸收进官方代码里，我们要给你提个醒，你的改动需要被FreeCAD开发者认可. 所以，聪明的做法是，先到论坛上去发帖，跟大家讨论一下，说说你的初衷和想法. 免得到头来，出乎你的意料，你的改动被拒绝.

如果你有兴趣，深入探索FreeCAD源代码，下面就是一些提醒，一点信息，帮助你走上正轨.


 * FreeCAD代码主要采用C++编程，但是重度依赖Python. FreeCAD的功能里，非常大的部分都提供相应的Python包装. FreeCAD的核心哲学之一，就是对任何C++开发的新属性，都要给出Python接口. 为此，FreeCAD通篇都重度使用CPython（Python自己提供的C界面工具），特别是PyCXX. FreeCAD代码里也提供了很多模版和定制工具，便于你包装相应的Python封装. FreeCAD一些更高层的部分，完全是用Python写的代码.


 * FreeCAD的源代码完全跨平台，我们一直很小心，保证让大家在最广泛的平台上和配置上都能使用此应用，避免让用户感觉不爽. 因此，如果一个所需组件的新版本不能跨平台，不能广泛而容易地在所有平台上使用，那会是我们的错，我们将尽可能地避免这样的情况放生. 同时，保证向后兼容性（用旧版本的FreeCAD也能打开新版本所创建的文件）也有重要的优先级.


 * Almost all the functionality of FreeCAD is separated in two different parts, named App and Gui. This separation is reflected everywhere in the files structure of the source code. App contains all the functionality that needs to run in pure console mode (no GUI). As FreeCAD can be compiled and run without its Graphical User Interface, the code in App is independent of any GUI-related library. Gui contains all the code needed to run in GUI mode, and is built around the App functionality.


 * Most of FreeCAD's functionality is implemented in Modules. FreeCAD without its module is a simple container window that can just open and save a file. All the geometry tools and workbenches are implemented in Modules. Modules can be written in C++, in Python, or combining the best of the two worlds, can be hybrid C++/Python modules, where solid, core functionality is programmed in C++ while end-user tools are made in Python and are therefore much easier to extend and adapt by FreeCAD users. Each module usually defines and creates a Workbench in the FreeCAD interface, when used in GUI mode, usually with the same name, but it is not mandatory for modules to contain a workbench.


 * FreeCAD modules often depend on other modules. Most modules that use solid geometry depend on the Part module, which is the most fundamental module of FreeCAD, and implements most of the interfacing with OpenCasCade. Although other module can use OpenCasCade functionality directly, they often rely on higher-level functions provided by Part.


 * Modules are always initialized from Python. Even if they are written fully in C++, they always contain a minimal Python/CPython structure.


 * FreeCAD is an avid user of other open-source libraries. Besides Python and Qt, used by the core and almost all of the modules, the two heavyweight libraries used throughout most modules are OpenCasCade Technology and Coin3D. OpenCasCade is used to create and manage all the solid geometry of FreeCAD, while coin3D is used to manage the 3D view. OpenCascade is used mainly in the App world, and coin3D exclusively in the Gui world. A basic understanding of OpenCasCade is fundamental to do any geometry-related work with FreeCAD. More specific modules make use of more specific libraries, and since there are usually no restrictions on that point apart from these libraries to be easily available on all platforms, the list of dependencies of a full FreeCAD installation with all its modules can be quite large.


 * FreeCAD's document objects, which are all the objects contained in a FreeCAD document, are what appear in the Tree View in the GUI and in FreeCAD.ActiveDocument.Objects in Python. They may or may not have any geometrical data, and may or may not show anything in the 3D view. They are always separated in App and Gui parts. The Gui part is not loaded when running in console mode. Standard geometrical objects, such as those found in Part or PartDesign, have their OpenCasCade-based geometry defined in their App counterpart, while the Gui counterpart (also usually called "View Provider") is responsible for creating a coin3D representation of that geometry, which will be inserted into the main coin3D scene graph of the 3D view.


 * The basic directory structure of the source code is organized like this:
 * App: contains the FreeCAD console-mode application, defines basic structures and base classes for document objects, that are used by modules to build their own.
 * Base: contains core functionality commonly used everywhere in FreeCAD: 3D vectors, units, matrices, placements, etc.
 * Gui: contains the FreeCAD GUI-mode application, defines the 3D view, contains many tools and functions to be used by workbenches to interact with the interface and with the 3D view, defines base classes for view providers.
 * Doc: contains mainly an all-in-one Qt help file generated from this wiki.
 * Mod: contains all the modules, themselves further separated into App and Gui (except for python modules, which don't always follow that rule so clearly).

Related

 * Source Code Management
 * The Power users hub contains a lot of documentation about modules, OpenCasCade and Coin3D, mainly for the Python programmer. However, as the functionality is similar, these pages will be of interest to the C++ programmer as well.