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