0029559: Samples - wrong copyright statement in FuncDemo
[occt.git] / tools / DFBrowser / DFBrowser_OpenApplication.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/DFBrowser_OpenApplication.hxx>
17
18 #include <BinDrivers.hxx>
19 #include <BinLDrivers.hxx>
20 #include <BinXCAFDrivers.hxx>
21 #include <PCDM_ReadWriter.hxx>
22 #include <Standard_Version.hxx>
23 #include <StdDrivers.hxx>
24 #include <StdLDrivers.hxx>
25 #include <STEPCAFControl_Reader.hxx>
26 #include <STEPCAFControl_Controller.hxx>
27 #include <TPrsStd_DriverTable.hxx>
28 #include <XCAFApp_Application.hxx>
29 #include <XCAFPrs_Driver.hxx>
30 #include <XmlDrivers.hxx>
31 #include <XmlLDrivers.hxx>
32 #include <XmlXCAFDrivers.hxx>
33 #include <UTL.hxx>
34
35 namespace DFBrowser_OpenApplication
36 {
37
38   // =======================================================================
39   // function : OpenApplication
40   // purpose :
41   // =======================================================================
42   Handle(TDocStd_Application) OpenApplication (const TCollection_AsciiString& theFileName, bool& isSTEPFile)
43   {
44     Handle(TDocStd_Application) anApplication = CreateApplicationBySTEPFile (theFileName);
45     if (!anApplication.IsNull())
46     {
47       isSTEPFile = true;
48       return anApplication;
49     }
50
51 #if OCC_VERSION_HEX > 0x060901
52     // Load static variables for STEPCAF (ssv; 16.08.2012)
53     STEPCAFControl_Controller::Init();
54
55     anApplication = new TDocStd_Application();
56     // Initialize standard document formats at creation - they should
57     // be available even if this DRAW plugin is not loaded by pload command
58     StdLDrivers::DefineFormat (anApplication);
59     BinLDrivers::DefineFormat (anApplication);
60     XmlLDrivers::DefineFormat (anApplication);
61     StdDrivers::DefineFormat (anApplication);
62     BinDrivers::DefineFormat (anApplication);
63     XmlDrivers::DefineFormat (anApplication);
64
65     // Initialize XCAF formats
66     BinXCAFDrivers::DefineFormat (anApplication);
67     XmlXCAFDrivers::DefineFormat (anApplication);
68
69     // Register driver in global table for displaying XDE documents 
70     // in 3d viewer using OCAF mechanics
71     TPrsStd_DriverTable::Get()->AddDriver (XCAFPrs_Driver::GetID(), new XCAFPrs_Driver);
72
73     Handle(TDocStd_Document) aDocument;
74     PCDM_ReaderStatus aStatus = anApplication->Open (theFileName, aDocument);
75     if (aStatus != PCDM_RS_OK)
76       return Handle(TDocStd_Application)();
77 #endif
78     return anApplication;
79   }
80
81   // =======================================================================
82   // function : CreateApplicationBySTEPFile
83   // purpose :
84   // =======================================================================
85   Handle(TDocStd_Application) CreateApplicationBySTEPFile (const TCollection_AsciiString& theFileName)
86   {
87     if (!theFileName.EndsWith (".step") && !theFileName.EndsWith (".stp"))
88       return Handle(TDocStd_Application)();
89
90     Handle(TDocStd_Application) aTmpApplication = XCAFApp_Application::GetApplication();
91     STEPCAFControl_Reader aStepReader;
92
93     const TCollection_AsciiString aStr (theFileName);
94     IFSelect_ReturnStatus aStatus = aStepReader.ReadFile (aStr.ToCString());
95     if (aStatus != IFSelect_RetDone)
96       return Handle(TDocStd_Application)();
97
98     aStepReader.SetColorMode (Standard_True);
99     aStepReader.SetLayerMode (Standard_True);
100     aStepReader.SetNameMode (Standard_True);
101
102     Handle(TDocStd_Document) aDocument;
103     aTmpApplication->NewDocument ("BinOcaf", aDocument);
104     return aStepReader.Transfer (aDocument) ? aTmpApplication : Handle(TDocStd_Application)();
105   }
106 }