File size: 6,353 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************

 *   Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This program is free software; you can redistribute it and/or modify  *

 *   it under the terms of the GNU Library General Public License (LGPL)   *

 *   as published by the Free Software Foundation; either version 2 of     *

 *   the License, or (at your option) any later version.                   *

 *   for detail see the LICENCE text file.                                 *

 *                                                                         *

 *   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 Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with FreeCAD; if not, write to the Free Software        *

 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *

 *   USA                                                                   *

 *                                                                         *

 ***************************************************************************/

#include "../FCConfig.h"

#if HAVE_CONFIG_H
# include <config.h>
#endif  // HAVE_CONFIG_H

#include <cstdio>
#include <ostream>
#include <QString>

// FreeCAD Base header
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>

// FreeCAD doc header
#include <App/Application.h>


using App::Application;
using Base::Console;

const char sBanner[]
    = "(C) 2001-2025 FreeCAD contributors\n"
      "FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.\n\n";

int main(int argc, char** argv)

{
    // Make sure that we use '.' as decimal point
    setlocale(LC_ALL, "");
    setlocale(LC_NUMERIC, "C");

#if defined(__MINGW32__)
    const char* mingw_prefix = getenv("MINGW_PREFIX");
    const char* py_home = getenv("PYTHONHOME");
    if (!py_home && mingw_prefix) {
        _putenv_s("PYTHONHOME", mingw_prefix);
    }
#endif

    // Name and Version of the Application
    App::Application::Config()["ExeName"] = "FreeCAD";
    App::Application::Config()["ExeVendor"] = "FreeCAD";
    App::Application::Config()["AppDataSkipVendor"] = "true";

    // set the banner (for logging and console)
    App::Application::Config()["CopyrightInfo"] = sBanner;

    try {
        // Init phase ===========================================================
        // sets the default run mode for FC, starts with command prompt if not overridden in
        // InitConfig...
        App::Application::Config()["RunMode"] = "Exit";
        App::Application::Config()["LoggingConsole"] = "1";

        // Inits the Application
        App::Application::init(argc, argv);
    }
    catch (const Base::UnknownProgramOption& e) {
        std::cerr << e.what();
        exit(1);
    }
    catch (const Base::ProgramInformation& e) {
        if (std::strcmp(e.what(), App::Application::verboseVersionEmitMessage) == 0) {
            QString data;
            QTextStream str(&data);
            const std::map<std::string, std::string> config = App::Application::Config();

            App::Application::getVerboseCommonInfo(str, config);
            App::Application::getVerboseAddOnsInfo(str, config);

            std::cout << data.toStdString();
            exit(0);
        }
        std::cout << e.what();
        exit(0);
    }
    catch (const Base::Exception& e) {
        std::string appName = App::Application::Config()["ExeName"];
        std::cout << "While initializing " << appName << " the following exception occurred: '"
                  << e.what() << "'\n\n";
        std::cout << "Python is searching for its runtime files in the following directories:\n"
                  << Base::Interpreter().getPythonPath() << "\n\n";
        std::cout << "Python version information:\n" << Py_GetVersion() << "\n";
        const char* pythonhome = getenv("PYTHONHOME");
        if (pythonhome) {
            std::cout << "\nThe environment variable PYTHONHOME is set to '" << pythonhome << "'.";
            std::cout << "\nSetting this environment variable might cause Python to fail. "
                         "Please contact your administrator to unset it on your system.";
        }
        else {
            std::cout << "\nPlease contact the application's support team for more information.";
        }
        std::cout << std::endl;
        exit(100);
    }
    catch (...) {
        std::string appName = App::Application::Config()["ExeName"];
        std::cout << "Unknown runtime error occurred while initializing " << appName << ".\n\n";
        std::cout << "Please contact the application's support team for more information.";
        std::cout << std::endl;
        exit(101);
    }

    // Run phase ===========================================================
    try {
        Application::runApplication();
    }
    catch (const Base::SystemExitException& e) {
        exit(e.getExitCode());
    }
    catch (const Base::Exception& e) {
        e.reportException();
        exit(1);
    }
    catch (...) {
        Console().error("Application unexpectedly terminated\n");
        exit(1);
    }

    // Destruction phase ===========================================================
    Console().log("FreeCAD terminating...\n");

    try {
        // close open documents
        App::GetApplication().closeAllDocuments();
    }
    catch (...) {
    }

    // cleans up
    Application::destruct();

    Console().log("FreeCAD completely terminated\n");

    return 0;
}