Salome HOME
Merge from rnv/geom_plugin_imp branch
[plugins/acisplugin.git] / src / ACISPlugin_ImportDriver.cxx
diff --git a/src/ACISPlugin_ImportDriver.cxx b/src/ACISPlugin_ImportDriver.cxx
new file mode 100644 (file)
index 0000000..d23aad4
--- /dev/null
@@ -0,0 +1,206 @@
+// Copyright (C) 2014-2015  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
+//
+
+// internal includes
+#include "ACISPlugin_ImportDriver.hxx"
+#include "ACISPlugin_IImport.hxx"
+
+// KERNEL includes
+#include <Basics_Utils.hxx>
+#include <utilities.h>
+
+// GEOM includes
+#include <GEOM_Function.hxx>
+#include <GEOMImpl_Types.hxx>
+
+// OOCT includes
+#include <AcisAttr_AttribGenName.hxx>
+#include <AcisEnt_Attrib.hxx>
+#include <IFSelect_ReturnStatus.hxx>
+#include <Interface_InterfaceModel.hxx>
+#include <SatControl_Reader.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TDataStd_Name.hxx>
+#include <TDF_Label.hxx>
+#include <TNaming_Builder.hxx>
+#include <TopoDS_Shape.hxx>
+#include <TransferBRep.hxx>
+#include <Transfer_Binder.hxx>
+#include <Transfer_TransientProcess.hxx>
+#include <XSControl_TransferReader.hxx>
+#include <XSControl_WorkSession.hxx>
+
+#include <StdFail_NotDone.hxx>
+
+#ifdef ACIS_HASLICENSE
+#include "ACISPlugin_license.h"
+
+#include <OCCLicense_Activate.hxx>
+#include <Standard_LicenseError.hxx>
+#endif // ACIS_HASLICENSE
+
+//=======================================================================
+//function : GetID
+//purpose  :
+//=======================================================================
+const Standard_GUID& ACISPlugin_ImportDriver::GetID()
+{
+  static Standard_GUID aGUID("4e2fea67-84bc-44fe-9a32-ac52a246154c");
+  return aGUID;
+}
+
+//=======================================================================
+//function : ACISPlugin_ImportDriver
+//purpose  :
+//=======================================================================
+ACISPlugin_ImportDriver::ACISPlugin_ImportDriver()
+{
+}
+
+//=======================================================================
+//function : Execute
+//purpose  :
+//=======================================================================
+Standard_Integer ACISPlugin_ImportDriver::Execute( TFunction_Logbook& log ) const
+{
+  if( Label().IsNull() ) return 0;
+  Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
+
+  ACISPlugin_IImport aData( aFunction );
+
+  TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
+
+  MESSAGE("Import ACIS from file " << aFileName);
+  TopoDS_Shape aResShape;
+
+#ifdef ACIS_HASLICENSE
+  try {
+    OCCLicense_Activate( "SAT-R-"OCC_VERSION_STRING, ACIS_READ_LICENSE);
+  }
+  catch (Standard_LicenseError) {
+    return aResShape;
+  }
+#endif // ACIS_HASLICENSE
+
+  // Set "C" numeric locale to save numbers correctly
+  Kernel_Utils::Localizer loc;
+
+  SatControl_Reader aReader;
+
+  IFSelect_ReturnStatus status = aReader.ReadFile( aFileName.ToCString() );
+  if (status == IFSelect_RetDone) {
+    aReader.TransferRoots();
+    aResShape = aReader.OneShape();
+    Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
+    Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
+    if( !TR.IsNull() ) {
+      Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
+      Standard_Integer nb = Model->NbEntities();
+      for (Standard_Integer i = 1; i <= nb; i ++) {
+        Handle(AcisEnt_Attrib) attr = Handle(AcisEnt_Attrib)::DownCast ( Model->Value(i) );
+        if ( attr.IsNull() ) continue; //not only Entity Label (f.18) but Name Property also
+
+        TCollection_AsciiString aName;
+        if( attr->IsKind(STANDARD_TYPE(AcisAttr_AttribGenName)) ) {
+          Handle(AcisAttr_AttribGenName) attrname = Handle(AcisAttr_AttribGenName)::DownCast ( attr );
+          aName = attrname->myNameAttr;
+        }
+        else continue; // no name assigned
+
+        // find target entity
+        Handle(AcisEnt_Entity) ent;
+        do {
+          ent = attr->myEntity;
+          attr = Handle(AcisEnt_Attrib)::DownCast ( ent );
+        } while ( ! attr.IsNull() );
+        if ( ent.IsNull() ) continue; // strange - no normal entity found ?
+
+        // find target shape
+        Handle(Transfer_Binder) binder = TP->Find ( ent );
+        if( binder.IsNull() ) continue;
+          TopoDS_Shape S = TransferBRep::ShapeResult (binder);
+          if( S.IsNull() ) continue;
+
+        // create label and set shape
+        TDF_Label L;
+        TDF_TagSource aTag;
+        L = aTag.NewChild(aFunction->GetNamingEntry());
+        TNaming_Builder tnBuild(L);
+        tnBuild.Generated(S);
+
+        // set a name
+        TCollection_ExtendedString str(aName);
+        TDataStd_Name::Set(L,str);
+      }
+    }
+  }
+  else {
+    TCollection_AsciiString anError = "Wrong format of the imported file. Can't import file.";
+    StdFail_NotDone::Raise( anError.ToCString() );
+    aResShape.Nullify();
+  }
+
+  if( aResShape.IsNull() ) return 0;
+
+  aFunction->SetValue( aResShape );
+
+  log.SetTouched( Label() );
+
+  return 1;
+}
+
+//=======================================================================
+//function : MustExecute
+//purpose  :
+//=======================================================================
+Standard_Boolean ACISPlugin_ImportDriver::MustExecute( const TFunction_Logbook& ) const
+{
+  return Standard_True;
+}
+
+//================================================================================
+/*!
+ * \brief Returns a name of creation operation and names and values of creation parameters
+ */
+//================================================================================
+
+bool ACISPlugin_ImportDriver::
+GetCreationInformation( std::string&             theOperationName,
+                        std::vector<GEOM_Param>& theParams )
+{
+  if( Label().IsNull() ) return 0;
+  Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
+
+  ACISPlugin_IImport aCI( function );
+  Standard_Integer aType = function->GetType();
+
+  theOperationName = "ImportACIS";
+
+  switch ( aType ) {
+  case IMPORT_SHAPE:
+    AddParam( theParams, "File name", aCI.GetFileName() );
+    break;
+  default:
+    return false;
+  }
+  return true;
+}
+
+IMPLEMENT_STANDARD_HANDLE( ACISPlugin_ImportDriver, GEOM_BaseDriver );
+IMPLEMENT_STANDARD_RTTIEXT( ACISPlugin_ImportDriver, GEOM_BaseDriver );