Salome HOME
Updated for PAL14857.
[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_Failure.hxx>
55 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
56
57 //=============================================================================
58 /*!
59  *   constructor:
60  */
61 //=============================================================================
62 GEOMImpl_ILocalOperations::GEOMImpl_ILocalOperations (GEOM_Engine* theEngine, int theDocID)
63 : GEOM_IOperations(theEngine, theDocID)
64 {
65   MESSAGE("GEOMImpl_ILocalOperations::GEOMImpl_ILocalOperations");
66 }
67
68 //=============================================================================
69 /*!
70  *  destructor
71  */
72 //=============================================================================
73 GEOMImpl_ILocalOperations::~GEOMImpl_ILocalOperations()
74 {
75   MESSAGE("GEOMImpl_ILocalOperations::~GEOMImpl_ILocalOperations");
76 }
77
78
79 //=============================================================================
80 /*!
81  *  MakeFilletAll
82  */
83 //=============================================================================
84 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeFilletAll
85                                     (Handle(GEOM_Object) theShape, double theR)
86 {
87   SetErrorCode(KO);
88
89   //Add a new Fillet object
90   Handle(GEOM_Object) aFillet = GetEngine()->AddObject(GetDocID(), GEOM_FILLET);
91
92   //Add a new Fillet function
93   Handle(GEOM_Function) aFunction =
94     aFillet->AddFunction(GEOMImpl_FilletDriver::GetID(), FILLET_SHAPE_ALL);
95   if (aFunction.IsNull()) return NULL;
96
97   //Check if the function is set correctly
98   if (aFunction->GetDriverGUID() != GEOMImpl_FilletDriver::GetID()) return NULL;
99
100   GEOMImpl_IFillet aCI (aFunction);
101
102   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
103   if (aRefShape.IsNull()) return NULL;
104
105   aCI.SetShape(aRefShape);
106   aCI.SetR(theR);
107
108   //Compute the Fillet value
109   try {
110 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
111     OCC_CATCH_SIGNALS;
112 #endif
113     if (!GetSolver()->ComputeFunction(aFunction)) {
114       SetErrorCode("Fillet driver failed");
115       return NULL;
116     }
117   }
118   catch (Standard_Failure) {
119     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
120     SetErrorCode(aFail->GetMessageString());
121     return NULL;
122   }
123
124   //Make a Python command
125   GEOM::TPythonDump(aFunction) << aFillet << " = geompy.MakeFilletAll("
126                                << theShape << ", " << theR << ")";
127
128   SetErrorCode(OK);
129   return aFillet;
130 }
131
132 //=============================================================================
133 /*!
134  *  MakeFilletEdges
135  */
136 //=============================================================================
137 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeFilletEdges
138        (Handle(GEOM_Object) theShape, double theR, list<int> theEdges)
139 {
140   SetErrorCode(KO);
141
142   //Add a new Fillet object
143   Handle(GEOM_Object) aFillet = GetEngine()->AddObject(GetDocID(), GEOM_FILLET);
144
145   //Add a new Fillet function
146   Handle(GEOM_Function) aFunction =
147     aFillet->AddFunction(GEOMImpl_FilletDriver::GetID(), FILLET_SHAPE_EDGES);
148   if (aFunction.IsNull()) return NULL;
149
150   //Check if the function is set correctly
151   if (aFunction->GetDriverGUID() != GEOMImpl_FilletDriver::GetID()) return NULL;
152
153   GEOMImpl_IFillet aCI (aFunction);
154
155   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
156   if (aRefShape.IsNull()) return NULL;
157
158   aCI.SetShape(aRefShape);
159   aCI.SetR(theR);
160   int aLen = theEdges.size();
161   aCI.SetLength(aLen);
162
163   int ind = 1;
164   list<int>::iterator it = theEdges.begin();
165   for (; it != theEdges.end(); it++, ind++) {
166     aCI.SetEdge(ind, (*it));
167   }
168
169   //Compute the Fillet value
170   try {
171 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
172     OCC_CATCH_SIGNALS;
173 #endif
174     if (!GetSolver()->ComputeFunction(aFunction)) {
175       SetErrorCode("Fillet driver failed");
176       return NULL;
177     }
178   }
179   catch (Standard_Failure) {
180     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
181     SetErrorCode(aFail->GetMessageString());
182     return NULL;
183   }
184
185   //Make a Python command
186   GEOM::TPythonDump pd (aFunction);
187   pd << aFillet << " = geompy.MakeFillet(" << theShape
188     << ", " << theR << ", geompy.ShapeType[\"EDGE\"], [";
189
190   it = theEdges.begin();
191   pd << (*it++);
192   while (it != theEdges.end()) {
193     pd << ", " << (*it++);
194   }
195   pd << "])";
196
197   SetErrorCode(OK);
198   return aFillet;
199 }
200
201 //=============================================================================
202 /*!
203  *  MakeFilletFaces
204  */
205 //=============================================================================
206 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeFilletFaces
207        (Handle(GEOM_Object) theShape, double theR, list<int> theFaces)
208 {
209   SetErrorCode(KO);
210
211   //Add a new Fillet object
212   Handle(GEOM_Object) aFillet = GetEngine()->AddObject(GetDocID(), GEOM_FILLET);
213
214   //Add a new Fillet function
215   Handle(GEOM_Function) aFunction =
216     aFillet->AddFunction(GEOMImpl_FilletDriver::GetID(), FILLET_SHAPE_FACES);
217   if (aFunction.IsNull()) return NULL;
218
219   //Check if the function is set correctly
220   if (aFunction->GetDriverGUID() != GEOMImpl_FilletDriver::GetID()) return NULL;
221
222   GEOMImpl_IFillet aCI (aFunction);
223
224   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
225   if (aRefShape.IsNull()) return NULL;
226
227   aCI.SetShape(aRefShape);
228   aCI.SetR(theR);
229   int aLen = theFaces.size();
230   aCI.SetLength(aLen);
231
232   int ind = 1;
233   list<int>::iterator it = theFaces.begin();
234   for (; it != theFaces.end(); it++, ind++) {
235     aCI.SetFace(ind, (*it));
236   }
237
238   //Compute the Fillet value
239   try {
240 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
241     OCC_CATCH_SIGNALS;
242 #endif
243     if (!GetSolver()->ComputeFunction(aFunction)) {
244       SetErrorCode("Fillet driver failed");
245       return NULL;
246     }
247   }
248   catch (Standard_Failure) {
249     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
250     SetErrorCode(aFail->GetMessageString());
251     return NULL;
252   }
253
254   //Make a Python command
255   GEOM::TPythonDump pd (aFunction);
256   pd << aFillet << " = geompy.MakeFillet(" << theShape
257     << ", " << theR << ", geompy.ShapeType[\"FACE\"], [";
258
259   it = theFaces.begin();
260   pd << (*it++);
261   while (it != theFaces.end()) {
262     pd << ", " << (*it++);
263   }
264   pd << "])";
265
266   SetErrorCode(OK);
267   return aFillet;
268 }
269
270 //=============================================================================
271 /*!
272  *  MakeChamferAll
273  */
274 //=============================================================================
275 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeChamferAll (Handle(GEOM_Object) theShape, double theD)
276 {
277   SetErrorCode(KO);
278
279   //Add a new Chamfer object
280   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_CHAMFER);
281
282   //Add a new Chamfer function
283   Handle(GEOM_Function) aFunction =
284     aChamfer->AddFunction(GEOMImpl_ChamferDriver::GetID(), CHAMFER_SHAPE_ALL);
285   if (aFunction.IsNull()) return NULL;
286
287   //Check if the function is set correctly
288   if (aFunction->GetDriverGUID() != GEOMImpl_ChamferDriver::GetID()) return NULL;
289
290   GEOMImpl_IChamfer aCI (aFunction);
291
292   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
293   if (aRefShape.IsNull()) return NULL;
294
295   aCI.SetShape(aRefShape);
296   aCI.SetD(theD);
297
298   //Compute the Chamfer value
299   try {
300 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
301     OCC_CATCH_SIGNALS;
302 #endif
303     if (!GetSolver()->ComputeFunction(aFunction)) {
304       SetErrorCode("Chamfer driver failed");
305       return NULL;
306     }
307   }
308   catch (Standard_Failure) {
309     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
310     SetErrorCode(aFail->GetMessageString());
311     return NULL;
312   }
313
314   //Make a Python command
315   GEOM::TPythonDump(aFunction) << aChamfer << " = geompy.MakeChamferAll("
316                                << theShape << ", " << theD << ")";
317
318   SetErrorCode(OK);
319   return aChamfer;
320 }
321
322 //=============================================================================
323 /*!
324  *  MakeChamferEdge
325  */
326 //=============================================================================
327 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeChamferEdge
328                             (Handle(GEOM_Object) theShape, double theD1, double theD2,
329                              int theFace1, int theFace2)
330 {
331   SetErrorCode(KO);
332
333   //Add a new Chamfer object
334   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_CHAMFER);
335
336   //Add a new Chamfer function
337   Handle(GEOM_Function) aFunction =
338     aChamfer->AddFunction(GEOMImpl_ChamferDriver::GetID(), CHAMFER_SHAPE_EDGE);
339   if (aFunction.IsNull()) return NULL;
340
341   //Check if the function is set correctly
342   if (aFunction->GetDriverGUID() != GEOMImpl_ChamferDriver::GetID()) return NULL;
343
344   GEOMImpl_IChamfer aCI (aFunction);
345
346   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
347   if (aRefShape.IsNull()) return NULL;
348
349   aCI.SetShape(aRefShape);
350   aCI.SetD1(theD1);
351   aCI.SetD2(theD2);
352   aCI.SetFace1(theFace1);
353   aCI.SetFace2(theFace2);
354
355   //Compute the Chamfer value
356   try {
357 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
358     OCC_CATCH_SIGNALS;
359 #endif
360     if (!GetSolver()->ComputeFunction(aFunction)) {
361       SetErrorCode("Chamfer driver failed");
362       return NULL;
363     }
364   }
365   catch (Standard_Failure) {
366     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
367     SetErrorCode(aFail->GetMessageString());
368     return NULL;
369   }
370
371   //Make a Python command
372   GEOM::TPythonDump(aFunction) << aChamfer
373     << " = geompy.MakeChamferEdge(" << theShape << ", " << theD1
374       << ", " << theD2 << ", " << theFace1 << ", " << theFace2 << ")";
375
376   SetErrorCode(OK);
377   return aChamfer;
378 }
379
380 //=============================================================================
381 /*!
382  *  MakeChamferFaces
383  */
384 //=============================================================================
385 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeChamferFaces
386                             (Handle(GEOM_Object) theShape, double theD1, double theD2,
387                              list<int> theFaces)
388 {
389   SetErrorCode(KO);
390
391   //Add a new Chamfer object
392   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_CHAMFER);
393
394   //Add a new Chamfer function
395   Handle(GEOM_Function) aFunction =
396     aChamfer->AddFunction(GEOMImpl_ChamferDriver::GetID(), CHAMFER_SHAPE_FACES);
397   if (aFunction.IsNull()) return NULL;
398
399   //Check if the function is set correctly
400   if (aFunction->GetDriverGUID() != GEOMImpl_ChamferDriver::GetID()) return NULL;
401
402   GEOMImpl_IChamfer aCI (aFunction);
403
404   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
405   if (aRefShape.IsNull()) return NULL;
406
407   aCI.SetShape(aRefShape);
408   aCI.SetD1(theD1);
409   aCI.SetD2(theD2);
410   int aLen = theFaces.size();
411   aCI.SetLength(aLen);
412
413   int ind = 1;
414   list<int>::iterator it = theFaces.begin();
415   for (; it != theFaces.end(); it++, ind++) {
416     aCI.SetFace(ind, (*it));
417   }
418
419   //Compute the Chamfer value
420   try {
421 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
422     OCC_CATCH_SIGNALS;
423 #endif
424     if (!GetSolver()->ComputeFunction(aFunction)) {
425       SetErrorCode("Chamfer driver failed");
426       return NULL;
427     }
428   }
429   catch (Standard_Failure) {
430     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
431     SetErrorCode(aFail->GetMessageString());
432     return NULL;
433   }
434
435   //Make a Python command
436   GEOM::TPythonDump pd (aFunction);
437   pd << aChamfer << " = geompy.MakeChamferFaces(" << theShape
438     << ", " << theD1 << ", " << theD2 << ", [";
439
440   it = theFaces.begin();
441   pd << (*it++);
442   while (it != theFaces.end()) {
443     pd << ", " << (*it++);
444   }
445   pd << "])";
446
447   SetErrorCode(OK);
448   return aChamfer;
449 }
450
451 //=============================================================================
452 /*!
453  *  Archimede
454  */
455 //=============================================================================
456 Handle(GEOM_Object) GEOMImpl_ILocalOperations::MakeArchimede (Handle(GEOM_Object) theShape,
457                                                               double theWeight,
458                                                               double theWaterDensity,
459                                                               double theMeshingDeflection)
460 {
461   SetErrorCode(KO);
462
463   //Add a new Archimede object
464   Handle(GEOM_Object) aChamfer = GetEngine()->AddObject(GetDocID(), GEOM_ARCHIMEDE);
465
466   //Add a new Archimede function
467   Handle(GEOM_Function) aFunction = aChamfer->AddFunction(GEOMImpl_ArchimedeDriver::GetID(), ARCHIMEDE_TYPE);
468   if (aFunction.IsNull()) return NULL;
469
470   //Check if the function is set correctly
471   if (aFunction->GetDriverGUID() != GEOMImpl_ArchimedeDriver::GetID()) return NULL;
472
473   GEOMImpl_IArchimede aAI (aFunction);
474
475   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
476   if (aRefShape.IsNull()) return NULL;
477
478   aAI.SetBasicShape(aRefShape);
479   aAI.SetWeight(theWeight);
480   aAI.SetDensity(theWaterDensity);
481   aAI.SetDeflection(theMeshingDeflection);
482
483   //Compute the Archimede value
484   try {
485 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
486     OCC_CATCH_SIGNALS;
487 #endif
488     if (!GetSolver()->ComputeFunction(aFunction)) {
489       SetErrorCode("Archimede driver failed");
490       return NULL;
491     }
492   }
493   catch (Standard_Failure) {
494     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
495     SetErrorCode(aFail->GetMessageString());
496     return NULL;
497   }
498
499   //Make a Python command
500   GEOM::TPythonDump(aFunction) << aChamfer
501     << " = geompy.Archimede(" << theShape << ", " << theWeight << ", "
502       << theWaterDensity << ", " << theMeshingDeflection << ")";
503
504   SetErrorCode(OK);
505   return aChamfer;
506 }
507
508 //=============================================================================
509 /*!
510  *  GetSubShape
511  */
512 //=============================================================================
513 bool GEOMImpl_ILocalOperations::GetSubShape (const TopoDS_Shape& theShape, const int theIndex,
514                                              TopoDS_Shape& theSubShape)
515 {
516   if (theShape.IsNull() || theIndex < 1)
517     return false;
518
519   TopTools_IndexedMapOfShape anIndices;
520   TopExp::MapShapes(theShape, anIndices);
521   if (theIndex > anIndices.Extent()) return false;
522   theSubShape = anIndices.FindKey(theIndex);
523
524   return true;
525 }
526
527 //=============================================================================
528 /*!
529  *  GetSubShapeIndex
530  */
531 //=============================================================================
532 Standard_Integer GEOMImpl_ILocalOperations::GetSubShapeIndex (Handle(GEOM_Object) theShape,
533                                                               Handle(GEOM_Object) theSubShape)
534 {
535   SetErrorCode(KO);
536
537   Standard_Integer anInd = -1;
538   GEOM_Engine* anEngine = GetEngine();
539   //GEOMImpl_Gen* aGen = dynamic_cast<GEOMImpl_Gen*>(anEngine);
540   GEOMImpl_Gen* aGen = (GEOMImpl_Gen*)anEngine;
541
542   if (aGen) {
543     GEOMImpl_IShapesOperations* anIShapesOperations =
544       aGen->GetIShapesOperations(GetDocID());
545     anInd = anIShapesOperations->GetSubShapeIndex(theShape, theSubShape);
546     SetErrorCode(anIShapesOperations->GetErrorCode());
547   }
548
549   return anInd;
550 }