[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.

This commit is contained in:
Konstantin Leontev 2024-11-07 09:02:05 +00:00
parent 67feb126ff
commit 512bf4c67a
12 changed files with 204 additions and 46 deletions

View File

@ -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 ---

View File

@ -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 <QRandomGenerator>
#include <QColor>
SALOMEDS::Color GEOM_ColorUtils::getPredefinedUniqueColor()
{
static QList<QColor> colors = []() {
QList<QColor> 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);
}

View File

@ -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 <Standard_Macro.hxx>
class GEOM_ColorUtils
{
public:
Standard_EXPORT static SALOMEDS::Color getPredefinedUniqueColor();
private:
Standard_EXPORT static int randomize(int size);
};
#endif // GEOMUTILS_COLOR_HXX

View File

@ -22,6 +22,9 @@
#include "GEOM_Object.hxx"
#include "GEOM_Engine.hxx"
#include "GEOM_ColorUtils.hxx"
#include <TDataStd_Integer.hxx>
#include <TDataStd_Real.hxx>
#include <TDataStd_RealArray.hxx>
@ -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 )

View File

@ -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)
};

View File

@ -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}
)

View File

@ -51,6 +51,7 @@
#include <GEOMGUI_AnnotationMgr.h>
#include <GEOMUtils.hxx>
#include "GEOM_ColorUtils.hxx"
#include <Material_Model.h>
@ -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<QColor> 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<SALOMEDS::Color>& 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<SALOMEDS::Color> aReservedColors;

View File

@ -209,7 +209,6 @@ public:
SalomeApp_Study* getStudy() const;
static SALOMEDS::Color getUniqueColor( const QList<SALOMEDS::Color>& );
static SALOMEDS::Color getPredefinedUniqueColor();
/*Get color of the geom object*/
static SALOMEDS::Color getColor(GEOM::GEOM_Object_var aGeomObject, bool& hasColor);

View File

@ -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}
)

View File

@ -37,6 +37,7 @@
#include "GEOMToolsGUI_MaterialPropertiesDlg.h"
#include "GEOMToolsGUI_LineWidthDlg.h"
#include "GEOMToolsGUI_ReduceStudyDlg.h"
#include "GEOM_ColorUtils.hxx"
#include <Material_Model.h>
#include <GEOM_VTKPropertyMaterial.hxx>
@ -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 );

View File

@ -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)'

View File

@ -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")