Salome HOME
Update mail address
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ILocalOperations.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_ILocalOperations.hxx>
23
24 #include <GEOM_Function.hxx>
25 #include <GEOM_PythonDump.hxx>
26
27 #include <GEOMImpl_Types.hxx>
28
29 #include <GEOMImpl_FilletDriver.hxx>
30 #include <GEOMImpl_ChamferDriver.hxx>
31
32 #include <GEOMImpl_IFillet.hxx>
33 #include <GEOMImpl_IChamfer.hxx>
34
35 #include <GEOMImpl_IArchimede.hxx>
36 #include <GEOMImpl_ArchimedeDriver.hxx>
37
38 #include <GEOMImpl_Gen.hxx>
39 #include <GEOMImpl_IShapesOperations.hxx>
40
41 #include "utilities.h"
42 #include <OpUtil.hxx>
43 #include <Utils_ExceptHandlers.hxx>
44
45 #include <TFunction_DriverTable.hxx>
46 #include <TFunction_Driver.hxx>
47 #include <TFunction_Logbook.hxx>
48 #include <TDF_Tool.hxx>
49
50 #include <TopExp.hxx>
51 #include <TopoDS_TShape.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
53
54 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
55
56 //=============================================================================
57 /*!
58  *   constructor:
59  */
60 //=============================================================================
61 GEOMImpl_ILocalOperations::GEOMImpl_ILocalOperations (GEOM_Engine* theEngine, int theDocID)
62 : GEOM_IOperations(theEngine, theDocID)
63 {
64   MESSAGE("GEOMImpl_ILocalOperations::GEOMImpl_ILocalOperations");
65 }
66
67 //=============================================================================
68 /*!
69  *  destructor
70  */
71 //=============================================================================
72 GEOMImpl_ILocalOperations::~GEOMImpl_ILocalOperations()
73 {
74   MESSAGE("GEOMImpl_ILocalOperations::~GEOMImpl_ILocalOperations");
75 }
76
77
78 //=============================================================================
79 /*!
80  *  MakeFilletAll
81  */
82 //=============================================================================
83 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeFilletAll
84                                     (Handle(GEOM_Object) theShape, double theR)
85 {
86   SetErrorCode(KO);
87
88   //Add a new Fillet object
89   Handle(GEOM_Object) aFillet = GetEngine()->AddObject(GetDocID(), GEOM_FILLET);
90
91   //Add a new Fillet function
92   Handle(GEOM_Function) aFunction =
93     aFillet->AddFunction(GEOMImpl_FilletDriver::GetID(), FILLET_SHAPE_ALL);
94   if (aFunction.IsNull()) return NULL;
95
96   //Check if the function is set correctly
97   if (aFunction->GetDriverGUID() != GEOMImpl_FilletDriver::GetID()) return NULL;
98
99   GEOMImpl_IFillet aCI (aFunction);
100
101   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
102   if (aRefShape.IsNull()) return NULL;
103
104   aCI.SetShape(aRefShape);
105   aCI.SetR(theR);
106
107   //Compute the Fillet value
108   try {
109     if (!GetSolver()->ComputeFunction(aFunction)) {
110       SetErrorCode("Fillet driver failed");
111       return NULL;
112     }
113   }
114   catch (Standard_Failure) {
115     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
116     SetErrorCode(aFail->GetMessageString());
117     return NULL;
118   }
119
120   //Make a Python command
121   GEOM::TPythonDump(aFunction) << aFillet << " = geompy.MakeFilletAll("
122                                << theShape << ", " << theR << ")";
123
124   SetErrorCode(OK);
125   return aFillet;
126 }
127
128 //=============================================================================
129 /*!
130  *  MakeFilletEdges
131  */
132 //=============================================================================
133 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeFilletEdges
134        (Handle(GEOM_Object) theShape, double theR, list<int> theEdges)
135 {
136   SetErrorCode(KO);
137
138   //Add a new Fillet object
139   Handle(GEOM_Object) aFillet = GetEngine()->AddObject(GetDocID(), GEOM_FILLET);
140
141   //Add a new Fillet function
142   Handle(GEOM_Function) aFunction =
143     aFillet->AddFunction(GEOMImpl_FilletDriver::GetID(), FILLET_SHAPE_EDGES);
144   if (aFunction.IsNull()) return NULL;
145
146   //Check if the function is set correctly
147   if (aFunction->GetDriverGUID() != GEOMImpl_FilletDriver::GetID()) return NULL;
148
149   GEOMImpl_IFillet aCI (aFunction);
150
151   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
152   if (aRefShape.IsNull()) return NULL;
153
154   aCI.SetShape(aRefShape);
155   aCI.SetR(theR);
156   int aLen = theEdges.size();
157   aCI.SetLength(aLen);
158
159   int ind = 1;
160   list<int>::iterator it = theEdges.begin();
161   for (; it != theEdges.end(); it++, ind++) {
162     aCI.SetEdge(ind, (*it));
163   }
164
165   //Compute the Fillet value
166   try {
167     if (!GetSolver()->ComputeFunction(aFunction)) {
168       SetErrorCode("Fillet driver failed");
169       return NULL;
170     }
171   }
172   catch (Standard_Failure) {
173     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
174     SetErrorCode(aFail->GetMessageString());
175     return NULL;
176   }
177
178   //Make a Python command
179   GEOM::TPythonDump pd (aFunction);
180   pd << aFillet << " = geompy.MakeFillet(" << theShape
181     << ", " << theR << ", geompy.ShapeType[\"EDGE\"], [";
182
183   it = theEdges.begin();
184   pd << (*it++);
185   while (it != theEdges.end()) {
186     pd << ", " << (*it++);
187   }
188   pd << "])";
189
190   SetErrorCode(OK);
191   return aFillet;
192 }
193
194 //=============================================================================
195 /*!
196  *  MakeFilletFaces
197  */
198 //=============================================================================
199 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeFilletFaces
200        (Handle(GEOM_Object) theShape, double theR, list<int> theFaces)
201 {
202   SetErrorCode(KO);
203
204   //Add a new Fillet object
205   Handle(GEOM_Object) aFillet = GetEngine()->AddObject(GetDocID(), GEOM_FILLET);
206
207   //Add a new Fillet function
208   Handle(GEOM_Function) aFunction =
209     aFillet->AddFunction(GEOMImpl_FilletDriver::GetID(), FILLET_SHAPE_FACES);
210   if (aFunction.IsNull()) return NULL;
211
212   //Check if the function is set correctly
213   if (aFunction->GetDriverGUID() != GEOMImpl_FilletDriver::GetID()) return NULL;
214
215   GEOMImpl_IFillet aCI (aFunction);
216
217   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
218   if (aRefShape.IsNull()) return NULL;
219
220   aCI.SetShape(aRefShape);
221   aCI.SetR(theR);
222   int aLen = theFaces.size();
223   aCI.SetLength(aLen);
224
225   int ind = 1;
226   list<int>::iterator it = theFaces.begin();
227   for (; it != theFaces.end(); it++, ind++) {
228     aCI.SetFace(ind, (*it));
229   }
230
231   //Compute the Fillet value
232   try {
233     if (!GetSolver()->ComputeFunction(aFunction)) {
234       SetErrorCode("Fillet driver failed");
235       return NULL;
236     }
237   }
238   catch (Standard_Failure) {
239     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
240     SetErrorCode(aFail->GetMessageString());
241     return NULL;
242   }
243
244   //Make a Python command
245   GEOM::TPythonDump pd (aFunction);
246   pd << aFillet << " = geompy.MakeFillet(" << theShape
247     << ", " << theR << ", geompy.ShapeType[\"FACE\"], [";
248
249   it = theFaces.begin();
250   pd << (*it++);
251   while (it != theFaces.end()) {
252     pd << ", " << (*it++);
253   }
254   pd << "])";
255
256   SetErrorCode(OK);
257   return aFillet;
258 }
259
260 //=============================================================================
261 /*!
262  *  MakeChamferAll
263  */
264 //=============================================================================
265 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeChamferAll (Handle(GEOM_Object) theShape, double theD)
266 {
267   SetErrorCode(KO);
268
269   //Add a new Chamfer object
270   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_CHAMFER);
271
272   //Add a new Chamfer function
273   Handle(GEOM_Function) aFunction =
274     aChamfer->AddFunction(GEOMImpl_ChamferDriver::GetID(), CHAMFER_SHAPE_ALL);
275   if (aFunction.IsNull()) return NULL;
276
277   //Check if the function is set correctly
278   if (aFunction->GetDriverGUID() != GEOMImpl_ChamferDriver::GetID()) return NULL;
279
280   GEOMImpl_IChamfer aCI (aFunction);
281
282   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
283   if (aRefShape.IsNull()) return NULL;
284
285   aCI.SetShape(aRefShape);
286   aCI.SetD(theD);
287
288   //Compute the Chamfer value
289   try {
290     if (!GetSolver()->ComputeFunction(aFunction)) {
291       SetErrorCode("Chamfer driver failed");
292       return NULL;
293     }
294   }
295   catch (Standard_Failure) {
296     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
297     SetErrorCode(aFail->GetMessageString());
298     return NULL;
299   }
300
301   //Make a Python command
302   GEOM::TPythonDump(aFunction) << aChamfer << " = geompy.MakeChamferAll("
303                                << theShape << ", " << theD << ")";
304
305   SetErrorCode(OK);
306   return aChamfer;
307 }
308
309 //=============================================================================
310 /*!
311  *  MakeChamferEdge
312  */
313 //=============================================================================
314 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeChamferEdge
315                             (Handle(GEOM_Object) theShape, double theD1, double theD2,
316                              int theFace1, int theFace2)
317 {
318   SetErrorCode(KO);
319
320   //Add a new Chamfer object
321   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_CHAMFER);
322
323   //Add a new Chamfer function
324   Handle(GEOM_Function) aFunction =
325     aChamfer->AddFunction(GEOMImpl_ChamferDriver::GetID(), CHAMFER_SHAPE_EDGE);
326   if (aFunction.IsNull()) return NULL;
327
328   //Check if the function is set correctly
329   if (aFunction->GetDriverGUID() != GEOMImpl_ChamferDriver::GetID()) return NULL;
330
331   GEOMImpl_IChamfer aCI (aFunction);
332
333   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
334   if (aRefShape.IsNull()) return NULL;
335
336   aCI.SetShape(aRefShape);
337   aCI.SetD1(theD1);
338   aCI.SetD2(theD2);
339   aCI.SetFace1(theFace1);
340   aCI.SetFace2(theFace2);
341
342   //Compute the Chamfer value
343   try {
344     if (!GetSolver()->ComputeFunction(aFunction)) {
345       SetErrorCode("Chamfer driver failed");
346       return NULL;
347     }
348   }
349   catch (Standard_Failure) {
350     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
351     SetErrorCode(aFail->GetMessageString());
352     return NULL;
353   }
354
355   //Make a Python command
356   GEOM::TPythonDump(aFunction) << aChamfer
357     << " = geompy.MakeChamferEdge(" << theShape << ", " << theD1
358       << ", " << theD2 << ", " << theFace1 << ", " << theFace2 << ")";
359
360   SetErrorCode(OK);
361   return aChamfer;
362 }
363
364 //=============================================================================
365 /*!
366  *  MakeChamferFaces
367  */
368 //=============================================================================
369 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeChamferFaces
370                             (Handle(GEOM_Object) theShape, double theD1, double theD2,
371                              list<int> theFaces)
372 {
373   SetErrorCode(KO);
374
375   //Add a new Chamfer object
376   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_CHAMFER);
377
378   //Add a new Chamfer function
379   Handle(GEOM_Function) aFunction =
380     aChamfer->AddFunction(GEOMImpl_ChamferDriver::GetID(), CHAMFER_SHAPE_FACES);
381   if (aFunction.IsNull()) return NULL;
382
383   //Check if the function is set correctly
384   if (aFunction->GetDriverGUID() != GEOMImpl_ChamferDriver::GetID()) return NULL;
385
386   GEOMImpl_IChamfer aCI (aFunction);
387
388   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
389   if (aRefShape.IsNull()) return NULL;
390
391   aCI.SetShape(aRefShape);
392   aCI.SetD1(theD1);
393   aCI.SetD2(theD2);
394   int aLen = theFaces.size();
395   aCI.SetLength(aLen);
396
397   int ind = 1;
398   list<int>::iterator it = theFaces.begin();
399   for (; it != theFaces.end(); it++, ind++) {
400     aCI.SetFace(ind, (*it));
401   }
402
403   //Compute the Chamfer value
404   try {
405     if (!GetSolver()->ComputeFunction(aFunction)) {
406       SetErrorCode("Chamfer driver failed");
407       return NULL;
408     }
409   }
410   catch (Standard_Failure) {
411     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
412     SetErrorCode(aFail->GetMessageString());
413     return NULL;
414   }
415
416   //Make a Python command
417   GEOM::TPythonDump pd (aFunction);
418   pd << aChamfer << " = geompy.MakeChamferFaces(" << theShape
419     << ", " << theD1 << ", " << theD2 << ", [";
420
421   it = theFaces.begin();
422   pd << (*it++);
423   while (it != theFaces.end()) {
424     pd << ", " << (*it++);
425   }
426   pd << "])";
427
428   SetErrorCode(OK);
429   return aChamfer;
430 }
431
432 //=============================================================================
433 /*!
434  *  Archimede
435  */
436 //=============================================================================
437 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeArchimede (Handle(GEOM_Object) theShape,
438                                                               double theWeight,
439                                                               double theWaterDensity,
440                                                               double theMeshingDeflection)
441 {
442   SetErrorCode(KO);
443
444   //Add a new Archimede object
445   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_ARCHIMEDE);
446
447   //Add a new Archimede function
448   Handle(GEOM_Function) aFunction = aChamfer->AddFunction(GEOMImpl_ArchimedeDriver::GetID(), ARCHIMEDE_TYPE);
449   if (aFunction.IsNull()) return NULL;
450
451   //Check if the function is set correctly
452   if (aFunction->GetDriverGUID() != GEOMImpl_ArchimedeDriver::GetID()) return NULL;
453
454   GEOMImpl_IArchimede aAI (aFunction);
455
456   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
457   if (aRefShape.IsNull()) return NULL;
458
459   aAI.SetBasicShape(aRefShape);
460   aAI.SetWeight(theWeight);
461   aAI.SetDensity(theWaterDensity);
462   aAI.SetDeflection(theMeshingDeflection);
463
464   //Compute the Archimede value
465   try {
466     if (!GetSolver()->ComputeFunction(aFunction)) {
467       SetErrorCode("Archimede driver failed");
468       return NULL;
469     }
470   }
471   catch (Standard_Failure) {
472     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
473     SetErrorCode(aFail->GetMessageString());
474     return NULL;
475   }
476
477   //Make a Python command
478   GEOM::TPythonDump(aFunction) << aChamfer
479     << " = geompy.Archimede(" << theShape << ", " << theWeight << ", "
480       << theWaterDensity << ", " << theMeshingDeflection << ")";
481
482   SetErrorCode(OK);
483   return aChamfer;
484 }
485
486 //=============================================================================
487 /*!
488  *  GetSubShape
489  */
490 //=============================================================================
491 bool GEOMImpl_ILocalOperations::GetSubShape (const TopoDS_Shape& theShape, const int theIndex,
492                                              TopoDS_Shape& theSubShape)
493 {
494   if (theShape.IsNull() || theIndex < 1)
495     return false;
496
497   TopTools_IndexedMapOfShape anIndices;
498   TopExp::MapShapes(theShape, anIndices);
499   if (theIndex > anIndices.Extent()) return false;
500   theSubShape = anIndices.FindKey(theIndex);
501
502   return true;
503 }
504
505 //=============================================================================
506 /*!
507  *  GetSubShapeIndex
508  */
509 //=============================================================================
510 Standard_Integer GEOMImpl_ILocalOperations::GetSubShapeIndex (Handle(GEOM_Object) theShape,
511                                                               Handle(GEOM_Object) theSubShape)
512 {
513   SetErrorCode(KO);
514
515   Standard_Integer anInd = -1;
516   GEOM_Engine* anEngine = GetEngine();
517   //GEOMImpl_Gen* aGen = dynamic_cast<GEOMImpl_Gen*>(anEngine);
518   GEOMImpl_Gen* aGen = (GEOMImpl_Gen*)anEngine;
519
520   if (aGen) {
521     GEOMImpl_IShapesOperations* anIShapesOperations =
522       aGen->GetIShapesOperations(GetDocID());
523     anInd = anIShapesOperations->GetSubShapeIndex(theShape, theSubShape);
524     SetErrorCode(anIShapesOperations->GetErrorCode());
525   }
526
527   return anInd;
528 }