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