Skip to content

Commit 25796df

Browse files
authored
Merge pull request #176 from LIHPC-Computational-Geometry/materials-colors-dev
Materials colors dev
2 parents 9ec9bcd + 38053bd commit 25796df

File tree

9 files changed

+179
-2
lines changed

9 files changed

+179
-2
lines changed

src/Core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ add_library (Core ${CPP_SOURCES} ${HEADERS})
1818

1919
set (CORE_OPTIONAL_LIBRARIES_LIST)
2020

21-
find_package (PrefsCore REQUIRED)
21+
find_package (PrefsXerces REQUIRED)
2222
find_package (PythonUtil REQUIRED)
2323
find_package (GQGMDS REQUIRED)
2424
#find_package (MachineTypes REQUIRED)
@@ -87,7 +87,7 @@ target_include_directories (Core PUBLIC ${GMDS_INCLUDE_DIR})
8787
target_include_directories (Core PUBLIC ${OpenCASCADE_INCLUDE_DIR})
8888
target_link_libraries (Core PUBLIC ${OpenCASCADE_FoundationClasses_LIBRARIES} ${OpenCASCADE_ModelingAlgorithms_LIBRARIES} ${OpenCASCADE_DataExchange_LIBRARIES})
8989

90-
target_link_libraries (Core PUBLIC GQGMDS::GQGMDS Lima::Lima Utils PrefsCore::PrefsCore PythonUtil::PythonUtil)
90+
target_link_libraries (Core PUBLIC GQGMDS::GQGMDS Lima::Lima Utils PrefsXerces::PrefsXerces PythonUtil::PythonUtil)
9191
if (USE_TRITON)
9292
target_link_libraries (Core PUBLIC Triton2)
9393
endif()

src/Core/Geom/Curve.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "Geom/OCCHelper.h"
1010
#include "Geom/GeomProjectImplementation.h"
1111
#include "Group/Group1D.h"
12+
#include "Internal/EntitiesHelper.h"
1213
/*----------------------------------------------------------------------------*/
1314
#include <TkUtil/MemoryError.h>
1415
/*----------------------------------------------------------------------------*/
@@ -640,6 +641,16 @@ void Curve::add(Group::Group1D* grp)
640641
{
641642
//std::cout<<"Curve::add("<<grp->getName()<<") à "<<getName()<<std::endl;
642643
m_groups.push_back(grp);
644+
645+
TkUtil::Color color (0, 0, 0);
646+
std::vector<Group::GroupEntity*> groups = Internal::groupsFromTypedGroups (m_groups);
647+
if (true == getContext ( ).getGroupColor (groups, color))
648+
{
649+
getDisplayProperties ( ).setCloudColor (color);
650+
getDisplayProperties ( ).setWireColor (color);
651+
getDisplayProperties ( ).setSurfacicColor (color);
652+
getDisplayProperties ( ).setFontColor (color);
653+
} // if (true == getContext ( ).getGroupColor (groups, color))
643654
}
644655
/*----------------------------------------------------------------------------*/
645656
void Curve::remove(Group::Group1D* grp)

src/Core/Geom/Surface_Geom.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Geom/EntityFactory.h"
88
#include "Geom/GeomProjectImplementation.h"
99
#include "Group/Group2D.h"
10+
#include "Internal/EntitiesHelper.h"
1011
/*----------------------------------------------------------------------------*/
1112
#include <TkUtil/MemoryError.h>
1213
/*----------------------------------------------------------------------------*/
@@ -226,6 +227,16 @@ bool Surface::isA(const std::string& name)
226227
void Surface::add(Group::Group2D* grp)
227228
{
228229
m_groups.push_back(grp);
230+
231+
TkUtil::Color color (0, 0, 0);
232+
std::vector<Group::GroupEntity*> groups = Internal::groupsFromTypedGroups (m_groups);
233+
if (true == getContext ( ).getGroupColor (groups, color))
234+
{
235+
getDisplayProperties ( ).setCloudColor (color);
236+
getDisplayProperties ( ).setWireColor (color);
237+
getDisplayProperties ( ).setSurfacicColor (color);
238+
getDisplayProperties ( ).setFontColor (color);
239+
} // if (true == getContext ( ).getGroupColor (groups, color))
229240
}
230241
/*----------------------------------------------------------------------------*/
231242
void Surface::remove(Group::Group2D* grp)

src/Core/Geom/Vertex_Geom.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Geom/OCCHelper.h"
66
#include "Group/Group0D.h"
77
#include "Internal/Context.h"
8+
#include "Internal/EntitiesHelper.h"
89
/*----------------------------------------------------------------------------*/
910
#include <TopoDS_Vertex.hxx>
1011
#include <TopoDS_Shape.hxx>
@@ -139,6 +140,16 @@ TkUtil::UTF8String& operator<<(TkUtil::UTF8String& str, const Vertex& v)
139140
void Vertex::add(Group::Group0D* grp)
140141
{
141142
m_groups.push_back(grp);
143+
144+
TkUtil::Color color (0, 0, 0);
145+
std::vector<Group::GroupEntity*> groups = Internal::groupsFromTypedGroups (m_groups);
146+
if (true == getContext ( ).getGroupColor (groups, color))
147+
{
148+
getDisplayProperties ( ).setCloudColor (color);
149+
getDisplayProperties ( ).setWireColor (color);
150+
getDisplayProperties ( ).setSurfacicColor (color);
151+
getDisplayProperties ( ).setFontColor (color);
152+
} // if (true == getContext ( ).getGroupColor (groups, color))
142153
}
143154
/*----------------------------------------------------------------------------*/
144155
void Vertex::remove(Group::Group0D* grp)

src/Core/Geom/Volume_Geom.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Geom/OCCHelper.h"
77
#include "Group/Group3D.h"
88
#include "Internal/Context.h"
9+
#include "Internal/EntitiesHelper.h"
910
/*----------------------------------------------------------------------------*/
1011
#include <TkUtil/Exception.h>
1112
#include <TkUtil/MemoryError.h>
@@ -115,6 +116,16 @@ bool Volume::isA(const std::string& name)
115116
void Volume::add(Group::Group3D* grp)
116117
{
117118
m_groups.push_back(grp);
119+
120+
TkUtil::Color color (0, 0, 0);
121+
std::vector<Group::GroupEntity*> groups = Internal::groupsFromTypedGroups (m_groups);
122+
if (true == getContext ( ).getGroupColor (groups, color))
123+
{
124+
getDisplayProperties ( ).setCloudColor (color);
125+
getDisplayProperties ( ).setWireColor (color);
126+
getDisplayProperties ( ).setSurfacicColor (color);
127+
getDisplayProperties ( ).setFontColor (color);
128+
} // if (true == getContext ( ).getGroupColor (groups, color))
118129
}
119130
/*----------------------------------------------------------------------------*/
120131
void Volume::remove(Group::Group3D* grp)

src/Core/Group/GroupHelperForCommand.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ addToGroup(const std::string group_name, Geom::Volume* v)
1717
{
1818
Group3D* grp = m_group_manager.getNewGroup3D(group_name, &m_info_command);
1919
addEntityToGroup(grp, v);
20+
m_info_command.addGeomInfoEntity (v, Internal::InfoCommand::DISPMODIFIED);
2021
return grp;
2122
}
2223
/*----------------------------------------------------------------------------*/
@@ -25,6 +26,7 @@ addToGroup(const std::string group_name, Geom::Surface* s)
2526
{
2627
Group2D* grp = m_group_manager.getNewGroup2D(group_name, &m_info_command);
2728
addEntityToGroup(grp, s);
29+
m_info_command.addGeomInfoEntity (s, Internal::InfoCommand::DISPMODIFIED);
2830
return grp;
2931
}
3032
/*----------------------------------------------------------------------------*/
@@ -33,6 +35,7 @@ addToGroup(const std::string group_name, Geom::Curve* c)
3335
{
3436
Group1D* grp = m_group_manager.getNewGroup1D(group_name, &m_info_command);
3537
addEntityToGroup(grp, c);
38+
m_info_command.addGeomInfoEntity (c, Internal::InfoCommand::DISPMODIFIED);
3639
return grp;
3740
}
3841
/*----------------------------------------------------------------------------*/
@@ -41,6 +44,7 @@ addToGroup(const std::string group_name, Geom::Vertex* v)
4144
{
4245
Group0D* grp = m_group_manager.getNewGroup0D(group_name, &m_info_command);
4346
addEntityToGroup(grp, v);
47+
m_info_command.addGeomInfoEntity (v, Internal::InfoCommand::DISPMODIFIED);
4448
return grp;
4549
}
4650
/*----------------------------------------------------------------------------*/

src/Core/Internal/Context.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern TkUtil::PythonSession* createMgx3DPythonSession ( );
5050
#include <TkUtil/Process.h>
5151
#include <TkUtil/ThreadManager.h>
5252
#include <TkUtil/ThreadPool.h>
53+
#include <PrefsXerces/XMLLoader.h>
5354

5455
#include <signal.h>
5556
#include <unistd.h>
@@ -231,6 +232,7 @@ static TkUtil::Color nextColor (
231232
TkUtil::Log::TYPE Context::m_logMask = TkUtil::Log::PRODUCTION;
232233
TkUtil::ArgumentsMap Context::m_argumentsMap;
233234
TkUtil::Charset Context::m_outCharset (TkUtil::Charset::UTF_8);
235+
std::map<std::string, TkUtil::Color> Context::m_groups_colors;
234236

235237
TkUtil::Color Context::m_initial_geom_displayColor (255, 0, 127);
236238
TkUtil::Color Context::m_initial_topo_displayColor (0, 1, 1); // Initialisé dans initialize
@@ -705,6 +707,30 @@ void Context::initialize (int argc, char* argv [])
705707
255*Internal::InternalPreferences::instance ( )._topoColorWithoutProj.getRed ( ),
706708
255*Internal::InternalPreferences::instance ( )._topoColorWithoutProj.getGreen ( ),
707709
255*Internal::InternalPreferences::instance ( )._topoColorWithoutProj.getBlue ( )); // bleu, pas de proj
710+
711+
// Chargement des éventuelles associations nom de groupes -> couleurs transmises à la ligne de commande via un fichier xml :
712+
std::string fileName;
713+
try
714+
{
715+
if (true == m_argumentsMap.hasArg ("--groupsColors"))
716+
{
717+
fileName = m_argumentsMap.getArgValue ("--groupsColors", std::string ( ), true);
718+
std::cout << "Chargement des propriétés d'affichage des entités pour cette session depuis le fichier " << fileName << " ..." << std::endl;
719+
std::unique_ptr<Preferences::Section> mainSection (Preferences::XMLLoader::load (fileName));
720+
Preferences::Section& groupsSection = mainSection->getSection ("Groups");
721+
loadGroupsColors (groupsSection);
722+
std::cout << "Propriétés d'affichage des entités chargées." << std::endl;
723+
} // if (true == m_argumentsMap.hasArg ("--groupsColors"))
724+
}
725+
catch (const TkUtil::Exception& exc)
726+
{
727+
std::cerr << "Echec du chargement des propriétés d'affichage des entités depuis le fichier \"" << fileName << "\" :" << std::endl
728+
<< exc.getFullMessage ( ) << std::endl;
729+
}
730+
catch (...)
731+
{
732+
std::cerr << "Echec du chargement des propriétés d'affichage des entités depuis le fichier \"" << fileName << "\" : erreur non documentée." << std::endl;
733+
}
708734
}
709735
/*----------------------------------------------------------------------------*/
710736
void Context::finalize ( )
@@ -948,6 +974,27 @@ std::string Context::createName (const std::string& base) const
948974
return name.iso ( );
949975
} // Context::createName
950976
/*----------------------------------------------------------------------------*/
977+
void Context::loadGroupsColors (const Preferences::Section& section)
978+
{
979+
const std::vector<Preferences::Section*>& groups = section.getSections ( );
980+
for (std::vector<Preferences::Section*>::const_iterator itg = groups.begin ( ); groups.end ( ) != itg; itg++)
981+
{
982+
try
983+
{
984+
Preferences::NamedValue& namedValue = (*itg)->getNamedValue ("color");
985+
Preferences::ColorNamedValue* colorValue = dynamic_cast<Preferences::ColorNamedValue*>(&namedValue);
986+
if (0 != colorValue)
987+
{
988+
TkUtil::Color color ((unsigned char)(255 * colorValue->getRed ( )), (unsigned char)(255 * colorValue->getGreen ( )), (unsigned char)(255 * colorValue->getBlue ( )));
989+
m_groups_colors.insert_or_assign ((*itg)->getName ( ), color);
990+
} // if (0 != colorValue)
991+
}
992+
catch (...)
993+
{
994+
}
995+
} // for (std::vector<Preferences::Section*>::const_iterator itg = groups.begin ( ); groups.end ( ) != itg; itg++)
996+
} // Context::loadGroupsColors
997+
/*----------------------------------------------------------------------------*/
951998
unsigned long Context::newUniqueId()
952999
{
9531000
return getNameManager().getUniqueId();
@@ -1370,6 +1417,34 @@ void Context::newGraphicalRepresentation (Utils::Entity& entity)
13701417
entity.getDisplayProperties ( ).setGraphicalRepresentation (representation);
13711418
} // Context::newGraphicalRepresentation
13721419
/*----------------------------------------------------------------------------*/
1420+
bool Context::getGroupColor (const std::vector<Group::GroupEntity*>& groups, TkUtil::Color& color)
1421+
{
1422+
for (std::vector<Group::GroupEntity*>::const_iterator itg = groups.begin ( ); groups.end ( ) != itg; itg++)
1423+
{
1424+
try
1425+
{
1426+
color = getGroupColor ((*itg)->getName ( ));
1427+
return true;
1428+
}
1429+
catch (...)
1430+
{
1431+
}
1432+
} // for (std::vector<Group::GroupEntity*>::const_iterator itg = groups.begin ( ); groups.end ( ) != itg; itg++)
1433+
1434+
return false;
1435+
} // Context::getGroupColor
1436+
/*----------------------------------------------------------------------------*/
1437+
TkUtil::Color Context::getGroupColor (const std::string& name)
1438+
{
1439+
const std::map<std::string, TkUtil::Color>::iterator it = m_groups_colors.find (name);
1440+
if (m_groups_colors.end ( ) != it)
1441+
return it->second;
1442+
1443+
TkUtil::UTF8String message;
1444+
message << "Context::groupColor. Couleur pour le groupe de données \"" << name << "\" non recensée.";
1445+
throw TkUtil::Exception (message);
1446+
} // Context::getGroupColor
1447+
/*----------------------------------------------------------------------------*/
13731448
void Context::undo()
13741449
{
13751450
getCommandManager().undo();

src/Core/protected/Internal/Context.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,25 @@ class Context {
442442
* (<I>true == getDisplayProperties ( ).isDisplayable</I>.
443443
*/
444444
void newGraphicalRepresentation(Utils::Entity& entity);
445+
446+
/**
447+
* Recherche une couleur en configuration pour une entité. Une entité pouvant appartenir à plusieurs
448+
* groupes c'est ici la liste des groupes qui est transmise en argument.
449+
* \param Groupes dont on veut récupérer une éventuelle couleur définie en configuration.
450+
* La première couleur trouvée en configuration est retenue.
451+
* \param En retour, la première couleur trouvée en configuration, s'il y en a une.
452+
* \return true si une couleur est trouvée, false dans le cas contraire.
453+
* \see loadGroupsColors
454+
*/
455+
bool getGroupColor (const std::vector<Group::GroupEntity*>& groups, TkUtil::Color& color);
456+
457+
/**
458+
* Recherche une couleur en configuration pour une entité.
459+
* \param Nom du groupe dont on recherche une couleur.
460+
* \return La couleur associé au groupe dont le nom est transmis en argument.
461+
* \exception Une exception est levée si la couleur n'est pas trouvée en configuration.
462+
*/
463+
TkUtil::Color getGroupColor (const std::string& name);
445464
#endif
446465

447466
/** Passage en mode apperçu ou non. En mode apperçu les couleurs fournies
@@ -603,6 +622,12 @@ class Context {
603622
std::string createName (const std::string& base) const;
604623

605624
private:
625+
626+
/**
627+
* Le chargement optionnel des fichiers table groupe -> couleur.
628+
*/
629+
static void loadGroupsColors (const Preferences::Section& section);
630+
606631
/** Nom unique de l'instance. */
607632
std::string m_name;
608633

@@ -677,6 +702,9 @@ class Context {
677702
/** Le jeu de caractères utilisé par la sortie standard. */
678703
static TkUtil::Charset m_outCharset;
679704

705+
/** La table groupe -> couleur. */
706+
static std::map<std::string, TkUtil::Color> m_groups_colors;
707+
680708
/** booléen positionné à vrai seulement lorsque la sessione est terminée
681709
* (destruction du context)
682710
*/

src/Core/protected/Internal/EntitiesHelper.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,32 @@ template < typename E > inline std::vector< Mgx3D::Utils::Entity* > entitiesFrom
172172
return entities;
173173
} // entitiesFromTypedEntities
174174

175+
/**
176+
* \return Une liste de groupes issue d'une liste de groupes typés.
177+
*/
178+
template < typename G > inline std::vector< Mgx3D::Group::GroupEntity* > groupsFromTypedGroups (
179+
const std::vector< G* >& typedGroups, bool strict = true)
180+
{
181+
std::vector< Mgx3D::Group::GroupEntity* > groups;
182+
183+
for (typename std::vector< G* >::const_iterator it = typedGroups.begin ( );
184+
typedGroups.end ( ) != it; it++)
185+
{
186+
Mgx3D::Group::GroupEntity* group = dynamic_cast<Mgx3D::Group::GroupEntity*>(*it);
187+
if ((0 == group) && (true == strict))
188+
{
189+
TkUtil::UTF8String message (TkUtil::Charset::UTF_8);
190+
message << "Erreur, le groupe " << (*it)->getName ( )
191+
<< " n'est pas du bon type.";
192+
INTERNAL_ERROR (exc, message, "groupsFromTypedGroups")
193+
throw exc;
194+
} // if ((0 == group) && (true == strict))
195+
groups.push_back (group);
196+
}
197+
198+
return groups;
199+
} // groupsFromTypedGroups
200+
175201
/**
176202
* \return La différence entre les 2 listes d'entités reçues en premiers arguments.
177203
* \param before et after représentent la même liste d'entités avant et après un évènement, par exemple l'exécution d'une commande

0 commit comments

Comments
 (0)