Salome HOME
Merge from rnv/geom_plugin_imp branch
[plugins/dxfplugin.git] / src / DXFPlugin_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 "DXFPlugin_ImportDriver.hxx"
22 #include "DXFPlugin_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 <DxfControl_Reader.hxx>
34 #include <DxfSection_Block.hxx>
35 #include <DxfSection_HSequenceOfObject.hxx>
36 #include <DxfSection_Model.hxx>
37 #include <DxfSection_Section.hxx>
38 #include <IFSelect_ReturnStatus.hxx>
39 #include <TCollection_AsciiString.hxx>
40 #include <TCollection_HAsciiString.hxx>
41 #include <TDF_Label.hxx>
42 #include <TDataStd_Name.hxx>
43 #include <TNaming_Builder.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TransferBRep.hxx>
47 #include <Transfer_Binder.hxx>
48 #include <Transfer_TransientProcess.hxx>
49 #include <XSControl_TransferReader.hxx>
50 #include <XSControl_WorkSession.hxx>
51
52 #include <StdFail_NotDone.hxx>
53
54 #ifdef DXF_HASLICENSE
55 #include "DXFPlugin_license.h"
56
57 #include <OCCLicense_Activate.hxx>
58 #include <Standard_LicenseError.hxx>
59 #endif // DXF_HASLICENSE
60
61 //=======================================================================
62 //function : GetID
63 //purpose  :
64 //=======================================================================
65 const Standard_GUID& DXFPlugin_ImportDriver::GetID()
66 {
67   static Standard_GUID aGUID("dc74f282-3542-419f-a755-c5ab6aba17c5");
68   return aGUID;
69 }
70
71 //=======================================================================
72 //function : DXFPlugin_ImportDriver
73 //purpose  :
74 //=======================================================================
75 DXFPlugin_ImportDriver::DXFPlugin_ImportDriver()
76 {
77 }
78
79 //=======================================================================
80 //function : Execute
81 //purpose  :
82 //=======================================================================
83 Standard_Integer DXFPlugin_ImportDriver::Execute( TFunction_Logbook& log ) const
84 {
85   if( Label().IsNull() ) return 0;
86   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
87
88   DXFPlugin_IImport aData( aFunction );
89
90   TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
91
92   MESSAGE("Import DXF from file " << aFileName);
93
94   TopoDS_Shape aResShape;
95
96 #ifdef DXF_HASLICENSE
97   try {
98     OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
99   }
100   catch (Standard_LicenseError) {
101     return aResShape;
102   }
103 #endif
104
105   // Set "C" numeric locale to save numbers correctly
106   Kernel_Utils::Localizer loc;
107
108   DxfControl_Reader aReader;
109
110   IFSelect_ReturnStatus status = aReader.ReadFile( aFileName.ToCString() );
111   if (status == IFSelect_RetDone) {
112         aReader.TransferRoots();
113         aResShape = aReader.OneShape();
114
115         // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
116         // It should be removed after patching OCCT for bug OCC22436
117         // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
118         if (aResShape.ShapeType() == TopAbs_COMPOUND) {
119           int nbSub1 = 0;
120           TopoDS_Shape currShape;
121           TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
122           for (; It.More(); It.Next()) {
123             nbSub1++;
124             currShape = It.Value();
125           }
126           if (nbSub1 == 1)
127             aResShape = currShape;
128         }
129         Handle(DxfSection_Model) aModel = Handle(DxfSection_Model)::DownCast( aReader.WS()->Model() );
130         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
131         if (!TR.IsNull()) {
132           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
133
134           Handle(DxfSection_Section) aBlocks = aModel->FindSection("BLOCKS");
135           if( !aBlocks.IsNull() ) {
136             Handle(DxfSection_HSequenceOfObject) anObjects = aBlocks->GetObjects();
137             for (Standard_Integer i = 1; i <= anObjects->Length(); i++) {
138               Handle(DxfSection_Block) aBlock = Handle(DxfSection_Block)::DownCast(anObjects->Value(i));
139               if (aBlock.IsNull()) continue;
140               Handle(TCollection_HAsciiString) aName = aBlock->GetBlockName1();
141               if (aName.IsNull()) continue;
142
143               Handle(Transfer_Binder) binder = TP->Find ( aBlock );
144               if (binder.IsNull()) continue;
145               TopoDS_Shape aResShape = TransferBRep::ShapeResult (binder);
146               if (aResShape.IsNull()) continue;
147
148               // create label and set shape
149               TDF_Label L;
150               TDF_TagSource aTag;
151               L = aTag.NewChild(aFunction->GetNamingEntry());
152               TNaming_Builder tnBuild(L);
153               tnBuild.Generated(aResShape);
154
155               // set a name
156               TCollection_ExtendedString str( aName->ToCString() );
157               TDataStd_Name::Set(L,str);
158             }
159           }
160         }
161   }
162   else {
163     TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
164     StdFail_NotDone::Raise( anError.ToCString() );
165     aResShape.Nullify();
166   }
167
168   if( aResShape.IsNull() ) return 0;
169
170   aFunction->SetValue( aResShape );
171
172   log.SetTouched( Label() );
173
174   return 1;
175 }
176
177 //=======================================================================
178 //function : MustExecute
179 //purpose  :
180 //=======================================================================
181 Standard_Boolean DXFPlugin_ImportDriver::MustExecute( const TFunction_Logbook& ) const
182 {
183   return Standard_True;
184 }
185
186 //================================================================================
187 /*!
188  * \brief Returns a name of creation operation and names and values of creation parameters
189  */
190 //================================================================================
191
192 bool DXFPlugin_ImportDriver::
193 GetCreationInformation( std::string&             theOperationName,
194                         std::vector<GEOM_Param>& theParams )
195 {
196   if( Label().IsNull() ) return 0;
197   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
198
199   DXFPlugin_IImport aCI( function );
200   Standard_Integer aType = function->GetType();
201
202   theOperationName = "ImportDXF";
203
204   switch ( aType ) {
205   case IMPORT_SHAPE:
206     AddParam( theParams, "File name", aCI.GetFileName() );
207     break;
208   default:
209     return false;
210   }
211   return true;
212 }
213
214 IMPLEMENT_STANDARD_HANDLE( DXFPlugin_ImportDriver, GEOM_BaseDriver );
215 IMPLEMENT_STANDARD_RTTIEXT( DXFPlugin_ImportDriver, GEOM_BaseDriver );