0033232: Data Exchange, DE_Wrapper - Implement ability to change global session
[occt.git] / src / DE / DE_Wrapper.hxx
1 // Copyright (c) 2022 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _DE_Wrapper_HeaderFile
15 #define _DE_Wrapper_HeaderFile
16
17 #include <Message_ProgressRange.hxx>
18 #include <DE_ConfigurationNode.hxx>
19 #include <NCollection_IndexedDataMap.hxx>
20 #include <NCollection_DataMap.hxx>
21 #include <TColStd_ListOfAsciiString.hxx>
22
23 class TopoDS_Shape;
24 class XSControl_WorkSession;
25 class TDocStd_Document;
26
27 typedef NCollection_IndexedDataMap<TCollection_AsciiString, Handle(DE_ConfigurationNode), TCollection_AsciiString> DE_ConfigurationVendorMap;
28 typedef NCollection_DataMap<TCollection_AsciiString, DE_ConfigurationVendorMap, TCollection_AsciiString> DE_ConfigurationFormatMap;
29
30 //! The main class for working with CAD file exchange.
31 //! Loads and Saves special CAD transfer property.
32 //! Consolidates all supported Formats and Vendors.
33 //! Automatically recognizes CAD format and uses the preferred existed Vendor.
34 //! Note:
35 //!   If Vendor's format is not binded, the configuration loading doesn't affect on its property.
36 //!
37 //! Nodes are grouped by Vendor's name and Format's type.
38 //! The Vendors may have the same supported CAD formats.
39 //! Use a Vendor's priority for transfer operations.
40 //!
41 //! The algorithm for standalone transfer operation:
42 //! 1) Work with global wrapper directly or make deep copy and work with it
43 //! 2) Update the supported vendors and formats
44 //!   2.1) Create and initialize specialized configuration node of the required format and Vendor.
45 //!   2.2) Bind the created node to the internal map(::Bind)
46 //! 3) Configure the transfer property by resource string or file (::Load)
47 //!   3.1) Configuration can disable or enable some Vendors and formats
48 //!   3.2) Configuration can change the priority of Vendors
49 //! 4) Initiate the transfer process by calling "::Write" or "::Read" methods
50 //! 5) Validate the transfer process output
51 class DE_Wrapper : public Standard_Transient
52 {
53   DEFINE_STANDARD_RTTIEXT(DE_Wrapper, Standard_Transient)
54
55 public:
56
57   //! Initializes all field by default
58   Standard_EXPORT DE_Wrapper();
59
60   //! Copies values of all fields
61   //! @param[in] theWrapper object to copy
62   Standard_EXPORT DE_Wrapper(const Handle(DE_Wrapper)& theWrapper);
63
64   //! Gets global configuration singleton.
65   //! If wrapper is not set, create it by default as base class object.
66   //! @return point to global configuration
67   Standard_EXPORT static Handle(DE_Wrapper) GlobalWrapper();
68
69   //! Sets global configuration singleton
70   //! @param[in] theWrapper object to set as global configuration
71   Standard_EXPORT static void SetGlobalWrapper(const Handle(DE_Wrapper)& theWrapper);
72
73 public:
74
75   //! Reads a CAD file, according internal configuration
76   //! @param[in] thePath path to the import CAD file
77   //! @param[out] theDocument document to save result
78   //! @param[in] theWS current work session
79   //! @param theProgress[in] progress indicator
80   //! @return true if Read operation has ended correctly
81   Standard_EXPORT Standard_Boolean Read(const TCollection_AsciiString& thePath,
82                                         const Handle(TDocStd_Document)& theDocument,
83                                         Handle(XSControl_WorkSession)& theWS,
84                                         const Message_ProgressRange& theProgress = Message_ProgressRange());
85
86   //! Writes a CAD file, according internal configuration
87   //! @param[in] thePath path to the export CAD file
88   //! @param[out] theDocument document to export
89   //! @param[in] theWS current work session
90   //! @param theProgress[in] progress indicator
91   //! @return true if Write operation has ended correctly
92   Standard_EXPORT Standard_Boolean Write(const TCollection_AsciiString& thePath,
93                                          const Handle(TDocStd_Document)& theDocument,
94                                          Handle(XSControl_WorkSession)& theWS,
95                                          const Message_ProgressRange& theProgress = Message_ProgressRange());
96
97   //! Reads a CAD file, according internal configuration
98   //! @param[in] thePath path to the import CAD file
99   //! @param[out] theDocument document to save result
100   //! @param theProgress[in] progress indicator
101   //! @return true if Read operation has ended correctly
102   Standard_EXPORT Standard_Boolean Read(const TCollection_AsciiString& thePath,
103                                         const Handle(TDocStd_Document)& theDocument,
104                                         const Message_ProgressRange& theProgress = Message_ProgressRange());
105
106   //! Writes a CAD file, according internal configuration
107   //! @param[in] thePath path to the export CAD file
108   //! @param[out] theDocument document to export
109   //! @param theProgress[in] progress indicator
110   //! @return true if Write operation has ended correctly
111   Standard_EXPORT Standard_Boolean Write(const TCollection_AsciiString& thePath,
112                                          const Handle(TDocStd_Document)& theDocument,
113                                          const Message_ProgressRange& theProgress = Message_ProgressRange());
114
115   //! Reads a CAD file, according internal configuration
116   //! @param[in] thePath path to the import CAD file
117   //! @param[out] theShape shape to save result
118   //! @param[in] theWS current work session
119   //! @param theProgress[in] progress indicator
120   //! @return true if Read operation has ended correctly
121   Standard_EXPORT Standard_Boolean Read(const TCollection_AsciiString& thePath,
122                                         TopoDS_Shape& theShape,
123                                         Handle(XSControl_WorkSession)& theWS,
124                                         const Message_ProgressRange& theProgress = Message_ProgressRange());
125
126   //! Writes a CAD file, according internal configuration
127   //! @param[in] thePath path to the export CAD file
128   //! @param[out] theShape shape to export
129   //! @param[in] theWS current work session
130   //! @param theProgress[in] progress indicator
131   //! @return true if Write operation has ended correctly
132   Standard_EXPORT Standard_Boolean Write(const TCollection_AsciiString& thePath,
133                                          const TopoDS_Shape& theShape,
134                                          Handle(XSControl_WorkSession)& theWS,
135                                          const Message_ProgressRange& theProgress = Message_ProgressRange());
136
137   //! Reads a CAD file, according internal configuration
138   //! @param[in] thePath path to the import CAD file
139   //! @param[out] theShape shape to save result
140   //! @param theProgress[in] progress indicator
141   //! @return true if Read operation has ended correctly
142   Standard_EXPORT Standard_Boolean Read(const TCollection_AsciiString& thePath,
143                                         TopoDS_Shape& theShape,
144                                         const Message_ProgressRange& theProgress = Message_ProgressRange());
145
146   //! Writes a CAD file, according internal configuration
147   //! @param[in] thePath path to the export CAD file
148   //! @param[out] theShape shape to export
149   //! @param theProgress[in] progress indicator
150   //! @return true if Write operation has ended correctly
151   Standard_EXPORT Standard_Boolean Write(const TCollection_AsciiString& thePath,
152                                          const TopoDS_Shape& theShape,
153                                          const Message_ProgressRange& theProgress = Message_ProgressRange());
154
155 public:
156
157   //! Updates values according the resource file
158   //! @param[in] theResource file path to resource or resource value
159   //! @param[in] theIsRecursive flag to update all nodes
160   //! @return true if theResource has loaded correctly
161   Standard_EXPORT Standard_Boolean Load(const TCollection_AsciiString& theResource = "",
162                                         const Standard_Boolean theIsRecursive = Standard_True);
163
164   //! Updates values according the resource
165   //! @param[in] theResource input resource to use
166   //! @param[in] theIsRecursive flag to update all nodes
167   //! @return true if theResource has loaded correctly
168   Standard_EXPORT Standard_Boolean Load(const Handle(DE_ConfigurationContext)& theResource,
169                                         const Standard_Boolean theIsRecursive = Standard_True);
170
171   //! Writes configuration to the resource file
172   //! @param[in] theResourcePath file path to resource
173   //! @param[in] theIsRecursive flag to write values of all nodes
174   //! @param[in] theFormats list of formats to save. If empty, saves all available
175   //! @param[in] theVendors list of providers to save. If empty, saves all available
176   //! @return true if the Configuration has saved correctly
177   Standard_EXPORT Standard_Boolean Save(const TCollection_AsciiString& theResourcePath,
178                                         const Standard_Boolean theIsRecursive = Standard_True,
179                                         const TColStd_ListOfAsciiString& theFormats = TColStd_ListOfAsciiString(),
180                                         const TColStd_ListOfAsciiString& theVendors = TColStd_ListOfAsciiString());
181
182   //! Writes configuration to the string
183   //! @param[in] theIsRecursive flag to write values of all nodes
184   //! @param[in] theFormats list of formats to save. If empty, saves all available
185   //! @param[in] theVendors list of providers to save. If empty, saves all available
186   //! @return result resource string
187   Standard_EXPORT TCollection_AsciiString Save(const Standard_Boolean theIsRecursive = Standard_True,
188                                                const TColStd_ListOfAsciiString& theFormats = TColStd_ListOfAsciiString(),
189                                                const TColStd_ListOfAsciiString& theVendors = TColStd_ListOfAsciiString());
190
191   //! Creates new node copy and adds to the map
192   //! @param[in] theNode input node to copy
193   //! @return Standard_True if binded
194   Standard_EXPORT Standard_Boolean Bind(const Handle(DE_ConfigurationNode)& theNode);
195
196   //! Finds a node associated with input format and vendor
197   //! @param[in] theFormat input node CAD format
198   //! @param[in] theVendor input node vendor name
199   //! @param[out] theNode output node
200   //! @return Standard_True if the node is found
201   Standard_EXPORT Standard_Boolean Find(const TCollection_AsciiString& theFormat,
202                                         const TCollection_AsciiString& theVendor,
203                                         Handle(DE_ConfigurationNode)& theNode) const;
204
205   //! Changes provider priority to one format if it exists
206   //! @param[in] theFormat input node CAD format
207   //! @param[in] theVendorPriority priority of work with vendors
208   //! @param[in] theToDisable flag for disabling nodes that are not included in the priority
209   Standard_EXPORT void ChangePriority(const TCollection_AsciiString& theFormat,
210                                       const TColStd_ListOfAsciiString& theVendorPriority,
211                                       const Standard_Boolean theToDisable = Standard_False);
212
213   //! Changes provider priority to all loaded nodes
214   //! @param[in] theVendorPriority priority of work with vendors
215   //! @param[in] theToDisable flag for disabling nodes that are not included in the priority
216   Standard_EXPORT void ChangePriority(const TColStd_ListOfAsciiString& theVendorPriority,
217                                       const Standard_Boolean theToDisable = Standard_False);
218
219   //! Find available provider from the configuration.
220   //! If there are several providers, choose the one with the highest priority.
221   //! @param[in] thePath path to the CAD file
222   //! @param[in] theToImport flag to finds for import. Standard_True-import, Standard_False-export
223   //! @param[out] theProvider created new provider
224   //! @return Standard_True if provider found and created
225   Standard_EXPORT virtual Standard_Boolean FindProvider(const TCollection_AsciiString& thePath,
226                                                         const Standard_Boolean theToImport,
227                                                         Handle(DE_Provider)& theProvider) const;
228
229   //! Gets format map, contains vendor map with nodes
230   //! @return internal map of formats
231   Standard_EXPORT const DE_ConfigurationFormatMap& Nodes() const;
232
233   //! Copies values of all fields
234   //! @return new object with the same field values
235   Standard_EXPORT virtual Handle(DE_Wrapper) Copy() const;
236
237 protected:
238
239   //! Sorts the vendors according to the priority to work
240   //! Formats omitted from the resource are not modified
241   //! Vendors omitted from the format scope are disabled
242   //! @param[in] theResource resource to get priority
243   void sort(const Handle(DE_ConfigurationContext)& theResource);
244
245 public:
246
247   DE_ConfigurationNode::DE_SectionGlobal GlobalParameters; //!< Internal parameters for the all translators
248
249 private:
250
251   DE_ConfigurationFormatMap myConfiguration; //!< Internal map of formats
252 };
253
254 #endif // _DE_Wrapper_HeaderFile