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