From b86ed875ca3ab2b1d10a5d4d06a6954820849bed Mon Sep 17 00:00:00 2001 From: rraphael Date: Sat, 5 Dec 2020 17:51:22 +0100 Subject: [PATCH] add Picture import --- src/ExchangePlugin/ExchangePlugin_Import.cpp | 135 +++++++++------ src/ExchangePlugin/ExchangePlugin_Import.h | 114 +++++++++---- .../ExchangePlugin_ImportFeature.cpp | 107 ++++++++---- .../ExchangePlugin_ImportFeature.h | 155 ++++++++++++------ src/ExchangePlugin/ExchangePlugin_Plugin.cpp | 6 + src/ExchangePlugin/plugin-Exchange.xml | 22 ++- src/GeomAlgoAPI/CMakeLists.txt | 6 + src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.cpp | 99 +++++++++++ src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.h | 32 ++++ src/ModelAPI/ModelAPI_Object.h | 16 ++ src/PartSet/PartSet_Module.cpp | 38 +++++ src/XGUI/XGUI_Workshop.cpp | 19 +++ src/XGUI/XGUI_Workshop.h | 3 + 13 files changed, 586 insertions(+), 166 deletions(-) create mode 100644 src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.cpp create mode 100644 src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.h diff --git a/src/ExchangePlugin/ExchangePlugin_Import.cpp b/src/ExchangePlugin/ExchangePlugin_Import.cpp index e9b041582..6157782b1 100644 --- a/src/ExchangePlugin/ExchangePlugin_Import.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Import.cpp @@ -67,25 +67,22 @@ DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::wstring& thePartN } -ExchangePlugin_Import::ExchangePlugin_Import() -{ -} - -ExchangePlugin_Import::~ExchangePlugin_Import() -{ - // TODO Auto-generated destructor stub -} - /* * Request for initialization of data model of the feature: adding all attributes */ -void ExchangePlugin_Import::initAttributes() +void ExchangePlugin_ImportBase::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(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId()); +} + +void ExchangePlugin_Import::initAttributes() +{ + ExchangePlugin_ImportBase::initAttributes(); + + data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::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()); @@ -96,12 +93,12 @@ void ExchangePlugin_Import::initAttributes() /* * Computes or recomputes the results */ + void ExchangePlugin_Import::execute() { AttributeStringPtr aFormatAttr = this->string(ExchangePlugin_Import::IMPORT_TYPE_ID()); std::string aFormat = aFormatAttr->value(); - AttributeStringPtr aFilePathAttr; std::string aFilePath; AttributeStringArrayPtr aPartsAttr; @@ -142,23 +139,45 @@ void ExchangePlugin_Import::execute() } AttributeStringPtr aImportTypeAttr = - aData->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID()); + aData->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID()); aData->boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID()) - ->setValue(boolean(ExchangePlugin_Import::STEP_MATERIALS_ID())->value()); + ->setValue(boolean(ExchangePlugin_Import::STEP_MATERIALS_ID())->value()); aData->boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID()) - ->setValue(boolean(ExchangePlugin_Import::STEP_COLORS_ID())->value()); + ->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()); + ->setValue(boolean(ExchangePlugin_Import::STEP_SCALE_INTER_UNITS_ID())->value()); aPathAttr->setValue(aFilePathAttr->value()); aImportTypeAttr->setValue(aFormat); - aImportFeature->execute(); } } +void ExchangePlugin_Import_Image::execute() +{ + AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportBase::FILE_PATH_ID()); + std::string aFilePath = aFilePathAttr->value(); + if (aFilePath.empty()) { + setError("File path is empty."); + return; + } + + // get the document where to import + AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); + AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID()); + SessionPtr aSession = ModelAPI_Session::get(); + DocumentPtr aDoc = findDocument(aSession->moduleDocument(), + Locale::Convert::toWString(aPartsAttr->value(aTargetAttr->value()))); + if (aDoc.get()) { + FeaturePtr aImportFeature = aDoc->addFeature(ExchangePlugin_Import_ImageFeature::ID()); + DataPtr aData = aImportFeature->data(); + AttributeStringPtr aPathAttr = aData->string(ExchangePlugin_Import_ImageFeature::FILE_PATH_ID()); + aPathAttr->setValue(aFilePathAttr->value()); + aImportFeature->execute(); + } +} void ExchangePlugin_Import::attributeChanged(const std::string& theID) { AttributeStringPtr aFilePathAttr; @@ -184,41 +203,55 @@ void ExchangePlugin_Import::attributeChanged(const std::string& theID) } } -void ExchangePlugin_Import::updatePart(AttributeStringArrayPtr& thePartsAttr, - AttributeIntegerPtr& theTargetAttr) +void ExchangePlugin_Import_Image::attributeChanged(const std::string& theID) { - // update the list of target parts - SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDoc = document(); - bool isPartSet = aDoc == aSession->moduleDocument(); - if (isPartSet) { - std::list anAcceptedValues; - anAcceptedValues.push_back(THE_NEW_PART_STR); - - // append names of all parts - std::list aSubFeatures = aDoc->allFeatures(); - for (std::list::iterator aFIt = aSubFeatures.begin(); - aFIt != aSubFeatures.end(); ++aFIt) { - if ((*aFIt)->getKind() == PartSetPlugin_Part::ID()) - anAcceptedValues.push_back((*aFIt)->name()); - } + if (theID == FILE_PATH_ID()) { + AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID()); + if (aFilePathAttr->value().empty()) + return; - if ((size_t)thePartsAttr->size() != anAcceptedValues.size()) - theTargetAttr->setValue(0); + AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); + AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID()); + updatePart(aPartsAttr, aTargetAttr); + } +} - thePartsAttr->setSize((int)anAcceptedValues.size()); - std::list::iterator anIt = anAcceptedValues.begin(); - for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd) - thePartsAttr->setValue(anInd, Locale::Convert::toString(*anIt)); +void ExchangePlugin_ImportBase::updatePart(AttributeStringArrayPtr& aPartsAttr, + AttributeIntegerPtr& aTargetAttr) +{ + + // update the list of target parts + SessionPtr aSession = ModelAPI_Session::get(); + DocumentPtr aDoc = document(); + bool isPartSet = aDoc == aSession->moduleDocument(); + if (isPartSet) { + std::list anAcceptedValues; + anAcceptedValues.push_back(THE_NEW_PART_STR); + + // append names of all parts + std::list aSubFeatures = aDoc->allFeatures(); + for (std::list::iterator aFIt = aSubFeatures.begin(); + aFIt != aSubFeatures.end(); ++aFIt) { + if ((*aFIt)->getKind() == PartSetPlugin_Part::ID()) + anAcceptedValues.push_back((*aFIt)->name()); } - else { - // keep only the name of the current part - if (thePartsAttr->size() == 0) { - FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc); - - thePartsAttr->setSize(1); - thePartsAttr->setValue(0, Locale::Convert::toString(aPartFeature->name())); - theTargetAttr->setValue(0); - } + + if ((size_t)aPartsAttr->size() != anAcceptedValues.size()) + aTargetAttr->setValue(0); + + aPartsAttr->setSize((int)anAcceptedValues.size()); + std::list::iterator anIt = anAcceptedValues.begin(); + for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd) + aPartsAttr->setValue(anInd, Locale::Convert::toString(*anIt)); + } + else { + // keep only the name of the current part + if (aPartsAttr->size() == 0) { + FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc); + + aPartsAttr->setSize(1); + aPartsAttr->setValue(0, Locale::Convert::toString(aPartFeature->name())); + aTargetAttr->setValue(0); } + } } diff --git a/src/ExchangePlugin/ExchangePlugin_Import.h b/src/ExchangePlugin/ExchangePlugin_Import.h index eeac51794..7bb292894 100644 --- a/src/ExchangePlugin/ExchangePlugin_Import.h +++ b/src/ExchangePlugin/ExchangePlugin_Import.h @@ -26,9 +26,9 @@ #include #include #include - #include + /** * \class ExchangePlugin_ImportFeature * \ingroup Plugins @@ -36,21 +36,9 @@ * * The list of supported formats is defined in the configuration file. */ -class ExchangePlugin_Import : public ModelAPI_Feature +class ExchangePlugin_ImportBase : public ModelAPI_Feature { - public: - /// Feature kind - inline static const std::string& ID() - { - 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; - } + public: /// attribute name of file path inline static const std::string& FILE_PATH_ID() { @@ -69,7 +57,50 @@ 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 + /// Default constructor + EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportBase()= default; + /// Default destructor + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_ImportBase()= default; + + + /// Request for initialization of data model of the feature: adding all attributes + EXCHANGEPLUGIN_EXPORT virtual void initAttributes(); + + /// Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + EXCHANGEPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID) = 0; + + /// Computes or recomputes the results + EXCHANGEPLUGIN_EXPORT virtual void execute() = 0; + + /// Returns true if this feature is used as macro: creates other features and then removed. + EXCHANGEPLUGIN_EXPORT virtual bool isMacro() const { return true; } + + /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false. + EXCHANGEPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; } + + protected: + + EXCHANGEPLUGIN_EXPORT void updatePart(AttributeStringArrayPtr& aPartsAttr, + AttributeIntegerPtr& aTargetAttr); +}; + +EXCHANGEPLUGIN_EXPORT class ExchangePlugin_Import : public ExchangePlugin_ImportBase +{ + public: + /// Feature kind + inline static const std::string& ID() + { + 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 step file path inline static const std::string& STEP_FILE_PATH_ID() { static const std::string MY_STEP_FILE_PATH_ID("step_file_path"); @@ -106,36 +137,55 @@ class ExchangePlugin_Import : public ModelAPI_Feature return MY_STEP_COLORS_ID; } /// Default constructor - EXCHANGEPLUGIN_EXPORT ExchangePlugin_Import(); + EXCHANGEPLUGIN_EXPORT ExchangePlugin_Import() = default; /// Default destructor - EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_Import(); + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_Import() = default; /// Returns the unique kind of a feature - EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() override { return ExchangePlugin_Import::ID(); } + /// Computes or recomputes the results + EXCHANGEPLUGIN_EXPORT virtual void execute() override; /// Request for initialization of data model of the feature: adding all attributes EXCHANGEPLUGIN_EXPORT virtual void initAttributes(); - /// Called on change of any argument-attribute of this object - /// \param theID identifier of changed attribute - EXCHANGEPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + // Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + EXCHANGEPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID) override; - /// Computes or recomputes the results - EXCHANGEPLUGIN_EXPORT virtual void execute(); +}; - /// Returns true if this feature is used as macro: creates other features and then removed. - EXCHANGEPLUGIN_EXPORT virtual bool isMacro() const { return true; } +EXCHANGEPLUGIN_EXPORT class ExchangePlugin_Import_Image : public ExchangePlugin_ImportBase +{ + public: + /// Feature kind + inline static const std::string& ID() + { + static const std::string MY_IMPORT_ID("ImportImageMacro"); + return MY_IMPORT_ID; + } - /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false. - EXCHANGEPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; } + /// Default constructor + EXCHANGEPLUGIN_EXPORT ExchangePlugin_Import_Image() = default; + /// Default destructor + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_Import_Image() = default; + + /// Returns the unique kind of a feature + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() override + { + return ExchangePlugin_Import_Image::ID(); + } + + /// Computes or recomputes the results + EXCHANGEPLUGIN_EXPORT virtual void execute() override; + + // Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + EXCHANGEPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID) override; - private: - /// Called to update part - EXCHANGEPLUGIN_EXPORT - void updatePart(AttributeStringArrayPtr& thePartsAttr, AttributeIntegerPtr& theTargetAttr); }; #endif /* IMPORT_IMPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index ee9e7a798..dd87d800c 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -65,36 +66,32 @@ #include - -ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature() -{ -} - -ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature() -{ - // TODO Auto-generated destructor stub -} - /* * Request for initialization of data model of the feature: adding all attributes */ -void ExchangePlugin_ImportFeature::initAttributes() +void ExchangePlugin_ImportFeatureBase::initAttributes() { - data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), + data()->addAttribute(ExchangePlugin_ImportFeatureBase::FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); AttributePtr aFeaturesAttribute = - data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), + data()->addAttribute(ExchangePlugin_ImportFeatureBase::FEATURES_ID(), ModelAPI_AttributeRefList::typeId()); + aFeaturesAttribute->setIsArgument(false); + + ModelAPI_Session::get()->validators()->registerNotObligatory( + getKind(), ExchangePlugin_ImportFeatureBase::FEATURES_ID()); +} + +void ExchangePlugin_ImportFeature::initAttributes() +{ + ExchangePlugin_ImportFeatureBase::initAttributes(); + 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()); + ModelAPI_Session::get()->validators()->registerNotObligatory( getKind(), ExchangePlugin_ImportFeature::STEP_COLORS_ID()); ModelAPI_Session::get()->validators()->registerNotObligatory( @@ -106,7 +103,6 @@ void ExchangePlugin_ImportFeature::initAttributes() ModelAPI_Session::get()->validators()->registerNotObligatory( getKind(), ExchangePlugin_ImportFeature::FILE_PATH_ID()); } - /* * Computes or recomputes the results */ @@ -130,12 +126,23 @@ void ExchangePlugin_ImportFeature::execute() importFile(aFilePath); } -std::shared_ptr ExchangePlugin_ImportFeature::createResultBody( - std::shared_ptr theGeomShape) +void ExchangePlugin_Import_ImageFeature::execute() +{ + AttributeStringPtr aFilePathAttr = string(ExchangePlugin_Import_ImageFeature::FILE_PATH_ID()); + std::string aFilePath = aFilePathAttr->value(); + if (aFilePath.empty()) { + setError("File path is empty."); + return; + } + importFile(aFilePath); +} + +std::shared_ptr ExchangePlugin_ImportFeatureBase::createResultBody( + std::shared_ptr aGeomShape) { std::shared_ptr aResultBody = document()->createBody(data()); //LoadNamingDS of the imported shape - loadNamingDS(theGeomShape, aResultBody); + loadNamingDS(aGeomShape, aResultBody); return aResultBody; } @@ -152,7 +159,6 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) // Perform the import std::string anError; std::shared_ptr aGeomShape; - std::map> theMaterialShape; std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName); @@ -163,7 +169,6 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) 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") { @@ -190,7 +195,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) aGeomShape = IGESImport(theFileName, anExtension, anError); } else if (anExtension == "STL") { aGeomShape = STLImport(theFileName, anError); - } else { + } else { anError = "Unsupported format: " + anExtension; } @@ -201,7 +206,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) } // Pass the results into the model - + loadNamingDS(aGeomShape, aResult); // create color group @@ -536,7 +541,7 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) } //============================================================================ -std::shared_ptr ExchangePlugin_ImportFeature::addFeature( +std::shared_ptr ExchangePlugin_ImportFeatureBase::addFeature( std::string theID) { std::shared_ptr aNew = document()->addFeature(theID, false); @@ -548,7 +553,7 @@ std::shared_ptr ExchangePlugin_ImportFeature::addFeature( } // LCOV_EXCL_START -void ExchangePlugin_ImportFeature::removeFeature( +void ExchangePlugin_ImportFeatureBase::removeFeature( std::shared_ptr theFeature) { if (!data()->isValid()) @@ -558,12 +563,12 @@ void ExchangePlugin_ImportFeature::removeFeature( } // LCOV_EXCL_STOP -int ExchangePlugin_ImportFeature::numberOfSubs(bool /*forTree*/) const +int ExchangePlugin_ImportFeatureBase::numberOfSubs(bool /*forTree*/) const { return data()->reflist(FEATURES_ID())->size(true); } -std::shared_ptr ExchangePlugin_ImportFeature::subFeature( +std::shared_ptr ExchangePlugin_ImportFeatureBase::subFeature( const int theIndex, bool /*forTree*/) { ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false); @@ -572,7 +577,7 @@ std::shared_ptr ExchangePlugin_ImportFeature::subFeature( } // LCOV_EXCL_START -int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const +int ExchangePlugin_ImportFeatureBase::subFeatureId(const int theIndex) const { std::shared_ptr aRefList = std::dynamic_pointer_cast< ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID())); @@ -591,7 +596,7 @@ int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const } // LCOV_EXCL_STOP -bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const +bool ExchangePlugin_ImportFeatureBase::isSub(ObjectPtr theObject) const { // check is this feature of result FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); @@ -601,12 +606,48 @@ bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const } //============================================================================ -void ExchangePlugin_ImportFeature::loadNamingDS( +void ExchangePlugin_ImportFeatureBase::loadNamingDS( std::shared_ptr theGeomShape, std::shared_ptr theResultBody) { //load result theResultBody->store(theGeomShape); + std::string aNameMS = "Shape"; theResultBody->loadFirstLevel(theGeomShape, aNameMS); } + +void ExchangePlugin_Import_ImageFeature::importFile(const std::string& theFileName) +{ + + std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName); + std::string theTextureFileName = ""; + // Perform the import + std::string anError; + std::shared_ptr aGeomShape; + if (anExtension == "PNG" || anExtension == "GIF" || + anExtension == "TIFF" || anExtension == "JPE" || + anExtension == "JPG" || anExtension == "JPEG" || + anExtension == "BMP"|| anExtension == "PPM" + ) { + aGeomShape = ImageImport(theFileName, anError); + if(anError == "") + theTextureFileName = theFileName; + } else { + anError = "Unsupported format: " + anExtension; + } + + // Check if shape is valid + if (!anError.empty()) { + setError("An error occurred while importing " + theFileName + ": " + anError); + return; + } + + // Pass the results into the model + std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName); + data()->setName(Locale::Convert::toWString(anObjectName)); + + auto resultBody = createResultBody(aGeomShape); + resultBody->setTextureFile(theTextureFileName); + setResult(resultBody); +} diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h index 3c34eced6..fa4ac0f7f 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h @@ -35,73 +35,40 @@ * * The list of supported formats is defined in the configuration file. */ -class ExchangePlugin_ImportFeature : public ModelAPI_CompositeFeature +class ExchangePlugin_ImportFeatureBase : public ModelAPI_CompositeFeature { - public: - /// Feature kind - inline static const std::string& ID() - { - 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; - } + public: /// 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() + /// attribute name of target part + inline static const std::string& TARGET_PART_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_colors"); - return MY_STEP_COLORS_ID; + static const std::string MY_TARGET_PART_ID("target_part"); + return MY_TARGET_PART_ID; } /// Default constructor - EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeature(); + EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeatureBase() = default; /// Default destructor - EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_ImportFeature(); + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_ImportFeatureBase() = default; /// Returns the unique kind of a feature - EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() - { - return ExchangePlugin_ImportFeature::ID(); - } + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() = 0; /// Request for initialization of data model of the feature: adding all attributes EXCHANGEPLUGIN_EXPORT virtual void initAttributes(); /// Computes or recomputes the results - EXCHANGEPLUGIN_EXPORT virtual void execute(); + EXCHANGEPLUGIN_EXPORT virtual void execute() = 0; /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false. EXCHANGEPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; } @@ -126,19 +93,82 @@ class ExchangePlugin_ImportFeature : public ModelAPI_CompositeFeature protected: /// Performs the import of the file - EXCHANGEPLUGIN_EXPORT void importFile(const std::string& theFileName); - - /// Performs the import of XAO file - EXCHANGEPLUGIN_EXPORT void importXAO(const std::string& theFileName); + EXCHANGEPLUGIN_EXPORT virtual void importFile(const std::string& theFileName) = 0; /// Creates and prepares a result body from the shape std::shared_ptr createResultBody( std::shared_ptr aGeomShape); -private: /// Loads Naming data structure to the document void loadNamingDS(std::shared_ptr theGeomShape, std::shared_ptr theResultBody); +}; + +class ExchangePlugin_ImportFeature : public ExchangePlugin_ImportFeatureBase +{ +public: + /// Feature kind + inline static const std::string& ID() + { + 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& STEP_FILE_PATH_ID() + { + static const std::string MY_STEP_FILE_PATH_ID("step_file_path"); + return MY_STEP_FILE_PATH_ID; + } + + /// Default constructor + EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeature() = default; + /// Default destructor + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_ImportFeature() = default; + + /// 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_colors"); + return MY_STEP_COLORS_ID; + } + /// Returns the unique kind of a feature + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() override + { + return ExchangePlugin_ImportFeature::ID(); + } + + /// Computes or recomputes the results + EXCHANGEPLUGIN_EXPORT virtual void execute() override; + + /// Request for initialization of data model of the feature: adding all attributes + EXCHANGEPLUGIN_EXPORT virtual void initAttributes(); + +protected: + /// Performs the import of the file + EXCHANGEPLUGIN_EXPORT void importFile(const std::string& theFileName) override; + + /// Performs the import of XAO file + EXCHANGEPLUGIN_EXPORT void importXAO(const std::string& theFileName); + +private: /// Set groups of color void setColorGroups(std::shared_ptr theResultBody); @@ -153,4 +183,33 @@ private: }; +class ExchangePlugin_Import_ImageFeature : public ExchangePlugin_ImportFeatureBase +{ + public: + /// Feature kind + inline static const std::string& ID() + { + static const std::string MY_IMPORT_ID("ImportImage"); + return MY_IMPORT_ID; + } + + /// Default constructor + EXCHANGEPLUGIN_EXPORT ExchangePlugin_Import_ImageFeature() = default; + /// Default destructor + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_Import_ImageFeature() = default; + + /// Returns the unique kind of a feature + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() override + { + return ExchangePlugin_Import_ImageFeature::ID(); + } + + /// Computes or recomputes the results + EXCHANGEPLUGIN_EXPORT virtual void execute() override; + +protected: + /// Performs the import of the file + EXCHANGEPLUGIN_EXPORT void importFile(const std::string& theFileName) override; + +}; #endif /* IMPORT_IMPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp index 235ddd17f..1436dcf03 100644 --- a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp @@ -54,6 +54,9 @@ FeaturePtr ExchangePlugin_Plugin::createFeature(std::string theFeatureID) { if (theFeatureID == ExchangePlugin_ImportFeature::ID()) { return FeaturePtr(new ExchangePlugin_ImportFeature); + } else + if (theFeatureID == ExchangePlugin_Import_ImageFeature::ID()) { + return FeaturePtr(new ExchangePlugin_Import_ImageFeature); } else if (theFeatureID == ExchangePlugin_ExportFeature::ID()) { return FeaturePtr(new ExchangePlugin_ExportFeature); @@ -69,6 +72,9 @@ FeaturePtr ExchangePlugin_Plugin::createFeature(std::string theFeatureID) } else if (theFeatureID == ExchangePlugin_Import::ID()) { return FeaturePtr(new ExchangePlugin_Import); + } else + if (theFeatureID == ExchangePlugin_Import_Image::ID()) { + return FeaturePtr(new ExchangePlugin_Import_Image); } // feature of such kind is not found return FeaturePtr(); diff --git a/src/ExchangePlugin/plugin-Exchange.xml b/src/ExchangePlugin/plugin-Exchange.xml index c6f58a196..b553ded18 100644 --- a/src/ExchangePlugin/plugin-Exchange.xml +++ b/src/ExchangePlugin/plugin-Exchange.xml @@ -1,7 +1,7 @@ - @@ -49,7 +49,18 @@ label="Import to" tooltip="Select the image to import the document" /> - + + + + + + @@ -84,6 +95,13 @@ + + + + + diff --git a/src/GeomAlgoAPI/CMakeLists.txt b/src/GeomAlgoAPI/CMakeLists.txt index 1948f8161..7f5752e09 100644 --- a/src/GeomAlgoAPI/CMakeLists.txt +++ b/src/GeomAlgoAPI/CMakeLists.txt @@ -51,6 +51,7 @@ SET(PROJECT_HEADERS GeomAlgoAPI_STEPExport.h GeomAlgoAPI_STLExport.h GeomAlgoAPI_STLImport.h + GeomAlgoAPI_ImageImport.h GeomAlgoAPI_IGESExport.h GeomAlgoAPI_Transform.h GeomAlgoAPI_ShapeTools.h @@ -119,6 +120,7 @@ SET(PROJECT_SOURCES GeomAlgoAPI_STEPExport.cpp GeomAlgoAPI_STLExport.cpp GeomAlgoAPI_STLImport.cpp + GeomAlgoAPI_ImageImport.cpp GeomAlgoAPI_IGESExport.cpp GeomAlgoAPI_Transform.cpp GeomAlgoAPI_ShapeTools.cpp @@ -177,6 +179,10 @@ SET(CMAKE_SWIG_FLAGS -w325,321,362,383,302) SET_SOURCE_FILES_PROPERTIES(GeomAlgoAPI.i PROPERTIES CPLUSPLUS ON) SET_SOURCE_FILES_PROPERTIES(GeomAlgoAPI.i PROPERTIES SWIG_DEFINITIONS "-shadow") +INCLUDE(UseQtExt) +# additional include directories +INCLUDE_DIRECTORIES(${QT_INCLUDES}) + INCLUDE_DIRECTORIES( ../GeomAPI ../GeomAlgoImpl diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.cpp new file mode 100644 index 000000000..a95bf2b41 --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.cpp @@ -0,0 +1,99 @@ +// 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 + +std::shared_ptr ImageImport(const std::string& theFileName, + std::string& theError) +{ + TopoDS_Shape aResShape; + try + { + QPixmap* pixmap = new QPixmap(theFileName.c_str()); + int height = pixmap->height(); + int width = pixmap->width(); + + delete pixmap; + + TopoDS_Vertex aTriVertexes[4]; + gp_Pnt aPnt1( -0.5*width, -0.5*height, 0); + gp_Pnt aPnt2( 0.5*width, -0.5*height, 0); + gp_Pnt aPnt3( 0.5*width, 0.5*height, 0 );; + gp_Pnt aPnt4( -0.5*width, 0.5*height, 0 ); + TopoDS_Face aFace; + TopoDS_Wire aWire; + + aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1); + aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2); + aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3); + aTriVertexes[3] = BRepBuilderAPI_MakeVertex (aPnt4); + + aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2], aTriVertexes[3], Standard_True); + + BRepBuilderAPI_Sewing aSewingTool; + aSewingTool.Init (1.0e-06, Standard_True); + + TopoDS_Compound aComp; + BRep_Builder BuildTool; + BuildTool.MakeCompound(aComp); + + if(!aWire.IsNull()) + { + aFace = BRepBuilderAPI_MakeFace (aWire); + if (!aFace.IsNull()) + { + BuildTool.Add (aComp, aFace); + } + } + + aSewingTool.Load (aComp); + aSewingTool.Perform(); + aResShape = aSewingTool.SewedShape(); + if (aResShape.IsNull()) + { + aResShape = aComp; + } + } + catch (Standard_Failure const& anException) { + theError = anException.GetMessageString(); + aResShape.Nullify(); + } + + std::shared_ptr aGeomShape(new GeomAPI_Shape); + aGeomShape->setImpl(new TopoDS_Shape(aResShape)); + + return aGeomShape; +} diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.h b/src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.h new file mode 100644 index 000000000..d9798b5e0 --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ImageImport.h @@ -0,0 +1,32 @@ +// 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_IMAGEIMPORT_H_ +#define GEOMALGOAPI_IMAGEIMPORT_H_ + +#include +#include + +#include + +/// Implementation of the import STL files algorithms +GEOMALGOAPI_EXPORT +std::shared_ptr ImageImport(const std::string& theFileName, + std::string& theError); + +#endif /* GEOMALGOAPI_IMAGEIMPORT_H_ */ diff --git a/src/ModelAPI/ModelAPI_Object.h b/src/ModelAPI/ModelAPI_Object.h index a905e6d29..a8b08adc6 100644 --- a/src/ModelAPI/ModelAPI_Object.h +++ b/src/ModelAPI/ModelAPI_Object.h @@ -45,6 +45,7 @@ class ModelAPI_Object: public ModelAPI_Entity { std::shared_ptr myData; ///< manager of the data model of a feature std::shared_ptr myDoc; ///< document this object belongs to + std::string textureFile = ""; public: #ifdef DEBUG_NAMES std::wstring myName; // name of this object @@ -100,6 +101,21 @@ class ModelAPI_Object: public ModelAPI_Entity /// signal. MODELAPI_EXPORT virtual void setDisplayed(const bool theDisplay); + MODELAPI_EXPORT bool hasTextureFile() + { + return (textureFile != ""); + } + + MODELAPI_EXPORT void setTextureFile(const std::string & theTextureFile) + { + textureFile = theTextureFile; + } + + MODELAPI_EXPORT const std::string & getTextureFile() + { + return textureFile; + } + protected: /// This method is called just after creation of the object: it must initialize /// all fields, normally initialized in the constructor diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index d9648b56e..64b63156b 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -145,6 +145,9 @@ #include #include +#include +#include + #define FEATURE_ITEM_COLOR "0,0,225" @@ -1445,6 +1448,41 @@ void PartSet_Module::customizePresentation(const ObjectPtr& theObject, thePrs->setDeflection(getResultDeflection(aResult)); thePrs->setTransparency(getResultTransparency(aResult)); + + /// set texture parameters + if(aResult->hasTextureFile()) + { + Handle(AIS_InteractiveObject) anAIS = thePrs->impl(); + if (!anAIS.IsNull()) + { + std::vector aColor = {167, 167, 167 }; + thePrs->setColor(aColor[0], aColor[1], aColor[2]); + Quantity_Color myShadingColor(NCollection_Vec3(aColor[0]/255., aColor[1]/255., aColor[2]/255.)); + + Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS); + if (!anAISShape.IsNull()) + { + auto myDrawer = new Prs3d_ShadingAspect(); + if (!anAISShape->Attributes()->HasOwnShadingAspect()) + { + anAISShape->Attributes()->SetShadingAspect (myDrawer); + } + + myDrawer->SetColor(myShadingColor); + + Handle(Image_PixMap) aPixmap; + QPixmap px(aResult->getTextureFile().c_str()); + if (!px.isNull() ) + aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() ); + + anAISShape->Attributes()->ShadingAspect()->Aspect()->SetTextureMap + (new Graphic3d_Texture2Dmanual(aPixmap)); + anAISShape->Attributes()->ShadingAspect()->Aspect()->SetTextureMapOn(); + + anAISShape->SetDisplayMode(AIS_Shaded); + } + } + } } FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); if (aFeature.get()) { diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 5bc03f55e..b78a019bc 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -490,6 +490,12 @@ void XGUI_Workshop::initMenu() QKeySequence(), false, "MEN_DESK_FILE", tr("Import"), 10, 10); connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onImportShape())); + aAction = salomeConnector()->addDesktopCommand("IMPORT_IMAGE_CMD", tr("Picture..."), + tr("Import a picture from an image file"), + QIcon(), + QKeySequence(), false, "MEN_DESK_FILE", tr("Import"), 10, 10); + connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onImportImage())); + // Export sub-menu aAction = salomeConnector()->addDesktopCommand("SAVEAS_CMD", tr("Part set..."), tr("Export the current document into a native file"), @@ -1340,6 +1346,19 @@ void XGUI_Workshop::onImportShape() } } +//****************************************************** +void XGUI_Workshop::onImportImage() +{ + if (abortAllOperations()) { + ModuleBase_OperationFeature* anImportOp = dynamic_cast( + module()->createOperation(ExchangePlugin_Import_Image::ID())); + anImportOp->setHelpFileName(QString("ExchangePlugin") + QDir::separator() + + "importFeature.html"); + myPropertyPanel->updateApplyPlusButton(anImportOp->feature()); + operationMgr()->startOperation(anImportOp); + } +} + //****************************************************** void XGUI_Workshop::onExportShape() { diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index c503990f9..cc243b62a 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -410,6 +410,9 @@ signals: /// Import shape from a file void onImportShape(); + /// Import image from a file + void onImportImage(); + /// Export features to a file void onExportPart(); -- 2.39.2