From: Jérôme Date: Mon, 14 Dec 2020 15:02:51 +0000 (+0100) Subject: #20497 code refactoring X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=e16cba3409f06350277a3af628762f92bc19c0b0;p=modules%2Fshaper.git #20497 code refactoring --- diff --git a/src/ConnectorAPI/Test/TestImportSTEP.py b/src/ConnectorAPI/Test/TestImportSTEP.py index 84b6d90ce..843dec1be 100644 --- a/src/ConnectorAPI/Test/TestImportSTEP.py +++ b/src/ConnectorAPI/Test/TestImportSTEP.py @@ -31,14 +31,10 @@ import os import math from tempfile import TemporaryDirectory -import GEOM - from ModelAPI import * from salome.shaper import model -from salome.geom import geomBuilder - from GeomAPI import GeomAPI_Shape from GeomAlgoAPI import * @@ -46,7 +42,6 @@ from GeomAlgoAPI import * __updated__ = "2015-05-22" salome.salome_init(1) -geompy = geomBuilder.New() #========================================================================= # Help functions diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 6a8f36cb1..08a9cde70 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -105,17 +105,17 @@ void ExchangePlugin_ExportFeature::initAttributes() ModelAPI_AttributeSelectionList::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); - data()->addAttribute(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED(), + data()->addAttribute(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED(), ModelAPI_AttributeSelection::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE(), ModelAPI_AttributeString::typeId()); - data()->addAttribute(ExchangePlugin_ExportFeature::STL_RELATIVE(), + data()->addAttribute(ExchangePlugin_ExportFeature::STL_RELATIVE(), ModelAPI_AttributeDouble::typeId()); - + double defelection = Config_PropManager::real("Visualization", "body_deflection"); real(ExchangePlugin_ExportFeature::STL_RELATIVE())->setValue(defelection); - data()->addAttribute(ExchangePlugin_ExportFeature::STL_ABSOLUTE(), + data()->addAttribute(ExchangePlugin_ExportFeature::STL_ABSOLUTE(), ModelAPI_AttributeDouble::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE(), ModelAPI_AttributeString::typeId()); @@ -370,7 +370,7 @@ void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName) if (string(STL_FILE_TYPE())->value() == STL_FILE_TYPE_ASCII()) { anIsASCII = true; - } + } // Perform the export std::string anError; bool aResult = false; @@ -379,7 +379,7 @@ void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName) "STL", aShape, aValue, - anIsRelative, + anIsRelative, anIsASCII, anError); diff --git a/src/ExchangePlugin/ExchangePlugin_Import.h b/src/ExchangePlugin/ExchangePlugin_Import.h index 2babe4fbe..e1ebd6e2d 100644 --- a/src/ExchangePlugin/ExchangePlugin_Import.h +++ b/src/ExchangePlugin/ExchangePlugin_Import.h @@ -123,9 +123,6 @@ 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(); @@ -134,6 +131,11 @@ class ExchangePlugin_Import : public ModelAPI_Feature /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false. EXCHANGEPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; } + + private: + + EXCHANGEPLUGIN_EXPORT + void updatePart(AttributeStringArrayPtr &aPartsAttr, AttributeIntegerPtr &aTargetAttr); }; #endif /* IMPORT_IMPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index 158a1cc67..d4bb5d218 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -63,8 +63,6 @@ #include -#include -#include ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature() @@ -154,7 +152,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) std::string anError; std::shared_ptr aGeomShape; - std::map< std::wstring, std::list> theMaterialShape; + std::map< std::wstring,std::list> theMaterialShape; std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName); data()->setName(Locale::Convert::toWString(anObjectName)); @@ -171,31 +169,22 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) bool anScalInterUnits = boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())->value(); - try{ + // Process groups/fields + std::shared_ptr aRefListOfGroups = + std::dynamic_pointer_cast(data()->attribute(FEATURES_ID())); - aResult->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, aResult, anScalInterUnits, - anMaterialsGroupSelected, anColorGroupSelected, - theMaterialShape,anError); - - } - catch (OSD_Exception& e) { - //Try to load STEP file without colors... - aGeomShape = STEPImport(theFileName, anExtension,anScalInterUnits,anError); + // 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, aResult, anScalInterUnits, + anMaterialsGroupSelected, anColorGroupSelected, + theMaterialShape,anError); } else if (anExtension == "IGES" || anExtension == "IGS") { aGeomShape = IGESImport(theFileName, anExtension, anError); } else { @@ -224,6 +213,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) } setResult(aResult); + aResult->clearShapeNameAndColor(); } @@ -232,13 +222,13 @@ void ExchangePlugin_ImportFeature::setColorGroups( { std::vector aColor; int anIndice = 1; - std::list< std::vector > aColorsRead; + std::list> aColorsRead; ModelAPI_Tools::getColor(theResultBody, aColor); if (!aColor.empty() ){ std::wstringstream aColorName; aColorName <shape(); - aSelectionList->setSelectionType(aShape->shapeTypeStr() ); + aSelectionList->setSelectionType(aShape->shapeTypeStr()); aSelectionList->append(theResultBody,aShape); } } @@ -292,7 +282,7 @@ void ExchangePlugin_ImportFeature::setColorGroup( if (!aColor.empty() ){ if (aRes->get() && aColor == theColor ) { - aSelectionList->setSelectionType(aShape->shapeTypeStr() ); + aSelectionList->setSelectionType(aShape->shapeTypeStr()); aSelectionList->append(theResultBody,aShape); } } @@ -304,17 +294,17 @@ void ExchangePlugin_ImportFeature::setColorGroup( ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor); - if (aSelectionList->size() == 0 ){ + if (aSelectionList->size() == 0) { document()->removeFeature(aGroupFeature); } } void ExchangePlugin_ImportFeature::setMaterielGroup( std::shared_ptr theResultBody, - std::map< std::wstring,std::list> &theMaterialShape) + std::map> &theMaterialShape) { std::map< std::wstring, std::list>::iterator anIt; - for( anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) { + for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) { std::shared_ptr aGroupFeature = addFeature("Group"); // group name @@ -322,19 +312,18 @@ void ExchangePlugin_ImportFeature::setMaterielGroup( // fill selection AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list"); - GeomShapePtr aShape = theResultBody->shape(); std::list allRes; ModelAPI_Tools::allSubs(theResultBody, allRes); - for(std::list::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) { + for (std::list::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) { GeomShapePtr aShape = (*aRes)->shape(); - for(std::list::iterator aResMat = anIt->second.begin(); + for (std::list::iterator aResMat = anIt->second.begin(); aResMat != anIt->second.end(); ++aResMat) { if (aRes->get() && ((*aRes)->data()->name() == (*aResMat))) { aSelectionList->append(theResultBody,aShape); - aSelectionList->setSelectionType(aShape->shapeTypeStr() ); + aSelectionList->setSelectionType(aShape->shapeTypeStr()); break; } } diff --git a/src/ExchangePlugin/ExchangePlugin_msg_fr.ts b/src/ExchangePlugin/ExchangePlugin_msg_fr.ts index 72f1663ec..780a0c9a7 100644 --- a/src/ExchangePlugin/ExchangePlugin_msg_fr.ts +++ b/src/ExchangePlugin/ExchangePlugin_msg_fr.ts @@ -324,6 +324,7 @@ Mettre àl'échelle du système international d'unités + Import:step_materials Materials @@ -331,7 +332,7 @@ - Import:step_materials + Import:step_colors Colors Couleurs @@ -347,11 +348,11 @@ Import a file Importer un fichier - - + Create groups from créer des groupes à partir + ImportMacro:target_part diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp index a10212ec3..e44112135 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImport.cpp @@ -59,6 +59,46 @@ #include #include // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC +#include + +//================================================================================================= +bool readUnits(const STEPControl_Reader& aReader, + const bool theScalInterUnits, + std::string& theError) +{ + // Regard or not the model units + if (!theScalInterUnits) { + // 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."; + return false; + } + // 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"< STEPImport(const std::string& theFileName, IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str()); if (status == IFSelect_RetDone) { - // Regard or not the model units - if (!theScalInterUnits) { - // 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."; - std::shared_ptr aGeomShape(new GeomAPI_Shape); - aGeomShape->setImpl(new TopoDS_Shape(aResShape)); - 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"< aGeomShape(new GeomAPI_Shape); + aGeomShape->setImpl(new TopoDS_Shape()); + return aGeomShape; } Standard_Boolean failsonly = Standard_False; @@ -209,75 +221,54 @@ GeomShapePtr STEPImportAttributs(const std::string& theFileName, std::string& theError) { - STEPControl_Reader aReader; - std::shared_ptr aGeomShape(new GeomAPI_Shape); + try{ - Interface_Static::SetCVal("xstep.cascade.unit","M"); - Interface_Static::SetIVal("read.step.ideas", 1); - Interface_Static::SetIVal("read.step.nonmanifold", 1); + STEPControl_Reader aReader; + std::shared_ptr aGeomShape(new GeomAPI_Shape); - try { - OCC_CATCH_SIGNALS; + Interface_Static::SetCVal("xstep.cascade.unit","M"); + Interface_Static::SetIVal("read.step.ideas", 1); + Interface_Static::SetIVal("read.step.nonmanifold", 1); - IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str()); + try { + OCC_CATCH_SIGNALS; - if (status == IFSelect_RetDone) { + IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str()); - // Regard or not the model units - if (!theScalInterUnits) { - // 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."; + if (status == IFSelect_RetDone) { + // Regard or not the model units + if( !readUnits(aReader,theScalInterUnits,theError)) { 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; - } + catch (Standard_Failure const& anException) { + theError = anException.GetMessageString(); + aGeomShape->setImpl(new TopoDS_Shape()); + return aGeomShape; + } - STEPCAFControl_Reader aCafreader; - aCafreader.SetColorMode(true); - aCafreader.SetNameMode(true); - aCafreader.SetMatMode(true); + STEPCAFControl_Reader aCafreader; + aCafreader.SetColorMode(true); + aCafreader.SetNameMode(true); + aCafreader.SetMatMode(true); - if(aCafreader.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; - } + if(aCafreader.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(aCafreader, - theResultBody, - theMaterials, - theMaterialShape, - theError); + return readAttributes(aCafreader, + theResultBody, + theMaterials, + theMaterialShape, + theError); + } + catch (OSD_Exception& e) { + //Try to load STEP file without colors... + return STEPImport(theFileName,"",theScalInterUnits,theError); + } } \ No newline at end of file diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp index 807d4f86d..b77defd14 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp @@ -329,12 +329,12 @@ void storeMaterial( std::shared_ptr theResultBody, Handle(StepRepr_ProductDefinitionShape)::DownCast(theEnti); Handle(StepBasic_ProductDefinition) aProdDef; - if (aPDS.IsNull() == Standard_False) { + if (!aPDS.IsNull()) { // Product Definition Shape ==> Product Definition aProdDef = aPDS->Definition().ProductDefinition(); } - if (aProdDef.IsNull() == Standard_False) { + if (!aProdDef.IsNull()) { // Product Definition ==> Property Definition const Interface_Graph &aGraph = theTP->Graph(); Interface_EntityIterator aSubs = aGraph.Sharings(aProdDef); @@ -344,7 +344,7 @@ void storeMaterial( std::shared_ptr theResultBody, Handle(StepRepr_PropertyDefinition) aPropD = Handle(StepRepr_PropertyDefinition)::DownCast(aSubs.Value()); - if (aPropD.IsNull() == Standard_False) { + if (!aPropD.IsNull()) { // Property Definition ==> Representation. Interface_EntityIterator aSubs1 = aGraph.Sharings(aPropD); @@ -353,11 +353,11 @@ void storeMaterial( std::shared_ptr theResultBody, Handle(StepRepr_PropertyDefinitionRepresentation):: DownCast(aSubs1.Value()); - if (aPDR.IsNull() == Standard_False) { + if (!aPDR.IsNull()) { // Property Definition ==> Material Name. Handle(StepRepr_Representation) aRepr = aPDR->UsedRepresentation(); - if (aRepr.IsNull() == Standard_False) { + if (!aRepr.IsNull()) { Standard_Integer anIr; for(anIr = 1; anIr <= aRepr->NbItems(); anIr++) { @@ -365,10 +365,10 @@ void storeMaterial( std::shared_ptr theResultBody, Handle(StepRepr_DescriptiveRepresentationItem) aDRI = Handle(StepRepr_DescriptiveRepresentationItem)::DownCast(aRI); - if (aDRI.IsNull() == Standard_False) { + if (!aDRI.IsNull()) { // Get shape from Product Definition Handle(TCollection_HAsciiString) aMatName = aDRI->Name(); - if (aMatName.IsNull() == Standard_False) { + if (!aMatName.IsNull()) { TCollection_ExtendedString aMatNameExt (aMatName->ToCString()); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.cpp index e7d82b642..b9928428b 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.cpp @@ -41,7 +41,7 @@ bool STLExport(const std::string& theFileName, const std::string& /*theFormatName*/, const std::shared_ptr& theShape, const double aDeflection, - const bool anIsRelative, + const bool anIsRelative, const bool anIsASCII, std::string& theError) { diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.h b/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.h index 327dce41e..e256c037d 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_STLExport.h @@ -32,7 +32,7 @@ bool STLExport(const std::string& theFileName, const std::string& /*theFormatName*/, const std::shared_ptr& theShape, const double aDeflection, - const bool anIsRelative, + const bool anIsRelative, const bool anIsASCII, std::string& theError); diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 7b7876165..4f8899641 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -117,7 +117,7 @@ SET(PROJECT_SOURCES SET(PROJECT_LIBRARIES Config GeomAPI - ${OpenCASCADE_ApplicationFramework_LIBRARIES} + Locale ) SET(CMAKE_SWIG_FLAGS -threads -w325,321,362,383,302,403,451,473) ADD_DEFINITIONS(-DMODELAPI_EXPORTS) @@ -131,10 +131,6 @@ 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} )