Skip to content

Commit a19fac3

Browse files
committed
Code refactoring
1 parent aa85f9a commit a19fac3

File tree

6 files changed

+133
-553
lines changed

6 files changed

+133
-553
lines changed

src/Core/Geom/GeomManager.cpp

Lines changed: 49 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -4778,58 +4778,22 @@ void GeomManager::remove (Vertex* v)
47784778
/*----------------------------------------------------------------------------*/
47794779
std::string GeomManager::getLastVolume() const
47804780
{
4781-
std::string nom("");
4782-
if (m_volumes.empty())
4783-
return nom;
4784-
// recherche du dernier non détruit, en partant de la fin
4785-
int i;
4786-
for (i=m_volumes.size()-1; i>=0 && m_volumes[i]->isDestroyed(); i--)
4787-
{}
4788-
if (i>=0)
4789-
nom = m_volumes[i]->getName();
4790-
return nom;
4781+
return getLastEntityName(m_volumes);
47914782
}
47924783
/*----------------------------------------------------------------------------*/
47934784
std::string GeomManager::getLastSurface() const
47944785
{
4795-
std::string nom("");
4796-
if (m_surfaces.empty())
4797-
return nom;
4798-
// recherche du dernier non détruit, en partant de la fin
4799-
int i;
4800-
for (i=m_surfaces.size()-1; i>=0 && m_surfaces[i]->isDestroyed(); i--)
4801-
{}
4802-
if (i>=0)
4803-
nom = m_surfaces[i]->getName();
4804-
return nom;
4786+
return getLastEntityName(m_surfaces);
48054787
}
48064788
/*----------------------------------------------------------------------------*/
48074789
std::string GeomManager::getLastCurve() const
48084790
{
4809-
std::string nom("");
4810-
if (m_curves.empty())
4811-
return nom;
4812-
// recherche du dernier non détruit, en partant de la fin
4813-
int i;
4814-
for (i=m_curves.size()-1; i>=0 && m_curves[i]->isDestroyed(); i--)
4815-
{}
4816-
if (i>=0)
4817-
nom = m_curves[i]->getName();
4818-
return nom;
4791+
return getLastEntityName(m_curves);
48194792
}
48204793
/*----------------------------------------------------------------------------*/
48214794
std::string GeomManager::getLastVertex() const
48224795
{
4823-
std::string nom("");
4824-
if (m_vertices.empty())
4825-
return nom;
4826-
// recherche du dernier non détruit, en partant de la fin
4827-
int i;
4828-
for (i=m_vertices.size()-1; i>=0 && m_vertices[i]->isDestroyed(); i--)
4829-
{}
4830-
if (i>=0)
4831-
nom = m_vertices[i]->getName();
4832-
return nom;
4796+
return getLastEntityName(m_vertices);
48334797
}
48344798
/*----------------------------------------------------------------------------*/
48354799
Internal::M3DCommandResult* GeomManager::addToGroup(std::vector<std::string>& ve, int dim, const std::string& groupName)
@@ -4845,57 +4809,8 @@ Internal::M3DCommandResult* GeomManager::addToGroup(std::vector<std::string>& ve
48454809
message << "], "<<(short)dim<<", "<<groupName<<")";
48464810
log (TkUtil::TraceLog (message, TkUtil::Log::TRACE_3));
48474811

4848-
4849-
Mesh::CommandAddRemoveGroupName* command = 0;
4850-
4851-
switch(dim){
4852-
case(0):{
4853-
// reconstitue le vecteur de sommets
4854-
std::vector<Vertex*> vertices;
4855-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4856-
iter != ve.end(); ++iter)
4857-
vertices.push_back(getVertex(*iter, true));
4858-
4859-
command = new Mesh::CommandAddRemoveGroupName(getContext(), vertices, groupName, Mesh::CommandAddRemoveGroupName::add);
4860-
}
4861-
break;
4862-
case(1):{
4863-
// reconstitue le vecteur de courbes
4864-
std::vector<Curve*> curves;
4865-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4866-
iter != ve.end(); ++iter)
4867-
curves.push_back(getCurve(*iter, true));
4868-
4869-
command = new Mesh::CommandAddRemoveGroupName(getContext(), curves, groupName, Mesh::CommandAddRemoveGroupName::add);
4870-
}
4871-
break;
4872-
case(2):{
4873-
// reconstitue le vecteur de surfaces
4874-
std::vector<Surface*> surfaces;
4875-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4876-
iter != ve.end(); ++iter)
4877-
surfaces.push_back(getSurface(*iter, true));
4878-
4879-
command = new Mesh::CommandAddRemoveGroupName(getContext(), surfaces, groupName, Mesh::CommandAddRemoveGroupName::add);
4880-
}
4881-
break;
4882-
case(3):{
4883-
// reconstitue le vecteur de volumes
4884-
std::vector<Volume*> volumes;
4885-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4886-
iter != ve.end(); ++iter)
4887-
volumes.push_back(getVolume(*iter, true));
4888-
4889-
command = new Mesh::CommandAddRemoveGroupName(getContext(), volumes, groupName, Mesh::CommandAddRemoveGroupName::add);
4890-
}
4891-
break;
4892-
default:{
4893-
TkUtil::Exception ("Dimension erronée");
4894-
}
4895-
break;
4896-
}
4897-
4898-
CHECK_NULL_PTR_ERROR(command);
4812+
std::vector<GeomEntity*> entities = getEntitiesFromNames(ve, dim);
4813+
Mesh::CommandAddRemoveGroupName* command = new Mesh::CommandAddRemoveGroupName(getContext(), entities, dim, groupName, Mesh::CommandAddRemoveGroupName::add);
48994814

49004815
// trace dans le script
49014816
TkUtil::UTF8String cmd (TkUtil::Charset::UTF_8);
@@ -4930,57 +4845,8 @@ Internal::M3DCommandResult* GeomManager::removeFromGroup(std::vector<std::string
49304845
message << "], "<<(short)dim<<", "<<groupName<<")";
49314846
log (TkUtil::TraceLog (message, TkUtil::Log::TRACE_3));
49324847

4933-
4934-
Mesh::CommandAddRemoveGroupName* command = 0;
4935-
4936-
switch(dim){
4937-
case(0):{
4938-
// reconstitue le vecteur de sommets
4939-
std::vector<Vertex*> vertices;
4940-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4941-
iter != ve.end(); ++iter)
4942-
vertices.push_back(getVertex(*iter, true));
4943-
4944-
command = new Mesh::CommandAddRemoveGroupName(getContext(), vertices, groupName, Mesh::CommandAddRemoveGroupName::remove);
4945-
}
4946-
break;
4947-
case(1):{
4948-
// reconstitue le vecteur de courbes
4949-
std::vector<Curve*> curves;
4950-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4951-
iter != ve.end(); ++iter)
4952-
curves.push_back(getCurve(*iter, true));
4953-
4954-
command = new Mesh::CommandAddRemoveGroupName(getContext(), curves, groupName, Mesh::CommandAddRemoveGroupName::remove);
4955-
}
4956-
break;
4957-
case(2):{
4958-
// reconstitue le vecteur de surfaces
4959-
std::vector<Surface*> surfaces;
4960-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4961-
iter != ve.end(); ++iter)
4962-
surfaces.push_back(getSurface(*iter, true));
4963-
4964-
command = new Mesh::CommandAddRemoveGroupName(getContext(), surfaces, groupName, Mesh::CommandAddRemoveGroupName::remove);
4965-
}
4966-
break;
4967-
case(3):{
4968-
// reconstitue le vecteur de volumes
4969-
std::vector<Volume*> volumes;
4970-
for (std::vector<std::string>::const_iterator iter = ve.begin();
4971-
iter != ve.end(); ++iter)
4972-
volumes.push_back(getVolume(*iter, true));
4973-
4974-
command = new Mesh::CommandAddRemoveGroupName(getContext(), volumes, groupName, Mesh::CommandAddRemoveGroupName::remove);
4975-
}
4976-
break;
4977-
default:{
4978-
TkUtil::Exception ("Dimension erronée");
4979-
}
4980-
break;
4981-
}
4982-
4983-
CHECK_NULL_PTR_ERROR(command);
4848+
std::vector<GeomEntity*> entities = getEntitiesFromNames(ve, dim);
4849+
Mesh::CommandAddRemoveGroupName* command = new Mesh::CommandAddRemoveGroupName(getContext(), entities, dim, groupName, Mesh::CommandAddRemoveGroupName::remove);
49844850

49854851
// trace dans le script
49864852
TkUtil::UTF8String cmd (TkUtil::Charset::UTF_8);
@@ -5015,57 +4881,8 @@ Internal::M3DCommandResult* GeomManager::setGroup(std::vector<std::string>& ve,
50154881
message << "], "<<(short)dim<<", "<<groupName<<")";
50164882
log (TkUtil::TraceLog (message, TkUtil::Log::TRACE_3));
50174883

5018-
5019-
Mesh::CommandAddRemoveGroupName* command = 0;
5020-
5021-
switch(dim){
5022-
case(0):{
5023-
// reconstitue le vecteur de sommets
5024-
std::vector<Vertex*> vertices;
5025-
for (std::vector<std::string>::const_iterator iter = ve.begin();
5026-
iter != ve.end(); ++iter)
5027-
vertices.push_back(getVertex(*iter, true));
5028-
5029-
command = new Mesh::CommandAddRemoveGroupName(getContext(), vertices, groupName, Mesh::CommandAddRemoveGroupName::set);
5030-
}
5031-
break;
5032-
case(1):{
5033-
// reconstitue le vecteur de courbes
5034-
std::vector<Curve*> curves;
5035-
for (std::vector<std::string>::const_iterator iter = ve.begin();
5036-
iter != ve.end(); ++iter)
5037-
curves.push_back(getCurve(*iter, true));
5038-
5039-
command = new Mesh::CommandAddRemoveGroupName(getContext(), curves, groupName, Mesh::CommandAddRemoveGroupName::set);
5040-
}
5041-
break;
5042-
case(2):{
5043-
// reconstitue le vecteur de surfaces
5044-
std::vector<Surface*> surfaces;
5045-
for (std::vector<std::string>::const_iterator iter = ve.begin();
5046-
iter != ve.end(); ++iter)
5047-
surfaces.push_back(getSurface(*iter, true));
5048-
5049-
command = new Mesh::CommandAddRemoveGroupName(getContext(), surfaces, groupName, Mesh::CommandAddRemoveGroupName::set);
5050-
}
5051-
break;
5052-
case(3):{
5053-
// reconstitue le vecteur de volumes
5054-
std::vector<Volume*> volumes;
5055-
for (std::vector<std::string>::const_iterator iter = ve.begin();
5056-
iter != ve.end(); ++iter)
5057-
volumes.push_back(getVolume(*iter, true));
5058-
5059-
command = new Mesh::CommandAddRemoveGroupName(getContext(), volumes, groupName, Mesh::CommandAddRemoveGroupName::set);
5060-
}
5061-
break;
5062-
default:{
5063-
TkUtil::Exception ("Dimension erronée");
5064-
}
5065-
break;
5066-
}
5067-
5068-
CHECK_NULL_PTR_ERROR(command);
4884+
std::vector<GeomEntity*> entities = getEntitiesFromNames(ve, dim);
4885+
Mesh::CommandAddRemoveGroupName* command = new Mesh::CommandAddRemoveGroupName(getContext(), entities, dim, groupName, Mesh::CommandAddRemoveGroupName::set);
50694886

50704887
// trace dans le script
50714888
TkUtil::UTF8String cmd (TkUtil::Charset::UTF_8);
@@ -5114,6 +4931,45 @@ hasRefTopo(std::vector<Geom::GeomEntity*>& entities)
51144931
return false;
51154932
}
51164933
/*----------------------------------------------------------------------------*/
4934+
template <typename T, typename = std::enable_if<std::is_base_of<GeomEntity, T>::value>>
4935+
std::string GeomManager::getLastEntityName(const std::vector<T*>& entities) const
4936+
{
4937+
std::string nom("");
4938+
if (entities.empty())
4939+
return nom;
4940+
// recherche du dernier non détruit, en partant de la fin
4941+
int i;
4942+
for (i=entities.size()-1; i>=0 && entities[i]->isDestroyed(); i--)
4943+
{}
4944+
if (i>=0)
4945+
nom = entities[i]->getName();
4946+
return nom;
4947+
}
4948+
/*----------------------------------------------------------------------------*/
4949+
std::vector<GeomEntity*> GeomManager::getEntitiesFromNames(const std::vector<std::string>& names, const int dim) const
4950+
{
4951+
std::vector<GeomEntity*> entities;
4952+
4953+
switch(dim){
4954+
case(0):
4955+
for (std::string name : names) entities.push_back(getVertex(name));
4956+
break;
4957+
case(1):
4958+
for (std::string name : names) entities.push_back(getCurve(name));
4959+
break;
4960+
case(2):
4961+
for (std::string name : names) entities.push_back(getSurface(name));
4962+
break;
4963+
case(3):
4964+
for (std::string name : names) entities.push_back(getVolume(name));
4965+
break;
4966+
default:
4967+
throw TkUtil::Exception ("Dimension erronée");
4968+
}
4969+
4970+
return entities;
4971+
}
4972+
/*----------------------------------------------------------------------------*/
51174973
} // end namespace Geom
51184974
/*----------------------------------------------------------------------------*/
51194975
} // end namespace Mgx3D

0 commit comments

Comments
 (0)