Salome HOME
39ca9ae1574f4097a7110856fce8065150429f3d
[plugins/dxfplugin.git] / src / DXFImport / DXFImport.cxx
1 // Copyright (C) 2014  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 #include <DXFPLUGIN_exports.h>
21 #include <DXFPLUGIN_version.h>
22
23 #include <Basics_Utils.hxx>
24
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>
43
44 #ifdef DXF_HASLICENSE
45 #include <DXFPLUGIN_license.h>
46
47 #include <Standard_LicenseError.hxx>
48 #include <OCCLicense_Activate.hxx>
49 #endif // DXF_HASLICENSE
50
51 extern "C"
52 {
53   /*!
54     \brief Get version of the plugin.
55     \return the version of the plugin
56   */
57   DXFPLUGIN_EXPORT
58   int GetVersion()
59   {
60     return DXFPLUGIN_VERSION;
61   }
62
63   /*!
64     \brief Get version of the plugin.
65     \return the string representation of the plugin's version
66   */
67   DXFPLUGIN_EXPORT
68   char* GetVersionStr()
69   {
70     return (char*)DXFPLUGIN_VERSION_STR;
71   }
72
73   /*!
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
80   */
81   DXFPLUGIN_EXPORT
82   TopoDS_Shape Import(const TCollection_AsciiString& theFileName,
83                       const TCollection_AsciiString& /*theFormatName*/,
84                       TCollection_AsciiString&       theError,
85                       const TDF_Label& theShapeLabel)
86   {
87     TopoDS_Shape aResShape;
88     
89 #ifdef DXF_HASLICENSE
90     try {
91       OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
92     }
93     catch (Standard_LicenseError) {
94       return aResShape;
95     }
96 #endif
97     
98     // Set "C" numeric locale to save numbers correctly
99     Kernel_Utils::Localizer loc;
100     
101     DxfControl_Reader aReader;
102     
103     try {
104       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
105       if (status == IFSelect_RetDone) {
106         aReader.TransferRoots();
107         aResShape = aReader.OneShape();
108         
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) {
113           int nbSub1 = 0;
114           TopoDS_Shape currShape;
115           TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
116           for (; It.More(); It.Next()) {
117             nbSub1++;
118             currShape = It.Value();
119           }
120           if (nbSub1 == 1)
121             aResShape = currShape;
122         }
123         Handle(DxfSection_Model) aModel = Handle(DxfSection_Model)::DownCast( aReader.WS()->Model() );
124         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
125         if (!TR.IsNull()) {
126           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
127           
128           Handle(DxfSection_Section) aBlocks = aModel->FindSection("BLOCKS");
129           if (aBlocks.IsNull()) return aResShape;
130           
131           Handle(DxfSection_HSequenceOfObject) anObjects = aBlocks->GetObjects();
132           if (anObjects.IsNull()) return aResShape;
133           
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;
139             
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;
144     
145             // create label and set shape
146             TDF_Label L;
147             TDF_TagSource aTag;
148             L = aTag.NewChild(theShapeLabel);
149             TNaming_Builder tnBuild(L);
150             tnBuild.Generated(aResShape);
151             
152             // set a name
153             TCollection_ExtendedString str(aName->ToCString());
154             TDataStd_Name::Set(L,str);
155           }
156         }
157       }
158       else {
159         theError = "Wrong format of the imported file. Can't import file.";
160         aResShape.Nullify();
161       }
162     }
163     catch (Standard_Failure) {
164       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
165       theError = aFail->GetMessageString();
166       aResShape.Nullify();
167     }
168     return aResShape;
169   }
170 } // end of extern "C"
171