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