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