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