/
Application.cpp
2132 lines (1855 loc) · 76.9 KB
/
Application.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* (c) Juergen Riegel (juergen.riegel@web.de) 2002 *
* *
* 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 *
* *
* Juergen Riegel 2002 *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <iostream>
# include <sstream>
# include <exception>
# if defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
# include <unistd.h>
# include <pwd.h>
# include <sys/types.h>
# elif defined(__MINGW32__)
# define WINVER 0x502 // needed for SetDllDirectory
# include <Windows.h>
# endif
# include <ctime>
# include <csignal>
# include <boost/program_options.hpp>
#endif
#ifdef FC_OS_WIN32
# include <Shlobj.h>
#endif
#include "Application.h"
#include "Document.h"
// FreeCAD Base header
#include <Base/Interpreter.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Base/Console.h>
#include <Base/Factory.h>
#include <Base/FileInfo.h>
#include <Base/Type.h>
#include <Base/BaseClass.h>
#include <Base/Persistence.h>
#include <Base/Reader.h>
#include <Base/MatrixPy.h>
#include <Base/VectorPy.h>
#include <Base/AxisPy.h>
#include <Base/BoundBoxPy.h>
#include <Base/PlacementPy.h>
#include <Base/RotationPy.h>
#include <Base/Sequencer.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <Base/QuantityPy.h>
#include <Base/UnitPy.h>
#include "GeoFeature.h"
#include "FeatureTest.h"
#include "FeaturePython.h"
#include "ComplexGeoData.h"
#include "Property.h"
#include "PropertyContainer.h"
#include "PropertyUnits.h"
#include "PropertyFile.h"
#include "PropertyLinks.h"
#include "PropertyPythonObject.h"
#include "PropertyExpressionEngine.h"
#include "Document.h"
#include "DocumentObjectGroup.h"
#include "DocumentObjectFileIncluded.h"
#include "InventorObject.h"
#include "VRMLObject.h"
#include "Annotation.h"
#include "MeasureDistance.h"
#include "Placement.h"
#include "Plane.h"
#include "MaterialObject.h"
#include "Expression.h"
// If you stumble here, run the target "BuildExtractRevision" on Windows systems
// or the Python script "SubWCRev.py" on Linux based systems which builds
// src/Build/Version.h. Or create your own from src/Build/Version.h.in!
#include <Build/Version.h>
#include "Branding.h"
#include <boost/tokenizer.hpp>
#include <boost/token_functions.hpp>
#include <boost/signals.hpp>
#include <boost/bind.hpp>
#include <boost/version.hpp>
#include <QDir>
#include <QFileInfo>
using namespace App;
using namespace std;
using namespace boost;
using namespace boost::program_options;
// scriptings (scripts are build in but can be overridden by command line option)
#include "InitScript.h"
#include "TestScript.h"
#ifdef _MSC_VER // New handler for Microsoft Visual C++ compiler
# include <new.h>
# include <eh.h> // VC exception handling
#else // Ansi C/C++ new handler
# include <new>
#endif
//using Base::GetConsole;
using namespace Base;
using namespace App;
using namespace std;
//==========================================================================
// Application
//==========================================================================
ParameterManager *App::Application::_pcSysParamMngr;
ParameterManager *App::Application::_pcUserParamMngr;
Base::ConsoleObserverStd *Application::_pConsoleObserverStd =0;
Base::ConsoleObserverFile *Application::_pConsoleObserverFile =0;
AppExport std::map<std::string,std::string> Application::mConfig;
BaseExport extern PyObject* Base::BaseExceptionFreeCADError;
//**************************************************************************
// Construction and destruction
PyDoc_STRVAR(FreeCAD_doc,
"The functions in the FreeCAD module allow working with documents.\n"
"The FreeCAD instance provides a list of references of documents which\n"
"can be addressed by a string. Hence the document name must be unique.\n"
"\n"
"The document has the read-only attribute FileName which points to the\n"
"file the document should be stored to.\n"
);
PyDoc_STRVAR(Console_doc,
"FreeCAD Console\n"
);
Application::Application(ParameterManager * /*pcSysParamMngr*/,
ParameterManager * /*pcUserParamMngr*/,
std::map<std::string,std::string> &mConfig)
://_pcSysParamMngr(pcSysParamMngr),
//_pcUserParamMngr(pcUserParamMngr),
_mConfig(mConfig),
_pActiveDoc(0)
{
//_hApp = new ApplicationOCC;
mpcPramManager["System parameter"] = _pcSysParamMngr;
mpcPramManager["User parameter"] = _pcUserParamMngr;
// setting up Python binding
Base::PyGILStateLocker lock;
PyObject* pAppModule = Py_InitModule3("FreeCAD", Application::Methods, FreeCAD_doc);
Py::Module(pAppModule).setAttr(std::string("ActiveDocument"),Py::None());
PyObject* pConsoleModule = Py_InitModule3("__FreeCADConsole__", ConsoleSingleton::Methods, Console_doc);
// introducing additional classes
// NOTE: To finish the initialization of our own type objects we must
// call PyType_Ready, otherwise we run into a segmentation fault, later on.
// This function is responsible for adding inherited slots from a type's base class.
if (PyType_Ready(&Base::VectorPy::Type) < 0) return;
union PyType_Object pyVecType = {&Base::VectorPy::Type};
PyModule_AddObject(pAppModule, "Vector", pyVecType.o);
if (PyType_Ready(&Base::MatrixPy::Type) < 0) return;
union PyType_Object pyMtxType = {&Base::MatrixPy::Type};
PyModule_AddObject(pAppModule, "Matrix", pyMtxType.o);
if (PyType_Ready(&Base::BoundBoxPy::Type) < 0) return;
union PyType_Object pyBoundBoxType = {&Base::BoundBoxPy::Type};
PyModule_AddObject(pAppModule, "BoundBox", pyBoundBoxType.o);
if (PyType_Ready(&Base::PlacementPy::Type) < 0) return;
union PyType_Object pyPlacementPyType = {&Base::PlacementPy::Type};
PyModule_AddObject(pAppModule, "Placement", pyPlacementPyType.o);
if (PyType_Ready(&Base::RotationPy::Type) < 0) return;
union PyType_Object pyRotationPyType = {&Base::RotationPy::Type};
PyModule_AddObject(pAppModule, "Rotation", pyRotationPyType.o);
if (PyType_Ready(&Base::AxisPy::Type) < 0) return;
union PyType_Object pyAxisPyType = {&Base::AxisPy::Type};
PyModule_AddObject(pAppModule, "Axis", pyAxisPyType.o);
// Note: Create an own module 'Base' which should provide the python
// binding classes from the base module. At a later stage we should
// remove these types from the FreeCAD module.
PyObject* pBaseModule = Py_InitModule3("__FreeCADBase__", NULL,
"The Base module contains the classes for the geometric basics\n"
"like vector, matrix, bounding box, placement, rotation, axis, ...");
Base::BaseExceptionFreeCADError = PyErr_NewException(
"Base.FreeCADError", PyExc_RuntimeError, NULL);
Py_INCREF(Base::BaseExceptionFreeCADError);
PyModule_AddObject(pBaseModule, "FreeCADError",
Base::BaseExceptionFreeCADError);
Base::Interpreter().addType(&Base::VectorPy ::Type,pBaseModule,"Vector");
Base::Interpreter().addType(&Base::MatrixPy ::Type,pBaseModule,"Matrix");
Base::Interpreter().addType(&Base::BoundBoxPy ::Type,pBaseModule,"BoundBox");
Base::Interpreter().addType(&Base::PlacementPy ::Type,pBaseModule,"Placement");
Base::Interpreter().addType(&Base::RotationPy ::Type,pBaseModule,"Rotation");
Base::Interpreter().addType(&Base::AxisPy ::Type,pBaseModule,"Axis");
//insert Base and Console
Py_INCREF(pBaseModule);
PyModule_AddObject(pAppModule, "Base", pBaseModule);
Py_INCREF(pConsoleModule);
PyModule_AddObject(pAppModule, "Console", pConsoleModule);
//insert Units module
PyObject* pUnitsModule = Py_InitModule3("Units", Base::UnitsApi::Methods,
"The Unit API");
Base::Interpreter().addType(&Base::QuantityPy ::Type,pUnitsModule,"Quantity");
// make sure to set the 'nb_true_divide' slot
Base::QuantityPy::Type.tp_as_number->nb_true_divide = Base::QuantityPy::Type.tp_as_number->nb_divide;
Base::Interpreter().addType(&Base::UnitPy ::Type,pUnitsModule,"Unit");
Py_INCREF(pUnitsModule);
PyModule_AddObject(pAppModule, "Units", pUnitsModule);
Base::ProgressIndicatorPy::init_type();
Base::Interpreter().addType(Base::ProgressIndicatorPy::type_object(),
pBaseModule,"ProgressIndicator");
}
Application::~Application()
{
}
//**************************************************************************
// Interface
/// get called by the document when the name is changing
void Application::renameDocument(const char *OldName, const char *NewName)
{
std::map<std::string,Document*>::iterator pos;
pos = DocMap.find(OldName);
if (pos != DocMap.end()) {
Document* temp;
temp = pos->second;
DocMap.erase(pos);
DocMap[NewName] = temp;
signalRenameDocument(*temp);
}
else {
throw Base::Exception("Application::renameDocument(): no document with this name to rename!");
}
}
Document* Application::newDocument(const char * Name, const char * UserName)
{
// get anyway a valid name!
if (!Name || Name[0] == '\0')
Name = "Unnamed";
string name = getUniqueDocumentName(Name);
std::string userName;
if (UserName && UserName[0] != '\0') {
userName = UserName;
}
else {
userName = Name;
std::vector<std::string> names;
names.reserve(DocMap.size());
std::map<string,Document*>::const_iterator pos;
for (pos = DocMap.begin();pos != DocMap.end();++pos) {
names.push_back(pos->second->Label.getValue());
}
if (!names.empty())
userName = Base::Tools::getUniqueName(userName, names);
}
// create the FreeCAD document
auto_ptr<Document> newDoc(new Document() );
// add the document to the internal list
DocMap[name] = newDoc.release(); // now owned by the Application
_pActiveDoc = DocMap[name];
// connect the signals to the application for the new document
_pActiveDoc->signalNewObject.connect(boost::bind(&App::Application::slotNewObject, this, _1));
_pActiveDoc->signalDeletedObject.connect(boost::bind(&App::Application::slotDeletedObject, this, _1));
_pActiveDoc->signalChangedObject.connect(boost::bind(&App::Application::slotChangedObject, this, _1, _2));
_pActiveDoc->signalRelabelObject.connect(boost::bind(&App::Application::slotRelabelObject, this, _1));
_pActiveDoc->signalActivatedObject.connect(boost::bind(&App::Application::slotActivatedObject, this, _1));
_pActiveDoc->signalUndo.connect(boost::bind(&App::Application::slotUndoDocument, this, _1));
_pActiveDoc->signalRedo.connect(boost::bind(&App::Application::slotRedoDocument, this, _1));
// make sure that the active document is set in case no GUI is up
{
Base::PyGILStateLocker lock;
Py::Object active(_pActiveDoc->getPyObject(), true);
Py::Module("FreeCAD").setAttr(std::string("ActiveDocument"),active);
}
signalNewDocument(*_pActiveDoc);
// set the UserName after notifying all observers
_pActiveDoc->Label.setValue(userName);
return _pActiveDoc;
}
bool Application::closeDocument(const char* name)
{
map<string,Document*>::iterator pos = DocMap.find( name );
if (pos == DocMap.end()) // no such document
return false;
// Trigger observers before removing the document from the internal map.
// Some observers might rely on that this document is still there.
signalDeleteDocument(*pos->second);
// For exception-safety use a smart pointer
if (_pActiveDoc == pos->second)
setActiveDocument((Document*)0);
auto_ptr<Document> delDoc (pos->second);
DocMap.erase( pos );
// Trigger observers after removing the document from the internal map.
signalDeletedDocument();
return true;
}
void Application::closeAllDocuments(void)
{
std::map<std::string,Document*>::iterator pos;
while((pos = DocMap.begin()) != DocMap.end())
closeDocument(pos->first.c_str());
}
App::Document* Application::getDocument(const char *Name) const
{
std::map<std::string,Document*>::const_iterator pos;
pos = DocMap.find(Name);
if (pos == DocMap.end())
return 0;
return pos->second;
}
const char * Application::getDocumentName(const App::Document* doc) const
{
for (std::map<std::string,Document*>::const_iterator it = DocMap.begin(); it != DocMap.end(); ++it)
if (it->second == doc)
return it->first.c_str();
return 0;
}
std::vector<App::Document*> Application::getDocuments() const
{
std::vector<App::Document*> docs;
for (std::map<std::string,Document*>::const_iterator it = DocMap.begin(); it != DocMap.end(); ++it)
docs.push_back(it->second);
return docs;
}
std::string Application::getUniqueDocumentName(const char *Name) const
{
if (!Name || *Name == '\0')
return std::string();
std::string CleanName = Base::Tools::getIdentifier(Name);
// name in use?
std::map<string,Document*>::const_iterator pos;
pos = DocMap.find(CleanName);
if (pos == DocMap.end()) {
// if not, name is OK
return CleanName;
}
else {
std::vector<std::string> names;
names.reserve(DocMap.size());
for (pos = DocMap.begin();pos != DocMap.end();++pos) {
names.push_back(pos->first);
}
return Base::Tools::getUniqueName(CleanName, names);
}
}
Document* Application::openDocument(const char * FileName)
{
FileInfo File(FileName);
if (!File.exists()) {
std::stringstream str;
str << "File '" << FileName << "' does not exist!";
throw Base::Exception(str.str().c_str());
}
// Before creating a new document we check whether the document is already open
std::string filepath = File.filePath();
for (std::map<std::string,Document*>::iterator it = DocMap.begin(); it != DocMap.end(); ++it) {
// get unique path separators
std::string fi = FileInfo(it->second->FileName.getValue()).filePath();
if (filepath == fi) {
std::stringstream str;
str << "The project '" << FileName << "' is already open!";
throw Base::Exception(str.str().c_str());
}
}
// Use the same name for the internal and user name.
// The file name is UTF-8 encoded which means that the internal name will be modified
// to only contain valid ASCII characters but the user name will be kept.
Document* newDoc = newDocument(File.fileNamePure().c_str(), File.fileNamePure().c_str());
newDoc->FileName.setValue(File.filePath());
// read the document
newDoc->restore();
return newDoc;
}
Document* Application::getActiveDocument(void) const
{
return _pActiveDoc;
}
void Application::setActiveDocument(Document* pDoc)
{
_pActiveDoc = pDoc;
// make sure that the active document is set in case no GUI is up
if (pDoc) {
Base::PyGILStateLocker lock;
Py::Object active(pDoc->getPyObject(), true);
Py::Module("FreeCAD").setAttr(std::string("ActiveDocument"),active);
}
else {
Base::PyGILStateLocker lock;
Py::Module("FreeCAD").setAttr(std::string("ActiveDocument"),Py::None());
}
if (pDoc)
signalActiveDocument(*pDoc);
}
void Application::setActiveDocument(const char *Name)
{
// Allows that no active document is set.
if (*Name == '\0') {
_pActiveDoc = 0;
return;
}
std::map<std::string,Document*>::iterator pos;
pos = DocMap.find(Name);
if (pos != DocMap.end()) {
setActiveDocument(pos->second);
}
else {
std::stringstream s;
s << "Try to activate unknown document '" << Name << "'";
throw Base::Exception(s.str());
}
}
const char* Application::getHomePath(void) const
{
return _mConfig["AppHomePath"].c_str();
}
const char* Application::getExecutableName(void) const
{
return _mConfig["ExeName"].c_str();
}
std::string Application::getTempPath()
{
return mConfig["AppTempPath"];
}
std::string Application::getTempFileName(const char* FileName)
{
return Base::FileInfo::getTempFileName(FileName, getTempPath().c_str());
}
std::string Application::getUserAppDataDir()
{
return mConfig["UserAppData"];
}
std::string Application::getResourceDir()
{
#ifdef RESOURCEDIR
std::string path(RESOURCEDIR);
path.append("/");
QDir dir(QString::fromUtf8(RESOURCEDIR));
if (dir.isAbsolute())
return path;
else
return mConfig["AppHomePath"] + path;
#else
return mConfig["AppHomePath"];
#endif
}
std::string Application::getHelpDir()
{
#ifdef DOCDIR
std::string path(DOCDIR);
path.append("/");
QDir dir(QString::fromUtf8(DOCDIR));
if (dir.isAbsolute())
return path;
else
return mConfig["AppHomePath"] + path;
#else
return mConfig["DocPath"];
#endif
}
ParameterManager & Application::GetSystemParameter(void)
{
return *_pcSysParamMngr;
}
ParameterManager & Application::GetUserParameter(void)
{
return *_pcUserParamMngr;
}
ParameterManager * Application::GetParameterSet(const char* sName) const
{
std::map<std::string,ParameterManager *>::const_iterator it = mpcPramManager.find(sName);
if ( it != mpcPramManager.end() )
return it->second;
else
return 0;
}
const std::map<std::string,ParameterManager *> & Application::GetParameterSetList(void) const
{
return mpcPramManager;
}
void Application::AddParameterSet(const char* sName)
{
std::map<std::string,ParameterManager *>::const_iterator it = mpcPramManager.find(sName);
if ( it != mpcPramManager.end() )
return;
mpcPramManager[sName] = new ParameterManager();
}
void Application::RemoveParameterSet(const char* sName)
{
std::map<std::string,ParameterManager *>::iterator it = mpcPramManager.find(sName);
// Must not delete user or system parameter
if ( it == mpcPramManager.end() || it->second == _pcUserParamMngr || it->second == _pcSysParamMngr )
return;
delete it->second;
mpcPramManager.erase(it);
}
Base::Reference<ParameterGrp> Application::GetParameterGroupByPath(const char* sName)
{
std::string cName = sName,cTemp;
std::string::size_type pos = cName.find(':');
// is there a path seperator ?
if (pos == std::string::npos) {
throw Base::Exception("Application::GetParameterGroupByPath() no parameter set name specified");
}
// assigning the parameter set name
cTemp.assign(cName,0,pos);
cName.erase(0,pos+1);
// test if name is valid
std::map<std::string,ParameterManager *>::iterator It = mpcPramManager.find(cTemp.c_str());
if (It == mpcPramManager.end())
throw Base::Exception("Application::GetParameterGroupByPath() unknown parameter set name specified");
return It->second->GetGroup(cName.c_str());
}
void Application::addImportType(const char* Type, const char* ModuleName)
{
FileTypeItem item;
item.filter = Type;
item.module = ModuleName;
// Extract each filetype from 'Type' literal
std::string::size_type pos = item.filter.find("*.");
while ( pos != std::string::npos ) {
std::string::size_type next = item.filter.find_first_of(" )", pos+1);
std::string::size_type len = next-pos-2;
std::string type = item.filter.substr(pos+2,len);
item.types.push_back(type);
pos = item.filter.find("*.", next);
}
// Due to branding stuff replace FreeCAD through the application name
if (strncmp(Type, "FreeCAD", 7) == 0) {
std::string AppName = Config()["ExeName"];
AppName += item.filter.substr(7);
item.filter = AppName;
// put to the front of the array
_mImportTypes.insert(_mImportTypes.begin(),item);
}
else {
_mImportTypes.push_back(item);
}
}
std::vector<std::string> Application::getImportModules(const char* Type) const
{
std::vector<std::string> modules;
for (std::vector<FileTypeItem>::const_iterator it = _mImportTypes.begin(); it != _mImportTypes.end(); ++it) {
const std::vector<std::string>& types = it->types;
for (std::vector<std::string>::const_iterator jt = types.begin(); jt != types.end(); ++jt) {
#ifdef __GNUC__
if (strcasecmp(Type,jt->c_str()) == 0)
#else
if (_stricmp(Type,jt->c_str()) == 0)
#endif
modules.push_back(it->module);
}
}
return modules;
}
std::vector<std::string> Application::getImportModules() const
{
std::vector<std::string> modules;
for (std::vector<FileTypeItem>::const_iterator it = _mImportTypes.begin(); it != _mImportTypes.end(); ++it)
modules.push_back(it->module);
std::sort(modules.begin(), modules.end());
modules.erase(std::unique(modules.begin(), modules.end()), modules.end());
return modules;
}
std::vector<std::string> Application::getImportTypes(const char* Module) const
{
std::vector<std::string> types;
for (std::vector<FileTypeItem>::const_iterator it = _mImportTypes.begin(); it != _mImportTypes.end(); ++it) {
#ifdef __GNUC__
if (strcasecmp(Module,it->module.c_str()) == 0)
#else
if (_stricmp(Module,it->module.c_str()) == 0)
#endif
types.insert(types.end(), it->types.begin(), it->types.end());
}
return types;
}
std::vector<std::string> Application::getImportTypes(void) const
{
std::vector<std::string> types;
for (std::vector<FileTypeItem>::const_iterator it = _mImportTypes.begin(); it != _mImportTypes.end(); ++it) {
types.insert(types.end(), it->types.begin(), it->types.end());
}
std::sort(types.begin(), types.end());
types.erase(std::unique(types.begin(), types.end()), types.end());
return types;
}
std::map<std::string, std::string> Application::getImportFilters(const char* Type) const
{
std::map<std::string, std::string> moduleFilter;
for (std::vector<FileTypeItem>::const_iterator it = _mImportTypes.begin(); it != _mImportTypes.end(); ++it) {
const std::vector<std::string>& types = it->types;
for (std::vector<std::string>::const_iterator jt = types.begin(); jt != types.end(); ++jt) {
#ifdef __GNUC__
if (strcasecmp(Type,jt->c_str()) == 0)
#else
if (_stricmp(Type,jt->c_str()) == 0)
#endif
moduleFilter[it->filter] = it->module;
}
}
return moduleFilter;
}
std::map<std::string, std::string> Application::getImportFilters(void) const
{
std::map<std::string, std::string> filter;
for (std::vector<FileTypeItem>::const_iterator it = _mImportTypes.begin(); it != _mImportTypes.end(); ++it) {
filter[it->filter] = it->module;
}
return filter;
}
void Application::addExportType(const char* Type, const char* ModuleName)
{
FileTypeItem item;
item.filter = Type;
item.module = ModuleName;
// Extract each filetype from 'Type' literal
std::string::size_type pos = item.filter.find("*.");
while ( pos != std::string::npos ) {
std::string::size_type next = item.filter.find_first_of(" )", pos+1);
std::string::size_type len = next-pos-2;
std::string type = item.filter.substr(pos+2,len);
item.types.push_back(type);
pos = item.filter.find("*.", next);
}
// Due to branding stuff replace FreeCAD through the application name
if (strncmp(Type, "FreeCAD", 7) == 0) {
std::string AppName = Config()["ExeName"];
AppName += item.filter.substr(7);
item.filter = AppName;
// put to the front of the array
_mExportTypes.insert(_mExportTypes.begin(),item);
}
else {
_mExportTypes.push_back(item);
}
}
std::vector<std::string> Application::getExportModules(const char* Type) const
{
std::vector<std::string> modules;
for (std::vector<FileTypeItem>::const_iterator it = _mExportTypes.begin(); it != _mExportTypes.end(); ++it) {
const std::vector<std::string>& types = it->types;
for (std::vector<std::string>::const_iterator jt = types.begin(); jt != types.end(); ++jt) {
#ifdef __GNUC__
if (strcasecmp(Type,jt->c_str()) == 0)
#else
if (_stricmp(Type,jt->c_str()) == 0)
#endif
modules.push_back(it->module);
}
}
return modules;
}
std::vector<std::string> Application::getExportModules() const
{
std::vector<std::string> modules;
for (std::vector<FileTypeItem>::const_iterator it = _mExportTypes.begin(); it != _mExportTypes.end(); ++it)
modules.push_back(it->module);
std::sort(modules.begin(), modules.end());
modules.erase(std::unique(modules.begin(), modules.end()), modules.end());
return modules;
}
std::vector<std::string> Application::getExportTypes(const char* Module) const
{
std::vector<std::string> types;
for (std::vector<FileTypeItem>::const_iterator it = _mExportTypes.begin(); it != _mExportTypes.end(); ++it) {
#ifdef __GNUC__
if (strcasecmp(Module,it->module.c_str()) == 0)
#else
if (_stricmp(Module,it->module.c_str()) == 0)
#endif
types.insert(types.end(), it->types.begin(), it->types.end());
}
return types;
}
std::vector<std::string> Application::getExportTypes(void) const
{
std::vector<std::string> types;
for (std::vector<FileTypeItem>::const_iterator it = _mExportTypes.begin(); it != _mExportTypes.end(); ++it) {
types.insert(types.end(), it->types.begin(), it->types.end());
}
std::sort(types.begin(), types.end());
types.erase(std::unique(types.begin(), types.end()), types.end());
return types;
}
std::map<std::string, std::string> Application::getExportFilters(const char* Type) const
{
std::map<std::string, std::string> moduleFilter;
for (std::vector<FileTypeItem>::const_iterator it = _mExportTypes.begin(); it != _mExportTypes.end(); ++it) {
const std::vector<std::string>& types = it->types;
for (std::vector<std::string>::const_iterator jt = types.begin(); jt != types.end(); ++jt) {
#ifdef __GNUC__
if (strcasecmp(Type,jt->c_str()) == 0)
#else
if (_stricmp(Type,jt->c_str()) == 0)
#endif
moduleFilter[it->filter] = it->module;
}
}
return moduleFilter;
}
std::map<std::string, std::string> Application::getExportFilters(void) const
{
std::map<std::string, std::string> filter;
for (std::vector<FileTypeItem>::const_iterator it = _mExportTypes.begin(); it != _mExportTypes.end(); ++it) {
filter[it->filter] = it->module;
}
return filter;
}
//**************************************************************************
// signaling
void Application::slotNewObject(const App::DocumentObject&O)
{
this->signalNewObject(O);
}
void Application::slotDeletedObject(const App::DocumentObject&O)
{
this->signalDeletedObject(O);
}
void Application::slotChangedObject(const App::DocumentObject&O, const App::Property& P)
{
this->signalChangedObject(O,P);
}
void Application::slotRelabelObject(const App::DocumentObject&O)
{
this->signalRelabelObject(O);
}
void Application::slotActivatedObject(const App::DocumentObject&O)
{
this->signalActivatedObject(O);
}
void Application::slotUndoDocument(const App::Document& d)
{
this->signalUndoDocument(d);
}
void Application::slotRedoDocument(const App::Document& d)
{
this->signalRedoDocument(d);
}
//**************************************************************************
// Init, Destruct and singleton
Application * Application::_pcSingleton = 0;
int Application::_argc;
char ** Application::_argv;
void Application::destruct(void)
{
// saving system parameter
Console().Log("Saving system parameter...\n");
_pcSysParamMngr->SaveDocument(mConfig["SystemParameter"].c_str());
// saving the User parameter
Console().Log("Saving system parameter...done\n");
Console().Log("Saving user parameter...\n");
_pcUserParamMngr->SaveDocument(mConfig["UserParameter"].c_str());
Console().Log("Saving user parameter...done\n");
// clean up
delete _pcSysParamMngr;
delete _pcUserParamMngr;
// not initialized or doubel destruct!
assert(_pcSingleton);
delete _pcSingleton;
// We must detach from console and delete the observer to save our file
destructObserver();
Base::Interpreter().finalize();
ScriptFactorySingleton::Destruct();
InterpreterSingleton::Destruct();
Base::Type::destruct();
}
void Application::destructObserver(void)
{
if ( _pConsoleObserverFile ) {
Console().DetachObserver(_pConsoleObserverFile);
delete _pConsoleObserverFile;
_pConsoleObserverFile = 0;
}
if ( _pConsoleObserverStd ) {
Console().DetachObserver(_pConsoleObserverStd);
delete _pConsoleObserverStd;
_pConsoleObserverStd = 0;
}
}
/** freecadNewHandler()
* prints an error message and throws an exception
*/
#ifdef _MSC_VER // New handler for Microsoft Visual C++ compiler
int __cdecl freecadNewHandler(size_t size )
{
// throw an exception
throw Base::MemoryException();
return 0;
}
#else // Ansi C/C++ new handler
static void freecadNewHandler ()
{
// throw an exception
throw Base::MemoryException();
}
#endif
void segmentation_fault_handler(int sig)
{
switch (sig) {
case SIGSEGV:
std::cerr << "Illegal storage access..." << std::endl;
#if !defined(_DEBUG)
throw Base::Exception("Illegal storage access! Please save your work under a new file name and restart the application!");
#endif
break;
case SIGABRT:
std::cerr << "Abnormal program termination..." << std::endl;
#if !defined(_DEBUG)
throw Base::Exception("Break signal occoured");
#endif
break;
default:
std::cerr << "Unknown error occurred..." << std::endl;
break;
}
}
void my_terminate_handler()
{
std::cerr << "Terminating..." << std::endl;
}
void unexpection_error_handler()
{
std::cerr << "Unexpected error occurred..." << std::endl;
// try to throw to give the user evantually a change to save
#if !defined(_DEBUG)
throw Base::Exception("Unexpected error occurred! Please save your work under a new file name and restart the application!");
#endif
terminate();
}
#ifdef _MSC_VER // Microsoft compiler
void my_trans_func( unsigned int code, EXCEPTION_POINTERS* pExp )
{
//switch (code)
//{
// case FLT_DIVIDE_BY_ZERO :
// //throw CMyFunkyDivideByZeroException(code, pExp);
// throw Base::Exception("Devision by zero!");
// break;
//}