1 // Copyright (C) 2014 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "DXFPlugin_ImportDriver.hxx"
22 #include "DXFPlugin_IImport.hxx"
23 #include "DXFPlugin_Engine.hxx"
26 #include <Basics_Utils.hxx>
27 #include <utilities.h>
30 #include <GEOM_Function.hxx>
31 #include <GEOMImpl_Types.hxx>
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>
53 #include <StdFail_NotDone.hxx>
56 #include "DXFPlugin_license.h"
58 #include <OCCLicense_Activate.hxx>
59 #include <Standard_LicenseError.hxx>
60 #endif // DXF_HASLICENSE
62 //=======================================================================
65 //=======================================================================
66 const Standard_GUID& DXFPlugin_ImportDriver::GetID()
68 static Standard_GUID aGUID("dc74f282-3542-419f-a755-c5ab6aba17c5");
72 //=======================================================================
73 //function : DXFPlugin_ImportDriver
75 //=======================================================================
76 DXFPlugin_ImportDriver::DXFPlugin_ImportDriver()
80 //=======================================================================
83 //=======================================================================
84 Standard_Integer DXFPlugin_ImportDriver::Execute( TFunction_Logbook& log ) const
86 if( Label().IsNull() ) return 0;
87 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
89 DXFPlugin_IImport aData( aFunction );
91 TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
93 MESSAGE("Import DXF from file " << aFileName);
95 TopoDS_Shape aResShape;
99 OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
101 catch (Standard_LicenseError) {
106 // Set "C" numeric locale to save numbers correctly
107 Kernel_Utils::Localizer loc;
109 DxfControl_Reader aReader;
111 IFSelect_ReturnStatus status = aReader.ReadFile( aFileName.ToCString() );
112 if (status == IFSelect_RetDone) {
113 aReader.TransferRoots();
114 aResShape = aReader.OneShape();
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) {
121 TopoDS_Shape currShape;
122 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
123 for (; It.More(); It.Next()) {
125 currShape = It.Value();
128 aResShape = currShape;
130 Handle(DxfSection_Model) aModel = Handle(DxfSection_Model)::DownCast( aReader.WS()->Model() );
131 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
133 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
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;
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;
149 // create label and set shape
152 L = aTag.NewChild(aFunction->GetNamingEntry());
153 TNaming_Builder tnBuild(L);
154 tnBuild.Generated(aResShape);
157 TCollection_ExtendedString str( aName->ToCString() );
158 TDataStd_Name::Set(L,str);
164 TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
165 StdFail_NotDone::Raise( anError.ToCString() );
169 if( aResShape.IsNull() ) return 0;
171 aFunction->SetValue( aResShape );
173 log.SetTouched( Label() );
178 //=======================================================================
179 //function : MustExecute
181 //=======================================================================
182 Standard_Boolean DXFPlugin_ImportDriver::MustExecute( const TFunction_Logbook& ) const
184 return Standard_True;
187 //================================================================================
189 * \brief Returns a name of creation operation and names and values of creation parameters
191 //================================================================================
193 bool DXFPlugin_ImportDriver::
194 GetCreationInformation( std::string& theOperationName,
195 std::vector<GEOM_Param>& theParams )
197 if( Label().IsNull() ) return 0;
198 Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
200 DXFPlugin_IImport aCI( function );
201 Standard_Integer aType = function->GetType();
203 theOperationName = "ImportDXF";
207 AddParam( theParams, "File name", aCI.GetFileName() );
215 IMPLEMENT_STANDARD_HANDLE( DXFPlugin_ImportDriver, GEOM_BaseDriver );
216 IMPLEMENT_STANDARD_RTTIEXT( DXFPlugin_ImportDriver, GEOM_BaseDriver );