MAGiC  V5.0
Mailleurs Automatiques de Géometries intégrés à la Cao
poly_build_occ.cpp
Aller à la documentation de ce fichier.
1 //####//------------------------------------------------------------
2 //####//------------------------------------------------------------
3 //####// MAGiC
4 //####// Jean Christophe Cuilliere et Vincent FRANCOIS
5 //####// Departement de Genie Mecanique - UQTR
6 //####//------------------------------------------------------------
7 //####// MAGIC est un projet de recherche de l equipe ERICCA
8 //####// du departement de genie mecanique de l Universite du Quebec a Trois Rivieres
9 //####// http://www.uqtr.ca/ericca
10 //####// http://www.uqtr.ca/
11 //####//------------------------------------------------------------
12 //####//------------------------------------------------------------
13 //####//
14 //####// poly_build_occ.cpp
15 //####//
16 //####//------------------------------------------------------------
17 //####//------------------------------------------------------------
18 //####// COPYRIGHT 2000-2024
19 //####// jeu 13 jun 2024 11:54:00 EDT
20 //####//------------------------------------------------------------
21 //####//------------------------------------------------------------
22 #include "poly_build_occ.h"
23 #include "poly_voro.h"
24 #include "poly_noeud.h"
25 #include "poly_face.h"
26 #include "poly_cellule.h"
27 
28 
29 #include <TopoDS_Shape.hxx>
30 #include <gp_Pnt.hxx>
31 #include <BRepPrimAPI_MakeBox.hxx>
32 #include <BRepAlgoAPI_BuilderAlgo.hxx>
33 #include <STEPCAFControl_Writer.hxx>
34 #include <BRepTools.hxx>
35 #include <BRepBuilderAPI_MakeVertex.hxx>
36 #include <BRepBuilderAPI_MakeEdge.hxx>
37 #include <BRepBuilderAPI_MakeWire.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <BRepBuilderAPI_Sewing.hxx>
40 #include <BRepBuilderAPI_MakeSolid.hxx>
41 #include <TopoDS.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43 #include <TopExp.hxx>
44 #include <BRep_Builder.hxx>
45 #include <BRepGProp.hxx>
46 #include <GProp_GProps.hxx>
47 #include <STEPControl_Writer.hxx>
48 
49 Poly_Build_OCC::Poly_Build_OCC(Poly_Voro* vorotmp,std::string nom):POLY_AFFICHE(),nomfichier(nom),voro(vorotmp)
50 {
51 }
52 
53 
54 void Poly_Build_OCC::construit(bool avecstep)
55  {
56  TopTools_ListOfShape list_shell;
57 
58  // conteneur (boite)
59  Poly_Point* pnt_min = voro->get_point(0);
60  Poly_Point* pnt_max = voro->get_point(6);
61  gp_Pnt coin_min(pnt_min->get_x(), pnt_min->get_y(), pnt_min->get_z());
62  gp_Pnt coin_max(pnt_max->get_x(), pnt_max->get_y(), pnt_max->get_z());
63  TopoDS_Shape boite = BRepPrimAPI_MakeBox(coin_min,coin_max).Shape();
64  GProp_GProps vprops;
65  BRepGProp::VolumeProperties(boite,vprops);
66  double volumeboite=vprops.Mass();
67  list_shell.Append(boite);
68 
69  for (int i=0; i < voro->get_nb_cell(); i++)
70  {
71  bool passeface=true;
72  BRepBuilderAPI_Sewing sewing;
73  Poly_Cellule* cell = voro->get_cell(i);
74 
75  // création des vertex
76  std::vector< TopoDS_Vertex > cell_vertex;
77  for(int j=0; j < cell->get_nb_noeud(); j++)
78 {
79  Poly_Noeud* noeud = cell->get_noeud(j);
80  if (noeud->get_maitre_fusion()==noeud)
81  {
82  double x = noeud->get_x();
83  double y = noeud->get_y();
84  double z = noeud->get_z();
85  gp_Pnt pnt(x, y, z);
86  TopoDS_Vertex vtx = BRepBuilderAPI_MakeVertex(pnt);
87  cell_vertex.push_back(vtx);
88  noeud->change_vertex(vtx);
89  }
90  }
91  // création des edges, faces et shell
92  for(int j=0; j < cell->get_nb_face(); j++)
93  {
94  Poly_Face* cell_face = cell->get_face(j);
95  BRepBuilderAPI_MakeWire brep_wire;
96  bool passe=false;
97  for(int k=0; k < cell_face->get_nb_noeud()-1; k++)
98  {
99  int v1 = cell_face->get_noeud(k);
100  int v2 = cell_face->get_noeud(k+1);
101  Poly_Noeud* n1=cell->get_noeud(v1);
102  Poly_Noeud* n2=cell->get_noeud(v2);
103  TopoDS_Vertex vtx1=n1->get_maitre_fusion()->get_vertex();
104  TopoDS_Vertex vtx2=n2->get_maitre_fusion()->get_vertex();
105  /*gp_Pnt pnt1=BRep_Tool::Pnt(vtx1);
106  gp_Pnt pnt2=BRep_Tool::Pnt(vtx2);
107  double x = pnt1.X();
108  double y = pnt1.Y();
109  double z = pnt1.Z();
110  double x2 = pnt2.X();
111  double y2 = pnt2.Y();
112  double z2 = pnt2.Z();*/
113  if (vtx1!=vtx2)
114  {
115  TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(vtx1,vtx2);
116  brep_wire.Add(edge);
117  passe=true;
118  }
119  }
120 
121  int v1 = cell_face->get_last_noeud();
122  int v2 = cell_face->get_noeud(0);
123  Poly_Noeud* n1=cell->get_noeud(v1);
124  Poly_Noeud* n2=cell->get_noeud(v2);
125  TopoDS_Vertex vtx1=n1->get_maitre_fusion()->get_vertex();
126  TopoDS_Vertex vtx2=n2->get_maitre_fusion()->get_vertex();
127  if (vtx1!=vtx2)
128  {
129  TopoDS_Edge lastedge = BRepBuilderAPI_MakeEdge(vtx1,vtx2);
130  brep_wire.Add(lastedge); passe=true;
131  }
132 
133  if (passe)
134  {
135  TopoDS_Wire Wire_face = brep_wire.Wire();
136  BRepBuilderAPI_MakeFace facemaker(Wire_face);
137  if (facemaker.IsDone())
138  {
139  const TopoDS_Face newFace = facemaker.Face();
140  sewing.Add(newFace);
141  }
142  else
143  passeface=false;
144  }
145  else passeface=false;
146  }
147 
148  if (passeface==true)
149  {
150  sewing.Perform();
151  TopoDS_Shell shell = TopoDS::Shell(sewing.SewedShape());
152  BRepBuilderAPI_MakeSolid solidmaker(shell);
153  TopoDS_Solid solid = solidmaker.Solid();
154  //list_shell.Append(solid);
155  list_shell.Append(shell);
156  }
157  }
158  char message[2000];
159  sprintf(message," Nombre de cristaux OCC construits = %d",list_shell.Size()-1);
160  //sprintf(message," Nombre de cristaux OCC construits = %d",list_shell.Size());
161  affiche(message);
162  /*
163  TopTools_ListOfShape list_shell2;
164  list_shell2.Append(list_shell.First());
165  list_shell.RemoveFirst();
166  list_shell2.Append(list_shell.First());
167  list_shell.RemoveFirst();
168  TopoDS_Shape resultat;
169  int nb=list_shell.Size();
170  for (int i=0;i<nb+1;i++)
171  {
172  BRepAlgoAPI_BuilderAlgo fragments;
173  fragments.SetArguments(list_shell2);
174  fragments.Build();
175  resultat = fragments.Shape();
176  TopTools_IndexedMapOfShape map_TopoDS_Solid;
177  if (i!=nb)
178  {
179  TopExp::MapShapes(resultat, TopAbs_SOLID,map_TopoDS_Solid);
180 
181  list_shell2.Clear();
182  for(TopTools_IndexedMapOfShape::Iterator it_solid(map_TopoDS_Solid);it_solid.More();it_solid.Next())
183  list_shell2.Append(it_solid.Value());
184  list_shell2.Append(list_shell.First());
185  list_shell.RemoveFirst();
186  }
187  }
188 
189  */
190 
191 
192  BRepAlgoAPI_BuilderAlgo fragments;
193 
194 
195  fragments.SetArguments(list_shell);
196  fragments.Build();
197  TopoDS_Shape resultat = fragments.Shape();
198 
199  TopTools_IndexedMapOfShape map_TopoDS_Solid;
200 
201  TopExp::MapShapes(resultat, TopAbs_SOLID,map_TopoDS_Solid);
202 
203  /*TopTools_IndexedMapOfShape map_TopoDS_Solid;
204  TopExp::MapShapes(resultat, TopAbs_SOLID,map_TopoDS_Solid);*/
205 
206 
207  BRep_Builder brep_builder;
208  TopoDS_Compound Compound;
209  brep_builder.MakeCompound(Compound);
210  double volumetotal=0.;
211  for(TopTools_IndexedMapOfShape::Iterator it_solid(map_TopoDS_Solid);it_solid.More();it_solid.Next())
212  {
213  GProp_GProps vprops;
214  BRepGProp::VolumeProperties(it_solid.Value(),vprops);
215  double volume=vprops.Mass();
216  if (volume<0.95*volumeboite)
217  if (volume>1e-10)
218  {
219  brep_builder.Add(Compound,it_solid.Value());
220  volumetotal=volumetotal+volume;
221  }
222  }
223  sprintf(message," Volume des cristaux OCC assemblés = %lf",volumetotal);
224  affiche(message);
225 
226  BRepTools::Write(Compound,(char*)nomfichier.c_str());
227 
228  if (avecstep)
229  {
230  STEPControl_Writer stepwriter;
231  stepwriter.Transfer(Compound,STEPControl_ManifoldSolidBrep);
232  std::string nomstep=nomfichier.substr(0,nomfichier.find_last_of("."))+".stp";
233  stepwriter.Write(nomstep.c_str());
234  affiche((char*)"");
235  }
236 }
237 
238 
240 {
241 }
242 
Poly_Build_OCC::Poly_Build_OCC
Poly_Build_OCC(Poly_Voro *voro, std::string nom)
Definition: poly_build_occ.cpp:49
Poly_Build_OCC::voro
Poly_Voro * voro
Definition: poly_build_occ.h:41
Poly_Build_OCC::construit
virtual void construit(bool avecstep=false)
Definition: poly_build_occ.cpp:54
Poly_Cellule
Definition: poly_cellule.h:31
poly_cellule.h
Poly_Point::get_x
double get_x(void)
Definition: poly_point.cpp:36
Poly_Cellule::get_face
Poly_Face * get_face(int num)
Definition: poly_cellule.cpp:48
Poly_Point::get_z
double get_z(void)
Definition: poly_point.cpp:38
Poly_Point
Definition: poly_point.h:25
poly_noeud.h
Poly_Voro
Definition: poly_voro.h:31
Poly_Voro::get_nb_cell
virtual int get_nb_cell(void)
Definition: poly_voro.cpp:326
POLY_AFFICHE
Definition: poly_affiche.h:29
Poly_Cellule::get_nb_noeud
int get_nb_noeud(void)
Definition: poly_cellule.cpp:53
Poly_Noeud::change_vertex
void change_vertex(TopoDS_Vertex p)
Definition: poly_noeud.cpp:74
Poly_Face::get_last_noeud
int get_last_noeud(void)
Definition: poly_face.cpp:41
Poly_Cellule::get_nb_face
int get_nb_face(void)
Definition: poly_cellule.cpp:54
Poly_Face::get_noeud
int get_noeud(int num)
Definition: poly_face.cpp:40
Poly_Noeud::get_maitre_fusion
Poly_Noeud * get_maitre_fusion(void)
Definition: poly_noeud.cpp:91
POLY_AFFICHE::affiche
virtual void affiche(char *mess)
Definition: poly_affiche.cpp:42
Poly_Face::get_nb_noeud
int get_nb_noeud(void)
Definition: poly_face.cpp:43
Poly_Noeud::get_vertex
TopoDS_Vertex get_vertex(void)
Definition: poly_noeud.cpp:79
poly_face.h
poly_voro.h
poly_build_occ.h
Poly_Build_OCC::~Poly_Build_OCC
~Poly_Build_OCC()
Definition: poly_build_occ.cpp:239
Poly_Cellule::get_noeud
Poly_Noeud * get_noeud(int num)
Definition: poly_cellule.cpp:47
Poly_Voro::get_point
Poly_Point * get_point(int num)
Definition: poly_voro.cpp:316
Poly_Build_OCC::nomfichier
std::string nomfichier
Definition: poly_build_occ.h:40
Poly_Point::get_y
double get_y(void)
Definition: poly_point.cpp:37
Poly_Voro::get_cell
Poly_Cellule * get_cell(int num)
Definition: poly_voro.cpp:321
Poly_Noeud
Definition: poly_noeud.h:31
Poly_Face
Definition: poly_face.h:27