]> SALOME platform Git repositories - plugins/acisplugin.git/blob - src/ACISPlugin_ImportDriver.cxx
Salome HOME
Merge from rnv/geom_plugin_imp branch
[plugins/acisplugin.git] / src / ACISPlugin_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 "ACISPlugin_ImportDriver.hxx"
22 #include "ACISPlugin_IImport.hxx"
23
24 // KERNEL includes
25 #include <Basics_Utils.hxx>
26 #include <utilities.h>
27
28 // GEOM includes
29 #include <GEOM_Function.hxx>
30 #include <GEOMImpl_Types.hxx>
31
32 // OOCT includes
33 #include <AcisAttr_AttribGenName.hxx>
34 #include <AcisEnt_Attrib.hxx>
35 #include <IFSelect_ReturnStatus.hxx>
36 #include <Interface_InterfaceModel.hxx>
37 #include <SatControl_Reader.hxx>
38 #include <TCollection_AsciiString.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <TDF_Label.hxx>
41 #include <TNaming_Builder.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TransferBRep.hxx>
44 #include <Transfer_Binder.hxx>
45 #include <Transfer_TransientProcess.hxx>
46 #include <XSControl_TransferReader.hxx>
47 #include <XSControl_WorkSession.hxx>
48
49 #include <StdFail_NotDone.hxx>
50
51 #ifdef ACIS_HASLICENSE
52 #include "ACISPlugin_license.h"
53
54 #include <OCCLicense_Activate.hxx>
55 #include <Standard_LicenseError.hxx>
56 #endif // ACIS_HASLICENSE
57
58 //=======================================================================
59 //function : GetID
60 //purpose  :
61 //=======================================================================
62 const Standard_GUID& ACISPlugin_ImportDriver::GetID()
63 {
64   static Standard_GUID aGUID("4e2fea67-84bc-44fe-9a32-ac52a246154c");
65   return aGUID;
66 }
67
68 //=======================================================================
69 //function : ACISPlugin_ImportDriver
70 //purpose  :
71 //=======================================================================
72 ACISPlugin_ImportDriver::ACISPlugin_ImportDriver()
73 {
74 }
75
76 //=======================================================================
77 //function : Execute
78 //purpose  :
79 //=======================================================================
80 Standard_Integer ACISPlugin_ImportDriver::Execute( TFunction_Logbook& log ) const
81 {
82   if( Label().IsNull() ) return 0;
83   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
84
85   ACISPlugin_IImport aData( aFunction );
86
87   TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
88
89   MESSAGE("Import ACIS from file " << aFileName);
90   TopoDS_Shape aResShape;
91
92 #ifdef ACIS_HASLICENSE
93   try {
94     OCCLicense_Activate( "SAT-R-"OCC_VERSION_STRING, ACIS_READ_LICENSE);
95   }
96   catch (Standard_LicenseError) {
97     return aResShape;
98   }
99 #endif // ACIS_HASLICENSE
100
101   // Set "C" numeric locale to save numbers correctly
102   Kernel_Utils::Localizer loc;
103
104   SatControl_Reader aReader;
105
106   IFSelect_ReturnStatus status = aReader.ReadFile( aFileName.ToCString() );
107   if (status == IFSelect_RetDone) {
108     aReader.TransferRoots();
109     aResShape = aReader.OneShape();
110     Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
111     Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
112     if( !TR.IsNull() ) {
113       Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
114       Standard_Integer nb = Model->NbEntities();
115       for (Standard_Integer i = 1; i <= nb; i ++) {
116         Handle(AcisEnt_Attrib) attr = Handle(AcisEnt_Attrib)::DownCast ( Model->Value(i) );
117         if ( attr.IsNull() ) continue; //not only Entity Label (f.18) but Name Property also
118
119         TCollection_AsciiString aName;
120         if( attr->IsKind(STANDARD_TYPE(AcisAttr_AttribGenName)) ) {
121           Handle(AcisAttr_AttribGenName) attrname = Handle(AcisAttr_AttribGenName)::DownCast ( attr );
122           aName = attrname->myNameAttr;
123         }
124         else continue; // no name assigned
125
126         // find target entity
127         Handle(AcisEnt_Entity) ent;
128         do {
129           ent = attr->myEntity;
130           attr = Handle(AcisEnt_Attrib)::DownCast ( ent );
131         } while ( ! attr.IsNull() );
132         if ( ent.IsNull() ) continue; // strange - no normal entity found ?
133
134         // find target shape
135         Handle(Transfer_Binder) binder = TP->Find ( ent );
136         if( binder.IsNull() ) continue;
137           TopoDS_Shape S = TransferBRep::ShapeResult (binder);
138           if( S.IsNull() ) continue;
139
140         // create label and set shape
141         TDF_Label L;
142         TDF_TagSource aTag;
143         L = aTag.NewChild(aFunction->GetNamingEntry());
144         TNaming_Builder tnBuild(L);
145         tnBuild.Generated(S);
146
147         // set a name
148         TCollection_ExtendedString str(aName);
149         TDataStd_Name::Set(L,str);
150       }
151     }
152   }
153   else {
154     TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
155     StdFail_NotDone::Raise( anError.ToCString() );
156     aResShape.Nullify();
157   }
158
159   if( aResShape.IsNull() ) return 0;
160
161   aFunction->SetValue( aResShape );
162
163   log.SetTouched( Label() );
164
165   return 1;
166 }
167
168 //=======================================================================
169 //function : MustExecute
170 //purpose  :
171 //=======================================================================
172 Standard_Boolean ACISPlugin_ImportDriver::MustExecute( const TFunction_Logbook& ) const
173 {
174   return Standard_True;
175 }
176
177 //================================================================================
178 /*!
179  * \brief Returns a name of creation operation and names and values of creation parameters
180  */
181 //================================================================================
182
183 bool ACISPlugin_ImportDriver::
184 GetCreationInformation( std::string&             theOperationName,
185                         std::vector<GEOM_Param>& theParams )
186 {
187   if( Label().IsNull() ) return 0;
188   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
189
190   ACISPlugin_IImport aCI( function );
191   Standard_Integer aType = function->GetType();
192
193   theOperationName = "ImportACIS";
194
195   switch ( aType ) {
196   case IMPORT_SHAPE:
197     AddParam( theParams, "File name", aCI.GetFileName() );
198     break;
199   default:
200     return false;
201   }
202   return true;
203 }
204
205 IMPLEMENT_STANDARD_HANDLE( ACISPlugin_ImportDriver, GEOM_BaseDriver );
206 IMPLEMENT_STANDARD_RTTIEXT( ACISPlugin_ImportDriver, GEOM_BaseDriver );