Salome HOME
Implementation of DXFPLUGIN as a GEOM plugin (removed and renamed files)
[plugins/dxfplugin.git] / src / DXFPlugin_ImportDriver.cxx
diff --git a/src/DXFPlugin_ImportDriver.cxx b/src/DXFPlugin_ImportDriver.cxx
new file mode 100644 (file)
index 0000000..39ca9ae
--- /dev/null
@@ -0,0 +1,171 @@
+// Copyright (C) 2014  OPEN CASCADE
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#include <DXFPLUGIN_exports.h>
+#include <DXFPLUGIN_version.h>
+
+#include <Basics_Utils.hxx>
+
+#include <DxfControl_Reader.hxx>
+#include <DxfSection_Block.hxx>
+#include <DxfSection_HSequenceOfObject.hxx>
+#include <DxfSection_Model.hxx>
+#include <DxfSection_Section.hxx>
+#include <IFSelect_ReturnStatus.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TCollection_HAsciiString.hxx>
+#include <TDF_Label.hxx>
+#include <TDataStd_Name.hxx>
+#include <TNaming_Builder.hxx>
+#include <TopoDS_Shape.hxx>
+#include <TopoDS_Iterator.hxx>
+#include <TransferBRep.hxx>
+#include <Transfer_Binder.hxx>
+#include <Transfer_TransientProcess.hxx>
+#include <XSControl_TransferReader.hxx>
+#include <XSControl_WorkSession.hxx>
+
+#ifdef DXF_HASLICENSE
+#include <DXFPLUGIN_license.h>
+
+#include <Standard_LicenseError.hxx>
+#include <OCCLicense_Activate.hxx>
+#endif // DXF_HASLICENSE
+
+extern "C"
+{
+  /*!
+    \brief Get version of the plugin.
+    \return the version of the plugin
+  */
+  DXFPLUGIN_EXPORT
+  int GetVersion()
+  {
+    return DXFPLUGIN_VERSION;
+  }
+
+  /*!
+    \brief Get version of the plugin.
+    \return the string representation of the plugin's version
+  */
+  DXFPLUGIN_EXPORT
+  char* GetVersionStr()
+  {
+    return (char*)DXFPLUGIN_VERSION_STR;
+  }
+
+  /*!
+    \brief Import shape from the DXF format.
+    \param theFileName file path
+    \param theFormatName file format signature
+    \param theError error description, if there's any, is returned via this parameter
+    \param theShapeLabel a label in the CAF tree where shape attributes are set to
+    \return imported shape
+  */
+  DXFPLUGIN_EXPORT
+  TopoDS_Shape Import(const TCollection_AsciiString& theFileName,
+                     const TCollection_AsciiString& /*theFormatName*/,
+                     TCollection_AsciiString&       theError,
+                     const TDF_Label& theShapeLabel)
+  {
+    TopoDS_Shape aResShape;
+    
+#ifdef DXF_HASLICENSE
+    try {
+      OCCLicense_Activate( "DXF-R-"OCC_VERSION_STRING, DXF_READ_LICENSE);
+    }
+    catch (Standard_LicenseError) {
+      return aResShape;
+    }
+#endif
+    
+    // Set "C" numeric locale to save numbers correctly
+    Kernel_Utils::Localizer loc;
+    
+    DxfControl_Reader aReader;
+    
+    try {
+      IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
+      if (status == IFSelect_RetDone) {
+       aReader.TransferRoots();
+       aResShape = aReader.OneShape();
+       
+       // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
+       // It should be removed after patching OCCT for bug OCC22436
+       // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
+       if (aResShape.ShapeType() == TopAbs_COMPOUND) {
+         int nbSub1 = 0;
+         TopoDS_Shape currShape;
+         TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
+         for (; It.More(); It.Next()) {
+           nbSub1++;
+           currShape = It.Value();
+         }
+         if (nbSub1 == 1)
+           aResShape = currShape;
+       }
+       Handle(DxfSection_Model) aModel = Handle(DxfSection_Model)::DownCast( aReader.WS()->Model() );
+       Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
+       if (!TR.IsNull()) {
+         Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
+         
+         Handle(DxfSection_Section) aBlocks = aModel->FindSection("BLOCKS");
+         if (aBlocks.IsNull()) return aResShape;
+         
+         Handle(DxfSection_HSequenceOfObject) anObjects = aBlocks->GetObjects();
+         if (anObjects.IsNull()) return aResShape;
+         
+         for (Standard_Integer i = 1; i <= anObjects->Length(); i++) {
+           Handle(DxfSection_Block) aBlock = Handle(DxfSection_Block)::DownCast(anObjects->Value(i));
+           if (aBlock.IsNull()) continue;
+           Handle(TCollection_HAsciiString) aName = aBlock->GetBlockName1();
+           if (aName.IsNull()) continue;
+           
+           Handle(Transfer_Binder) binder = TP->Find ( aBlock );
+           if (binder.IsNull()) continue;
+           TopoDS_Shape aResShape = TransferBRep::ShapeResult (binder);
+           if (aResShape.IsNull()) continue;
+    
+           // create label and set shape
+           TDF_Label L;
+           TDF_TagSource aTag;
+           L = aTag.NewChild(theShapeLabel);
+           TNaming_Builder tnBuild(L);
+           tnBuild.Generated(aResShape);
+           
+           // set a name
+           TCollection_ExtendedString str(aName->ToCString());
+           TDataStd_Name::Set(L,str);
+         }
+       }
+      }
+      else {
+       theError = "Wrong format of the imported file. Can't import file.";
+       aResShape.Nullify();
+      }
+    }
+    catch (Standard_Failure) {
+      Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+      theError = aFail->GetMessageString();
+      aResShape.Nullify();
+    }
+    return aResShape;
+  }
+} // end of extern "C"
+