geom/src/GEOMImpl/GEOMImpl_IInsertOperations.cxx

757 lines
24 KiB
C++
Raw Normal View History

2012-08-09 13:58:02 +06:00
// Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// 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.
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// 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.
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// 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
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
2005-08-11 10:43:57 +06:00
#include <Standard_Stream.hxx>
2004-12-01 15:39:14 +05:00
2005-08-11 10:43:57 +06:00
#include <GEOMImpl_IInsertOperations.hxx>
2004-12-01 15:39:14 +05:00
2012-08-09 13:58:02 +06:00
#include <GEOMImpl_CopyDriver.hxx>
#include <GEOMImpl_ExportDriver.hxx>
#include <GEOMImpl_ImportDriver.hxx>
#include <GEOMImpl_ICopy.hxx>
#include <GEOMImpl_IImportExport.hxx>
#include <GEOMImpl_Types.hxx>
#include <GEOM_Function.hxx>
#include <GEOM_PythonDump.hxx>
#include <Basics_OCCTVersion.hxx>
2004-12-01 15:39:14 +05:00
#include "utilities.h"
2005-08-11 10:43:57 +06:00
#include <OpUtil.hxx>
#include <Utils_ExceptHandlers.hxx>
2004-12-01 15:39:14 +05:00
#include <TFunction_DriverTable.hxx>
#include <TFunction_Driver.hxx>
#include <TFunction_Logbook.hxx>
#include <TDF_Tool.hxx>
2012-08-09 13:58:02 +06:00
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
2012-10-08 17:16:36 +06:00
#include <BRep_Builder.hxx>
2012-08-09 13:58:02 +06:00
#include <BRep_Tool.hxx>
2012-10-08 17:16:36 +06:00
#include <BRepTools.hxx>
2012-08-09 13:58:02 +06:00
#include <gp_Pnt.hxx>
2004-12-01 15:39:14 +05:00
2012-08-09 13:58:02 +06:00
#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
#include <TColStd_HArray1OfByte.hxx>
#else
#include <TDataStd_HArray1OfByte.hxx>
#endif
2004-12-01 15:39:14 +05:00
#include <Standard_Failure.hxx>
2004-12-01 15:39:14 +05:00
#include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
//=============================================================================
/*!
2012-10-08 17:16:36 +06:00
* constructor
2004-12-01 15:39:14 +05:00
*/
//=============================================================================
GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
: GEOM_IOperations(theEngine, theDocID)
{
MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
}
//=============================================================================
/*!
* destructor
*/
//=============================================================================
GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
{
MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
}
//=============================================================================
/*!
* MakeCopy
*/
//=============================================================================
2012-10-08 17:16:36 +06:00
Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
2004-12-01 15:39:14 +05:00
{
SetErrorCode(KO);
if (theOriginal.IsNull()) return NULL;
//Add a new Copy object
Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
//Add a Copy function for creation a copy object
Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
//Check if the function is set correctly
if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
GEOMImpl_ICopy aCI(aFunction);
Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
if (aRefFunction.IsNull()) return NULL;
aCI.SetOriginal(aRefFunction);
//Compute the Copy value
try {
2012-08-09 13:58:02 +06:00
#if OCC_VERSION_LARGE > 0x06010000
OCC_CATCH_SIGNALS;
#endif
2004-12-01 15:39:14 +05:00
if (!GetSolver()->ComputeFunction(aFunction)) {
SetErrorCode("Copy driver failed");
return NULL;
}
}
catch (Standard_Failure) {
Handle(Standard_Failure) aFail = Standard_Failure::Caught();
SetErrorCode(aFail->GetMessageString());
return NULL;
}
//Make a Python command
GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
2004-12-01 15:39:14 +05:00
SetErrorCode(OK);
return aCopy;
}
//=============================================================================
/*!
* Export
*/
//=============================================================================
void GEOMImpl_IInsertOperations::Export
(const Handle(GEOM_Object) theOriginal,
2012-08-09 13:58:02 +06:00
const TCollection_AsciiString& theFileName,
const TCollection_AsciiString& theFormatName)
2004-12-01 15:39:14 +05:00
{
SetErrorCode(KO);
if (theOriginal.IsNull()) return;
Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
2012-08-09 13:58:02 +06:00
//Add a new result object
Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
2004-12-01 15:39:14 +05:00
//Add an Export function
2012-08-09 13:58:02 +06:00
Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
2004-12-01 15:39:14 +05:00
if (aFunction.IsNull()) return;
//Check if the function is set correctly
if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
Handle(TCollection_HAsciiString) aHLibName;
if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
2004-12-01 15:39:14 +05:00
return;
}
TCollection_AsciiString aLibName = aHLibName->String();
//Set parameters
GEOMImpl_IImportExport aCI (aFunction);
aCI.SetOriginal(aRefFunction);
aCI.SetFileName(theFileName);
aCI.SetFormatName(theFormatName);
2004-12-01 15:39:14 +05:00
aCI.SetPluginName(aLibName);
//Perform the Export
try {
2012-08-09 13:58:02 +06:00
#if OCC_VERSION_LARGE > 0x06010000
OCC_CATCH_SIGNALS;
#endif
2004-12-01 15:39:14 +05:00
if (!GetSolver()->ComputeFunction(aFunction)) {
2009-02-13 17:16:39 +05:00
SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
2004-12-01 15:39:14 +05:00
return;
}
}
catch (Standard_Failure) {
Handle(Standard_Failure) aFail = Standard_Failure::Caught();
SetErrorCode(aFail->GetMessageString());
return;
}
//Make a Python command
GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
<< theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
2004-12-01 15:39:14 +05:00
SetErrorCode(OK);
}
//=============================================================================
/*!
* Import
*/
//=============================================================================
Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
(const TCollection_AsciiString& theFileName,
2012-08-09 13:58:02 +06:00
const TCollection_AsciiString& theFormatName)
2004-12-01 15:39:14 +05:00
{
SetErrorCode(KO);
if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
2004-12-01 15:39:14 +05:00
//Add a new result object
Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
//Add an Import function
Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
if (aFunction.IsNull()) return result;
//Check if the function is set correctly
if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
Handle(TCollection_HAsciiString) aHLibName;
2013-02-12 17:35:16 +06:00
if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
2004-12-01 15:39:14 +05:00
return result;
}
TCollection_AsciiString aLibName = aHLibName->String();
//Set parameters
GEOMImpl_IImportExport aCI (aFunction);
aCI.SetFileName(theFileName);
aCI.SetFormatName(theFormatName);
2004-12-01 15:39:14 +05:00
aCI.SetPluginName(aLibName);
//Perform the Import
try {
2012-08-09 13:58:02 +06:00
#if OCC_VERSION_LARGE > 0x06010000
OCC_CATCH_SIGNALS;
#endif
2004-12-01 15:39:14 +05:00
if (!GetSolver()->ComputeFunction(aFunction)) {
SetErrorCode("Import driver failed");
return NULL;
}
}
catch (Standard_Failure) {
Handle(Standard_Failure) aFail = Standard_Failure::Caught();
SetErrorCode(aFail->GetMessageString());
return NULL;
}
//Make a Python command
2012-12-13 14:40:36 +06:00
if (theFormatName != "IGES_UNIT") {
GEOM::TPythonDump pd (aFunction);
if (theFormatName == "BREP")
pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
else if (theFormatName == "IGES")
pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
else if (theFormatName == "IGES_SCALE")
pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
else if (theFormatName == "STEP")
pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
else if (theFormatName == "STEP_SCALE")
pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
else {
pd << result << " = geompy.ImportFile(\""
<< theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
}
2012-08-09 13:58:02 +06:00
}
2004-12-01 15:39:14 +05:00
SetErrorCode(OK);
2012-08-09 13:58:02 +06:00
2012-10-08 17:16:36 +06:00
// OLD CODE: begin
if (theFormatName == "IGES_UNIT") {
2012-08-09 13:58:02 +06:00
TopoDS_Shape S = aFunction->GetValue();
TopoDS_Vertex V = TopoDS::Vertex(S);
gp_Pnt P = BRep_Tool::Pnt(V);
double scale = P.X();
TCollection_AsciiString aUnitName = "UNIT_M";
2012-12-13 14:40:36 +06:00
if (fabs(scale-0.01) < 1.e-6)
2012-08-09 13:58:02 +06:00
aUnitName = "UNIT_CM";
2012-12-13 14:40:36 +06:00
else if (fabs(scale-0.001) < 1.e-6)
2012-08-09 13:58:02 +06:00
aUnitName = "UNIT_MM";
//cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
SetErrorCode(aUnitName);
}
2012-10-08 17:16:36 +06:00
// OLD CODE: end
2012-08-09 13:58:02 +06:00
2004-12-01 15:39:14 +05:00
return result;
}
2012-10-08 17:16:36 +06:00
//=============================================================================
/*!
* ReadValue
*/
//=============================================================================
TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
(const TCollection_AsciiString& theFileName,
const TCollection_AsciiString& theFormatName,
const TCollection_AsciiString& theParameterName)
{
SetErrorCode(KO);
TCollection_AsciiString aValue, anError;
if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
Handle(TCollection_HAsciiString) aHLibName;
2013-02-12 17:35:16 +06:00
if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
2012-10-08 17:16:36 +06:00
return aValue;
}
TCollection_AsciiString aLibName = aHLibName->String();
aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
if (anError.IsEmpty())
SetErrorCode(OK);
else
SetErrorCode(anError.ToCString());
return aValue;
}
2004-12-01 15:39:14 +05:00
//=============================================================================
/*!
* ImportTranslators
*/
//=============================================================================
Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
(Handle(TColStd_HSequenceOfAsciiString)& theFormats,
2012-08-09 13:58:02 +06:00
Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
2004-12-01 15:39:14 +05:00
{
if (theFormats.IsNull())
theFormats = new TColStd_HSequenceOfAsciiString;
else
theFormats->Clear();
if (thePatterns.IsNull())
thePatterns = new TColStd_HSequenceOfAsciiString;
else
thePatterns->Clear();
if (!InitResMgr()) return Standard_False;
// Read Import formats list from install directory
2004-12-01 15:39:14 +05:00
if (myResMgr->Find("Import")) {
TCollection_AsciiString aFormats (myResMgr->Value("Import"));
TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
int i = 1;
for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
theFormats->Append(aToken);
}
}
// Read Import formats from user directory
if (myResMgrUser->Find("Import")) {
TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
int i = 1;
for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
int aLenFormats = theFormats->Length();
bool isFound = false;
for(int aInd=1;aInd<=aLenFormats;aInd++){
2012-08-09 13:58:02 +06:00
if( theFormats->Value(aInd) == aToken){
isFound = true;
break;
}
}
if(!isFound)
2012-08-09 13:58:02 +06:00
theFormats->Append(aToken);
}
}
2004-12-01 15:39:14 +05:00
// Read Patterns for each supported format
int j = 1, len = theFormats->Length();
for (; j <= len; j++) {
TCollection_AsciiString aKey, aPattern;
aKey = theFormats->Value(j) + ".ImportPattern";
2004-12-01 15:39:14 +05:00
if (myResMgr->Find(aKey.ToCString()))
aPattern = myResMgr->Value(aKey.ToCString());
else if(myResMgrUser->Find(aKey.ToCString()))
aPattern = myResMgrUser->Value(aKey.ToCString());
2004-12-01 15:39:14 +05:00
else {
aKey = theFormats->Value(j) + ".Pattern";
if (myResMgr->Find(aKey.ToCString()))
aPattern = myResMgr->Value(aKey.ToCString());
else if(myResMgrUser->Find(aKey.ToCString()))
2012-08-09 13:58:02 +06:00
aPattern = myResMgrUser->Value(aKey.ToCString());
else {
aPattern = theFormats->Value(j);
aPattern += " Files ( *.* )";
}
2004-12-01 15:39:14 +05:00
}
thePatterns->Append(aPattern);
}
return (!theFormats->IsEmpty());
}
//=============================================================================
/*!
* ExportTranslators
*/
//=============================================================================
Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
(Handle(TColStd_HSequenceOfAsciiString)& theFormats,
2012-08-09 13:58:02 +06:00
Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
2004-12-01 15:39:14 +05:00
{
if (theFormats.IsNull())
theFormats = new TColStd_HSequenceOfAsciiString;
else
theFormats->Clear();
if (thePatterns.IsNull())
thePatterns = new TColStd_HSequenceOfAsciiString;
else
thePatterns->Clear();
if (!InitResMgr()) return Standard_False;
// Read Export formats list from install directory
2004-12-01 15:39:14 +05:00
if (myResMgr->Find("Export")) {
TCollection_AsciiString aFormats (myResMgr->Value("Export"));
TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
int i = 1;
for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
theFormats->Append(aToken);
}
}
// Read Export formats list from user directory
if (myResMgrUser->Find("Export")) {
TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
int i = 1;
for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
int aLenFormats = theFormats->Length();
bool isFound = false;
for(int aInd=1;aInd<=aLenFormats;aInd++){
2012-08-09 13:58:02 +06:00
if( theFormats->Value(aInd) == aToken){
isFound = true;
break;
}
}
if(!isFound)
2012-08-09 13:58:02 +06:00
theFormats->Append(aToken);
}
}
2004-12-01 15:39:14 +05:00
// Read Patterns for each supported format
int j = 1, len = theFormats->Length();
for (; j <= len; j++) {
TCollection_AsciiString aKey, aPattern;
aKey = theFormats->Value(j) + ".ExportPattern";
2004-12-01 15:39:14 +05:00
if (myResMgr->Find(aKey.ToCString()))
aPattern = myResMgr->Value(aKey.ToCString());
else if (myResMgrUser->Find(aKey.ToCString()))
aPattern = myResMgrUser->Value(aKey.ToCString());
2004-12-01 15:39:14 +05:00
else {
aKey = theFormats->Value(j) + ".Pattern";
if (myResMgr->Find(aKey.ToCString()))
aPattern = myResMgr->Value(aKey.ToCString());
else if (myResMgrUser->Find(aKey.ToCString()))
2012-08-09 13:58:02 +06:00
aPattern = myResMgrUser->Value(aKey.ToCString());
else {
aPattern = theFormats->Value(j);
aPattern += " Files ( *.* )";
}
2004-12-01 15:39:14 +05:00
}
thePatterns->Append(aPattern);
}
return (!theFormats->IsEmpty());
}
//=============================================================================
/*!
* IsSupported
*/
//=============================================================================
Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
(const Standard_Boolean isImport,
const TCollection_AsciiString& theFormat,
2004-12-01 15:39:14 +05:00
Handle(TCollection_HAsciiString)& theLibName)
{
if (!InitResMgr()) return Standard_False;
// Import/Export mode
TCollection_AsciiString aMode;
//Standard_CString aMode;
2004-12-01 15:39:14 +05:00
if (isImport) aMode = "Import";
else aMode = "Export";
// Read supported formats for the certain mode from install directory
if (myResMgr->Find(aMode.ToCString())) {
TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
2004-12-01 15:39:14 +05:00
if (aFormats.Search(theFormat) > -1) {
// Read library name for the supported format
TCollection_AsciiString aKey (theFormat);
aKey += ".";
aKey += aMode;
if (myResMgr->Find(aKey.ToCString())) {
TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
#ifndef WNT
aLibName += ".so";
#else
aLibName += ".dll";
#endif
2004-12-01 15:39:14 +05:00
theLibName = new TCollection_HAsciiString (aLibName);
return Standard_True;
}
}
}
// Read supported formats for the certain mode from user directory
if (myResMgrUser->Find(aMode.ToCString())) {
TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
if (aFormats.Search(theFormat) > -1) {
// Read library name for the supported format
TCollection_AsciiString aKey (theFormat);
aKey += ".";
aKey += aMode;
if (myResMgrUser->Find(aKey.ToCString())) {
TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
theLibName = new TCollection_HAsciiString (aLibName);
return Standard_True;
}
}
}
2004-12-01 15:39:14 +05:00
return Standard_False;
}
//=============================================================================
/*!
* InitResMgr
*/
//=============================================================================
Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
{
bool isResourceFound = false;
bool isResourceFoundUser = false;
TCollection_AsciiString aUserResDir,aResDir;
2004-12-01 15:39:14 +05:00
if (myResMgr.IsNull()) {
// Initialize the Resource Manager
TCollection_AsciiString aNull;
aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
2004-12-01 15:39:14 +05:00
#ifdef WNT
aResDir += "\\share\\salome\\resources\\geom";
2004-12-01 15:39:14 +05:00
#else
aResDir += "/share/salome/resources/geom";
2004-12-01 15:39:14 +05:00
#endif
myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
isResourceFound = true;
if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
// instead of complains in Resource_Manager
isResourceFound = false;
INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
}
} else
isResourceFound = true;
if (myResMgrUser.IsNull()) {
char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
TCollection_AsciiString aNull;
if ( dir )
{
aUserResDir = dir;
}
else
{
aUserResDir = getenv("HOME");
2004-12-01 15:39:14 +05:00
#ifdef WNT
aUserResDir += "\\.salome\\resources";
2004-12-01 15:39:14 +05:00
#else
aUserResDir += "/.salome/resources";
2004-12-01 15:39:14 +05:00
#endif
}
2004-12-01 15:39:14 +05:00
myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
isResourceFoundUser = true;
if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
// instead of complains in Resource_Manager
isResourceFoundUser = false;
}
} else
isResourceFoundUser = true;
if(!isResourceFound && !isResourceFoundUser){
INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
}
2004-12-01 15:39:14 +05:00
return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
2012-08-09 13:58:02 +06:00
myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
}
2012-10-08 17:16:36 +06:00
//=============================================================================
/*!
* RestoreShape
*/
//=============================================================================
Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
{
SetErrorCode(KO);
//Add a new result object
Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
//Add a Copy function
Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
if (aFunction.IsNull()) return NULL;
//Check if the function is set correctly
if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
//Read a shape from the stream
TopoDS_Shape aShape;
BRep_Builder B;
BRepTools::Read(aShape, theStream, B);
if (aShape.IsNull()) {
SetErrorCode("RestoreShape error: BREP reading failed");
}
//Set function value
aFunction->SetValue(aShape);
//Special dump to avoid restored shapes publication.
//See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
//GEOM::TPythonDump(aFunction) << "#";
GEOM::TPythonDump(aFunction) << result
<< " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
SetErrorCode(OK);
return result;
}
2012-08-09 13:58:02 +06:00
int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
{
SetErrorCode(KO);
if (theTextureFile.IsEmpty()) return 0;
#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
Handle(TColStd_HArray1OfByte) aTexture;
#else
Handle(TDataStd_HArray1OfByte) aTexture;
#endif
FILE* fp = fopen(theTextureFile.ToCString(), "r");
if (!fp) return 0;
std::list<std::string> lines;
char buffer[4096];
int maxlen = 0;
while (!feof(fp)) {
if ((fgets(buffer, 4096, fp)) == NULL) break;
int aLen = strlen(buffer);
if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
lines.push_back(buffer);
maxlen = std::max(maxlen, (int)strlen(buffer));
}
fclose(fp);
int lenbytes = maxlen/8;
if (maxlen%8) lenbytes++;
if (lenbytes == 0 || lines.empty())
return 0;
std::list<unsigned char> bytedata;
std::list<std::string>::const_iterator it;
for (it = lines.begin(); it != lines.end(); ++it) {
std::string line = *it;
int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
for (int i = 0; i < lenline/8; i++) {
unsigned char byte = 0;
for (int j = 0; j < 8; j++)
byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
bytedata.push_back(byte);
}
for (int i = lenline/8; i < lenbytes; i++)
bytedata.push_back((unsigned char)0);
}
if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
return 0;
#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
#else
aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
#endif
std::list<unsigned char>::iterator bdit;
int i;
for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
aTexture->SetValue(i, (Standard_Byte)(*bdit));
int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
if (aTextureId > 0) SetErrorCode(OK);
return aTextureId;
}
int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
const Handle(TColStd_HArray1OfByte)& theTexture)
#else
const Handle(TDataStd_HArray1OfByte)& theTexture)
#endif
{
SetErrorCode(KO);
int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
if (aTextureId > 0) SetErrorCode(OK);
return aTextureId;
}
#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
#else
Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
#endif
int& theWidth, int& theHeight)
{
SetErrorCode(KO);
#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
Handle(TColStd_HArray1OfByte) aTexture;
#else
Handle(TDataStd_HArray1OfByte) aTexture;
#endif
theWidth = theHeight = 0;
TCollection_AsciiString aFileName;
if (theTextureId <= 0)
return aTexture;
aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
return aTexture;
}
std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
{
SetErrorCode(KO);
std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
SetErrorCode(OK);
return id_list;
2004-12-01 15:39:14 +05:00
}