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"
25 #include <Basics_Utils.hxx>
26 #include <utilities.h>
29 #include <GEOM_Function.hxx>
30 #include <GEOMImpl_Types.hxx>
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>
52 #include <StdFail_NotDone.hxx>
55 #include "DXFPlugin_license.h"
57 #include <OCCLicense_Activate.hxx>
58 #include <Standard_LicenseError.hxx>
59 #endif // DXF_HASLICENSE
61 //=======================================================================
64 //=======================================================================
65 const Standard_GUID& DXFPlugin_ImportDriver::GetID()
67 static Standard_GUID aGUID("dc74f282-3542-419f-a755-c5ab6aba17c5");
71 //=======================================================================
72 //function : DXFPlugin_ImportDriver
74 //=======================================================================
75 DXFPlugin_ImportDriver::DXFPlugin_ImportDriver()
79 //=======================================================================
82 //=======================================================================
83 Standard_Integer DXFPlugin_ImportDriver::Execute( TFunction_Logbook& log ) const
85 if( Label().IsNull() ) return 0;
86 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
88 DXFPlugin_IImport aData( aFunction );
90 TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
92 MESSAGE("Import DXF from file " << aFileName);
94 TopoDS_Shape aResShape;
98 OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
100 catch (Standard_LicenseError) {
105 // Set "C" numeric locale to save numbers correctly
106 Kernel_Utils::Localizer loc;
108 DxfControl_Reader aReader;
110 IFSelect_ReturnStatus status = aReader.ReadFile( aFileName.ToCString() );
111 if (status == IFSelect_RetDone) {
112 aReader.TransferRoots();
113 aResShape = aReader.OneShape();
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) {
120 TopoDS_Shape currShape;
121 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
122 for (; It.More(); It.Next()) {
124 currShape = It.Value();
127 aResShape = currShape;
129 Handle(DxfSection_Model) aModel = Handle(DxfSection_Model)::DownCast( aReader.WS()->Model() );
130 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
132 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
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;
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;
148 // create label and set shape
151 L = aTag.NewChild(aFunction->GetNamingEntry());
152 TNaming_Builder tnBuild(L);
153 tnBuild.Generated(aResShape);
156 TCollection_ExtendedString str( aName->ToCString() );
157 TDataStd_Name::Set(L,str);
163 TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
164 StdFail_NotDone::Raise( anError.ToCString() );
168 if( aResShape.IsNull() ) return 0;
170 aFunction->SetValue( aResShape );
172 log.SetTouched( Label() );
177 //=======================================================================
178 //function : MustExecute
180 //=======================================================================
181 Standard_Boolean DXFPlugin_ImportDriver::MustExecute( const TFunction_Logbook& ) const
183 return Standard_True;
186 //================================================================================
188 * \brief Returns a name of creation operation and names and values of creation parameters
190 //================================================================================
192 bool DXFPlugin_ImportDriver::
193 GetCreationInformation( std::string& theOperationName,
194 std::vector<GEOM_Param>& theParams )
196 if( Label().IsNull() ) return 0;
197 Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
199 DXFPlugin_IImport aCI( function );
200 Standard_Integer aType = function->GetType();
202 theOperationName = "ImportDXF";
206 AddParam( theParams, "File name", aCI.GetFileName() );
214 IMPLEMENT_STANDARD_HANDLE( DXFPlugin_ImportDriver, GEOM_BaseDriver );
215 IMPLEMENT_STANDARD_RTTIEXT( DXFPlugin_ImportDriver, GEOM_BaseDriver );