From 8ae310999388a165f077da9442532d7e51c766fd Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Fri, 18 Aug 2006 17:49:35 +0000
Subject: [PATCH] make sure to rest the model before doing a add_in_file in the
 old geo code (since the parsed command could delete an entity!)

---
 Fltk/GUI.cpp        |  4 ++--
 Geo/GModel.cpp      | 13 +++++++++----
 Geo/GModel.h        | 10 +++++++---
 Geo/Geo.cpp         |  5 ++++-
 Geo/gmshModel.cpp   |  5 +----
 Graphics/Mesh.cpp   |  5 ++---
 Parser/OpenFile.cpp |  6 ++----
 7 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/Fltk/GUI.cpp b/Fltk/GUI.cpp
index 8980ca1521..fec93c0782 100644
--- a/Fltk/GUI.cpp
+++ b/Fltk/GUI.cpp
@@ -1,4 +1,4 @@
-// $Id: GUI.cpp,v 1.530 2006-08-18 02:22:40 geuzaine Exp $
+// $Id: GUI.cpp,v 1.531 2006-08-18 17:49:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -190,13 +190,13 @@ Context_Item menu_geometry[] = {
     Context_Item menu_geometry_elementary[] = {
       {"0Geometry>Elementary", NULL} ,
       {"Add",       (Fl_Callback *)geometry_elementary_add_cb} ,
+      {"Delete",    (Fl_Callback *)geometry_elementary_delete_cb} ,
       {"Translate", (Fl_Callback *)geometry_elementary_translate_cb} ,
       {"Rotate",    (Fl_Callback *)geometry_elementary_rotate_cb} ,
       {"Scale",     (Fl_Callback *)geometry_elementary_scale_cb} ,
       {"Symmetry",  (Fl_Callback *)geometry_elementary_symmetry_cb} ,
       {"Extrude",   (Fl_Callback *)geometry_elementary_extrude_cb} ,
       {"Coherence", (Fl_Callback *)geometry_elementary_coherence_cb} ,
-      {"Delete",    (Fl_Callback *)geometry_elementary_delete_cb} ,
       {0} 
     };  
         Context_Item menu_geometry_elementary_add[] = {
diff --git a/Geo/GModel.cpp b/Geo/GModel.cpp
index 7afdac7eb2..796a27cc0a 100644
--- a/Geo/GModel.cpp
+++ b/Geo/GModel.cpp
@@ -1,4 +1,4 @@
-// $Id: GModel.cpp,v 1.12 2006-08-18 02:22:40 geuzaine Exp $
+// $Id: GModel.cpp,v 1.13 2006-08-18 17:49:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -21,17 +21,22 @@
 
 #include "GModel.h"
 
-GModel::~GModel()
-{ 
+void GModel::destroy()
+{
   for(riter it = firstRegion(); it != lastRegion(); ++it)
     delete *it;
+  regions.clear();
   for(fiter it = firstFace(); it != lastFace(); ++it)
     delete *it;
+  faces.clear();
   for(eiter it = firstEdge(); it != lastEdge(); ++it)
     delete *it;
+  edges.clear();
   for(viter it = firstVertex(); it != lastVertex(); ++it)
     delete *it;
-  delete normals; 
+  vertices.clear();
+  if(normals) delete normals;
+  normals = 0;
   MVertex::resetGlobalNumber();
   MElement::resetGlobalNumber();
 }
diff --git a/Geo/GModel.h b/Geo/GModel.h
index 2fe07200ce..7af51784c1 100644
--- a/Geo/GModel.h
+++ b/Geo/GModel.h
@@ -44,15 +44,19 @@ class GModel
 
  public:
   GModel(const std::string &name) : modelName(name), normals(0) {}
-  virtual ~GModel();
-
-  virtual void import(){}
+  virtual ~GModel(){ destroy(); }
 
   typedef std::set<GRegion*, GEntityLessThan>::iterator riter;
   typedef std::set<GFace*, GEntityLessThan>::iterator fiter;
   typedef std::set<GEdge*, GEntityLessThan>::iterator eiter;
   typedef std::set<GVertex*, GEntityLessThan>::iterator viter;
 
+  // Deletes everything in a GModel 
+  virtual void destroy();
+
+  // Imports data into a GModel from an external source
+  virtual void import(){}
+
   // Returns the geometric tolerance for the entire model.
   virtual double tolerance() const { return 1.e-14; }
 
diff --git a/Geo/Geo.cpp b/Geo/Geo.cpp
index 2ea599b17c..f18b51747a 100644
--- a/Geo/Geo.cpp
+++ b/Geo/Geo.cpp
@@ -1,4 +1,4 @@
-// $Id: Geo.cpp,v 1.54 2006-08-13 20:46:54 geuzaine Exp $
+// $Id: Geo.cpp,v 1.55 2006-08-18 17:49:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -100,6 +100,9 @@ void add_infile(char *text, char *fich)
   fprintf(file, "%s\n", text);
   fclose(file);
 
+  // we need to start from scratch since the command just parsed could
+  // have deleted some entities
+  GMODEL->destroy();
   GMODEL->import();
 }
 
diff --git a/Geo/gmshModel.cpp b/Geo/gmshModel.cpp
index d14775393b..4ba63d8bc1 100644
--- a/Geo/gmshModel.cpp
+++ b/Geo/gmshModel.cpp
@@ -1,4 +1,4 @@
-// $Id: gmshModel.cpp,v 1.12 2006-08-15 06:26:53 geuzaine Exp $
+// $Id: gmshModel.cpp,v 1.13 2006-08-18 17:49:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -33,9 +33,6 @@ extern Mesh *THEM;
 
 void gmshModel::import()
 {
-  // When en entity that already exists is reimported, we don't do
-  // anything (except making it invisible if we are asked to)
-
   if(Tree_Nbr(THEM->Points)) {
     List_T *points = Tree2List(THEM->Points);
     for(int i = 0; i < List_Nbr(points); i++){
diff --git a/Graphics/Mesh.cpp b/Graphics/Mesh.cpp
index 5865e4b097..792673b957 100644
--- a/Graphics/Mesh.cpp
+++ b/Graphics/Mesh.cpp
@@ -1,4 +1,4 @@
-// $Id: Mesh.cpp,v 1.177 2006-08-18 15:13:34 geuzaine Exp $
+// $Id: Mesh.cpp,v 1.178 2006-08-18 17:49:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -821,8 +821,7 @@ void Draw_Mesh()
       if(status >= 1)
 	std::for_each(GMODEL->firstEdge(), GMODEL->lastEdge(), initMeshGEdge());
       if(status >= 2){
-	if(GMODEL->normals) 
-	  delete GMODEL->normals;
+	if(GMODEL->normals) delete GMODEL->normals;
 	GMODEL->normals = new smooth_normals(CTX.mesh.angle_smooth_normals);
 	if(CTX.mesh.smooth_normals)
 	  std::for_each(GMODEL->firstFace(), GMODEL->lastFace(), initSmoothNormalsGFace());
diff --git a/Parser/OpenFile.cpp b/Parser/OpenFile.cpp
index 6ab4dbbf02..3c7f847bfb 100644
--- a/Parser/OpenFile.cpp
+++ b/Parser/OpenFile.cpp
@@ -1,4 +1,4 @@
-// $Id: OpenFile.cpp,v 1.113 2006-08-17 03:22:22 geuzaine Exp $
+// $Id: OpenFile.cpp,v 1.114 2006-08-18 17:49:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -344,9 +344,7 @@ void OpenProblem(char *name)
   }
   CTX.threads_lock = 1;
 
-  // recreate a brand new gmsh model
-  if(GMODEL) delete GMODEL;
-  GMODEL = new gmshModel;
+  GMODEL->destroy();
 
   Init_Mesh();
 
-- 
GitLab