From: Jérôme Date: Thu, 24 Sep 2020 10:02:52 +0000 (+0200) Subject: Implementation import STEP X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=a26c0031de178d4596a3bf359c0f434879c8112e;p=modules%2Fshaper.git Implementation import STEP --- diff --git a/src/ExchangeAPI/CMakeLists.txt b/src/ExchangeAPI/CMakeLists.txt index bdf5bcb34..b61f22ce9 100644 --- a/src/ExchangeAPI/CMakeLists.txt +++ b/src/ExchangeAPI/CMakeLists.txt @@ -33,12 +33,14 @@ SET(PROJECT_SOURCES SET(PROJECT_LIBRARIES ModelAPI ModelHighAPI + GeomAlgoAPI ) INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/src/Events ${PROJECT_SOURCE_DIR}/src/ModelAPI ${PROJECT_SOURCE_DIR}/src/ModelHighAPI + ${PROJECT_SOURCE_DIR}/src/GeomAlgoAPI ) # Plugin headers dependency diff --git a/src/ExchangeAPI/ExchangeAPI_Import.cpp b/src/ExchangeAPI/ExchangeAPI_Import.cpp index b1a6c832d..981c92d9d 100644 --- a/src/ExchangeAPI/ExchangeAPI_Import.cpp +++ b/src/ExchangeAPI/ExchangeAPI_Import.cpp @@ -28,6 +28,8 @@ #include #include #include +#include + //-------------------------------------------------------------------------------------- #include @@ -47,11 +49,37 @@ ExchangeAPI_Import::ExchangeAPI_Import( setFilePath(theFilePath); } +ExchangeAPI_Import::ExchangeAPI_Import( + const std::shared_ptr & theFeature, + const std::string & theFilePath, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor) +: ModelHighAPI_Interface(theFeature) +{ + if (initialize()) + setParameters(theFeature,theFilePath,anScalInterUnits,anMaterials,anColor ); +} + ExchangeAPI_Import::~ExchangeAPI_Import() { } +//-------------------------------------------------------------------------------------- +void ExchangeAPI_Import::setParameters(const std::shared_ptr & theFeature, + const std::string & theFilePath, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor) +{ + fillAttribute(theFilePath, myfilePath); + fillAttribute(anScalInterUnits, theFeature->boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())); + fillAttribute(anMaterials, theFeature->boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())); + fillAttribute(anColor, theFeature->boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())); + execute(); +} + //-------------------------------------------------------------------------------------- void ExchangeAPI_Import::setFilePath(const std::string & theFilePath) { @@ -66,7 +94,16 @@ void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const FeaturePtr aBase = feature(); std::string aPartName = theDumper.name(aBase->document()); - std::string aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value(); + AttributeStringPtr aImportTypeAttr = aBase->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID()); + std::string aFormat = aImportTypeAttr->value(); + std::string aFilePath; + if (aFormat == "STEP" || aFormat == "STP") + { + aFilePath = aBase->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID())->value(); + }else{ + aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value(); + } + std::string aFrom = "\\"; std::string aTo = "\\\\"; for(std::size_t aPos = aFilePath.find(aFrom); @@ -75,9 +112,19 @@ void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const aFilePath.replace(aPos, aFrom.size(), aTo); aPos += aTo.size(); } - - theDumper << aBase << " = model.addImport(" << aPartName << ", \"" + std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath); + if( anExtension == "STP" || anExtension == "STEP"){ + theDumper << aBase << " = model.addImportStep(" << aPartName << ", \"" + << aFilePath << "\"" ; + + theDumper << ", " << scalinterunits()->value() + << ", " << materials()->value() + << ", " << colors()->value() << ")"<< std::endl; + }else{ + theDumper << aBase << " = model.addImport(" << aPartName << ", \"" << aFilePath << "\")" << std::endl; + } + // to make import have results theDumper << "model.do()" << std::endl; @@ -102,6 +149,18 @@ ImportPtr addImport( return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath)); } +ImportPtr addImportStep( + const std::shared_ptr & thePart, + const std::string & theFilePath, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor ) +{ + std::shared_ptr aFeature = thePart->addFeature(ExchangeAPI_Import::ID()); + return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath, + anScalInterUnits, anMaterials, anColor)); +} + void importPart(const std::shared_ptr & thePart, const std::string & theFilePath, const ModelHighAPI_Reference & theAfterThis) diff --git a/src/ExchangeAPI/ExchangeAPI_Import.h b/src/ExchangeAPI/ExchangeAPI_Import.h index f38db9bbb..c477a0c98 100644 --- a/src/ExchangeAPI/ExchangeAPI_Import.h +++ b/src/ExchangeAPI/ExchangeAPI_Import.h @@ -46,18 +46,40 @@ public: EXCHANGEAPI_EXPORT ExchangeAPI_Import(const std::shared_ptr & theFeature, const std::string & theFilePath); + + /// Constructor with values for Step file + EXCHANGEAPI_EXPORT + ExchangeAPI_Import(const std::shared_ptr & theFeature, + const std::string & theFilePath, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor); + /// Destructor EXCHANGEAPI_EXPORT virtual ~ExchangeAPI_Import(); - INTERFACE_1(ExchangePlugin_ImportFeature::ID(), + INTERFACE_4(ExchangePlugin_ImportFeature::ID(), filePath, ExchangePlugin_ImportFeature::FILE_PATH_ID(), - ModelAPI_AttributeString, /** File path */ + ModelAPI_AttributeString, /** File path */, + scalinterunits, ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID(), + ModelAPI_AttributeBoolean, /** Scale internationals units */, + materials, ExchangePlugin_ImportFeature::STEP_MATERIALS_ID(), + ModelAPI_AttributeBoolean, /** Materials */, + colors, ExchangePlugin_ImportFeature::STEP_COLORS_ID(), + ModelAPI_AttributeBoolean, /** Colors */ ) /// Set point values EXCHANGEAPI_EXPORT void setFilePath(const std::string & theFilePath); + + EXCHANGEAPI_EXPORT + void setParameters(const std::shared_ptr & theFeature, + const std::string & theFilePath, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor); /// Dump wrapped feature EXCHANGEAPI_EXPORT @@ -74,6 +96,16 @@ EXCHANGEAPI_EXPORT ImportPtr addImport(const std::shared_ptr & thePart, const std::string & theFilePath); +/**\ingroup CPPHighAPI + * \brief Create Import Step feature + */ +EXCHANGEAPI_EXPORT +ImportPtr addImportStep(const std::shared_ptr & thePart, + const std::string & theFilePath, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor); + /** \ingroup CPPHighAPI * \brief Import features from the file to the document after the current feature (or to the end). */ diff --git a/src/ExchangePlugin/CMakeLists.txt b/src/ExchangePlugin/CMakeLists.txt index 67880c8e6..3df98bece 100644 --- a/src/ExchangePlugin/CMakeLists.txt +++ b/src/ExchangePlugin/CMakeLists.txt @@ -31,6 +31,7 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/Events ${PROJECT_SOURCE_DIR}/src/XAO ${PROJECT_SOURCE_DIR}/src/ConstructionPlugin ${PROJECT_SOURCE_DIR}/src/PartSetPlugin + ${OpenCASCADE_INCLUDE_DIR} ) SET(PROJECT_HEADERS diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 0d38d857c..6a8f36cb1 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -71,6 +71,8 @@ #include +#include + ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature() { } diff --git a/src/ExchangePlugin/ExchangePlugin_Import.cpp b/src/ExchangePlugin/ExchangePlugin_Import.cpp index b5a51ff08..304d456d5 100644 --- a/src/ExchangePlugin/ExchangePlugin_Import.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Import.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -81,8 +82,15 @@ ExchangePlugin_Import::~ExchangePlugin_Import() void ExchangePlugin_Import::initAttributes() { data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId()); data()->addAttribute(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId()); + data()->addAttribute(STEP_TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId()); + data()->addAttribute(STEP_TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId()); + data()->addAttribute(STEP_MATERIALS_ID(), ModelAPI_AttributeBoolean::typeId()); + data()->addAttribute(STEP_COLORS_ID(), ModelAPI_AttributeBoolean::typeId()); + data()->addAttribute(STEP_SCALE_INTER_UNITS_ID(), ModelAPI_AttributeBoolean::typeId()); } /* @@ -90,26 +98,61 @@ void ExchangePlugin_Import::initAttributes() */ void ExchangePlugin_Import::execute() { - AttributeStringPtr aFilePathAttr = string(ExchangePlugin_Import::FILE_PATH_ID()); - std::string aFilePath = aFilePathAttr->value(); - if (aFilePath.empty()) { - setError("File path is empty."); - return; + AttributeStringPtr aFormatAttr = + this->string(ExchangePlugin_Import::IMPORT_TYPE_ID()); + std::string aFormat = aFormatAttr->value(); + + AttributeStringPtr aFilePathAttr; + std::string aFilePath; + AttributeStringArrayPtr aPartsAttr; + AttributeIntegerPtr aTargetAttr; + if (aFormat == "STEP" || aFormat == "STP") + { + aFilePathAttr = string(ExchangePlugin_Import::STEP_FILE_PATH_ID()); + aFilePath = aFilePathAttr->value(); + // get the document where to import + aPartsAttr = stringArray(STEP_TARGET_PARTS_LIST_ID()); + aTargetAttr = integer(STEP_TARGET_PART_ID()); + }else{ + aFilePathAttr = string(ExchangePlugin_Import::FILE_PATH_ID()); + aFilePath = aFilePathAttr->value(); + // get the document where to import + aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); + aTargetAttr = integer(TARGET_PART_ID()); } - // get the document where to import - AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); - AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID()); + if (aFilePath.empty()) { + setError("File path is empty."); + return; + } SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDoc = - findDocument(aSession->moduleDocument(), + DocumentPtr aDoc = findDocument(aSession->moduleDocument(), Locale::Convert::toWString(aPartsAttr->value(aTargetAttr->value()))); if (aDoc.get()) { FeaturePtr aImportFeature = aDoc->addFeature(ExchangePlugin_ImportFeature::ID()); DataPtr aData = aImportFeature->data(); - AttributeStringPtr aPathAttr = aData->string(ExchangePlugin_ImportFeature::FILE_PATH_ID()); + AttributeStringPtr aPathAttr; + if (aFormat == "STEP" || aFormat == "STP") + { + aPathAttr = aData->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID()); + }else + { + aPathAttr = aData->string(ExchangePlugin_ImportFeature::FILE_PATH_ID()); + } + + AttributeStringPtr aImportTypeAttr = aData->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID()); + + aData->boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID()) + ->setValue(boolean(ExchangePlugin_Import::STEP_MATERIALS_ID())->value()); + aData->boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID()) + ->setValue(boolean(ExchangePlugin_Import::STEP_COLORS_ID())->value()); + aData->boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID()) + ->setValue(boolean(ExchangePlugin_Import::STEP_SCALE_INTER_UNITS_ID())->value()); + aPathAttr->setValue(aFilePathAttr->value()); + aImportTypeAttr->setValue(aFormat); + aImportFeature->execute(); } } @@ -117,14 +160,31 @@ void ExchangePlugin_Import::execute() void ExchangePlugin_Import::attributeChanged(const std::string& theID) { - if (theID == FILE_PATH_ID()) { - AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID()); - if (aFilePathAttr->value().empty()) + AttributeStringPtr aFilePathAttr; + AttributeStringArrayPtr aPartsAttr; + AttributeIntegerPtr aTargetAttr; + + if (theID == FILE_PATH_ID() ||theID == STEP_FILE_PATH_ID() ) { + aFilePathAttr = string(FILE_PATH_ID()); + if (theID == FILE_PATH_ID() && aFilePathAttr->value().empty()) return; + aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); + aTargetAttr = integer(TARGET_PART_ID()); + + updatePart(aPartsAttr, aTargetAttr); - AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); - AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID()); + aFilePathAttr = string(STEP_FILE_PATH_ID()); + if (theID == STEP_FILE_PATH_ID() && aFilePathAttr->value().empty()) + return; + aPartsAttr = stringArray(STEP_TARGET_PARTS_LIST_ID()); + aTargetAttr = integer(STEP_TARGET_PART_ID()); + updatePart(aPartsAttr, aTargetAttr); + } +} + +void ExchangePlugin_Import::updatePart(AttributeStringArrayPtr &aPartsAttr, AttributeIntegerPtr &aTargetAttr) +{ // update the list of target parts SessionPtr aSession = ModelAPI_Session::get(); DocumentPtr aDoc = document(); @@ -159,5 +219,4 @@ void ExchangePlugin_Import::attributeChanged(const std::string& theID) aTargetAttr->setValue(0); } } - } } diff --git a/src/ExchangePlugin/ExchangePlugin_Import.h b/src/ExchangePlugin/ExchangePlugin_Import.h index c09b3ebab..38d3e05e7 100644 --- a/src/ExchangePlugin/ExchangePlugin_Import.h +++ b/src/ExchangePlugin/ExchangePlugin_Import.h @@ -24,6 +24,8 @@ #include #include +#include +#include #include @@ -43,6 +45,12 @@ class ExchangePlugin_Import : public ModelAPI_Feature static const std::string MY_IMPORT_ID("ImportMacro"); return MY_IMPORT_ID; } + /// Feature kind + inline static const std::string& IMPORT_TYPE_ID() + { + static const std::string MY_IMPORT_TYPE_ID("ImportType"); + return MY_IMPORT_TYPE_ID; + } /// attribute name of file path inline static const std::string& FILE_PATH_ID() { @@ -61,6 +69,42 @@ class ExchangePlugin_Import : public ModelAPI_Feature static const std::string MY_TARGET_PARTS_LIST_ID("target_parts_list"); return MY_TARGET_PARTS_LIST_ID; } + /// attribute name of step file path + inline static const std::string& STEP_FILE_PATH_ID() + { + static const std::string MY_STEP_FILE_PATH_ID("step_file_path"); + return MY_STEP_FILE_PATH_ID; + } + /// attribute name of step target part + inline static const std::string& STEP_TARGET_PART_ID() + { + static const std::string MY_STEP_TARGET_PART_ID("step_target_part"); + return MY_STEP_TARGET_PART_ID; + } + /// attribute name of list ofstep target parts + inline static const std::string& STEP_TARGET_PARTS_LIST_ID() + { + static const std::string MY_STEP_TARGET_PARTS_LIST_ID("step_target_parts_list"); + return MY_STEP_TARGET_PARTS_LIST_ID; + } + /// attribute name of step Scale to International System Units + inline static const std::string& STEP_SCALE_INTER_UNITS_ID() + { + static const std::string MY_STEP_SCALE_INTER_UNITS_ID("step_scale_inter_units"); + return MY_STEP_SCALE_INTER_UNITS_ID; + } + /// attribute name of step materiels + inline static const std::string& STEP_MATERIALS_ID() + { + static const std::string MY_STEP_MATERIALS_ID("step_materials"); + return MY_STEP_MATERIALS_ID; + } + /// attribute name of step colors + inline static const std::string& STEP_COLORS_ID() + { + static const std::string MY_STEP_COLORS_ID("step_colours"); + return MY_STEP_COLORS_ID; + } /// Default constructor EXCHANGEPLUGIN_EXPORT ExchangePlugin_Import(); /// Default destructor @@ -79,6 +123,9 @@ class ExchangePlugin_Import : public ModelAPI_Feature /// \param theID identifier of changed attribute EXCHANGEPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + EXCHANGEPLUGIN_EXPORT + void updatePart(AttributeStringArrayPtr &aPartsAttr, AttributeIntegerPtr &aTargetAttr); + /// Computes or recomputes the results EXCHANGEPLUGIN_EXPORT virtual void execute(); diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index ade0a3115..402b660e9 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -42,6 +43,8 @@ #include #include #include +#include +#include #include #include #include @@ -60,6 +63,10 @@ #include +#include +#include + + ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature() { } @@ -77,10 +84,16 @@ void ExchangePlugin_ImportFeature::initAttributes() data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); AttributePtr aFeaturesAttribute = - data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), + data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), ModelAPI_AttributeRefList::typeId()); - aFeaturesAttribute->setIsArgument(false); + data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(STEP_MATERIALS_ID(), ModelAPI_AttributeBoolean::typeId()); + data()->addAttribute(STEP_COLORS_ID(), ModelAPI_AttributeBoolean::typeId()); + data()->addAttribute(STEP_SCALE_INTER_UNITS_ID(), ModelAPI_AttributeBoolean::typeId()); + aFeaturesAttribute->setIsArgument(false); + ModelAPI_Session::get()->validators()->registerNotObligatory( getKind(), ExchangePlugin_ImportFeature::FEATURES_ID()); } @@ -90,7 +103,15 @@ void ExchangePlugin_ImportFeature::initAttributes() */ void ExchangePlugin_ImportFeature::execute() { - AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID()); + AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID()); + std::string aFormat = aImportTypeAttr->value(); + AttributeStringPtr aFilePathAttr; + if (aFormat == "STEP" || aFormat == "STP") + { + aFilePathAttr = string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID()); + }else{ + aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID()); + } std::string aFilePath = aFilePathAttr->value(); if (aFilePath.empty()) { setError("File path is empty."); @@ -101,7 +122,7 @@ void ExchangePlugin_ImportFeature::execute() } std::shared_ptr ExchangePlugin_ImportFeature::createResultBody( - std::shared_ptr aGeomShape) + std::shared_ptr aGeomShape) { std::shared_ptr aResultBody = document()->createBody(data()); //LoadNamingDS of the imported shape @@ -122,10 +143,47 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) // Perform the import std::string anError; std::shared_ptr aGeomShape; + + std::map< std::wstring, std::list> theMaterialShape; + + std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName); + data()->setName(Locale::Convert::toWString(anObjectName)); + + ResultBodyPtr result = document()->createBody(data()); + + bool anColorGroupSelected = boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())->value(); + bool anMaterialsGroupSelected = boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())->value(); + if (anExtension == "BREP" || anExtension == "BRP") { aGeomShape = BREPImport(theFileName, anExtension, anError); } else if (anExtension == "STEP" || anExtension == "STP") { - aGeomShape = STEPImport(theFileName, anExtension, anError); + bool anScalInterUnits = boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())->value(); + + + try{ + + result->clearShapeNameAndColor(); + // Process groups/fields + std::shared_ptr aRefListOfGroups = + std::dynamic_pointer_cast(data()->attribute(FEATURES_ID())); + + // Remove previous groups/fields stored in RefList + std::list anGroupList = aRefListOfGroups->list(); + std::list::iterator anGroupIt = anGroupList.begin(); + for (; anGroupIt != anGroupList.end(); ++anGroupIt) { + std::shared_ptr aFeature = ModelAPI_Feature::feature(*anGroupIt); + if (aFeature) + document()->removeFeature(aFeature); + } + + aGeomShape = STEPImportAttributs(theFileName, result, anScalInterUnits, + anMaterialsGroupSelected, anColorGroupSelected,theMaterialShape,anError); + + } + catch (OSD_Exception& e) { + //Try to load STEP file without colors... + aGeomShape = STEPImport(theFileName, anExtension,anScalInterUnits,anError); + } } else if (anExtension == "IGES" || anExtension == "IGS") { aGeomShape = IGESImport(theFileName, anExtension, anError); } else { @@ -139,12 +197,138 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) } // Pass the results into the model - std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName); - data()->setName(Locale::Convert::toWString(anObjectName)); - setResult(createResultBody(aGeomShape)); + loadNamingDS(aGeomShape, result); + + // create color group + if (anColorGroupSelected) + { + setColorGroups(result); + } + + // create Materiel group + if (anMaterialsGroupSelected){ + setMaterielGroup(result,theMaterialShape); + } + + setResult(result); + } +void ExchangePlugin_ImportFeature::setColorGroups(std::shared_ptr theResultBody) +{ + std::vector aColor; + int indice = 1; + std::list< std::vector > aColorsRead; + + + ModelAPI_Tools::getColor(theResultBody, aColor); + if (!aColor.empty() ){ + std::wstringstream colorName; + colorName< allRes; + ModelAPI_Tools::allSubs(theResultBody, allRes); + for(std::list::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) { + ModelAPI_Tools::getColor(*aRes, aColor); + if (!aColor.empty() ){ + auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor); + if ( it == aColorsRead.end() ){ + std::wstringstream colorName; + colorName< theResultBody, + std::vector &theColor, + const std::wstring& theName ) +{ + std::vector aColor; + std::shared_ptr aGroupFeature = addFeature("Group"); + + // group name + aGroupFeature->data()->setName(theName); + + // fill selection + AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list"); + + ModelAPI_Tools::getColor(theResultBody, aColor); + if (!aColor.empty() ){ + if( aColor == theColor ) { + GeomShapePtr aShape = theResultBody->shape(); + aSelectionList->setSelectionType(aShape->shapeTypeStr() ); + aSelectionList->append(theResultBody,aShape); + } + } + // add element with the same color + std::list allRes; + ModelAPI_Tools::allSubs(theResultBody, allRes); + for(std::list::iterator aRes = allRes.begin(); + aRes != allRes.end(); ++aRes) { + ModelAPI_Tools::getColor(*aRes, aColor); + GeomShapePtr aShape = (*aRes)->shape(); + + if (!aColor.empty() ){ + if( aRes->get() && aColor == theColor ) { + aSelectionList->setSelectionType(aShape->shapeTypeStr() ); + aSelectionList->append(theResultBody,aShape); + } + } + } + + if (aSelectionList->size() == 0 ){ + document()->removeFeature(aGroupFeature); + } +} + +void ExchangePlugin_ImportFeature::setMaterielGroup(std::shared_ptr theResultBody, + std::map< std::wstring, std::list> &theMaterialShape) +{ + int indice = 1; + std::map< std::wstring, std::list>::iterator it; + for( it = theMaterialShape.begin(); it != theMaterialShape.end(); ++it) { + + std::shared_ptr aGroupFeature = addFeature("Group"); + // group name + aGroupFeature->data()->setName((*it).first); + + // fill selection + AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list"); + std::string aSelectionType = "solid" ; + aSelectionList->setSelectionType(aSelectionType); + GeomShapePtr aShape = theResultBody->shape(); + + std::list allRes; + ModelAPI_Tools::allSubs(theResultBody, allRes); + for(std::list::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) { + + GeomShapePtr aShape = (*aRes)->shape(); + for(std::list::iterator aResMat = it->second.begin(); + aResMat != it->second.end(); ++aResMat) { + if( aRes->get() && ((*aRes)->data()->name() == (*aResMat))) + { + aSelectionList->append(theResultBody,aShape); + break; + } + } + } + if (aSelectionList->size() == 0){ + document()->removeFeature(aGroupFeature); + } + } +} + + + void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) { try { @@ -415,7 +599,6 @@ void ExchangePlugin_ImportFeature::loadNamingDS( { //load result theResultBody->store(theGeomShape); - std::string aNameMS = "Shape"; theResultBody->loadFirstLevel(theGeomShape, aNameMS); } diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h index c087af2ab..2875d83ab 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h @@ -24,6 +24,7 @@ #include #include +#include #include @@ -43,18 +44,48 @@ class ExchangePlugin_ImportFeature : public ModelAPI_CompositeFeature static const std::string MY_IMPORT_ID("Import"); return MY_IMPORT_ID; } + /// Feature kind + inline static const std::string& IMPORT_TYPE_ID() + { + static const std::string MY_IMPORT_TYPE_ID("ImportType"); + return MY_IMPORT_TYPE_ID; + } /// attribute name of file path inline static const std::string& FILE_PATH_ID() { static const std::string MY_FILE_PATH_ID("file_path"); return MY_FILE_PATH_ID; } + /// attribute name of file path + inline static const std::string& STEP_FILE_PATH_ID() + { + static const std::string MY_STEP_FILE_PATH_ID("step_file_path"); + return MY_STEP_FILE_PATH_ID; + } /// All features (list of references) inline static const std::string& FEATURES_ID() { static const std::string MY_FEATURES_ID("Features"); return MY_FEATURES_ID; } + /// attribute name of step Scale to International System Units + inline static const std::string& STEP_SCALE_INTER_UNITS_ID() + { + static const std::string MY_STEP_SCALE_INTER_UNITS_ID("step_scale_inter_units"); + return MY_STEP_SCALE_INTER_UNITS_ID; + } + /// attribute name of step materiels + inline static const std::string& STEP_MATERIALS_ID() + { + static const std::string MY_STEP_MATERIALS_ID("step_materials"); + return MY_STEP_MATERIALS_ID; + } + /// attribute name of step colors + inline static const std::string& STEP_COLORS_ID() + { + static const std::string MY_STEP_COLORS_ID("step_colours"); + return MY_STEP_COLORS_ID; + } /// Default constructor EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeature(); /// Default destructor @@ -108,6 +139,19 @@ private: /// Loads Naming data structure to the document void loadNamingDS(std::shared_ptr theGeomShape, std::shared_ptr theResultBody); + + // Set groups of color + void setColorGroups(std::shared_ptr theResultBody); + + // set a group of color + void setColorGroup(std::shared_ptr theResultBody, + std::vector &aColor, + const std::wstring& theName ); + + // set Materiel group of color + void setMaterielGroup(std::shared_ptr theResultBody, + std::map< std::wstring, std::list> &theMaterialShape); + }; #endif /* IMPORT_IMPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/plugin-Exchange.xml b/src/ExchangePlugin/plugin-Exchange.xml index 7f240ba69..275ceb69b 100644 --- a/src/ExchangePlugin/plugin-Exchange.xml +++ b/src/ExchangePlugin/plugin-Exchange.xml @@ -4,19 +4,70 @@ - - - + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + diff --git a/src/GeomAlgoAPI/CMakeLists.txt b/src/GeomAlgoAPI/CMakeLists.txt index 2d3df2aee..a03ae213f 100644 --- a/src/GeomAlgoAPI/CMakeLists.txt +++ b/src/GeomAlgoAPI/CMakeLists.txt @@ -45,6 +45,7 @@ SET(PROJECT_HEADERS GeomAlgoAPI_Placement.h GeomAlgoAPI_BREPImport.h GeomAlgoAPI_STEPImport.h + GeomAlgoAPI_STEPImportXCAF.h GeomAlgoAPI_IGESImport.h GeomAlgoAPI_BREPExport.h GeomAlgoAPI_STEPExport.h @@ -111,6 +112,7 @@ SET(PROJECT_SOURCES GeomAlgoAPI_Placement.cpp GeomAlgoAPI_BREPImport.cpp GeomAlgoAPI_STEPImport.cpp + GeomAlgoAPI_STEPImportXCAF.cpp GeomAlgoAPI_IGESImport.cpp GeomAlgoAPI_BREPExport.cpp GeomAlgoAPI_STEPExport.cpp @@ -178,6 +180,7 @@ INCLUDE_DIRECTORIES( ../GeomAlgoImpl ../ModelAPI ../XAO + ${PROJECT_SOURCE_DIR}/src/Locale ${OpenCASCADE_INCLUDE_DIR} ) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp index 3a824f106..281f689e3 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp @@ -54,22 +54,37 @@ #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include +#include #include #include // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC + +// ---------------------------------------------------------------------------- + std::shared_ptr STEPImport(const std::string& theFileName, const std::string& theFormatName, + const bool anScalInterUnits, std::string& theError) { + TopoDS_Shape aResShape; // Set "C" numeric locale to save numbers correctly // Kernel_Utils::Localizer loc; - + STEPControl_Reader aReader; //VSR: 16/09/09: Convert to METERS @@ -89,7 +104,7 @@ std::shared_ptr STEPImport(const std::string& theFileName, if (status == IFSelect_RetDone) { // Regard or not the model units - if (theFormatName == "STEP_SCALE") { + if (!anScalInterUnits) { // set UnitFlag to units from file TColStd_SequenceOfAsciiString anUnitLengthNames; TColStd_SequenceOfAsciiString anUnitAngleNames; @@ -194,3 +209,83 @@ std::shared_ptr STEPImport(const std::string& theFileName, aGeomShape->setImpl(new TopoDS_Shape(aResShape)); return aGeomShape; } + + +std::shared_ptr STEPImportAttributs(const std::string& theFileName, + std::shared_ptr theResultBody, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor, + std::map< std::wstring, std::list> &theMaterialShape, + std::string& theError) +{ + + STEPControl_Reader aReader; + std::shared_ptr aGeomShape(new GeomAPI_Shape); + //VSR: 16/09/09: Convert to METERS + Interface_Static::SetCVal("xstep.cascade.unit","M"); + Interface_Static::SetIVal("read.step.ideas", 1); + Interface_Static::SetIVal("read.step.nonmanifold", 1); + + try { + OCC_CATCH_SIGNALS; + + IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str()); + + if (status == IFSelect_RetDone) { + + // Regard or not the model units + if (!anScalInterUnits) { + // set UnitFlag to units from file + TColStd_SequenceOfAsciiString anUnitLengthNames; + TColStd_SequenceOfAsciiString anUnitAngleNames; + TColStd_SequenceOfAsciiString anUnitSolidAngleNames; + aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames); + if (anUnitLengthNames.Length() > 0) { + TCollection_AsciiString aLenUnits = anUnitLengthNames.First(); + if (aLenUnits == "millimetre") + Interface_Static::SetCVal("xstep.cascade.unit", "MM"); + else if (aLenUnits == "centimetre") + Interface_Static::SetCVal("xstep.cascade.unit", "CM"); + else if (aLenUnits == "metre" || aLenUnits.IsEmpty()) + Interface_Static::SetCVal("xstep.cascade.unit", "M"); + else if (aLenUnits == "INCH") + Interface_Static::SetCVal("xstep.cascade.unit", "INCH"); + else { + theError = "The file contains not supported units."; + aGeomShape->setImpl(new TopoDS_Shape()); + return aGeomShape; + } + // TODO (for other units than mm, cm, m or inch) + //else if (aLenUnits == "") + // Interface_Static::SetCVal("xstep.cascade.unit", "???"); + } + } + else { + //cout<<"need re-scale a model"<setImpl(new TopoDS_Shape()); + return aGeomShape; + } + + STEPCAFControl_Reader cafreader; + cafreader.SetColorMode(true); + cafreader.SetNameMode(true); + cafreader.SetMatMode(true); + + if(cafreader.ReadFile(theFileName.c_str()) != IFSelect_RetDone) { + theError = "Wrong format of the imported file. Can't import file."; + std::shared_ptr aGeomShape(new GeomAPI_Shape); + aGeomShape->setImpl(new TopoDS_Shape()); + return aGeomShape; + } + + return readAttributes(cafreader,theResultBody,anMaterials, theMaterialShape, "STEP-XCAF"); + } + diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.h b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.h index 470791a3b..9433a0a70 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.h @@ -23,13 +23,26 @@ #include #include - #include +#include + + /// Implementation of the import STEP files algorithms GEOMALGOAPI_EXPORT std::shared_ptr STEPImport(const std::string& theFileName, const std::string& theFormatName, + const bool anScalInterUnits, std::string& theError); +/// Implementation of the import STEP files algorithms with Attributs (Name, Color, Materials) +GEOMALGOAPI_EXPORT +std::shared_ptr STEPImportAttributs(const std::string& theFileName, + std::shared_ptr theResultBody, + const bool anScalInterUnits, + const bool anMaterials, + const bool anColor, + std::map< std::wstring, std::list> &theMaterialShape, + std::string& theError); + #endif /* GEOMALGOAPI_STEPIMPORT_H_ */ diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp new file mode 100644 index 000000000..3e2a700cf --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp @@ -0,0 +1,446 @@ +// Copyright (C) 2014-2020 CEA/DEN, EDF R&D +// +// 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 + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC + + +//============================================================================= +/*! + * GetShape() + */ +//============================================================================= + +TopoDS_Shape GetShape(const Handle(Standard_Transient) &theEnti, + const Handle(Transfer_TransientProcess) &theTP) +{ + TopoDS_Shape aResult; + Handle(Transfer_Binder) aBinder = theTP->Find(theEnti); + + if (aBinder.IsNull()) { + return aResult; + } + + aResult = TransferBRep::ShapeResult(aBinder); + + return aResult; +} + +// ---------------------------------------------------------------------------- + +std::shared_ptr readAttributes( STEPCAFControl_Reader &reader, + std::shared_ptr theResultBody, + const bool anMaterials, + std::map< std::wstring, std::list> &theMaterialShape, + const std::string &format) +{ + // dummy XCAF Application to handle the STEP XCAF Document + Handle(XCAFApp_Application) dummy_app = XCAFApp_Application::GetApplication(); + // XCAF Document to contain the STEP/IGES file itself + Handle(TDocStd_Document) doc; + // check if a file is already open under this handle, if so, close it to + // prevent segfaults when trying to create a new document + if(dummy_app->NbDocuments() > 0) { + dummy_app->GetDocument(1, doc); + dummy_app->Close(doc); + } + + dummy_app->NewDocument( TCollection_ExtendedString("MDTV-CAF"), doc); + // transfer STEP/IGES into the document, and get the main label + reader.Transfer(doc); + TDF_Label mainLabel = doc->Main(); + Handle_XCAFDoc_ShapeTool shapeTool = XCAFDoc_DocumentTool::ShapeTool(mainLabel); + Handle_XCAFDoc_ColorTool colorTool = XCAFDoc_DocumentTool::ColorTool(mainLabel); + Handle(XCAFDoc_MaterialTool) materialTool = XCAFDoc_DocumentTool::MaterialTool(mainLabel); + // traverse the labels recursively to set attributes on shapes + setShapeAttributes(shapeTool, colorTool, materialTool, mainLabel, + TopLoc_Location(),theResultBody,theMaterialShape,false); + + + std::shared_ptr ageom = setgeom(shapeTool,mainLabel); + + STEPControl_Reader aReader = reader.ChangeReader(); + + // BEGIN: reading materials of sub-shapes from file + if ( anMaterials ) + { + TopTools_IndexedMapOfShape anIndices; + TopExp::MapShapes(ageom->impl(), anIndices); + + 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 ie = 1; ie <= nb; ie++) { + Handle(Standard_Transient) enti = Model->Value(ie); + + // Store materials. + StoreMaterial(theResultBody,enti, anIndices, TP, mainLabel,theMaterialShape); + } + } + } + + return ageom; +} + +std::shared_ptr setgeom(const Handle(XCAFDoc_ShapeTool) &shapeTool, + const TDF_Label &label) +{ + BRep_Builder B; + TopoDS_Compound compound; + B.MakeCompound(compound); + + TDF_LabelSequence frshapes; + shapeTool->GetShapes(frshapes); + + std::shared_ptr aGeomShape(new GeomAPI_Shape); + + if (frshapes.Length() == 0) { + aGeomShape->setImpl(new TopoDS_Shape()); + return aGeomShape; + } else if (frshapes.Length() == 1) { + TopoDS_Shape shape = shapeTool->GetShape(frshapes.Value(1)); + aGeomShape->setImpl(new TopoDS_Shape(shape)); + return aGeomShape; + } else { + for (Standard_Integer i=1; iGetShape(frshapes.Value(i)); + + TDF_Label aLabel = shapeTool->FindShape(S, Standard_False); + if ( (!aLabel.IsNull()) && (shapeTool->IsShape(aLabel)) ) { + if (shapeTool->IsFree(aLabel) ) { + if (S.IsNull()) { + continue; + } + else { + B.Add(compound, S); + } + } + } + } + TopoDS_Shape shape = compound; + aGeomShape->setImpl(new TopoDS_Shape(shape)); + return aGeomShape; + } +} + +void setShapeAttributes(const Handle(XCAFDoc_ShapeTool) &shapeTool, + const Handle(XCAFDoc_ColorTool) &colorTool, + const Handle(XCAFDoc_MaterialTool) &materialTool, + const TDF_Label &label, + const TopLoc_Location &loc, + std::shared_ptr theResultBody, + std::map< std::wstring, std::list> &theMaterialShape, + bool isRef) +{ + std::wstring shapeName; + Handle(TDataStd_Name) n; + + if(label.FindAttribute(TDataStd_Name::GetID(), n)) { + TCollection_ExtendedString name = n->Get(); + + shapeName = Locale::Convert::toWString(TCollection_AsciiString(name).ToCString()) ; + } + + TopLoc_Location partLoc = loc; + Handle(XCAFDoc_Location) l; + if(label.FindAttribute(XCAFDoc_Location::GetID(), l)) { + if(isRef) + partLoc = partLoc * l->Get(); + else + partLoc = l->Get(); + } + + TDF_Label ref; + if(shapeTool->IsReference(label) && shapeTool->GetReferredShape(label, ref)) { + + setShapeAttributes( shapeTool, colorTool, materialTool, ref, + partLoc,theResultBody,theMaterialShape,true); + } + + if( shapeTool->IsSimpleShape(label) && (isRef || shapeTool->IsFree(label))) { + + TopoDS_Shape shape = shapeTool->GetShape(label); + + std::shared_ptr aShapeGeom(new GeomAPI_Shape); + if (!loc.IsIdentity()){ + shape.Move(loc); + } + aShapeGeom->setImpl(new TopoDS_Shape(shape)); + shapeName = theResultBody->addShapeName(aShapeGeom, shapeName); + + + shape.Location(isRef ? loc : partLoc); + int dim = + (shape.ShapeType() == TopAbs_VERTEX) ? + 0 : + (shape.ShapeType() == TopAbs_EDGE || shape.ShapeType() == TopAbs_WIRE) ? + 1 : + (shape.ShapeType() == TopAbs_FACE || + shape.ShapeType() == TopAbs_SHELL) ? 2 :3; + + Handle(TCollection_HAsciiString) matName; + Handle(TCollection_HAsciiString) matDescription; + Standard_Real matDensity; + Handle(TCollection_HAsciiString) matDensName; + Handle(TCollection_HAsciiString) matDensValType; + + if(materialTool->GetMaterial(label, matName, matDescription, matDensity, + matDensName, matDensValType)) { + std::wstring nameMaterial = Locale::Convert::toWString(matName->ToCString()); + + theMaterialShape[nameMaterial].push_back(shapeName); + } + + + Quantity_Color col; + if(colorTool->GetColor(label, XCAFDoc_ColorGen, col)) { + double r = col.Red(), g = col.Green(), b = col.Blue(); + std::vector ColRGB = {int(r*255),int(g*255),int(b*255)}; + theResultBody->addShapeColor(shapeName, ColRGB); + } + else if(colorTool->GetColor(label, XCAFDoc_ColorSurf, col)) { + double r = col.Red(), g = col.Green(), b = col.Blue(); + std::vector ColRGB = {int(r*255),int(g*255),int(b*255)}; + theResultBody->addShapeColor(shapeName, ColRGB); + } + else if(colorTool->GetColor(label, XCAFDoc_ColorCurv, col)) { + double r = col.Red(), g = col.Green(), b = col.Blue(); + std::vector ColRGB = {int(r*255),int(g*255),int(b*255)}; + theResultBody->addShapeColor(shapeName, ColRGB); + } + // check explicit coloring of boundary entities + if(dim == 3) { + TopExp_Explorer xp2(shape, TopAbs_FACE); + while(xp2.More()) { + if(colorTool->GetColor(xp2.Current(), XCAFDoc_ColorGen, col) || + colorTool->GetColor(xp2.Current(), XCAFDoc_ColorSurf, col) || + colorTool->GetColor(xp2.Current(), XCAFDoc_ColorCurv, col)) { + double r = col.Red(), g = col.Green(), b = col.Blue(); + TopoDS_Face face = TopoDS::Face(xp2.Current()); + std::vector ColRGB = {int(r*255),int(g*255),int(b*255)}; + std::wstringstream aNameFace; + TopoDS_Shape shapeface = xp2.Current(); + if (!loc.IsIdentity()){ + shapeface.Move(loc); + } + aShapeGeom->setImpl(new TopoDS_Shape(shapeface)); + theResultBody->addShapeColor( + theResultBody->addShapeName(aShapeGeom , aNameFace.str()), ColRGB); + } + xp2.Next(); + } + } + if(dim == 2) { + TopExp_Explorer xp1(shape, TopAbs_EDGE); + while(xp1.More()) { + if(colorTool->GetColor(xp1.Current(), XCAFDoc_ColorGen, col) || + colorTool->GetColor(xp1.Current(), XCAFDoc_ColorSurf, col) || + colorTool->GetColor(xp1.Current(), XCAFDoc_ColorCurv, col)) { + double r = col.Red(), g = col.Green(), b = col.Blue(); + std::vector ColRGB = {int(r*255),int(g*255),int(b*255)}; + std::wstringstream aNameEdge; + aNameEdge << L"Edge_"<< shapeName; + aShapeGeom->setImpl(new TopoDS_Shape(xp1.Current() )); + theResultBody->addShapeColor( + theResultBody->addShapeName(aShapeGeom , aNameEdge.str()), ColRGB); + } + xp1.Next(); + } + } + } + else { + int indiceChild = 1; + + if (!shapeTool->IsReference(label)){ + TopoDS_Shape shape = shapeTool->GetShape(label); + + std::shared_ptr aShapeGeom(new GeomAPI_Shape); + if (!loc.IsIdentity()){ + shape.Move(loc); + } + aShapeGeom->setImpl(new TopoDS_Shape(shape)); + shapeName = theResultBody->addShapeName(aShapeGeom, shapeName); + } + for(TDF_ChildIterator it(label); it.More(); it.Next()) { + + setShapeAttributes( shapeTool, colorTool, materialTool, + it.Value(), partLoc,theResultBody,theMaterialShape, isRef); + indiceChild++; + } + } +} + + +//============================================================================= + /*! + * StoreMaterial() + */ + //============================================================================= + + void StoreMaterial( std::shared_ptr theResultBody, + const Handle(Standard_Transient) &theEnti, + const TopTools_IndexedMapOfShape &theIndices, + const Handle(Transfer_TransientProcess) &theTP, + const TDF_Label &theShapeLabel, + std::map< std::wstring, std::list> &theMaterialShape ) + { + // Treat Product Definition Shape only. + Handle(StepRepr_ProductDefinitionShape) aPDS = + Handle(StepRepr_ProductDefinitionShape)::DownCast(theEnti); + Handle(StepBasic_ProductDefinition) aProdDef; + + if(aPDS.IsNull() == Standard_False) { + // Product Definition Shape ==> Product Definition + aProdDef = aPDS->Definition().ProductDefinition(); + } + + if (aProdDef.IsNull() == Standard_False) { + // Product Definition ==> Property Definition + const Interface_Graph &aGraph = theTP->Graph(); + Interface_EntityIterator aSubs = aGraph.Sharings(aProdDef); + TopoDS_Shape aShape; + + for(aSubs.Start(); aSubs.More(); aSubs.Next()) { + Handle(StepRepr_PropertyDefinition) aPropD = + Handle(StepRepr_PropertyDefinition)::DownCast(aSubs.Value()); + + if(aPropD.IsNull() == Standard_False) { + // Property Definition ==> Representation. + Interface_EntityIterator aSubs1 = aGraph.Sharings(aPropD); + + for(aSubs1.Start(); aSubs1.More(); aSubs1.Next()) { + Handle(StepRepr_PropertyDefinitionRepresentation) aPDR = + Handle(StepRepr_PropertyDefinitionRepresentation):: + DownCast(aSubs1.Value()); + + if(aPDR.IsNull() == Standard_False) { + // Property Definition ==> Material Name. + Handle(StepRepr_Representation) aRepr = aPDR->UsedRepresentation(); + + if(aRepr.IsNull() == Standard_False) { + Standard_Integer ir; + + for(ir = 1; ir <= aRepr->NbItems(); ir++) { + Handle(StepRepr_RepresentationItem) aRI = aRepr->ItemsValue(ir); + Handle(StepRepr_DescriptiveRepresentationItem) aDRI = + Handle(StepRepr_DescriptiveRepresentationItem)::DownCast(aRI); + + if(aDRI.IsNull() == Standard_False) { + // Get shape from Product Definition + Handle(TCollection_HAsciiString) aMatName = aDRI->Name(); + if(aMatName.IsNull() == Standard_False) { + TCollection_ExtendedString + aMatNameExt (aMatName->ToCString()); + + if (aShape.IsNull()) { + //Get the shape. + aShape = GetShape(aProdDef, theTP); + if (aShape.IsNull()) { + return; + } + } + + // as PRODUCT can be included in the main shape + // several times, we look here for all iclusions. + Standard_Integer isub, nbSubs = theIndices.Extent(); + + for (isub = 1; isub <= nbSubs; isub++) { + TopoDS_Shape aSub = theIndices.FindKey(isub); + + if (aSub.IsPartner(aShape)) { + std::shared_ptr aShapeGeom(new GeomAPI_Shape); + aShapeGeom->setImpl(new TopoDS_Shape(aSub)); + std::wstring nom = theResultBody->findShapeName(aShapeGeom); + std::wstring matName= Locale::Convert::toWString(aMatName->ToCString()); + theMaterialShape[matName].push_back(nom); + + } + } + } + } + } + } + } + } + } + } + } + } + diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.h b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.h new file mode 100644 index 000000000..2955a5308 --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.h @@ -0,0 +1,74 @@ +// Copyright (C) 2014-2020 CEA/DEN, EDF R&D +// +// 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 +// + +#ifndef GEOMALGOAPI_STEPIMPORTXCAF_H_ +#define GEOMALGOAPI_STEPIMPORTXCAF_H_ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + + // read Attributs of step file + GEOMALGOAPI_EXPORT + std::shared_ptr readAttributes( STEPCAFControl_Reader &reader, + std::shared_ptr theResultBody, + const bool anMaterials, + std::map< std::wstring, std::list> &theMaterialShape, + const std::string &format); + // read attributs for label + GEOMALGOAPI_EXPORT + void setShapeAttributes(const Handle(XCAFDoc_ShapeTool) &shapeTool, + const Handle(XCAFDoc_ColorTool) &colorTool, + const Handle(XCAFDoc_MaterialTool) &materialTool, + const TDF_Label &label, + const TopLoc_Location &loc, + std::shared_ptr theResultBody, + std::map< std::wstring, std::list> &theMaterialShape, + bool isRef); + +// read geometry +GEOMALGOAPI_EXPORT +std::shared_ptr setgeom(const Handle(XCAFDoc_ShapeTool) &shapeTool, + const TDF_Label &label); + +// store Materiel for theShapeLabel in the map theMaterialShape +GEOMALGOAPI_EXPORT +void StoreMaterial( std::shared_ptr theResultBody, + const Handle(Standard_Transient) &theEnti, + const TopTools_IndexedMapOfShape &theIndices, + const Handle(Transfer_TransientProcess) &theTP, + const TDF_Label &theShapeLabel, + std::map< std::wstring, std::list> &theMaterialShape ); + +#endif /* GEOMALGOAPI_STEPIMPORTXCAF_H_ */ diff --git a/src/Model/Model_Objects.cpp b/src/Model/Model_Objects.cpp index 2c32988e2..63380e3e2 100644 --- a/src/Model/Model_Objects.cpp +++ b/src/Model/Model_Objects.cpp @@ -1226,7 +1226,8 @@ bool Model_Objects::hasCustomName(DataPtr theFeatureData, void Model_Objects::storeResult(std::shared_ptr theFeatureData, std::shared_ptr theResult, - const int theResultIndex) + const int theResultIndex, + const std::wstring& theNameShape) { theResult->init(); theResult->setDoc(myDoc); @@ -1240,11 +1241,15 @@ void Model_Objects::storeResult(std::shared_ptr theFeatureData, theResult->data()->setName(L""); } else { std::wstringstream aName; - aName << aNewName; - // if there are several results (issue #899: any number of result), - // add unique prefix starting from second - if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group()) - aName << "_" << theResultIndex + 1; + if( theNameShape != L"" ){ + aName << theNameShape; + }else{ + aName << aNewName; + // if there are several results (issue #899: any number of result), + // add unique prefix starting from second + if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group()) + aName << "_" << theResultIndex + 1; + } aNewName = aName.str(); } theResult->data()->setName(aNewName); @@ -1269,7 +1274,7 @@ std::shared_ptr Model_Objects::createConstruction( } std::shared_ptr Model_Objects::createBody( - const std::shared_ptr& theFeatureData, const int theIndex) + const std::shared_ptr& theFeatureData, const int theIndex,const std::wstring& theNameShape ) { TDF_Label aLab = resultLabel(theFeatureData, theIndex); TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str()); @@ -1280,7 +1285,7 @@ std::shared_ptr Model_Objects::createBody( } if (!aResult.get()) { aResult = std::shared_ptr(new Model_ResultBody); - storeResult(theFeatureData, aResult, theIndex); + storeResult(theFeatureData, aResult, theIndex,theNameShape); } return aResult; } diff --git a/src/Model/Model_Objects.h b/src/Model/Model_Objects.h index c929be8ce..fe3811543 100644 --- a/src/Model/Model_Objects.h +++ b/src/Model/Model_Objects.h @@ -124,7 +124,8 @@ class Model_Objects const std::shared_ptr& theFeatureData, const int theIndex = 0); /// Creates a body result std::shared_ptr createBody( - const std::shared_ptr& theFeatureData, const int theIndex = 0); + const std::shared_ptr& theFeatureData, const int theIndex = 0, + const std::wstring& theNameShape = L""); /// Creates a part result std::shared_ptr createPart( const std::shared_ptr& theFeatureData, const int theIndex = 0); @@ -219,12 +220,13 @@ class Model_Objects //! Initializes the data fields of the feature void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag); - - //! Allows to store the result in the data tree of the document + + //! Allows to store the result in the data tree of the document //! (attaches 'data' of result to tree) void storeResult(std::shared_ptr theFeatureData, std::shared_ptr theResult, - const int theResultIndex = 0); + const int theResultIndex = 0, + const std::wstring& theNameShape = L""); //! returns the label of result by index; creates this label if it was not created before TDF_Label resultLabel(const std::shared_ptr& theFeatureData, diff --git a/src/Model/Model_ResultBody.cpp b/src/Model/Model_ResultBody.cpp index 7d6918c6a..74b932c81 100644 --- a/src/Model/Model_ResultBody.cpp +++ b/src/Model/Model_ResultBody.cpp @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include @@ -233,6 +235,54 @@ void Model_ResultBody::updateConcealment() } } +void Model_ResultBody::addShapeColor( const std::wstring& theName,std::vector& color) { + + if( myColorsShape.find(theName) == myColorsShape.end()) + myColorsShape[ theName ] = color; +} + +std::wstring Model_ResultBody::addShapeName(std::shared_ptr theshape,const std::wstring& theName ){ + + int indice = 1; + std::wstringstream aName; + aName << theName; + while(myNamesShape.find(aName.str()) != myNamesShape.end() ){ + aName.str(L""); + aName << theName << L"__" << indice; + indice++; + } + myNamesShape[ aName.str() ] = theshape; + + return aName.str(); +} + +std::wstring Model_ResultBody::findShapeName(std::shared_ptr theshape){ + + TopoDS_Shape aShape = theshape->impl(); + for (std::map< std::wstring, std::shared_ptr >::iterator it = myNamesShape.begin(); + it != myNamesShape.end(); + ++it) + { + TopoDS_Shape curSelectedShape = (*it).second->impl(); + if( (aShape.IsSame(curSelectedShape))) { + return (*it).first; + } + + } + return L"material not found" ; +} + + +void Model_ResultBody::setShapeName(std::map< std::wstring, std::shared_ptr > &theshapename, + std::map< std::wstring, std::vector> & theColorsShape) +{ + myNamesShape = theshapename; + myColorsShape = theColorsShape; +} +void Model_ResultBody::clearShapeNameAndColor(){ + myNamesShape.clear(); + myColorsShape.clear(); +} void Model_ResultBody::updateSubs(const std::shared_ptr& theThisShape, const bool theShapeChanged) { @@ -262,13 +312,31 @@ void Model_ResultBody::updateSubs(const std::shared_ptr& theThisS aShape->setImpl(new TopoDS_Shape(aShapesIter.Value())); ResultBodyPtr aSub; if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result - aSub = anObjects->createBody(this->data(), aSubIndex); + std::wstring thenameshape = L""; + // find shape name read + for (std::map< std::wstring, std::shared_ptr >::iterator it = myNamesShape.begin(); + it != myNamesShape.end(); + ++it) + { + TopoDS_Shape curSelectedShape = (*it).second->impl(); + if( !(aShapesIter.Value().IsSame(curSelectedShape))) continue; + thenameshape = (*it).first; + break; + } + aSub = anObjects->createBody(this->data(), aSubIndex,thenameshape); + //finf color read + std::map< std::wstring, std::vector>::iterator itColor = myColorsShape.find(thenameshape); + if(itColor != myColorsShape.end()){ + ModelAPI_Tools::setColor(aSub,(*itColor).second); + } + aSub->setShapeName(myNamesShape,myColorsShape); mySubs.push_back(aSub); mySubsMap[aSub] = int(mySubs.size() - 1); if (isConcealed()) { // for issue #2579 note7 aSub->ModelAPI_ResultBody::setIsConcealed(true); std::dynamic_pointer_cast(aSub)->updateConcealment(); } + } else { // just update shape of this result aSub = mySubs[aSubIndex]; } diff --git a/src/Model/Model_ResultBody.h b/src/Model/Model_ResultBody.h index 94e9d9b7c..fe3987642 100644 --- a/src/Model/Model_ResultBody.h +++ b/src/Model/Model_ResultBody.h @@ -117,7 +117,7 @@ protected: /// Makes a body on the given feature Model_ResultBody(); - /// Updates the sub-bodies if shape of this object is composite-solid +/// Updates the sub-bodies if shape of this object is composite-solid void updateSubs(const std::shared_ptr& theThisShape, const bool theShapeChanged = true); @@ -134,6 +134,23 @@ protected: const std::list& theAllOlds, std::list& theOldForSub); friend class Model_Objects; + + // Add shape Name for read shape in step file + std::wstring addShapeName(std::shared_ptr,const std::wstring& theName) override; + // Add color for shape Name read shape in step file + void addShapeColor( const std::wstring& theName,std::vector& color) override; + // Set the map of name and color read shape in step file + void setShapeName(std::map< std::wstring, std::shared_ptr > &theshapename,std::map< std::wstring, std::vector> & theColorsShape) override; + // find the name of shapp read in step file + std::wstring findShapeName(std::shared_ptr theshape) override; + // Clear the map of name and color read shape in step file + void clearShapeNameAndColor() override; + + // map with the name read in step file and shape + std::map< std::wstring, std::shared_ptr > myNamesShape; + // map with the name contruct and color read + std::map< std::wstring, std::vector> myColorsShape; + }; #endif diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index dd2f4acd9..deefff6c1 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -118,6 +118,7 @@ SET(PROJECT_LIBRARIES Config GeomAPI Locale + ${OpenCASCADE_ApplicationFramework_LIBRARIES} ) SET(CMAKE_SWIG_FLAGS -threads -w325,321,362,383,302,403,451,473) ADD_DEFINITIONS(-DMODELAPI_EXPORTS) @@ -131,6 +132,10 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/Config ${PROJECT_SOURCE_DIR}/src/GeomAPI ${PROJECT_SOURCE_DIR}/src/GeomAlgoAPI ${PROJECT_SOURCE_DIR}/src/Locale + ${OpenCASCADE_INCLUDE_DIR} + ${OpenCASCADE_DataExchange_LIBRARIES} + ${OpenCASCADE_ModelingAlgorithms_LIBRARIES} + ${OpenCASCADE_ApplicationFramework_LIBRARIES} ) diff --git a/src/ModelAPI/ModelAPI_ResultBody.h b/src/ModelAPI/ModelAPI_ResultBody.h index c88fb08fc..11fdd2421 100644 --- a/src/ModelAPI/ModelAPI_ResultBody.h +++ b/src/ModelAPI/ModelAPI_ResultBody.h @@ -24,6 +24,8 @@ #include #include #include +#include +#include class ModelAPI_BodyBuilder; class GeomAlgoAPI_MakeShape; @@ -182,11 +184,26 @@ public: /// Cleans cash related to the already stored elements MODELAPI_EXPORT virtual void cleanCash() = 0; + + // Add shape Name for read shape in step file + MODELAPI_EXPORT virtual std::wstring addShapeName(std::shared_ptr,const std::wstring& theName) = 0; + // Add color for shape Name read shape in step file + MODELAPI_EXPORT virtual void addShapeColor(const std::wstring& theName,std::vector& color) = 0; + // Set the map of name and color read shape in step file + MODELAPI_EXPORT virtual void setShapeName(std::map< std::wstring, std::shared_ptr > &theshapename, + std::map< std::wstring, std::vector> & theColorsShape) = 0; + // Clear the map of name and color read shape in step file + MODELAPI_EXPORT virtual void clearShapeNameAndColor() = 0; + // find the name of shapp read in step file + MODELAPI_EXPORT virtual std::wstring findShapeName(std::shared_ptr theshape) = 0; + protected: /// Default constructor accessible only from Model_Objects MODELAPI_EXPORT ModelAPI_ResultBody(); + + }; //! Pointer on feature object diff --git a/src/PythonAPI/model/exchange/__init__.py b/src/PythonAPI/model/exchange/__init__.py index 0e71bf080..84b62bcc3 100644 --- a/src/PythonAPI/model/exchange/__init__.py +++ b/src/PythonAPI/model/exchange/__init__.py @@ -19,7 +19,7 @@ """Package for Exchange plugin for the Parametric Geometry API of the Modeler. """ -from ExchangeAPI import addImport, exportToFile, exportToXAO, exportToSTL +from ExchangeAPI import addImport, addImportStep, exportToFile, exportToXAO, exportToSTL from ExchangeAPI import exportPart, importPart from .tools import *