MainPy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Juergen Riegel (juergen.riegel@web.de) 2008                       *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Library General Public License (LGPL)   *
00008  *   as published by the Free Software Foundation; either version 2 of     *
00009  *   the License, or (at your option) any later version.                   *
00010  *   for detail see the LICENCE text file.                                 *
00011  *                                                                         *
00012  *   FreeCAD is distributed in the hope that it will be useful,            *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with FreeCAD; if not, write to the Free Software        *
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020  *   USA                                                                   *
00021  *                                                                         *
00022  *   Juergen Riegel 2002                                                   *
00023  ***************************************************************************/
00024 #include <FCConfig.h>
00025 
00026 #ifdef _PreComp_
00027 # undef _PreComp_
00028 #endif
00029 
00030 #ifdef FC_OS_LINUX
00031 #       include <unistd.h>
00032 #endif
00033 
00034 #ifdef FC_OS_MACOSX
00035 # include <mach-o/dyld.h>
00036 # include <string>
00037 #endif
00038 
00039 #if HAVE_CONFIG_H
00040 #       include <config.h>
00041 #endif // HAVE_CONFIG_H
00042 
00043 #include <stdio.h>
00044 #include <sstream>
00045 
00046 
00047 // FreeCAD Base header
00048 #include <Base/Exception.h>
00049 #include <App/Application.h>
00050 
00051 
00052 #if defined(FC_OS_WIN32)
00053 # include <windows.h>
00054 
00057 BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
00058 {
00059     switch (ul_reason_for_call) {
00060     case DLL_PROCESS_ATTACH: {
00061         // This name is preliminary, we pass it to Application::init() in initFreeCAD()
00062         // which does the rest.
00063         char  szFileName [MAX_PATH];
00064         GetModuleFileName((HMODULE)hModule, szFileName, MAX_PATH-1);
00065         App::Application::Config()["AppHomePath"] = szFileName;
00066     }
00067     break;
00068     default:
00069         break;
00070     }
00071 
00072     return TRUE;
00073 }
00074 #elif defined(FC_OS_LINUX)
00075 # ifndef GNU_SOURCE
00076 #   define GNU_SOURCE
00077 # endif
00078 # include <dlfcn.h>
00079 #elif defined(FC_OS_CYGWIN)
00080 # include <windows.h>
00081 #endif
00082 
00083 #ifdef FC_OS_WIN32
00084 #       define MainExport __declspec(dllexport)
00085 #else
00086 #       define MainExport
00087 #endif
00088 
00089 extern "C"
00090 {
00091     void MainExport initFreeCAD() {
00092 
00093         // Init phase ===========================================================
00094         App::Application::Config()["ExeName"] = "FreeCAD";
00095         App::Application::Config()["ExeVendor"] = "FreeCAD";
00096         App::Application::Config()["AppDataSkipVendor"] = "true";
00097 
00098 
00099         int    argc=1;
00100         char** argv;
00101         argv = (char**)malloc(sizeof(char*)* (argc+1));
00102 
00103 #if defined(FC_OS_WIN32)
00104         argv[0] = (char*)malloc(MAX_PATH);
00105         strncpy(argv[0],App::Application::Config()["AppHomePath"].c_str(),MAX_PATH);
00106         argv[0][MAX_PATH-1] = '\0'; // ensure null termination
00107 #elif defined(FC_OS_CYGWIN)
00108         HMODULE hModule = GetModuleHandle("FreeCAD.dll");
00109         char szFileName [MAX_PATH];
00110         GetModuleFileName(hModule, szFileName, MAX_PATH-1);
00111         argv[0] = (char*)malloc(MAX_PATH);
00112         strncpy(argv[0],szFileName,MAX_PATH);
00113         argv[0][MAX_PATH-1] = '\0'; // ensure null termination
00114 #elif defined(FC_OS_LINUX)
00115         // get whole path of the library
00116         Dl_info info;
00117         int ret = dladdr((void*)initFreeCAD, &info);
00118         if ((ret == 0) || (!info.dli_fname)) {
00119             PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
00120             return;
00121         }
00122 
00123         argv[0] = (char*)malloc(PATH_MAX);
00124         strncpy(argv[0], info.dli_fname,PATH_MAX);
00125         argv[0][PATH_MAX-1] = '\0'; // ensure null termination
00126 #elif defined(FC_OS_MACOSX)
00127         uint32_t sz = 0;
00128         char *buf;
00129 
00130         _NSGetExecutablePath(NULL, &sz);
00131         buf = (char*) malloc(++sz);
00132         int err=_NSGetExecutablePath(buf, &sz);
00133         if (err != 0) {
00134             PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
00135             return;
00136         }
00137 
00138         argv[0] = (char*)malloc(PATH_MAX);
00139         strncpy(argv[0], buf, PATH_MAX);
00140         argv[0][PATH_MAX-1] = '\0'; // ensure null termination
00141         free(buf);
00142 #else
00143 # error "Implement: Retrieve the path of the module for your platform."
00144 #endif
00145         argv[argc] = 0;
00146 
00147         try {
00148             // Inits the Application
00149             App::Application::init(argc,argv);
00150         }
00151         catch (const Base::Exception& e) {
00152             std::string appName = App::Application::Config()["ExeName"];
00153             std::stringstream msg;
00154             msg << "While initializing " << appName << " the  following exception occurred: '"
00155                 << e.what() << "'\n\n";
00156             msg << "\nPlease contact the application's support team for more information.\n\n";
00157             printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str());
00158         }
00159 
00160         free(argv[0]);
00161         free(argv);
00162 
00163         return;
00164     } //InitFreeCAD....
00165 } // extern "C"
00166 

Generated on Wed Nov 23 19:00:21 2011 for FreeCAD by  doxygen 1.6.1