]> SALOME platform Git repositories - plugins/xtplugin.git/blob - src/XTPlugin_ImportDriver.cxx
Salome HOME
Merge from rnv/geom_plugin_imp branch
[plugins/xtplugin.git] / src / XTPlugin_ImportDriver.cxx
1 // Copyright (C) 2014-2015  OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "XTPlugin_ImportDriver.hxx"
22 #include "XTPlugin_IImport.hxx"
23 #include "XTPlugin_Engine.hxx"
24
25 // KERNEL includes
26 #include <Basics_Utils.hxx>
27 #include <utilities.h>
28
29 // GEOM includes
30 #include <GEOM_Function.hxx>
31 #include <GEOMImpl_Types.hxx>
32
33 // OOCT includes
34 #include <IFSelect_ReturnStatus.hxx>
35 #include <TCollection_AsciiString.hxx>
36 #include <TDF_Label.hxx>
37 #include <TDataStd_Name.hxx>
38 #include <TNaming_Builder.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TransferBRep.hxx>
42 #include <Transfer_Binder.hxx>
43 #include <Transfer_TransientProcess.hxx>
44 #include <XtAttributes_AttribGroup.hxx>
45 #include <XtAttributes_Attribute.hxx>
46 #include <XtAttributes_CharValues.hxx>
47 #include <XSControl_TransferReader.hxx>
48 #include <XSControl_WorkSession.hxx>
49 #include <XtControl_Reader.hxx>
50 #include <XtData_HArray1OfObject.hxx>
51 #include <XtData_Model.hxx>
52 #include <XtData_Object.hxx>
53 #include <XtGeom_Curve.hxx>
54 #include <XtGeom_Point.hxx>
55 #include <XtGeom_Surface.hxx>
56 #include <XtTopoDS_Topology.hxx>
57
58 #include <StdFail_NotDone.hxx>
59
60 #ifdef XT_HASLICENSE
61 #include "XTPlugin_license.h"
62
63 #include <OCCLicense_Activate.hxx>
64 #include <Standard_LicenseError.hxx>
65 #endif // XT_HASLICENSE
66
67 //=======================================================================
68 //function : GetID
69 //purpose  :
70 //=======================================================================
71 const Standard_GUID& XTPlugin_ImportDriver::GetID()
72 {
73   static Standard_GUID aGUID("81cc16bb-486f-4ec7-87b2-960b4aed557f");
74   return aGUID;
75 }
76
77 //=======================================================================
78 //function : XTPlugin_ImportDriver
79 //purpose  :
80 //=======================================================================
81 XTPlugin_ImportDriver::XTPlugin_ImportDriver()
82 {
83 }
84
85 //=======================================================================
86 //function : Execute
87 //purpose  :
88 //=======================================================================
89 Standard_Integer XTPlugin_ImportDriver::Execute( TFunction_Logbook& log ) const
90 {
91   if( Label().IsNull() ) return 0;
92   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
93
94   XTPlugin_IImport aData( aFunction );
95
96   TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
97
98   MESSAGE("Import XT from file " << aFileName);
99
100   TopoDS_Shape aResShape;
101
102 #ifdef XT_HASLICENSE
103   try {
104     OCCLicense_Activate( "XT-R-"OCC_VERSION_STRING, XT_READ_LICENSE);
105   }
106   catch (Standard_LicenseError) {
107     return aResShape;
108   }
109 #endif // XT_HASLICENSE
110
111   // Set "C" numeric locale to save numbers correctly
112   Kernel_Utils::Localizer loc;
113
114   XtControl_Reader aReader;
115
116   IFSelect_ReturnStatus status = aReader.ReadFile(aFileName.ToCString());
117   if (status == IFSelect_RetDone) {
118     aReader.TransferRoots();
119         aResShape = aReader.OneShape();
120
121     // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
122     // It should be removed after patching OCCT for bug OCC22436
123     // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
124     if (aResShape.ShapeType() == TopAbs_COMPOUND) {
125       int nbSub1 = 0;
126       TopoDS_Shape currShape;
127       TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
128       for (; It.More(); It.Next()) {
129         nbSub1++;
130         currShape = It.Value();
131       }
132       if (nbSub1 == 1)
133         aResShape = currShape;
134     }
135     Handle(XtData_Model) aModel = Handle(XtData_Model)::DownCast( aReader.WS()->Model() );
136     Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
137     if (!TR.IsNull()) {
138       Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
139       Standard_Integer nb = aModel->NbEntities();
140       for (Standard_Integer i = 1; i <= nb; i ++) {
141         Handle(XtData_Object) XtOb = Handle(XtData_Object)::DownCast ( aModel->Value(i) );
142         if (XtOb.IsNull()) continue;
143
144         Handle(XtAttributes_AttribGroup) XtAG;
145         Standard_Boolean MayBeName = Standard_False;
146
147         if (XtOb->IsKind(STANDARD_TYPE(XtTopoDS_Topology))) {
148           Handle(XtTopoDS_Topology) XtT = Handle(XtTopoDS_Topology)::DownCast(XtOb);
149           XtAG = XtT->AttributesGroups();
150           MayBeName = Standard_True;
151         }
152         else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Surface))) {
153           Handle(XtGeom_Surface) XtGS = Handle(XtGeom_Surface)::DownCast(XtOb);
154           XtAG = XtGS->AttributesGroups();
155           MayBeName = Standard_True;
156         }
157         else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Curve))) {
158           Handle(XtGeom_Curve) XtGC = Handle(XtGeom_Curve)::DownCast(XtOb);
159           XtAG = XtGC->AttributesGroups();
160           MayBeName = Standard_True;
161         }
162         else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Point))) {
163           Handle(XtGeom_Point) XtGP = Handle(XtGeom_Point)::DownCast(XtOb);
164           XtAG = XtGP->AttributesGroups();
165           MayBeName = Standard_True;
166         }
167
168         if (MayBeName) {
169           TCollection_AsciiString string;
170           Standard_Boolean IsName = Standard_False;
171           Handle(XtAttributes_Attribute) XtA = Handle(XtAttributes_Attribute)::DownCast(XtAG);
172           if (!XtA.IsNull()) {
173             Handle(XtAttributes_AttribDef) XtAD = XtA->Definition();
174             while (!XtAD.IsNull()) {
175               if (XtAD->TypeID()==8017) {
176                 const Handle(XtData_HArray1OfObject) &HAFV = XtA->Fields();
177                 const Standard_Integer NbFV = HAFV->Length();
178                 for (Standard_Integer j=1; j<=NbFV; j++) {
179                   const Handle(XtData_Object) &FV = HAFV->Value(j);
180                   if(!FV.IsNull() && FV->IsKind(STANDARD_TYPE(XtAttributes_CharValues))) {
181                     string = Handle(XtAttributes_CharValues)::DownCast(FV)->Values();
182                     IsName = Standard_True;
183                   }
184                 }
185               }
186               XtAD = XtAD->Next();
187             }
188           }
189           if (IsName) {
190             // find target shape
191             Handle(Transfer_Binder) binder = TP->Find ( XtOb );
192             if ( binder.IsNull() ) continue;
193             TopoDS_Shape S = TransferBRep::ShapeResult (binder);
194             if ( S.IsNull() ) continue;
195
196             TDF_Label L;
197             TDF_TagSource aTag;
198             L = aTag.NewChild(aFunction->GetNamingEntry());
199             TNaming_Builder tnBuild(L);
200             tnBuild.Generated(S);
201
202             // set a name to the document
203             string.LeftAdjust();
204             string.RightAdjust();
205             TCollection_ExtendedString str ( string );
206             TDataStd_Name::Set ( L, str );
207           }
208         }
209       }
210     }
211   }
212   else {
213     TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
214     StdFail_NotDone::Raise( anError.ToCString() );
215     aResShape.Nullify();
216   }
217
218   if( aResShape.IsNull() ) return 0;
219
220   aFunction->SetValue( aResShape );
221
222   log.SetTouched( Label() );
223
224   return 1;
225 }
226
227 //=======================================================================
228 //function : MustExecute
229 //purpose  :
230 //=======================================================================
231 Standard_Boolean XTPlugin_ImportDriver::MustExecute( const TFunction_Logbook& ) const
232 {
233   return Standard_True;
234 }
235
236 //================================================================================
237 /*!
238  * \brief Returns a name of creation operation and names and values of creation parameters
239  */
240 //================================================================================
241
242 bool XTPlugin_ImportDriver::
243 GetCreationInformation( std::string&             theOperationName,
244                         std::vector<GEOM_Param>& theParams )
245 {
246   if( Label().IsNull() ) return 0;
247   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
248
249   XTPlugin_IImport aCI( function );
250   Standard_Integer aType = function->GetType();
251
252   theOperationName = "ImportXT";
253
254   switch ( aType ) {
255   case IMPORT_SHAPE:
256     AddParam( theParams, "File name", aCI.GetFileName() );
257     break;
258   default:
259     return false;
260   }
261   return true;
262 }
263
264 IMPLEMENT_STANDARD_HANDLE( XTPlugin_ImportDriver, GEOM_BaseDriver );
265 IMPLEMENT_STANDARD_RTTIEXT( XTPlugin_ImportDriver, GEOM_BaseDriver );