Salome HOME
Increment version: 8.5.0
[plugins/xtplugin.git] / src / XTPlugin_ImportDriver.cxx
1 // Copyright (C) 2014-2016  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 // OCCT 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 #include <Standard_Version.hxx>
60
61 #ifdef XT_HASLICENSE
62 #include "XTPlugin_license.h"
63
64 #include <OCCLicense_Activate.hxx>
65 #include <Standard_LicenseError.hxx>
66 #endif // XT_HASLICENSE
67
68 //=======================================================================
69 //function : GetID
70 //purpose  :
71 //=======================================================================
72 const Standard_GUID& XTPlugin_ImportDriver::GetID()
73 {
74   static Standard_GUID aGUID("81cc16bb-486f-4ec7-87b2-960b4aed557f");
75   return aGUID;
76 }
77
78 //=======================================================================
79 //function : XTPlugin_ImportDriver
80 //purpose  :
81 //=======================================================================
82 XTPlugin_ImportDriver::XTPlugin_ImportDriver()
83 {
84 }
85
86 //=======================================================================
87 //function : Execute
88 //purpose  :
89 //=======================================================================
90 Standard_Integer XTPlugin_ImportDriver::Execute( Handle(TFunction_Logbook)& log ) const
91 {
92   if( Label().IsNull() ) return 0;
93   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
94
95   XTPlugin_IImport aData( aFunction );
96
97   TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
98
99   MESSAGE("Import XT from file " << aFileName);
100
101   TopoDS_Shape aResShape;
102
103 #ifdef XT_HASLICENSE
104   try {
105     OCCLicense_Activate( "XT-R-"OCC_VERSION_STRING, XT_READ_LICENSE);
106   }
107   catch (Standard_LicenseError) {
108     return 0;
109   }
110 #endif // XT_HASLICENSE
111
112   // Set "C" numeric locale to save numbers correctly
113   Kernel_Utils::Localizer loc;
114
115   XtControl_Reader aReader;
116
117   IFSelect_ReturnStatus status = aReader.ReadFile(aFileName.ToCString());
118   if (status == IFSelect_RetDone) {
119     aReader.TransferRoots();
120         aResShape = aReader.OneShape();
121
122     // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
123     // It should be removed after patching OCCT for bug OCC22436
124     // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
125     if (aResShape.ShapeType() == TopAbs_COMPOUND) {
126       int nbSub1 = 0;
127       TopoDS_Shape currShape;
128       TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
129       for (; It.More(); It.Next()) {
130         nbSub1++;
131         currShape = It.Value();
132       }
133       if (nbSub1 == 1)
134         aResShape = currShape;
135     }
136     Handle(XtData_Model) aModel = Handle(XtData_Model)::DownCast( aReader.WS()->Model() );
137     Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
138     if (!TR.IsNull()) {
139       Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
140       Standard_Integer nb = aModel->NbEntities();
141       for (Standard_Integer i = 1; i <= nb; i ++) {
142         Handle(XtData_Object) XtOb = Handle(XtData_Object)::DownCast ( aModel->Value(i) );
143         if (XtOb.IsNull()) continue;
144
145         Handle(XtAttributes_AttribGroup) XtAG;
146         Standard_Boolean MayBeName = Standard_False;
147
148         if (XtOb->IsKind(STANDARD_TYPE(XtTopoDS_Topology))) {
149           Handle(XtTopoDS_Topology) XtT = Handle(XtTopoDS_Topology)::DownCast(XtOb);
150           XtAG = XtT->AttributesGroups();
151           MayBeName = Standard_True;
152         }
153         else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Surface))) {
154           Handle(XtGeom_Surface) XtGS = Handle(XtGeom_Surface)::DownCast(XtOb);
155           XtAG = XtGS->AttributesGroups();
156           MayBeName = Standard_True;
157         }
158         else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Curve))) {
159           Handle(XtGeom_Curve) XtGC = Handle(XtGeom_Curve)::DownCast(XtOb);
160           XtAG = XtGC->AttributesGroups();
161           MayBeName = Standard_True;
162         }
163         else if (XtOb->IsKind(STANDARD_TYPE(XtGeom_Point))) {
164           Handle(XtGeom_Point) XtGP = Handle(XtGeom_Point)::DownCast(XtOb);
165           XtAG = XtGP->AttributesGroups();
166           MayBeName = Standard_True;
167         }
168
169         if (MayBeName) {
170           TCollection_AsciiString string;
171           Standard_Boolean IsName = Standard_False;
172           Handle(XtAttributes_Attribute) XtA = Handle(XtAttributes_Attribute)::DownCast(XtAG);
173           if (!XtA.IsNull()) {
174             Handle(XtAttributes_AttribDef) XtAD = XtA->Definition();
175             while (!XtAD.IsNull()) {
176               if (XtAD->TypeID()==8017) {
177                 const Handle(XtData_HArray1OfObject) &HAFV = XtA->Fields();
178                 const Standard_Integer NbFV = HAFV->Length();
179                 for (Standard_Integer j=1; j<=NbFV; j++) {
180                   const Handle(XtData_Object) &FV = HAFV->Value(j);
181                   if(!FV.IsNull() && FV->IsKind(STANDARD_TYPE(XtAttributes_CharValues))) {
182                     string = Handle(XtAttributes_CharValues)::DownCast(FV)->Values();
183                     IsName = Standard_True;
184                   }
185                 }
186               }
187               XtAD = XtAD->Next();
188             }
189           }
190           if (IsName) {
191             // find target shape
192             Handle(Transfer_Binder) binder = TP->Find ( XtOb );
193             if ( binder.IsNull() ) continue;
194             TopoDS_Shape S = TransferBRep::ShapeResult (binder);
195             if ( S.IsNull() ) continue;
196
197             TDF_Label L;
198             TDF_TagSource aTag;
199             L = aTag.NewChild(aFunction->GetNamingEntry());
200             TNaming_Builder tnBuild(L);
201             tnBuild.Generated(S);
202
203             // set a name to the document
204             string.LeftAdjust();
205             string.RightAdjust();
206             TCollection_ExtendedString str ( string );
207             TDataStd_Name::Set ( L, str );
208           }
209         }
210       }
211     }
212   }
213   else {
214     TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
215     StdFail_NotDone::Raise( anError.ToCString() );
216     aResShape.Nullify();
217   }
218
219   if( aResShape.IsNull() ) return 0;
220
221   aFunction->SetValue( aResShape );
222
223   log->SetTouched(Label());
224
225   return 1;
226 }
227
228 //================================================================================
229 /*!
230  * \brief Returns a name of creation operation and names and values of creation parameters
231  */
232 //================================================================================
233
234 bool XTPlugin_ImportDriver::
235 GetCreationInformation( std::string&             theOperationName,
236                         std::vector<GEOM_Param>& theParams )
237 {
238   if( Label().IsNull() ) return 0;
239   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
240
241   XTPlugin_IImport aCI( function );
242   Standard_Integer aType = function->GetType();
243
244   theOperationName = "ImportXT";
245
246   switch ( aType ) {
247   case IMPORT_SHAPE:
248     AddParam( theParams, "File name", aCI.GetFileName() );
249     break;
250   default:
251     return false;
252   }
253   return true;
254 }
255
256 IMPLEMENT_STANDARD_RTTIEXT( XTPlugin_ImportDriver, GEOM_BaseDriver );