Salome HOME
PAL8378: fix a bug in Partition algorithm
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ICurvesOperations.cxx
1 using namespace std;
2
3 #include "GEOMImpl_ICurvesOperations.hxx"
4
5 #include "GEOM_Function.hxx"
6 #include "GEOMImpl_Types.hxx"
7
8 #include "GEOMImpl_PolylineDriver.hxx"
9 #include "GEOMImpl_CircleDriver.hxx"
10 #include "GEOMImpl_SplineDriver.hxx"
11 #include "GEOMImpl_EllipseDriver.hxx"
12 #include "GEOMImpl_ArcDriver.hxx"
13 #include "GEOMImpl_SketcherDriver.hxx"
14
15 #include "GEOMImpl_IPolyline.hxx"
16 #include "GEOMImpl_ICircle.hxx"
17 #include "GEOMImpl_ISpline.hxx"
18 #include "GEOMImpl_IEllipse.hxx"
19 #include "GEOMImpl_IArc.hxx"
20 #include "GEOMImpl_ISketcher.hxx"
21
22 #include "utilities.h"
23
24 #include <TDF_Tool.hxx>
25
26 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
27
28 //=============================================================================
29 /*!
30  *   constructor:
31  */
32 //=============================================================================
33 GEOMImpl_ICurvesOperations::GEOMImpl_ICurvesOperations (GEOM_Engine* theEngine, int theDocID)
34 : GEOM_IOperations(theEngine, theDocID)
35 {
36   MESSAGE("GEOMImpl_ICurvesOperations::GEOMImpl_ICurvesOperations");
37 }
38
39 //=============================================================================
40 /*!
41  *  destructor
42  */
43 //=============================================================================
44 GEOMImpl_ICurvesOperations::~GEOMImpl_ICurvesOperations()
45 {
46   MESSAGE("GEOMImpl_ICurvesOperations::~GEOMImpl_ICurvesOperations");
47 }
48
49
50 //=============================================================================
51 /*!
52  *  MakePolyline
53  */
54 //=============================================================================
55 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakePolyline (list<Handle(GEOM_Object)> thePoints)
56 {
57   SetErrorCode(KO);
58
59   //Add a new Polyline object
60   Handle(GEOM_Object) aPolyline = GetEngine()->AddObject(GetDocID(), GEOM_POLYLINE);
61
62   //Add a new Polyline function for creation a polyline relatively to points set
63   Handle(GEOM_Function) aFunction =
64     aPolyline->AddFunction(GEOMImpl_PolylineDriver::GetID(), POLYLINE_POINTS);
65   if (aFunction.IsNull()) return NULL;
66
67   //Check if the function is set correctly
68   if (aFunction->GetDriverGUID() != GEOMImpl_PolylineDriver::GetID()) return NULL;
69
70   GEOMImpl_IPolyline aCI (aFunction);
71
72   int aLen = thePoints.size();
73   aCI.SetLength(aLen);
74
75   int ind = 1;
76   list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
77   for (; it != thePoints.end(); it++, ind++) {
78     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
79     if (aRefPnt.IsNull()) {
80       SetErrorCode("NULL point for Polyline");
81       return NULL;
82     }
83     aCI.SetPoint(ind, aRefPnt);
84   }
85
86   //Compute the Polyline value
87   try {
88     if (!GetSolver()->ComputeFunction(aFunction)) {
89       SetErrorCode("Polyline driver failed");
90       return NULL;
91     }
92   }
93   catch (Standard_Failure) {
94     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
95     SetErrorCode(aFail->GetMessageString());
96     return NULL;
97   }
98
99   //Make a Python command
100   TCollection_AsciiString anEntry, aDescr("");
101   TDF_Tool::Entry(aPolyline->GetEntry(), anEntry);
102   aDescr += anEntry;
103   aDescr += " = ICurvesOperations.MakePolyline([";
104   it = thePoints.begin();
105   TDF_Tool::Entry((*it)->GetEntry(), anEntry);
106   it++;
107   aDescr += (anEntry+", ");
108   for (; it != thePoints.end(); it++) {
109     aDescr += ", ";
110     TDF_Tool::Entry((*it)->GetEntry(), anEntry);
111     aDescr += anEntry;
112   }
113   aDescr += "])";
114
115   aFunction->SetDescription(aDescr);
116
117   SetErrorCode(OK);
118   return aPolyline;
119 }
120
121 //=============================================================================
122 /*!
123  *  MakeCircleThreePnt
124  */
125 //=============================================================================
126 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCircleThreePnt (Handle(GEOM_Object) thePnt1,
127                                                                     Handle(GEOM_Object) thePnt2,
128                                                                     Handle(GEOM_Object) thePnt3)
129 {
130   SetErrorCode(KO);
131
132   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
133
134   //Add a new Circle object
135   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
136
137   //Add a new Circle function for creation a circle relatively to three points
138   Handle(GEOM_Function) aFunction =
139     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_THREE_PNT);
140   if (aFunction.IsNull()) return NULL;
141
142   //Check if the function is set correctly
143   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
144
145   GEOMImpl_ICircle aCI (aFunction);
146
147   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
148   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
149   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
150
151   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
152
153   aCI.SetPoint1(aRefPnt1);
154   aCI.SetPoint2(aRefPnt2);
155   aCI.SetPoint3(aRefPnt3);
156
157   //Compute the Circle value
158   try {
159     if (!GetSolver()->ComputeFunction(aFunction)) {
160       SetErrorCode("Circle driver failed");
161       return NULL;
162     }
163   }
164   catch (Standard_Failure) {
165     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
166     SetErrorCode(aFail->GetMessageString());
167     return NULL;
168   }
169
170   //Make a Python command
171   TCollection_AsciiString anEntry, aDescr("");
172   TDF_Tool::Entry(aCircle->GetEntry(), anEntry);
173   aDescr += anEntry;
174   aDescr += " = ICurvesOperations.MakeCircleThreePnt(";
175   TDF_Tool::Entry(thePnt1->GetEntry(), anEntry);
176   aDescr += (anEntry+", ");
177   TDF_Tool::Entry(thePnt2->GetEntry(), anEntry);
178   aDescr += (anEntry+", ");
179   TDF_Tool::Entry(thePnt3->GetEntry(), anEntry);
180   aDescr += (anEntry+")");
181
182   aFunction->SetDescription(aDescr);
183
184   SetErrorCode(OK);
185   return aCircle;
186 }
187
188 //=============================================================================
189 /*!
190  *  MakeCirclePntVecR
191  */
192 //=============================================================================
193 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCirclePntVecR
194        (Handle(GEOM_Object) thePnt, Handle(GEOM_Object) theVec, double theR)
195 {
196   SetErrorCode(KO);
197
198   if (thePnt.IsNull() || theVec.IsNull()) return NULL;
199
200   //Add a new Circle object
201   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
202
203   //Add a new Circle function for creation a circle relatively to point and vector
204   Handle(GEOM_Function) aFunction =
205     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_PNT_VEC_R);
206   if (aFunction.IsNull()) return NULL;
207
208   //Check if the function is set correctly
209   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
210
211   GEOMImpl_ICircle aCI (aFunction);
212
213   Handle(GEOM_Function) aRefPnt = thePnt->GetLastFunction();
214   Handle(GEOM_Function) aRefVec = theVec->GetLastFunction();
215
216   if (aRefPnt.IsNull() || aRefVec.IsNull()) return NULL;
217
218   aCI.SetCenter(aRefPnt);
219   aCI.SetVector(aRefVec);
220   aCI.SetRadius(theR);
221
222   //Compute the Circle value
223   try {
224     if (!GetSolver()->ComputeFunction(aFunction)) {
225       SetErrorCode("Circle driver failed");
226       return NULL;
227     }
228   }
229   catch (Standard_Failure) {
230     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
231     SetErrorCode(aFail->GetMessageString());
232     return NULL;
233   }
234
235   //Make a Python command
236   TCollection_AsciiString anEntry, aDescr("");
237   TDF_Tool::Entry(aCircle->GetEntry(), anEntry);
238   aDescr += anEntry;
239   aDescr += " = ICurvesOperations.MakeCirclePntVecR(";
240   TDF_Tool::Entry(thePnt->GetEntry(), anEntry);
241   aDescr += (anEntry+", ");
242   TDF_Tool::Entry(theVec->GetEntry(), anEntry);
243   aDescr += (anEntry+", ");
244   aDescr += (TCollection_AsciiString(theR)+")");
245
246   aFunction->SetDescription(aDescr);
247
248   SetErrorCode(OK);
249   return aCircle;
250 }
251
252 //=============================================================================
253 /*!
254  *  MakeEllipse
255  */
256 //=============================================================================
257 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeEllipse
258                        (Handle(GEOM_Object) thePnt, Handle(GEOM_Object) theVec,
259                         double theRMajor, double theRMinor)
260 {
261   SetErrorCode(KO);
262
263   if (thePnt.IsNull() || theVec.IsNull()) return NULL;
264
265   //Add a new Ellipse object
266   Handle(GEOM_Object) anEll = GetEngine()->AddObject(GetDocID(), GEOM_ELLIPSE);
267
268   //Add a new Ellipse function
269   Handle(GEOM_Function) aFunction =
270     anEll->AddFunction(GEOMImpl_EllipseDriver::GetID(), ELLIPSE_PNT_VEC_RR);
271   if (aFunction.IsNull()) return NULL;
272
273   //Check if the function is set correctly
274   if (aFunction->GetDriverGUID() != GEOMImpl_EllipseDriver::GetID()) return NULL;
275
276   GEOMImpl_IEllipse aCI (aFunction);
277
278   Handle(GEOM_Function) aRefPnt = thePnt->GetLastFunction();
279   Handle(GEOM_Function) aRefVec = theVec->GetLastFunction();
280
281   if (aRefPnt.IsNull() || aRefVec.IsNull()) return NULL;
282
283   aCI.SetCenter(aRefPnt);
284   aCI.SetVector(aRefVec);
285   aCI.SetRMajor(theRMajor);
286   aCI.SetRMinor(theRMinor);
287
288   //Compute the Ellipse value
289   try {
290     if (!GetSolver()->ComputeFunction(aFunction)) {
291       SetErrorCode("Ellipse 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   TCollection_AsciiString anEntry, aDescr("");
303   TDF_Tool::Entry(anEll->GetEntry(), anEntry);
304   aDescr += anEntry;
305   aDescr += " = ICurvesOperations.MakeEllipse(";
306   TDF_Tool::Entry(thePnt->GetEntry(), anEntry);
307   aDescr += (anEntry+", ");
308   TDF_Tool::Entry(theVec->GetEntry(), anEntry);
309   aDescr += (anEntry+", ");
310   aDescr += (TCollection_AsciiString(theRMajor)+", ");
311   aDescr += (TCollection_AsciiString(theRMinor)+")");
312
313   aFunction->SetDescription(aDescr);
314
315   SetErrorCode(OK);
316   return anEll;
317 }
318
319 //=============================================================================
320 /*!
321  *  MakeArc
322  */
323 //=============================================================================
324 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeArc (Handle(GEOM_Object) thePnt1,
325                                                          Handle(GEOM_Object) thePnt2,
326                                                          Handle(GEOM_Object) thePnt3)
327 {
328   SetErrorCode(KO);
329
330   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
331
332   //Add a new Circle Arc object
333   Handle(GEOM_Object) anArc = GetEngine()->AddObject(GetDocID(), GEOM_CIRC_ARC);
334
335   //Add a new Circle Arc function
336   Handle(GEOM_Function) aFunction =
337     anArc->AddFunction(GEOMImpl_ArcDriver::GetID(), CIRC_ARC_THREE_PNT);
338   if (aFunction.IsNull()) return NULL;
339
340   //Check if the function is set correctly
341   if (aFunction->GetDriverGUID() != GEOMImpl_ArcDriver::GetID()) return NULL;
342
343   GEOMImpl_IArc aCI (aFunction);
344
345   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
346   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
347   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
348
349   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
350
351   aCI.SetPoint1(aRefPnt1);
352   aCI.SetPoint2(aRefPnt2);
353   aCI.SetPoint3(aRefPnt3);
354
355   //Compute the Arc value
356   try {
357     if (!GetSolver()->ComputeFunction(aFunction)) {
358       SetErrorCode("Arc driver failed");
359       return NULL;
360     }
361   }
362   catch (Standard_Failure) {
363     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
364     SetErrorCode(aFail->GetMessageString());
365     return NULL;
366   }
367
368   //Make a Python command
369   TCollection_AsciiString anEntry, aDescr("");
370   TDF_Tool::Entry(anArc->GetEntry(), anEntry);
371   aDescr += anEntry;
372   aDescr += " = ICurvesOperations.MakeArc(";
373   TDF_Tool::Entry(thePnt1->GetEntry(), anEntry);
374   aDescr += (anEntry+", ");
375   TDF_Tool::Entry(thePnt2->GetEntry(), anEntry);
376   aDescr += (anEntry+", ");
377   TDF_Tool::Entry(thePnt3->GetEntry(), anEntry);
378   aDescr += (anEntry+")");
379
380   aFunction->SetDescription(aDescr);
381
382   SetErrorCode(OK);
383   return anArc;
384 }
385
386 //=============================================================================
387 /*!
388  *  MakeSplineBezier
389  */
390 //=============================================================================
391 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineBezier
392                                           (list<Handle(GEOM_Object)> thePoints)
393 {
394   SetErrorCode(KO);
395
396   //Add a new Spline object
397   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
398
399   //Add a new Spline function for creation a bezier curve relatively to points set
400   Handle(GEOM_Function) aFunction =
401     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_BEZIER);
402   if (aFunction.IsNull()) return NULL;
403
404   //Check if the function is set correctly
405   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
406
407   GEOMImpl_ISpline aCI (aFunction);
408
409   int aLen = thePoints.size();
410   aCI.SetLength(aLen);
411
412   int ind = 1;
413   list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
414   for (; it != thePoints.end(); it++, ind++) {
415     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
416
417     if (aRefPnt.IsNull()) return NULL;
418
419     aCI.SetPoint(ind, aRefPnt);
420   }
421
422   //Compute the Spline value
423   try {
424     if (!GetSolver()->ComputeFunction(aFunction)) {
425       SetErrorCode("Spline 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   TCollection_AsciiString anEntry, aDescr("");
437   TDF_Tool::Entry(aSpline->GetEntry(), anEntry);
438   aDescr += anEntry;
439   aDescr += " = ICurvesOperations.MakeSplineBezier([";
440   it = thePoints.begin();
441   TDF_Tool::Entry((*it)->GetEntry(), anEntry);
442   it++;
443   aDescr += (anEntry+", ");
444   for (; it != thePoints.end(); it++) {
445     aDescr += ", ";
446     TDF_Tool::Entry((*it)->GetEntry(), anEntry);
447     aDescr += anEntry;
448   }
449   aDescr += "])";
450
451   aFunction->SetDescription(aDescr);
452
453   SetErrorCode(OK);
454   return aSpline;
455 }
456
457 //=============================================================================
458 /*!
459  *  MakeSplineInterpolation
460  */
461 //=============================================================================
462 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineInterpolation
463                                           (list<Handle(GEOM_Object)> thePoints)
464 {
465   SetErrorCode(KO);
466
467   //Add a new Spline object
468   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
469
470   //Add a new Spline function for creation a bezier curve relatively to points set
471   Handle(GEOM_Function) aFunction =
472     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOLATION);
473   if (aFunction.IsNull()) return NULL;
474
475   //Check if the function is set correctly
476   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
477
478   GEOMImpl_ISpline aCI (aFunction);
479
480   int aLen = thePoints.size();
481   aCI.SetLength(aLen);
482
483   int ind = 1;
484   list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
485   for (; it != thePoints.end(); it++, ind++) {
486     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
487
488     if (aRefPnt.IsNull()) return NULL;
489
490     aCI.SetPoint(ind, aRefPnt);
491   }
492
493   //Compute the Spline value
494   try {
495     if (!GetSolver()->ComputeFunction(aFunction)) {
496       SetErrorCode("Spline driver failed");
497       return NULL;
498     }
499   }
500   catch (Standard_Failure) {
501     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
502     SetErrorCode(aFail->GetMessageString());
503     return NULL;
504   }
505
506   //Make a Python command
507   TCollection_AsciiString anEntry, aDescr("");
508   TDF_Tool::Entry(aSpline->GetEntry(), anEntry);
509   aDescr += anEntry;
510   aDescr += " = ICurvesOperations.MakeSplineInterpolation([";
511   it = thePoints.begin();
512   TDF_Tool::Entry((*it)->GetEntry(), anEntry);
513   it++;
514   aDescr += (anEntry+", ");
515   for (; it != thePoints.end(); it++) {
516     aDescr += ", ";
517     TDF_Tool::Entry((*it)->GetEntry(), anEntry);
518     aDescr += anEntry;
519   }
520   aDescr += "])";
521
522   aFunction->SetDescription(aDescr);
523
524   SetErrorCode(OK);
525   return aSpline;
526 }
527
528 //=============================================================================
529 /*!
530  *  MakeSketcher
531  */
532 //=============================================================================
533 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcher (const char* theCommand,
534                                                               list<double> theWorkingPlane)
535 {
536   SetErrorCode(KO);
537
538   if (!theCommand) return NULL;
539   if (strcmp(theCommand, "") == 0) return NULL;
540
541   //Add a new Sketcher object
542   Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_SKETCHER);
543
544   //Add a new Sketcher function
545   Handle(GEOM_Function) aFunction =
546     aSketcher->AddFunction(GEOMImpl_SketcherDriver::GetID(), SKETCHER_COMMAND);
547   if (aFunction.IsNull()) return NULL;
548
549   //Check if the function is set correctly
550   if (aFunction->GetDriverGUID() != GEOMImpl_SketcherDriver::GetID()) return NULL;
551
552   GEOMImpl_ISketcher aCI (aFunction);
553
554   TCollection_AsciiString aCommand ((char*) theCommand);
555   aCI.SetCommand(aCommand);
556
557   int ind = 1;
558   list<double>::iterator it = theWorkingPlane.begin();
559   for (; it != theWorkingPlane.end(); it++, ind++)
560     aCI.SetWorkingPlane(ind, *it);
561
562   //Compute the Sketcher value
563   try {
564     if (!GetSolver()->ComputeFunction(aFunction)) {
565       SetErrorCode("Sketcher driver failed");
566       return NULL;
567     }
568   }
569   catch (Standard_Failure) {
570     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
571     SetErrorCode(aFail->GetMessageString());
572     return NULL;
573   }
574
575   //Make a Python command
576   TCollection_AsciiString anEntry, aDescr;
577   TDF_Tool::Entry(aSketcher->GetEntry(), anEntry);
578   aDescr += anEntry;
579   aDescr += " = ICurvesOperations.MakeSketcher(";
580   aDescr += aCommand+", [";
581
582   it = theWorkingPlane.begin();
583   aDescr += TCollection_AsciiString(*it);
584   it++;
585   for (; it != theWorkingPlane.end(); it++) {
586     aDescr += ", ";
587     aDescr += TCollection_AsciiString(*it);
588   }
589
590   aDescr += "])";
591
592   aFunction->SetDescription(aDescr);
593
594   SetErrorCode(OK);
595   return aSketcher;
596 }