From: Konstantin Leontev Date: Thu, 7 Nov 2024 09:02:05 +0000 (+0000) Subject: [bos #43299][FORUM] Now after SetAutoColor(1) we can get automatically generated... X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;ds=inline;p=modules%2Fgeom.git [bos #43299][FORUM] Now after SetAutoColor(1) we can get automatically generated color back by GetColor() call. Previous implementation didn't set a color to an object, but calculated it only for a viewer. --- diff --git a/src/GEOM/CMakeLists.txt b/src/GEOM/CMakeLists.txt index a7a40dfbb..989788e07 100644 --- a/src/GEOM/CMakeLists.txt +++ b/src/GEOM/CMakeLists.txt @@ -26,11 +26,13 @@ INCLUDE_DIRECTORIES( ${KERNEL_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/src/SKETCHER ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_BINARY_DIR}/idl ) # additional preprocessor / compiler flags ADD_DEFINITIONS( ${OpenCASCADE_DEFINITIONS} + ${OMNIORB_DEFINITIONS} ) # libraries to link to @@ -39,6 +41,7 @@ SET(_link_LIBRARIES ${KERNEL_SALOMELocalTrace} ${KERNEL_OpUtil} GEOMSketcher + ${QT_LIBRARIES} ) # --- headers --- @@ -57,6 +60,7 @@ SET(GEOM_HEADERS GEOM_PythonDump.hxx GEOM_DataMapOfAsciiStringTransient.hxx GEOM_BaseObject.hxx + GEOM_ColorUtils.hxx ) # --- sources --- @@ -73,6 +77,7 @@ SET(GEOM_SOURCES GEOM_BaseDriver.cxx GEOM_SubShapeDriver.cxx GEOM_PythonDump.cxx + GEOM_ColorUtils.cxx ) # --- rules --- diff --git a/src/GEOM/GEOM_ColorUtils.cxx b/src/GEOM/GEOM_ColorUtils.cxx new file mode 100644 index 000000000..b8a6a9d1a --- /dev/null +++ b/src/GEOM/GEOM_ColorUtils.cxx @@ -0,0 +1,61 @@ +// Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// 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 "GEOM_ColorUtils.hxx" + +#include +#include + + +SALOMEDS::Color GEOM_ColorUtils::getPredefinedUniqueColor() +{ + static QList colors = []() { + QList tempColors; + for (int s = 0; s < 2; s++) + { + for (int v = 100; v >= 40; v = v - 20) + { + for (int h = 0; h < 359; h = h + 60) + { + tempColors.append(QColor::fromHsv(h, 255 - s * 127, v * 255 / 100)); + } + } + } + return tempColors; + }(); + + static int currentColor = randomize(colors.size()); + + SALOMEDS::Color color; + color.R = (double)colors[currentColor].red() / 255.0; + color.G = (double)colors[currentColor].green() / 255.0; + color.B = (double)colors[currentColor].blue() / 255.0; + + currentColor = (currentColor+1) % colors.count(); + + return color; +} + +int GEOM_ColorUtils::randomize(int size) +{ + return QRandomGenerator::global()->bounded(size); +} \ No newline at end of file diff --git a/src/GEOM/GEOM_ColorUtils.hxx b/src/GEOM/GEOM_ColorUtils.hxx new file mode 100644 index 000000000..ca6ae5c70 --- /dev/null +++ b/src/GEOM/GEOM_ColorUtils.hxx @@ -0,0 +1,40 @@ +// Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// 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 GEOMUTILS_COLOR_HXX +#define GEOMUTILS_COLOR_HXX + +#include "SALOMEDS_Attributes.hh" + +#include + + +class GEOM_ColorUtils +{ +public: + Standard_EXPORT static SALOMEDS::Color getPredefinedUniqueColor(); + +private: + Standard_EXPORT static int randomize(int size); +}; + +#endif // GEOMUTILS_COLOR_HXX \ No newline at end of file diff --git a/src/GEOM/GEOM_Object.cxx b/src/GEOM/GEOM_Object.cxx index 41d892786..1ea805c35 100644 --- a/src/GEOM/GEOM_Object.cxx +++ b/src/GEOM/GEOM_Object.cxx @@ -22,6 +22,9 @@ #include "GEOM_Object.hxx" +#include "GEOM_Engine.hxx" +#include "GEOM_ColorUtils.hxx" + #include #include #include @@ -157,6 +160,18 @@ GEOM_Object::Color GEOM_Object::GetColor() void GEOM_Object::SetAutoColor(bool theAutoColor) { TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor); + + // Set color for the object here, othrewise it will stay default forever, despite + // the color displayed in viewver being defined on GEOM_Displayer::getColor() call. + // It's not clear if we need to reset color when auto color is disabled. + if (theAutoColor) + { + const SALOMEDS::Color color = GEOM_ColorUtils::getPredefinedUniqueColor(); + SetColor({ color.R, color.G, color.B }); + } + + // Set color for all sub-shapes objects + SetAutoColorSubShapes(theAutoColor); } //============================================================================= @@ -296,5 +311,34 @@ GEOM_Object::GetLastFunctions( const std::list< Handle(GEOM_Object) >& theObject return funs; } +//============================================================================= +/*! + * Toggles an auto color mode for sub-shapes of GEOM_Object + */ +//============================================================================= +void GEOM_Object::SetAutoColorSubShapes(bool theAutoColor) +{ + const int nbFunctions = GetNbFunctions(); + for (int i = 1; i <= nbFunctions; i++) + { + Handle(GEOM_Function) aFunction = GetFunction(i); + if (aFunction.IsNull()) + continue; + + const TDataStd_ListOfExtendedString& aListEntries = aFunction->GetSubShapeReferences(); + for (TDataStd_ListIteratorOfListOfExtendedString anIt(aListEntries); anIt.More(); anIt.Next()) + { + const TCollection_AsciiString anEntry = anIt.Value(); + Handle(GEOM_Object) aSubObj = + Handle(GEOM_Object)::DownCast(GEOM_Engine::GetEngine()->GetObject(anEntry.ToCString(), false)); + if (aSubObj.IsNull()) + continue; + + aSubObj->SetAutoColor(theAutoColor); + } + } +} + + IMPLEMENT_STANDARD_RTTIEXT(GEOM_Object, GEOM_BaseObject ) diff --git a/src/GEOM/GEOM_Object.hxx b/src/GEOM/GEOM_Object.hxx index 7b24daa58..ab06794e8 100644 --- a/src/GEOM/GEOM_Object.hxx +++ b/src/GEOM/GEOM_Object.hxx @@ -114,6 +114,10 @@ class GEOM_Object : public GEOM_BaseObject Standard_EXPORT static Handle(TColStd_HSequenceOfTransient) GetLastFunctions( const std::list< Handle(GEOM_Object) >& theObjects ); +protected: + // Toggles an auto color mode for sub-shapes of GEOM_Object + Standard_EXPORT void SetAutoColorSubShapes(bool theAutoColor); + public: DEFINE_STANDARD_RTTIEXT(GEOM_Object,GEOM_BaseObject) }; diff --git a/src/GEOMGUI/CMakeLists.txt b/src/GEOMGUI/CMakeLists.txt index f549c5587..0814651a4 100644 --- a/src/GEOMGUI/CMakeLists.txt +++ b/src/GEOMGUI/CMakeLists.txt @@ -37,6 +37,7 @@ INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/src/GEOMImpl ${PROJECT_SOURCE_DIR}/src/GEOMUtils ${PROJECT_SOURCE_DIR}/src/GEOM_I + ${PROJECT_SOURCE_DIR}/src/GEOM ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/src/GEOMGUI/GEOM_Displayer.cxx b/src/GEOMGUI/GEOM_Displayer.cxx index 86ea2e56a..8681da1f1 100644 --- a/src/GEOMGUI/GEOM_Displayer.cxx +++ b/src/GEOMGUI/GEOM_Displayer.cxx @@ -51,6 +51,7 @@ #include #include +#include "GEOM_ColorUtils.hxx" #include @@ -295,19 +296,6 @@ namespace } } } - - uint randomize( uint size ) - { - static bool initialized = false; - if ( !initialized ) { - qsrand( QDateTime::currentDateTime().toTime_t() ); - initialized = true; - } - uint v = qrand(); - v = uint( (double)( v ) / RAND_MAX * size ); - v = qMax( uint(0), qMin ( v, size-1 ) ); - return v; - } } // namespace //================================================================ @@ -2490,36 +2478,6 @@ bool GEOM_Displayer::HasDisplayMode() const return myHasDisplayMode; } -SALOMEDS::Color GEOM_Displayer::getPredefinedUniqueColor() -{ - static QList colors; - - if ( colors.isEmpty() ) { - - for (int s = 0; s < 2 ; s++) - { - for (int v = 100; v >= 40; v = v - 20) - { - for (int h = 0; h < 359 ; h = h + 60) - { - colors.append(QColor::fromHsv(h, 255 - s * 127, v * 255 / 100)); - } - } - } - } - - static int currentColor = randomize( colors.size() ); - - SALOMEDS::Color color; - color.R = (double)colors[currentColor].red() / 255.0; - color.G = (double)colors[currentColor].green() / 255.0; - color.B = (double)colors[currentColor].blue() / 255.0; - - currentColor = (currentColor+1) % colors.count(); - - return color; -} - SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList& theReservedColors ) { int aHue = -1; @@ -2777,7 +2735,7 @@ SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bo GEOM::GEOM_Object_var aMainObject = theGeomObject->GetMainShape(); if ( !CORBA::is_nil( aMainObject ) && aMainObject->GetAutoColor() ) { #ifdef SIMPLE_AUTOCOLOR // simplified algorithm for auto-colors - aSColor = getPredefinedUniqueColor(); + aSColor = GEOM_ColorUtils::getPredefinedUniqueColor(); hasColor = true; #else // old algorithm for auto-colors QList aReservedColors; diff --git a/src/GEOMGUI/GEOM_Displayer.h b/src/GEOMGUI/GEOM_Displayer.h index 08f5745a8..67ad4c156 100644 --- a/src/GEOMGUI/GEOM_Displayer.h +++ b/src/GEOMGUI/GEOM_Displayer.h @@ -209,7 +209,6 @@ public: SalomeApp_Study* getStudy() const; static SALOMEDS::Color getUniqueColor( const QList& ); - static SALOMEDS::Color getPredefinedUniqueColor(); /*Get color of the geom object*/ static SALOMEDS::Color getColor(GEOM::GEOM_Object_var aGeomObject, bool& hasColor); diff --git a/src/GEOMToolsGUI/CMakeLists.txt b/src/GEOMToolsGUI/CMakeLists.txt index 9ed2eb5fb..ca7339db2 100644 --- a/src/GEOMToolsGUI/CMakeLists.txt +++ b/src/GEOMToolsGUI/CMakeLists.txt @@ -38,6 +38,7 @@ INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/src/Material ${PROJECT_SOURCE_DIR}/src/DependencyTree ${PROJECT_SOURCE_DIR}/src/GEOMUtils + ${PROJECT_SOURCE_DIR}/src/GEOM ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/src/GEOMToolsGUI/GEOMToolsGUI_1.cxx b/src/GEOMToolsGUI/GEOMToolsGUI_1.cxx index 452536148..7e68aba4e 100644 --- a/src/GEOMToolsGUI/GEOMToolsGUI_1.cxx +++ b/src/GEOMToolsGUI/GEOMToolsGUI_1.cxx @@ -37,6 +37,7 @@ #include "GEOMToolsGUI_MaterialPropertiesDlg.h" #include "GEOMToolsGUI_LineWidthDlg.h" #include "GEOMToolsGUI_ReduceStudyDlg.h" +#include "GEOM_ColorUtils.hxx" #include #include @@ -186,7 +187,7 @@ void GEOMToolsGUI::OnAutoColor() #endif // GENERAL_AUTOCOLOR #ifdef SIMPLE_AUTOCOLOR // simplified algorithm for auto-colors - SALOMEDS::Color aColor = GEOM_Displayer::getPredefinedUniqueColor(); + SALOMEDS::Color aColor = GEOM_ColorUtils::getPredefinedUniqueColor(); #else // old algorithm for auto-colors SALOMEDS::Color aColor = GEOM_Displayer::getUniqueColor( aReservedColors ); aReservedColors.append( aColor ); diff --git a/test/test_set_autocolor.py b/test/test_set_autocolor.py new file mode 100644 index 000000000..959dc3c48 --- /dev/null +++ b/test/test_set_autocolor.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +# Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +# Testing of setting and getting an automatically set color + +import salome +salome.salome_init() + +from salome.geom import geomBuilder +geompy = geomBuilder.New() + +# Create a box and extract its faces +Box_1 = geompy.MakeBoxDXDYDZ(200, 200, 200) +faces = geompy.ExtractShapes(Box_1, geompy.ShapeType["FACE"], True) +Box_1.SetAutoColor(1) +geompy.addToStudy(Box_1, 'Box_1') + +# Add each face to the study with a numeric name +for i, face in enumerate(faces, start=1): + geompy.addToStudyInFather(Box_1, face, f'Face_{i}') + +# Check color of each face +for face in faces: + color = face.GetColor() + print(f'{face.GetName()}: {color}') + assert color.R != -1 and color.G != -1 and color.B != -1, 'Auto color must be different than (-1, -1, -1)' \ No newline at end of file diff --git a/test/tests.set b/test/tests.set index 43b912ab2..6465dfcb5 100644 --- a/test/tests.set +++ b/test/tests.set @@ -20,6 +20,7 @@ SET(ALL_TESTS test_perf_01.py test_patch_face_01.py + test_set_autocolor.py ) IF(${OpenCASCADE_VERSION}.${OpenCASCADE_SP_VERSION} VERSION_GREATER "7.5.3.3")