1 // Copyright (C) 2014-2015 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
20 #include <DXFPLUGIN_exports.h>
21 #include <DXFPLUGIN_version.h>
23 #include <Basics_Utils.hxx>
25 #include <DxfControl_Reader.hxx>
26 #include <DxfSection_Block.hxx>
27 #include <DxfSection_HSequenceOfObject.hxx>
28 #include <DxfSection_Model.hxx>
29 #include <DxfSection_Section.hxx>
30 #include <IFSelect_ReturnStatus.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_HAsciiString.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TNaming_Builder.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Iterator.hxx>
38 #include <TransferBRep.hxx>
39 #include <Transfer_Binder.hxx>
40 #include <Transfer_TransientProcess.hxx>
41 #include <XSControl_TransferReader.hxx>
42 #include <XSControl_WorkSession.hxx>
45 #include <DXFPLUGIN_license.h>
47 #include <Standard_LicenseError.hxx>
48 #include <OCCLicense_Activate.hxx>
49 #endif // DXF_HASLICENSE
54 \brief Get version of the plugin.
55 \return the version of the plugin
60 return DXFPLUGIN_VERSION;
64 \brief Get version of the plugin.
65 \return the string representation of the plugin's version
70 return (char*)DXFPLUGIN_VERSION_STR;
74 \brief Import shape from the DXF format.
75 \param theFileName file path
76 \param theFormatName file format signature
77 \param theError error description, if there's any, is returned via this parameter
78 \param theShapeLabel a label in the CAF tree where shape attributes are set to
79 \return imported shape
82 TopoDS_Shape Import(const TCollection_AsciiString& theFileName,
83 const TCollection_AsciiString& /*theFormatName*/,
84 TCollection_AsciiString& theError,
85 const TDF_Label& theShapeLabel)
87 TopoDS_Shape aResShape;
91 OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
93 catch (Standard_LicenseError) {
98 // Set "C" numeric locale to save numbers correctly
99 Kernel_Utils::Localizer loc;
101 DxfControl_Reader aReader;
104 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
105 if (status == IFSelect_RetDone) {
106 aReader.TransferRoots();
107 aResShape = aReader.OneShape();
109 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
110 // It should be removed after patching OCCT for bug OCC22436
111 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
112 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
114 TopoDS_Shape currShape;
115 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
116 for (; It.More(); It.Next()) {
118 currShape = It.Value();
121 aResShape = currShape;
123 Handle(DxfSection_Model) aModel = Handle(DxfSection_Model)::DownCast( aReader.WS()->Model() );
124 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
126 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
128 Handle(DxfSection_Section) aBlocks = aModel->FindSection("BLOCKS");
129 if (aBlocks.IsNull()) return aResShape;
131 Handle(DxfSection_HSequenceOfObject) anObjects = aBlocks->GetObjects();
132 if (anObjects.IsNull()) return aResShape;
134 for (Standard_Integer i = 1; i <= anObjects->Length(); i++) {
135 Handle(DxfSection_Block) aBlock = Handle(DxfSection_Block)::DownCast(anObjects->Value(i));
136 if (aBlock.IsNull()) continue;
137 Handle(TCollection_HAsciiString) aName = aBlock->GetBlockName1();
138 if (aName.IsNull()) continue;
140 Handle(Transfer_Binder) binder = TP->Find ( aBlock );
141 if (binder.IsNull()) continue;
142 TopoDS_Shape aResShape = TransferBRep::ShapeResult (binder);
143 if (aResShape.IsNull()) continue;
145 // create label and set shape
148 L = aTag.NewChild(theShapeLabel);
149 TNaming_Builder tnBuild(L);
150 tnBuild.Generated(aResShape);
153 TCollection_ExtendedString str(aName->ToCString());
154 TDataStd_Name::Set(L,str);
159 theError = "Wrong format of the imported file. Can't import file.";
163 catch (Standard_Failure) {
164 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
165 theError = aFail->GetMessageString();
170 } // end of extern "C"