Salome HOME
0022757: [EDF] Vertex on Edge
[modules/geom.git] / src / GEOM_I / GEOM_IHealingOperations_i.cc
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <list>
26
27 #include "GEOM_IHealingOperations_i.hh"
28 #include "GEOM_Engine.hxx"
29 #include "GEOM_Object.hxx"
30
31 #include "utilities.h"
32 #include "OpUtil.hxx"
33 #include "Utils_ExceptHandlers.hxx"
34 #include <Basics_Utils.hxx>
35
36 #include <TColStd_HSequenceOfTransient.hxx>
37
38 //=============================================================================
39 /*!
40  *   constructor:
41  */
42 //=============================================================================
43
44 GEOM_IHealingOperations_i::GEOM_IHealingOperations_i (PortableServer::POA_ptr thePOA,
45                                                       GEOM::GEOM_Gen_ptr theEngine,
46                                                       ::GEOMImpl_IHealingOperations* theImpl)
47 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
48 {
49   MESSAGE("GEOM_IHealingOperations_i::GEOM_IHealingOperations_i");
50 }
51
52 //=============================================================================
53 /*!
54  *  destructor
55  */
56 //=============================================================================
57
58 GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i()
59 {
60   MESSAGE("GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i");
61 }
62
63 //=============================================================================
64 /*!
65  *  Convert
66  */
67 //=============================================================================
68 Handle(TColStd_HArray1OfInteger) GEOM_IHealingOperations_i::Convert
69                                           (const GEOM::short_array& theInArray)
70 {
71   Handle(TColStd_HArray1OfInteger) anOutArray;
72   int n = theInArray.length();
73   if ( n <= 0 )
74     return anOutArray;
75   anOutArray = new TColStd_HArray1OfInteger( 1, n );
76   for (int i = 0; i < n; i++)
77     anOutArray->SetValue( i+1, theInArray[i] );
78   return anOutArray;
79 }
80
81 //=============================================================================
82 /*!
83  *  ProcessShape
84  */
85 //=============================================================================
86 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ProcessShape (GEOM::GEOM_Object_ptr theObject,
87                                                                const GEOM::string_array& theOperations,
88                                                                const GEOM::string_array& theParams,
89                                                                const GEOM::string_array& theValues)
90 {
91   Kernel_Utils::Localizer loc;
92
93   GEOM::GEOM_Object_var aGEOMObject;
94
95   // Set a not done flag
96   GetOperations()->SetNotDone();
97
98   // Check if theOperations has more than 0 elements and theParams and theValues have the same length
99   //if (theOperations.length() <= 0 || theParams.length() != theValues.length())
100   //  return aGEOMObject._retn();
101
102   // Get the object itself
103   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
104   if (anObject.IsNull())
105     return aGEOMObject._retn();
106
107   // Perform
108   Handle(GEOM_Object) aNewObject = GetOperations()->ShapeProcess( anObject,
109     ConvertStringArray( theOperations ), ConvertStringArray( theParams ),
110     ConvertStringArray( theValues ) );
111   if ( !GetOperations()->IsDone() || aNewObject.IsNull() )
112     return aGEOMObject._retn();
113
114   return GetObject( aNewObject );
115 }
116
117 //=============================================================================
118 /*!
119  *  GetShapeProcessParameters
120  */
121 //=============================================================================
122 void GEOM_IHealingOperations_i::GetShapeProcessParameters(GEOM::string_array_out theOperations,
123                                                           GEOM::string_array_out theParams,
124                                                           GEOM::string_array_out theValues)
125 {
126   GEOM::string_array_var anOpArray = new GEOM::string_array();
127   GEOM::string_array_var aParArray = new GEOM::string_array();
128   GEOM::string_array_var aValArray = new GEOM::string_array();
129
130   // retrieve the values as stl-lists
131   std::list<std::string> operationsList, paramsList, valuesList;
132   GetOperations()->GetShapeProcessParameters( operationsList, paramsList, valuesList );
133   const int opSize = operationsList.size(),
134   parSize = paramsList.size(),
135   valSize = valuesList.size();
136
137   if ( opSize >= 0 && parSize >= 0 && parSize == valSize ) {
138     // allocate the CORBA arrays, sizes == returned lists' sizes
139     anOpArray->length(opSize);
140     aParArray->length(parSize);
141     aValArray->length(valSize);
142
143     // fill the local CORBA arrays with values from lists
144     std::list<std::string>::iterator opIt, parIt, valIt;
145     int i = 0;
146     for ( opIt = operationsList.begin(); opIt != operationsList.end(); i++,++opIt )
147       anOpArray[i] = CORBA::string_dup( (*opIt).c_str() );
148
149     for ( i = 0, parIt = paramsList.begin(), valIt = valuesList.begin();
150           parIt != paramsList.end(); i++, ++parIt,++valIt ) {
151       aParArray[i] = CORBA::string_dup( (*parIt).c_str() );
152       aValArray[i] = CORBA::string_dup( (*valIt).c_str() );
153     }
154   }
155
156   // initialize out-parameters with local arrays
157   theOperations = anOpArray._retn();
158   theParams = aParArray._retn();
159   theValues = aValArray._retn();
160 }
161
162 //=============================================================================
163 /*!
164  *  GetOperatorParameters
165  */
166 //=============================================================================
167 void GEOM_IHealingOperations_i::GetOperatorParameters (const char* theOperator,
168                                                        GEOM::string_array_out theParams,
169                                                        GEOM::string_array_out theValues)
170 {
171   GEOM::string_array_var aParArray = new GEOM::string_array();
172   GEOM::string_array_var aValArray = new GEOM::string_array();
173
174   // retrieve the values as stl-lists
175   std::list<std::string> paramsList, valuesList;
176   if ( GetOperations()->GetOperatorParameters( theOperator, paramsList, valuesList ) ) {
177     const int parSize = paramsList.size(), valSize = valuesList.size();
178
179     if ( parSize == valSize ) {
180       aParArray->length(parSize);
181       aValArray->length(valSize);
182
183       // fill the local CORBA arrays with values from lists
184       std::list<std::string>::iterator parIt, valIt;
185       int i;
186       for ( i = 0, parIt = paramsList.begin(), valIt = valuesList.begin();
187             parIt != paramsList.end(); i++, ++parIt,++valIt ) {
188         aParArray[i] = CORBA::string_dup( (*parIt).c_str() );
189         aValArray[i] = CORBA::string_dup( (*valIt).c_str() );
190       }
191     }
192   }
193
194   // initialize out-parameters with local arrays
195   theParams = aParArray._retn();
196   theValues = aValArray._retn();
197 }
198
199 //=============================================================================
200 /*!
201  *  SuppressFaces
202  */
203 //=============================================================================
204 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::SuppressFaces (GEOM::GEOM_Object_ptr theObject,
205                                                                 const GEOM::short_array& theFaces)
206 {
207   GEOM::GEOM_Object_var aGEOMObject;
208
209   // Set a not done flag
210   GetOperations()->SetNotDone();
211
212   // Get the object itself
213   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
214   if (anObject.IsNull())
215     return aGEOMObject._retn();
216
217   // if theFaces is empty - it's OK, it means that ALL faces must be removed
218
219   // Perform
220   Handle(GEOM_Object) aNewObject =
221     GetOperations()->SuppressFaces( anObject, Convert( theFaces ) );
222   if (!GetOperations()->IsDone() || aNewObject.IsNull())
223     return aGEOMObject._retn();
224
225   return  GetObject( aNewObject );
226 }
227
228 //=============================================================================
229 /*!
230  *  CloseContour
231  */
232 //=============================================================================
233 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::CloseContour (GEOM::GEOM_Object_ptr theObject,
234                                                                const GEOM::short_array& theWires,
235                                                                CORBA::Boolean isCommonVertex)
236 {
237   GEOM::GEOM_Object_var aGEOMObject;
238
239   // Set a not done flag
240   GetOperations()->SetNotDone();
241
242   // Get the object itself
243   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
244   if (anObject.IsNull())
245     return aGEOMObject._retn();
246
247   // Perform
248   Handle(GEOM_Object) aNewObject =
249     GetOperations()->CloseContour( anObject, Convert( theWires ), isCommonVertex );
250   if (!GetOperations()->IsDone() || aNewObject.IsNull())
251     return aGEOMObject._retn();
252
253   return GetObject(aNewObject);
254 }
255
256 //=============================================================================
257 /*!
258  *  RemoveIntWires
259  */
260 //=============================================================================
261 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::RemoveIntWires (GEOM::GEOM_Object_ptr theObject,
262                                                                  const GEOM::short_array& theWires)
263 {
264   GEOM::GEOM_Object_var aGEOMObject;
265
266   // Set a not done flag
267   GetOperations()->SetNotDone();
268
269   // Get the object itself
270   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
271   if (anObject.IsNull())
272     return aGEOMObject._retn();
273
274   // if theWires is empty - it's OK, it means that ALL wires should be removed
275
276   // Perform
277   Handle(GEOM_Object) aNewObject =
278     GetOperations()->RemoveIntWires( anObject, Convert( theWires ) );
279   if (!GetOperations()->IsDone() || aNewObject.IsNull())
280     return aGEOMObject._retn();
281
282   return GetObject(aNewObject);
283 }
284
285 //=============================================================================
286 /*!
287  *  FillHoles
288  */
289 //=============================================================================
290 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FillHoles (GEOM::GEOM_Object_ptr theObject,
291                                                             const GEOM::short_array& theWires)
292 {
293   GEOM::GEOM_Object_var aGEOMObject;
294
295   // Set a not done flag
296   GetOperations()->SetNotDone();
297
298   // Get the object itself
299   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
300   if (anObject.IsNull())
301     return aGEOMObject._retn();
302
303   // if theWires is empty - it's OK, it means that ALL wires should be removed
304
305   // Perform
306   Handle(GEOM_Object) aNewObject =
307     GetOperations()->FillHoles( anObject, Convert( theWires ) );
308   if (!GetOperations()->IsDone() || aNewObject.IsNull())
309     return aGEOMObject._retn();
310
311   return GetObject(aNewObject);
312 }
313
314 //=============================================================================
315 /*!
316  *  Sew
317  */
318 //=============================================================================
319 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::Sew (const GEOM::ListOfGO& theObjects,
320                                                       CORBA::Double         theTolerance)
321 {
322   GEOM::GEOM_Object_var aGEOMObject;
323
324   // Set a not done flag
325   GetOperations()->SetNotDone();
326
327   // Check parameters
328   if (theTolerance < 0)
329     return aGEOMObject._retn();
330
331   //Get the shapes
332   std::list< Handle(GEOM_Object) > aShapes;
333   if (! GetListOfObjectsImpl( theObjects, aShapes ))
334     return aGEOMObject._retn();
335
336   // Perform
337   Handle(GEOM_Object) aNewObject = GetOperations()->Sew( aShapes, theTolerance, false );
338   if (!GetOperations()->IsDone() || aNewObject.IsNull())
339     return aGEOMObject._retn();
340
341   return GetObject(aNewObject);
342 }
343
344 //=============================================================================
345 /*!
346  *  SewAllowNonManifold
347  */
348 //=============================================================================
349 GEOM::GEOM_Object_ptr
350 GEOM_IHealingOperations_i::SewAllowNonManifold (const GEOM::ListOfGO& theObjects,
351                                                 CORBA::Double         theTolerance)
352 {
353   GEOM::GEOM_Object_var aGEOMObject;
354
355   // Set a not done flag
356   GetOperations()->SetNotDone();
357
358   // Check parameters
359   if (theTolerance < 0)
360     return aGEOMObject._retn();
361
362   //Get the shapes
363   std::list< Handle(GEOM_Object) > aShapes;
364   if (! GetListOfObjectsImpl( theObjects, aShapes ))
365     return aGEOMObject._retn();
366
367   // Perform
368   Handle(GEOM_Object) aNewObject = GetOperations()->Sew( aShapes, theTolerance, true );
369   if (!GetOperations()->IsDone() || aNewObject.IsNull())
370     return aGEOMObject._retn();
371
372   return GetObject(aNewObject);
373 }
374
375 //=============================================================================
376 /*!
377  *  RemoveInternalFaces
378  */
379 //=============================================================================
380 GEOM::GEOM_Object_ptr
381 GEOM_IHealingOperations_i::RemoveInternalFaces (const GEOM::ListOfGO& theSolids)
382 {
383   GEOM::GEOM_Object_var aGEOMObject;
384
385   // Set a not done flag
386   GetOperations()->SetNotDone();
387
388   // Get the objects
389   std::list< Handle(GEOM_Object) > aShapes;
390   if (! GetListOfObjectsImpl( theSolids, aShapes ))
391     return aGEOMObject._retn();
392
393   // Perform
394   Handle(GEOM_Object) aNewObject = GetOperations()->RemoveInternalFaces(aShapes);
395   if (!GetOperations()->IsDone() || aNewObject.IsNull())
396     return aGEOMObject._retn();
397
398   return GetObject(aNewObject);
399 }
400
401 //=============================================================================
402 /*!
403  *  DivideEdge
404  */
405 //=============================================================================
406 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::DivideEdge (GEOM::GEOM_Object_ptr theObject,
407                                                              CORBA::Short theIndex,
408                                                              CORBA::Double theValue,
409                                                              CORBA::Boolean isByParameter)
410 {
411   GEOM::GEOM_Object_var aGEOMObject;
412
413   // Set a not done flag
414   GetOperations()->SetNotDone();
415
416   // Check parameters
417   if (theValue < 0 || theValue > 1)
418     return aGEOMObject._retn();
419
420   // Get the object itself
421   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
422   if (anObject.IsNull())
423     return aGEOMObject._retn();
424
425   // Perform
426   Handle(GEOM_Object) aNewObject =
427     GetOperations()->DivideEdge( anObject, theIndex, theValue, isByParameter );
428   if (!GetOperations()->IsDone() || aNewObject.IsNull())
429     return aGEOMObject._retn();
430
431   return GetObject(aNewObject);
432 }
433
434 //=============================================================================
435 /*!
436  *  DivideEdgeByPoint
437  */
438 //=============================================================================
439 GEOM::GEOM_Object_ptr
440 GEOM_IHealingOperations_i::DivideEdgeByPoint (GEOM::GEOM_Object_ptr theObject,
441                                               CORBA::Short          theIndex,
442                                               GEOM::GEOM_Object_ptr thePoint)
443 {
444   GEOM::GEOM_Object_var aGEOMObject;
445
446   // Set a not done flag
447   GetOperations()->SetNotDone();
448
449   // Get the object itself
450   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
451   if (anObject.IsNull())
452     return aGEOMObject._retn();
453
454   // Get the point
455   Handle(GEOM_Object) aPoint = GetObjectImpl(thePoint);
456   if (aPoint.IsNull())
457     return aGEOMObject._retn();
458
459   // Perform
460   Handle(GEOM_Object) aNewObject =
461     GetOperations()->DivideEdgeByPoint( anObject, theIndex, aPoint );
462   if (!GetOperations()->IsDone() || aNewObject.IsNull())
463     return aGEOMObject._retn();
464
465   return GetObject(aNewObject);
466 }
467
468 //=============================================================================
469 /*!
470  *  FuseCollinearEdgesWithinWire
471  */
472 //=============================================================================
473 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FuseCollinearEdgesWithinWire
474                                           (GEOM::GEOM_Object_ptr theWire,
475                                            const GEOM::ListOfGO& theVertices)
476 {
477   GEOM::GEOM_Object_var aGEOMObject;
478
479   //Set a not done flag
480   GetOperations()->SetNotDone();
481
482   //Get the reference objects
483   Handle(GEOM_Object) aWire = GetObjectImpl(theWire);
484   if (aWire.IsNull()) return aGEOMObject._retn();
485
486   int ind, aLen;
487   std::list<Handle(GEOM_Object)> aVerts;
488   //Get the shapes
489   aLen = theVertices.length();
490   for (ind = 0; ind < aLen; ind++) {
491     Handle(GEOM_Object) aSh = GetObjectImpl(theVertices[ind]);
492     if (aSh.IsNull()) return aGEOMObject._retn();
493     aVerts.push_back(aSh);
494   }
495
496   //Perform operation
497   Handle(GEOM_Object) anObject =
498     GetOperations()->FuseCollinearEdgesWithinWire(aWire, aVerts);
499   if (!GetOperations()->IsDone() || anObject.IsNull())
500     return aGEOMObject._retn();
501
502   return GetObject(anObject);
503 }
504
505 //=============================================================================
506 /*!
507  *  GetFreeBoundary
508  */
509 //=============================================================================
510 CORBA::Boolean
511 GEOM_IHealingOperations_i::GetFreeBoundary ( const GEOM::ListOfGO & theObjects,
512                                              GEOM::ListOfGO_out     theClosedWires,
513                                              GEOM::ListOfGO_out     theOpenWires )
514 {
515   theClosedWires = new GEOM::ListOfGO;
516   theOpenWires = new GEOM::ListOfGO;
517
518   // Set a not done flag
519   GetOperations()->SetNotDone();
520
521   // Get the objects
522   Handle(TColStd_HSequenceOfTransient) anObjects = new TColStd_HSequenceOfTransient();
523   for ( size_t i = 0; i < theObjects.length(); ++i )
524   {
525     Handle(GEOM_Object) anObject = GetObjectImpl(theObjects[i]);
526     if (anObject.IsNull())
527       return false;
528     anObjects->Append( anObject );
529   }
530
531   Handle(TColStd_HSequenceOfTransient) aClosed = new TColStd_HSequenceOfTransient();
532   Handle(TColStd_HSequenceOfTransient) anOpen  = new TColStd_HSequenceOfTransient();
533   bool res = GetOperations()->GetFreeBoundary( anObjects, aClosed, anOpen );
534
535   if ( !GetOperations()->IsDone() || !res )
536     return false;
537
538   int i, n = aClosed->Length();
539   theClosedWires->length( n );
540   for ( i = 1; i <= n; i++ )
541     (*theClosedWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aClosed->Value(i)));
542
543   n = anOpen->Length();
544   theOpenWires->length( n );
545   for ( i = 1, n = anOpen->Length(); i <= n; i++ )
546     (*theOpenWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(anOpen->Value(i)));
547
548   return true;
549 }
550
551
552 //=============================================================================
553 /*!
554  *  ChangeOrientation
555  */
556 //=============================================================================
557 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientation (GEOM::GEOM_Object_ptr theObject)
558 {
559   GEOM::GEOM_Object_var aGEOMObject;
560
561   // Set a not done flag
562   GetOperations()->SetNotDone();
563
564   // Check parameters
565   if ( CORBA::is_nil(theObject) )
566     return aGEOMObject._retn();
567
568   aGEOMObject = GEOM::GEOM_Object::_duplicate(theObject);
569
570   // Get the object itself
571   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
572   if (anObject.IsNull())
573     return aGEOMObject._retn();
574
575   // Perform
576 //  Handle(GEOM_Object) aNewObject =
577     GetOperations()->ChangeOrientation( anObject );
578 //  if (!GetOperations()->IsDone() || aNewObject.IsNull())
579 //    return aGEOMObject._retn();
580
581   //return GetObject(aNewObject);
582   return aGEOMObject._retn();
583 }
584
585
586 //=============================================================================
587 /*!
588  *  ChangeOrientationCopy
589  */
590 //=============================================================================
591 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientationCopy (GEOM::GEOM_Object_ptr theObject)
592 {
593   GEOM::GEOM_Object_var aGEOMObject;
594
595   // Set a not done flag
596   GetOperations()->SetNotDone();
597
598   // Get the object itself
599   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
600   if (anObject.IsNull())
601     return aGEOMObject._retn();
602
603   // Perform
604   Handle(GEOM_Object) aNewObject =
605     GetOperations()->ChangeOrientationCopy( anObject );
606   if (!GetOperations()->IsDone() || aNewObject.IsNull())
607     return aGEOMObject._retn();
608
609   return GetObject(aNewObject);
610 }
611
612 //=============================================================================
613 /*!
614  *  LimitTolerance
615  */
616 //=============================================================================
617 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::LimitTolerance (GEOM::GEOM_Object_ptr theObject,
618                                                                  CORBA::Double theTolerance)
619 {
620   GEOM::GEOM_Object_var aGEOMObject;
621
622   // Set a not done flag
623   GetOperations()->SetNotDone();
624
625   // Get the object itself
626   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
627   if (anObject.IsNull())
628     return aGEOMObject._retn();
629
630   // Perform
631   Handle(GEOM_Object) aNewObject =
632     GetOperations()->LimitTolerance(anObject, theTolerance);
633   if (!GetOperations()->IsDone() || aNewObject.IsNull())
634     return aGEOMObject._retn();
635
636   return GetObject(aNewObject);
637 }