Salome HOME
sources v1.2c
[modules/geom.git] / src / GEOM / GEOM_Gen_i.cc
1 //  GEOM GEOM : implementaion of GEOM_Gen.idl and GEOM_Shape.idl
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GEOM_GEN_i.cc file
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GEOM_Gen_i.hh"
31
32 #include "Partition_Spliter.hxx"
33 #include "Archimede_VolumeSection.hxx"
34
35 #include "Utils_CorbaException.hxx"
36 #include "utilities.h"
37
38 #include <stdio.h>
39
40 // Cascade headers
41
42 #include <Standard_Failure.hxx>
43
44 #include <gp_Circ.hxx>
45 #include <gp_Pln.hxx>
46 #include <Geom_Plane.hxx>
47 #include <Geom_Line.hxx>
48 #include <GeomFill_Line.hxx>
49 #include <GeomFill_AppSurf.hxx>
50 #include <GeomFill_SectionGenerator.hxx>
51 #include <Geom_BSplineSurface.hxx>
52 #include <Geom_TrimmedCurve.hxx>
53 #include <GC_MakeArcOfCircle.hxx>
54 #include <GC_Root.hxx>
55
56 #include <BRepCheck_Analyzer.hxx>
57 #if OCC_VERSION_MAJOR >= 5
58 #include <BRepAlgo.hxx>
59 #else
60 #include <BRepAlgoAPI.hxx>
61 #endif
62 #include <BRepAdaptor_Surface.hxx>
63 #include <BRepBuilderAPI_Copy.hxx>
64 #include <BRepAlgoAPI_Common.hxx>
65 #include <BRepAlgoAPI_Cut.hxx>
66 #include <BRepAlgoAPI_Fuse.hxx>
67 #include <BRepAlgoAPI_Section.hxx>
68 #include <BRepOffsetAPI_Sewing.hxx>
69 #include <BRepOffsetAPI_MakePipe.hxx>
70
71 #include <BRepBuilderAPI_MakeVertex.hxx>
72 #include <BRepBuilderAPI_MakeEdge.hxx>
73 #include <BRepBuilderAPI_MakeFace.hxx>
74
75 #include <BRepLib.hxx>
76 #include <BRepBndLib.hxx>
77 #include <Bnd_Box.hxx>
78
79 #include <BRepBuilderAPI_MakeShell.hxx>
80 #include <BRepPrim_Builder.hxx>
81 #include <BRepBuilderAPI_MakeSolid.hxx>
82 #include <BRepClass3d_SolidClassifier.hxx>
83
84 #include <BRepBuilderAPI_MakeWire.hxx>
85 #include <BRepBuilderAPI_Transform.hxx>
86 #include <BRepPrimAPI_MakeRevol.hxx>
87 #include <BRepPrimAPI_MakePrism.hxx>
88 #include <BRepPrimAPI_MakeTorus.hxx>
89 #include <BRepPrimAPI_MakeBox.hxx>
90 #include <BRepPrimAPI_MakeSphere.hxx>
91 #include <BRepPrimAPI_MakeCylinder.hxx>
92 #include <BRepPrimAPI_MakeCone.hxx>
93 #include <BRepFilletAPI_MakeFillet.hxx>
94 #include <BRepFilletAPI_MakeChamfer.hxx>
95 #include <BRepTools.hxx>
96 #include <BRepTools_Quilt.hxx>
97 #include <BRep_Tool.hxx>
98
99 #include <GeomAPI_ProjectPointOnCurve.hxx>
100
101 #include <BRepGProp.hxx>
102 #include <GProp_GProps.hxx>
103 #include <Precision.hxx>
104
105 //VRV: OCC 4.0 migration
106 #include <STEPControl_Reader.hxx>
107 #include <IGESControl_Reader.hxx>
108 //VRV: OCC 4.0 migration
109
110 #include <IFSelect_ReturnStatus.hxx>
111 #include <TColStd_HSequenceOfTransient.hxx>
112
113 //VRV: OCC 4.0 migration
114 #include <IGESControl_Writer.hxx>
115 #include <IGESControl_Controller.hxx>
116 #include <STEPControl_Writer.hxx>
117 #include <Interface_Static.hxx>
118 //#include <STEPControlStd_StepModelType.hxx>
119 //VRV: OCC 4.0 migration
120
121 #include <TopoDS_Shape.hxx>
122 #include <TopAbs.hxx>
123 #include <TopoDS_Wire.hxx>
124 #include <TopoDS_Edge.hxx>
125 #include <TopoDS_Compound.hxx>
126 #include <TopoDS_Solid.hxx>
127
128 #include <TopExp.hxx>
129 #include <TopExp_Explorer.hxx>
130 #include <TCollection_ExtendedString.hxx>
131 #include <TopoDS_Iterator.hxx>
132 #include <TopTools_MapOfShape.hxx>
133 #include <TopTools_MapIteratorOfMapOfShape.hxx>
134 #include <TopTools_ListIteratorOfListOfShape.hxx>
135 #include <TopTools_Array1OfShape.hxx>
136
137 #include <IGESData_IGESEntity.hxx>
138
139 #include <TDF_Tool.hxx>
140 #include <TDF_Label.hxx>
141 #include <TDataStd_Name.hxx>
142 #include <TDataStd_Comment.hxx>
143 #include <TDF_Reference.hxx>
144 #include <TDF_Data.hxx>
145 #include <TNaming_Builder.hxx>
146 #include <TNaming_NamedShape.hxx>
147 #include <TNaming_Tool.hxx>
148 //  #include <TDocStd_Owner.hxx>
149
150 #include "SALOMEDS_Tool.hxx"
151 #include "GEOMDS_Commands.hxx"
152 #include "GEOMDS_Explorer.hxx"
153
154 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
155
156 Standard_EXPORT static Standard_Boolean IsValid(const TopoDS_Shape& S) {
157 #if OCC_VERSION_MAJOR >= 5
158   return BRepAlgo::IsValid(S);
159 #else
160   return BRepAlgoAPI::IsValid(S);
161 #endif
162 }
163
164 //============================================================================
165 // function : GEOM_Gen_i()
166 // purpose  : constructor to be called for servant creation. 
167 //============================================================================
168 GEOM_Gen_i::GEOM_Gen_i(CORBA::ORB_ptr orb,
169                        PortableServer::POA_ptr poa,
170                        PortableServer::ObjectId * contId, 
171                        const char *instanceName, 
172                        const char *interfaceName) :
173   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
174 {
175   _thisObj = this ;
176   _id = _poa->activate_object(_thisObj);
177   // SCRUTE(this)
178   name_service = new SALOME_NamingService(_orb);
179   myOCAFApp    = new GEOMDS_Application();
180   myStudyID    = -1;
181   GetCurrentStudy(0);//for correct work of SuperVisor
182 }
183
184
185
186 //============================================================================
187 // function : ~GEOM_Gen_i()
188 // purpose  : destructor
189 //============================================================================
190 GEOM_Gen_i::~GEOM_Gen_i() {
191   delete name_service;
192 }
193
194
195 //============================================================================
196 // function : IORToLocalPersistentID()
197 // purpose  :
198 //============================================================================
199 char* GEOM_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
200                                          const char* IORString,
201                                          CORBA::Boolean isMultiFile,
202                                          CORBA::Boolean isASCII)
203 {
204   GEOM::GEOM_Shape_var aShape = GEOM::GEOM_Shape::_narrow(_orb->string_to_object(IORString));
205   if (!CORBA::is_nil(aShape)) {
206     return strdup(aShape->ShapeId());
207   }
208   return 0;
209 }
210
211
212 //============================================================================
213 // function : LocalPersistentIDToIOR()
214 // purpose  : Create/Load CORBA object from a persistent ref (an entry)
215 //          : Used when a study is loaded
216 //          : The IOR (IORName) of object created is returned
217 //============================================================================
218 char* GEOM_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
219                                          const char* aLocalPersistentID,
220                                          CORBA::Boolean isMultiFile,
221                                          CORBA::Boolean isASCII) 
222
223   SALOMEDS::Study_var myStudy = theSObject->GetStudy();
224   GetCurrentStudy(myStudy->StudyId());
225   Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(myStudyIDToDoc(myStudy->StudyId()));
226   CORBA::String_var aPersRefString = aLocalPersistentID;
227
228   /* For a GEOM::GEOM_Shape the pers_ref is the Entry in the OCAF document */
229   TCollection_ExtendedString MainIOR;
230   TDF_Label Lab;
231   TDF_Tool::Label(aDoc->GetData(), aPersRefString, Lab );
232   
233   Handle(TNaming_NamedShape) NS;
234   Lab.FindAttribute( TNaming_NamedShape::GetID(), NS );
235   TopoDS_Shape S = TNaming_Tool::GetShape(NS);
236
237   /* shapetype, index=0, topo, orb, shapetype, ismain=true and name are setted and modified later ? */
238   GEOM::GEOM_Shape_var result = CreateObject(S);
239   GEOMDS_Commands GC( aDoc->Main() ) ;
240   
241   if ( GC.HasIOR(Lab) ) { /* shape already created/loaded */
242     return 0 ;
243   }
244
245   /******************* Dependent object (not a main shape) *********************/
246   if( GC.IsDependentShape(Lab) ) {
247     
248     TDF_Label mainLabel ;
249     Standard_Boolean mainShapeOk = GC.GetMainShapeLabel(Lab, mainLabel) ;
250     
251     /* Main object not yet loaded we load/create it */
252     if( !GC.HasIOR(mainLabel) ) {
253       
254       TCollection_AsciiString entry;
255       TDF_Tool::Entry(mainLabel,entry);
256       CORBA::String_var ent = strdup(entry.ToCString());
257       
258       /* Create the main object recursively */
259       MainIOR = LocalPersistentIDToIOR(theSObject, ent, isMultiFile, isASCII) ;
260     } else {
261       GC.ReturnNameIOR( mainLabel, MainIOR ); 
262     }
263     
264     result->MainName( TCollection_AsciiString(MainIOR).ToCString() ) ;      
265     result->IsMainShape(false) ;
266     result->ShapeId(aPersRefString);
267     
268     Handle(TDF_Reference) aRef;
269     Lab.FindAttribute( TDF_Reference::GetID(), aRef );
270     TDF_Label myL = aRef->Get() ;
271     Handle(TNaming_NamedShape) NN;
272     myL.FindAttribute( TNaming_NamedShape::GetID(), NN );
273     TopoDS_Shape mainTopo = TNaming_Tool::GetShape(NN);
274
275     GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
276     
277     if(S.ShapeType() != TopAbs_COMPOUND) {
278       /* to set the index of a unique sub shape (Explode All ONLY for the moment !) */
279       ListOfID->length(1);
280       int index = 1;
281       TopTools_MapOfShape M;
282       TopExp_Explorer Exp ;
283       for( Exp.Init(mainTopo, TopAbs_ShapeEnum( result->ShapeType() )) ; Exp.More(); Exp.Next() )  {
284         if ( M.Add(Exp.Current()) ) {
285           if(Exp.Current().IsSame(S) ) {
286             ListOfID[0] = index;
287             break;
288           }
289           index++ ;
290         }       
291       }
292       result->Index(ListOfID) ;
293       return result->Name(); 
294     }
295     else {
296       /* Here is a TopAbs_COMPOUND : we set the list/index for a compound : containing two or more sub shapes  */
297       /* Warning : the Corba shape has a shapetype Compound : in GEOMDS_Client we have to retrieve the kind of */
298       /* subshapes contained in this compound !                                                                */
299       TopTools_SequenceOfShape SS;
300       TopoDS_Iterator it ;
301       TopExp_Explorer exp ;
302       TopAbs_ShapeEnum subType ;
303       
304       /* Set all sub shapes in a sequence of shapes  */
305       for ( it.Initialize( S, true, true ) ; it.More(); it.Next() ) {
306         subType = it.Value().ShapeType() ;
307         SS.Append( it.Value() ) ;
308       }
309       
310       ListOfID->length( SS.Length() ) ;
311       int j, k ;  /* in TopTools_SequenceOfShape index start at 1 */
312       
313       for( k=1; k<=SS.Length(); k++ ) {
314         j = 1 ;
315         for( exp.Init( mainTopo, subType ); exp.More(); exp.Next() ) {  
316           if( exp.Current().IsSame( SS.Value(k) ) ) {
317             ListOfID[k-1] = j ;
318           }
319           j++ ;
320         }
321       }
322       result->Index(ListOfID) ;
323       return result->Name();
324     }
325     
326   }
327   /******************* Independent object (not a sub shape) *********************/
328   else {
329     result->IsMainShape(true) ;
330     if( !GC.AddIORNameAttribute(Lab, result->Name() ) )  {
331       MESSAGE("in LocalPersistentIDToIOR, NAME/IOR attribute already exist." << endl ) ;
332     }
333     Handle(TNaming_NamedShape) NamedShape ;  
334     bool notTested = Lab.FindAttribute(TNaming_NamedShape::GetID(), NamedShape) ;
335     result->ShapeId(aPersRefString);
336     return result->Name(); 
337   }
338 }
339
340 //============================================================================
341 // function : CanPublishInStudy
342 // purpose  : 
343 //============================================================================
344 bool GEOM_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
345 {
346   GEOM::GEOM_Shape_var aShape = GEOM::GEOM_Shape::_narrow(theIOR);
347   return !(aShape->_is_nil());
348 }
349
350
351 //============================================================================
352 // function : PublishInStudy
353 // purpose  : 
354 //============================================================================
355 SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
356                                                  SALOMEDS::SObject_ptr theSObject,
357                                                  CORBA::Object_ptr theObject,
358                                                  const char* theName) throw (SALOME::SALOME_Exception)
359 {
360   SALOMEDS::SObject_var aResultSO;
361   if(CORBA::is_nil(theObject)) return aResultSO;
362
363   GEOM::GEOM_Shape_var aShape = GEOM::GEOM_Shape::_narrow(theObject);
364   if(aShape->_is_nil()) return aResultSO;
365
366   if(theStudy->_is_nil()) return aResultSO;
367
368   SALOMEDS::GenericAttribute_var anAttr;
369   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
370
371   SALOMEDS::SComponent_var       aFather = theStudy->FindComponent("GEOM"); 
372   if (aFather->_is_nil()) {
373     aFather = aStudyBuilder->NewComponent("GEOM");
374     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
375     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
376     aName->SetValue("Geometry");
377     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
378     SALOMEDS::AttributePixMap::_narrow(anAttr)->SetPixMap("ICON_OBJBROWSER_Geometry");
379     aStudyBuilder->DefineComponentInstance(aFather, GEOM_Gen::_this());
380   }
381   if (aFather->_is_nil()) return aResultSO;
382   
383   if (CORBA::is_nil(theSObject)) {
384     aResultSO = aStudyBuilder->NewObject(aFather);
385   } else {
386     if (!theSObject->ReferencedObject(aResultSO)) 
387       THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
388   }
389   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
390   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
391   anIOR->SetValue(aShape->Name());
392
393   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributePixMap");
394   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
395   TCollection_AsciiString aShapeName("Shape_");  
396
397   if ( aShape->ShapeType() == GEOM::COMPOUND ) {
398     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPOUND" );
399     aShapeName = "Compound_";
400   } else if ( aShape->ShapeType() == GEOM::COMPSOLID ) {
401     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPSOLID" );
402     aShapeName = "Compsolid_";
403   } else if ( aShape->ShapeType() == GEOM::SOLID ) {
404     aPixmap->SetPixMap( "ICON_OBJBROWSER_SOLID" );
405     aShapeName = "Solid_";
406   } else if ( aShape->ShapeType() == GEOM::SHELL ) {
407     aPixmap->SetPixMap( "ICON_OBJBROWSER_SHELL" );
408     aShapeName = "Shell_";
409   } else if ( aShape->ShapeType() == GEOM::FACE ) {
410     aPixmap->SetPixMap( "ICON_OBJBROWSER_FACE" );
411     aShapeName = "Face_";
412   } else if ( aShape->ShapeType() == GEOM::WIRE ) {
413     aPixmap->SetPixMap( "ICON_OBJBROWSER_WIRE" );
414     aShapeName = "Wire_";
415   } else if ( aShape->ShapeType() == GEOM::EDGE ) {
416     aPixmap->SetPixMap( "ICON_OBJBROWSER_EDGE" );
417     aShapeName = "Edge_";
418   } else if ( aShape->ShapeType() == GEOM::VERTEX ) {
419     aPixmap->SetPixMap( "ICON_OBJBROWSER_VERTEX" );
420     aShapeName = "Vertex_";
421   }                                          
422   if (strlen(theName) == 0) aShapeName += TCollection_AsciiString(aResultSO->Tag());
423   else aShapeName = TCollection_AsciiString(strdup(theName));
424
425   //Set a name of the added shape
426   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
427   SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
428   aNameAttrib->SetValue(aShapeName.ToCString());
429
430   //Add a reference to published object
431 //    aStudyBuilder->Addreference(theObject, aResultSO);
432   return aResultSO._retn();
433 }
434
435  
436 //============================================================================
437 // function : Save()
438 // purpose  : save OCAF/Geom document
439 //============================================================================
440 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
441                                     const char* theURL,
442                                     bool isMultiFile) {
443   SALOMEDS::TMPFile_var aStreamFile;
444   // Get a temporary directory to store a file
445   TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
446   // Create a list to store names of created files
447   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
448   aSeq->length(1);
449   // Prepare a file name to open
450   TCollection_AsciiString aNameWithExt(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
451   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
452   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
453   // Build a full file name of temporary file
454   TCollection_AsciiString aFullName = aTmpDir + aNameWithExt;
455   // Save GEOM component in this file
456   myOCAFApp->SaveAs(myCurrentOCAFDoc, aFullName);
457   // Conver a file to the byte stream
458   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
459   // Remove the created file and tmp directory
460   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
461   // Return the created byte stream
462   return aStreamFile._retn();
463 }
464
465 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
466                                          const char* theURL,
467                                          bool isMultiFile) {
468   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
469   return aStreamFile._retn();
470 }
471
472
473 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
474                                 const SALOMEDS::TMPFile& theStream,
475                                 const char* theURL,
476                                 bool isMultiFile) {
477
478   if (theStream.length() <= 9) {
479     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
480     return false;
481   }
482
483   // Get a temporary directory for a file
484   TCollection_AsciiString aTmpDir = isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
485   // Conver the byte stream theStream to a file and place it in tmp directory
486   SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream,
487                                                                        aTmpDir.ToCString(),
488                                                                        isMultiFile);
489
490   // Prepare a file name to open
491   TCollection_AsciiString aNameWithExt(aSeq[0]);
492   TCollection_AsciiString aFullName = aTmpDir + aNameWithExt;
493
494   // Open document
495   if (myOCAFApp->Open(aFullName, myCurrentOCAFDoc) != CDF_RS_OK) return false;
496
497   // Remove the created file and tmp directory
498   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
499
500   SALOMEDS::Study_var Study = theComponent->GetStudy();
501   TCollection_AsciiString name( strdup(Study->Name()) );
502
503   int StudyID = Study->StudyId();
504   myStudyIDToDoc.Bind( StudyID, myCurrentOCAFDoc );  
505   myStudyID = StudyID;
506     
507   /* We clear all IOR (nameIOR) attributes of all objects before reconstruction */
508   /* This information will be setted when each object is reconstructed          */
509   GEOMDS_Commands GC( myCurrentOCAFDoc->Main() ) ;
510   GC.ClearAllIOR(myCurrentOCAFDoc->Main());
511
512   return true;
513 }
514
515 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
516                                      const SALOMEDS::TMPFile& theStream,
517                                      const char* theURL,
518                                      bool isMultiFile) {
519   return Load(theComponent, theStream, theURL, isMultiFile);
520 }
521
522 //  //============================================================================
523 //  // function : Save()
524 //  // purpose  : save OCAF/Geom document
525 //  //============================================================================
526 //  void GEOM_Gen_i::Save(const char *IORSComponent, const char *aUrlOfFile) 
527 //  {
528
529 //    TCollection_ExtendedString path(strdup(aUrlOfFile));
530 //    TCollection_ExtendedString pathWithExt = path + TCollection_ExtendedString(".sgd");
531 //    myOCAFApp->SaveAs(myCurrentOCAFDoc,pathWithExt);
532 //  }
533
534
535 //  //============================================================================
536 //  // function : Load()
537 //  // purpose  : Load OCAF/Geom document
538 //  //============================================================================
539 //  void GEOM_Gen_i::Load(const char *IORSComponent, const char *aUrlOfFile) 
540 //  {
541
542 //    TCollection_ExtendedString path(strdup(aUrlOfFile));
543 //    TCollection_ExtendedString pathWithExt = path + TCollection_ExtendedString(".sgd");
544
545 //    myOCAFApp->Open(pathWithExt,myCurrentOCAFDoc);
546
547 //    SALOMEDS::SComponent_var SC = SALOMEDS::SComponent::_narrow(_orb->string_to_object(IORSComponent));
548 //    SALOMEDS::Study_var Study = SC->GetStudy();
549 //    TCollection_AsciiString name( strdup(Study->Name()) );
550
551 //    int StudyID = Study->StudyId();
552 //    myStudyIDToDoc.Bind( StudyID, myCurrentOCAFDoc );  
553 //    myStudyID = StudyID;
554
555 //    /* We clear all IOR (nameIOR) attributes of all objects before reconstruction */
556 //    /* This information will be setted when each object is reconstructed          */
557 //    GEOMDS_Commands GC( myCurrentOCAFDoc->Main() ) ;
558 //    GC.ClearAllIOR(myCurrentOCAFDoc->Main());
559
560 //    return ;
561 //  }
562
563
564 //============================================================================
565 // function : Close()
566 // purpose  :
567 //============================================================================
568 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
569 {
570   int anID = theComponent->GetStudy()->StudyId();
571   if (anID == myStudyID) GetCurrentStudy(0); // set default value of current study ID, if current is deleted
572   if (myStudyIDToDoc.IsBound(anID)) {
573     // close document in the application
574 //      Handle(TDocStd_Owner) anOwner;
575     Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(myStudyIDToDoc.Find(anID));
576 //      Handle(TDocStd_Document) anEmptyDoc;
577 //      if (aDoc->Main().Root().FindAttribute(TDocStd_Owner::GetID(), anOwner)) {
578 //        anOwner->SetDocument(anEmptyDoc);
579 //        cout<<"********** Nullify owner of document"<<endl;
580 //      }
581     myOCAFApp->Close(aDoc);
582     myStudyIDToDoc.UnBind(anID); // remove document from GEOM documents data map
583     }
584 }
585
586 //============================================================================
587 // function : CanCopy()
588 // purpose  :
589 //============================================================================
590 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
591   // Try to retrieve known by Geometry component GEOM_shape by given IOR
592   SALOMEDS::GenericAttribute_var anAttr;
593   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
594   GEOM::GEOM_Shape_var aShape = GetIORFromString(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
595   // If the object is null one it can't be copied: return false
596   if (aShape->_is_nil()) return false;
597   return true;
598 }
599
600 //============================================================================
601 // function : CopyFrom()
602 // purpose  :
603 //============================================================================
604 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
605   // Declare a sequence of the byte to store the copied object
606   SALOMEDS::TMPFile_var aStreamFile;
607
608   // Try to get GEOM_Shape object by given SObject
609   SALOMEDS::GenericAttribute_var anAttr;
610   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
611   GEOM::GEOM_Shape_var aShape = GetIORFromString(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
612   // If the object is null one it can't be copied: return false
613   if (aShape->_is_nil()) return aStreamFile._retn();
614  
615   GetCurrentStudy(theObject->GetStudy()->StudyId());
616
617   // Convert a TopoDS_Shape to a stream of bytes
618   TopoDS_Shape aTopology = GetTopoShape(aShape);
619   if (aTopology.IsNull()) return aStreamFile._retn();
620   ostrstream aStreamedShape;
621   BRepTools::Write(aTopology, aStreamedShape);
622   int aSize = aStreamedShape.pcount();
623   char* aBuffer = new char[aSize];
624   memcpy(aBuffer, aStreamedShape.str(), aSize);
625   aStreamedShape.rdbuf()->freeze(0);
626
627   aStreamFile = new SALOMEDS::TMPFile(aSize, aSize, (CORBA::Octet*)aBuffer, 1);
628   
629   // Assign an ID = 1 the the type GEOM_Shape
630   theObjectID = 1;
631
632   // Return created TMPFile
633   return aStreamFile._retn();
634 }
635
636 //============================================================================
637 // function : CanPaste()
638 // purpose  :
639 //============================================================================
640 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
641   // The Geometry component can paste only objects copied by Geometry component
642   // and with the object type = 1
643 //    cout<<"********** GEOM_Gen_i::CanPaste ("<<theComponentName<<","<<theObjectID<<")"<<endl;
644   if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
645   return true;
646 }
647
648 //============================================================================
649 // function : PasteInto()
650 // purpose  :
651 //============================================================================
652 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
653                                             CORBA::Long theObjectID,
654                                             SALOMEDS::SObject_ptr theObject) {
655   // Find the current Study and StudyBuilder
656   SALOMEDS::Study_var aStudy = theObject->GetStudy();
657   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
658
659   // Retrieve a TopoDS_Shape from byte stream
660   TopoDS_Shape aTopology;
661   istrstream aStreamedBrep((char*) &theStream[0], theStream.length());
662   BRep_Builder aBuilder;
663   try {
664     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
665   } catch (Standard_Failure) {
666 //      cout<<"GEOM_Gen_i::PasteInto exception"<<endl;
667     return false;
668   }
669   
670   // Create new object in Geometry component using retrieved topology
671   GEOM::GEOM_Shape_var aShape = CreateObject(aTopology);
672   GetCurrentStudy(aStudy->StudyId());
673   const char *anEntry = InsertInLabel(aTopology, aShape->Name(), myCurrentOCAFDoc) ;
674   aShape->ShapeId(anEntry) ;
675
676   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
677   SALOMEDS::SObject_var aNewSO;
678   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
679     aNewSO = aStudyBuilder->NewObject(theObject);
680   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
681   // Add IORAttribute to the Study and set IOR of the created GEOM_Shape to it
682   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
683   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
684   anIOR->SetValue(aShape->Name());
685
686   // Return the created in the Study SObject
687   return aNewSO._retn();
688 }
689
690 //============================================================================
691 // function : ComponentDataType()
692 // purpose  :
693 //============================================================================
694 char* GEOM_Gen_i::ComponentDataType()
695 {
696   return strdup("GEOM");
697 }
698
699 //============================================================================
700 // function : register() 
701 // purpose  : register 'name' in 'name_service'
702 //============================================================================
703 void GEOM_Gen_i::register_name(char * name)
704 {
705   GEOM::GEOM_Gen_ptr g = GEOM::GEOM_Gen::_narrow(POA_GEOM::GEOM_Gen::_this());
706   name_service->Register(g, strdup(name)); 
707 }
708
709
710
711 //================================================================================
712 // function : SequenceOfShapeFromListOfGeomShape()
713 // purpose  : Define a sequence of shapes from 'listShapes' and return its length.
714 //          : No control is made on shapes !
715 //================================================================================
716 int GEOM_Gen_i::SequenceOfShapeFromListOfGeomShape( const GEOM::GEOM_Gen::ListOfGeomShapes& listShapes,
717                                                     TopTools_SequenceOfShape& SS )
718 {
719   int nbShapes = listShapes.length() ;
720   if( nbShapes < 1)
721     return 0 ;
722   
723   for(int i=0; i<nbShapes; i++) {
724     GEOM::GEOM_Shape_var aGeomShape = listShapes[i] ;
725     TopoDS_Shape aShape = GetTopoShape(aGeomShape) ;
726     SS.Append(aShape) ;
727   }
728   return nbShapes ;
729 }
730
731
732
733
734 //=================================================================================
735 // function : GetTopoShape()
736 // purpose  : Returns a TopoDS_Shape from a GEOM::GEOM_Shape_ptr in 'myCurrentOCAFDoc'
737 //          : A null shape is returned if not possible
738 //=================================================================================
739 TopoDS_Shape GEOM_Gen_i::GetTopoShape(GEOM::GEOM_Shape_ptr shape_ptr)
740
741   TopoDS_Shape tds ;
742
743   TDF_Label lab ;
744   Handle(TDF_Data) D = myCurrentOCAFDoc->GetData() ;
745   TDF_Tool::Label( D, strdup(shape_ptr->ShapeId()), lab, true ) ;
746   Handle(TNaming_NamedShape) NamedShape ;  
747   bool res = lab.FindAttribute(TNaming_NamedShape::GetID(), NamedShape) ;
748
749   if( !res ) {
750     return tds ; /* a null shape is returned */
751   }
752   else {
753     return TNaming_Tool::GetShape(NamedShape) ;
754   }
755 }
756
757
758
759 //=================================================================================
760 // function : GetStringFromIOR()
761 // purpose  : returns a string that represents  a 'GEOM::GEOM_Shape_var'
762 //=================================================================================
763 const char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Shape_var shapeIOR) {
764   const char * ret = _orb->object_to_string(shapeIOR) ;
765   return ret ;
766 }
767
768
769
770 //=================================================================================
771 // function : GetIORFromString()
772 // purpose  : returns a 'GEOM::GEOM_Shape_var' from a string representing it
773 //=================================================================================
774 GEOM::GEOM_Shape_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
775   GEOM::GEOM_Shape_var shapeIOR;
776   if(strcmp(stringIOR,"") != 0){
777     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
778     if(!CORBA::is_nil(anObject))
779       shapeIOR =  GEOM::GEOM_Shape::_narrow(anObject.in()) ;
780   }
781   return shapeIOR._retn() ;
782 }
783
784
785
786 //==================================================================================
787 // function : InsertInLabel()
788 // purpose  : Insert S = Shape and mystr = name in a new Label of Geom/OCAF document
789 //          : and returns the corresponding OCAF entry
790 //==================================================================================
791 const char * GEOM_Gen_i::InsertInLabel(TopoDS_Shape S, const char *mystr, Handle(TDocStd_Document) OCAFDoc)
792 {
793   GEOMDS_Commands GC(OCAFDoc->Main());
794   /* add attributs S and mystr in a new label */
795   TDF_Label Lab = GC.AddShape (S, strdup(mystr));
796
797   TCollection_AsciiString entry;
798   TDF_Tool::Entry(Lab,entry);
799   const char *ent = entry.ToCString() ;
800   return ent ;
801 }
802
803
804 //==================================================================================
805 // function : InsertInLabelDependentShape()
806 // purpose  : Insert S = Shape and its nameIor in a new Label of Geom/OCAF document
807 //          : insert also a reference attribute (a label) to the main shape 'mainshap_ptr'.
808 //          : and returns the corresponding OCAF entry of the new label.
809 //==================================================================================
810 const char * GEOM_Gen_i::InsertInLabelDependentShape( TopoDS_Shape S,
811                                                       const char *nameIor,
812                                                       GEOM::GEOM_Shape_ptr mainshape_ptr,
813                                                       Handle(TDocStd_Document) OCAFDoc )
814 {
815   GEOMDS_Commands GC(OCAFDoc->Main());
816   /* add attributs S and nameIor in a new label */
817
818   /* retrieve the label of the main shape in the document */
819   TDF_Label mainRefLab;
820   TDF_Tool::Label(OCAFDoc->GetData(), mainshape_ptr->ShapeId(), mainRefLab);
821
822   /* add attributs : S, nameIor and ref to main */
823   TDF_Label Lab = GC.AddDependentShape(S, strdup(nameIor), mainRefLab);
824
825   TCollection_AsciiString entry;
826   TDF_Tool::Entry(Lab, entry);
827   const char *ent = entry.ToCString() ;
828   return ent ;
829 }
830
831
832 //=================================================================================
833 // function : InsertInLabelOneArgument()
834 // purpose  :
835 //=================================================================================
836 void GEOM_Gen_i::InsertInLabelOneArgument(TopoDS_Shape main_topo,
837                                           GEOM::GEOM_Shape_ptr shape_ptr,
838                                           TopoDS_Shape result_topo,     
839                                           GEOM::GEOM_Shape_ptr result,  
840                                           Handle(TDocStd_Document) OCAFDoc)
841 {
842   /* Put shape and name into geom/OCAF doc */
843   GEOMDS_Commands GC(OCAFDoc->Main());
844   /* Add attributs 'shape' and 'name_ior' in a new label */
845   TDF_Label Lab = GC.Generated( main_topo, result_topo, result->Name() );
846   TCollection_AsciiString entry;
847   TDF_Tool::Entry(Lab, entry);
848   result->ShapeId( entry.ToCString() ) ;
849   
850   /* Create a new label */
851   TDF_Label NewLab = Lab.NewChild();
852   TCollection_ExtendedString Value("Arguments");
853   TDataStd_Name::Set(NewLab,Value);
854   
855   TDF_Label NewLab1 = NewLab.NewChild();
856   TDF_Label RefLab;
857   TDF_Tool::Label(OCAFDoc->GetData(), shape_ptr->ShapeId(), RefLab);
858   TDF_Reference::Set(NewLab1, RefLab);
859 }
860
861
862 //=================================================================================
863 // function : InsertInLabelMoreArguments()
864 // purpose  :
865 //=================================================================================
866 void GEOM_Gen_i::InsertInLabelMoreArguments(TopoDS_Shape main_topo,
867                                             GEOM::GEOM_Shape_ptr result,
868                                             const GEOM::GEOM_Gen::ListOfIOR& ListShapes,                                
869                                             Handle(TDocStd_Document) OCAFDoc)
870 {
871   /* Put shape and name into geom/OCAF doc */
872   GEOMDS_Commands GC(OCAFDoc->Main());
873   /* Add attributs TopoDS and name_ior in a new label */
874   TDF_Label Lab = GC.AddShape(main_topo, result->Name() );
875   TCollection_AsciiString entry;
876   TDF_Tool::Entry(Lab, entry);
877   
878   /* Create a new label */
879   TDF_Label NewLab = Lab.NewChild();
880   TCollection_ExtendedString Value("Arguments");
881   TDataStd_Name::Set(NewLab, Value);
882
883   for (unsigned int ind = 0; ind < ListShapes.length(); ind++) {
884     
885     TDF_Label NewLab1 = NewLab.NewChild();    
886     GEOM::GEOM_Shape_var aShape = GetIORFromString( ListShapes[ind] );
887     
888     TDF_Label RefLab;
889     TDF_Tool::Label(OCAFDoc->GetData(), aShape->ShapeId(), RefLab);
890     TDF_Reference::Set(NewLab1, RefLab);
891   }
892   result->ShapeId(entry.ToCString());
893 }
894
895
896
897 //=================================================================================
898 // function: NbLabels()
899 // purpose : 
900 //=================================================================================
901 CORBA::Short GEOM_Gen_i::NbLabels()
902 {
903   return TDF_Tool::NbLabels( myCurrentOCAFDoc->Main() );
904 }
905
906
907
908 //=================================================================================
909 // function: GetCurrentStudy()
910 // purpose : Finds or creates the geom/OCAF document corresponding to the index
911 // 'StudyID'
912 //=================================================================================
913 void GEOM_Gen_i::GetCurrentStudy(CORBA::Long StudyID)
914 {
915   /* If StudyID is known we link myCurrentOCAFDoc to it */
916   if (myStudyIDToDoc.IsBound(StudyID)) {
917     myCurrentOCAFDoc =  Handle(TDocStd_Document)::DownCast(myStudyIDToDoc(StudyID));
918   }
919   /* Create a new OCAFDoc and link it to 'StudyID' argument */
920   else { 
921     myOCAFApp->NewDocument("SALOME_GEOM",myCurrentOCAFDoc);
922     myStudyIDToDoc.Bind(StudyID,myCurrentOCAFDoc);
923   } 
924   myStudyID = StudyID;
925 }
926
927
928 //================================================================================
929 // function : CreateObject() 
930 // purpose  : private function to create a complete CORBA object and return it
931 //================================================================================
932 GEOM::GEOM_Shape_ptr GEOM_Gen_i::CreateObject(TopoDS_Shape& tds)
933 {
934 //   if ( tds.ShapeType() == TopAbs_COMPOUND ) {
935 //     TopoDS_Iterator itr(tds);
936 //     TopoDS_Shape res;
937 //     int i = 0;
938 //     while (itr.More()) {
939 //       i++;
940 //       res = itr.Value();
941 //       itr.Next();
942 //     }
943     
944 //     if ( i == 1 )
945 //       tds = res;
946 //   }
947
948   GEOM::shape_type st = GEOM::shape_type(tds.ShapeType()) ; /* casting */
949   
950   /* Create the CORBA servant holding the TopoDS_Shape */
951   GEOM::GEOM_Gen_ptr engine = POA_GEOM::GEOM_Gen::_this();
952   GEOM::GEOM_Shape::ListOfSubShapeID_var index = new GEOM::GEOM_Shape::ListOfSubShapeID;
953   index->length(0);  
954   GEOM_Shape_i * shape_servant = new GEOM_Shape_i(tds, _orb, engine, index, st, true);
955   GEOM::GEOM_Shape_var shape = GEOM::GEOM_Shape::_narrow(shape_servant->_this()); 
956   
957   /* Create and set the name (IOR of shape converted into a string) */
958   string name_ior = _orb->object_to_string(shape) ;
959   shape->Name( name_ior.c_str() );  
960   shape->NameType( "" );
961   return shape;
962 }
963
964 //=======================================================================
965 //function : CreateSubObject
966 //purpose  : 
967 //=======================================================================
968
969 GEOM::GEOM_Shape_ptr GEOM_Gen_i::CreateSubObject(const TopoDS_Shape& SubShape,
970                                                  const GEOM::GEOM_Shape_ptr MainShape,
971                                                  const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID)
972 {
973   GEOM::shape_type st = GEOM::shape_type(SubShape.ShapeType()) ; /* casting */
974   
975   /* Create the CORBA servant holding the TopoDS_Shape */
976   GEOM::GEOM_Gen_ptr engine = POA_GEOM::GEOM_Gen::_this();
977   GEOM_Shape_i * shape_servant =
978     new GEOM_Shape_i(SubShape, _orb, engine, ListOfID, st, false);
979   GEOM::GEOM_Shape_var shape = GEOM::GEOM_Shape::_narrow(shape_servant->_this()); 
980   
981     /* Create and set the name (IOR of shape converted into a string) */
982   string name_ior = _orb->object_to_string(shape) ;
983   shape->Name( name_ior.c_str() );
984     /* create and set the mainname (IOR of shape converted into a string) */
985   const char *mainname_ior = _orb->object_to_string(MainShape) ;
986   shape->MainName(mainname_ior);
987     /* precaution : NameType will be set precisely in GUI */    
988   shape->NameType( "" );
989     /* add 'SubShape' its 'nameIOR' and a reference to the main shape thanks to method below  */
990   const char *entry =
991     InsertInLabelDependentShape(SubShape, shape->Name(), MainShape, myCurrentOCAFDoc) ;
992   shape->ShapeId( entry ) ;
993  
994   return shape;
995 }
996
997 //=======================================================================
998 // function : SuppressFacesGlue()
999 // purpose  : Define a compound of shells after suppress of mapFaces in the 
1000 //          : shape S and return the number of shells of the compound.
1001 //=======================================================================
1002 int GEOM_Gen_i::SuppressFacesGlue( const TopoDS_Shape& S,
1003                                    const TopTools_MapOfShape& mapFaces,
1004                                    TopoDS_Shape& aCompoundOfShells )
1005   throw (SALOME::SALOME_Exception)
1006 {  
1007   BRepTools_Quilt Glue;
1008   aCompoundOfShells.Nullify() ;
1009   
1010   for ( TopExp_Explorer exp( S, TopAbs_FACE); exp.More(); exp.Next() ) {
1011     const TopoDS_Face& F = TopoDS::Face(exp.Current());
1012     if ( !mapFaces.Contains(F) ) {
1013       /* this face must not to be suppressed */
1014       Glue.Add(F);
1015     }
1016   }
1017   
1018   /* Use specif method to calculate the compound of shells */
1019   aCompoundOfShells = Glue.Shells();
1020   
1021   if( aCompoundOfShells.ShapeType() != TopAbs_COMPOUND ) {
1022     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::SuppressFacesGlue() : not a GEOM::COMPOUND", SALOME::BAD_PARAM);
1023   }
1024
1025   /* explore Compound for verification and return the number of shells */
1026   int numberOfShell = 0 ;
1027   for ( TopExp_Explorer exp1( aCompoundOfShells, TopAbs_SHELL); exp1.More(); exp1.Next() )
1028     numberOfShell++ ;
1029
1030   return numberOfShell ;
1031 }
1032
1033
1034 //=====================================================================================
1035 // function : GetIndexTopology()
1036 // purpose  : return the index of a sub shape in a shape (index starts at 1)
1037 //          : Return -1 if not found
1038 //=====================================================================================
1039 int GEOM_Gen_i::GetIndexTopology(const TopoDS_Shape& subshape, const TopoDS_Shape& mainShape) 
1040
1041   if( mainShape.IsNull() || subshape.IsNull() ) 
1042     return -1 ; 
1043
1044   int index = 1; 
1045   if (subshape.ShapeType() == TopAbs_COMPOUND) 
1046     { 
1047       TopoDS_Iterator it; 
1048       TopTools_ListOfShape CL; 
1049       CL.Append( mainShape ); 
1050       TopTools_ListIteratorOfListOfShape itC; 
1051       for (itC.Initialize( CL ); itC.More(); itC.Next()) 
1052         { 
1053           for (it.Initialize( itC.Value() );  it.More(); it.Next()) 
1054             { 
1055               if ( it.Value().ShapeType() == TopAbs_COMPOUND) 
1056                 {
1057                   if (it.Value().IsSame(subshape)) 
1058                     return index; 
1059                   else 
1060                     index++; 
1061                   CL.Append( it.Value() ); 
1062                 }
1063             } 
1064         } 
1065     } 
1066   else 
1067     { 
1068       TopExp_Explorer Exp ( mainShape,  subshape.ShapeType() ); 
1069       TopTools_MapOfShape M; 
1070       while ( Exp.More() ) 
1071         { 
1072           if ( M.Add(Exp.Current()) ) 
1073             { 
1074               if ( Exp.Current().IsSame(subshape) ) 
1075                 return index; 
1076               index++; 
1077             } 
1078           Exp.Next(); 
1079         } 
1080     } 
1081   return -1; 
1082
1083
1084
1085 //================================================================================
1086 // function : IndexOfFacesOfSubShell()
1087 // purpose  : Return a list of indices corresponding to the faces of a 'subShell'
1088 //          : in the main shape 'S'
1089 //================================================================================
1090 GEOM::GEOM_Shape::ListOfSubShapeID* GEOM_Gen_i::IndexOfFacesOfSubShell( const TopoDS_Shape& S,
1091                                                                   const TopoDS_Shape subShell )
1092   throw (SALOME::SALOME_Exception)
1093 {
1094
1095   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
1096   ListOfID->length(0) ;
1097   if( subShell.IsNull() || subShell.ShapeType() != TopAbs_SHELL ) {
1098     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::IndexOfFacesOfSubShell() : null shape or not a GEOM::SHELL", SALOME::BAD_PARAM);
1099   }
1100
1101   /* put faces of subShell in a Map of faces */
1102   int j = 0 ;
1103   TopTools_MapOfShape mapFaces ;
1104   for( TopExp_Explorer Exp1( subShell, TopAbs_FACE );  Exp1.More(); Exp1.Next() ) {
1105     mapFaces.Add(Exp1.Current() ) ;
1106     j++ ;
1107   }
1108
1109   if( j<1 )
1110     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::IndexOfFacesOfSubShell() : no faces in sub shell", SALOME::BAD_PARAM);
1111   
1112   /* Find index of each face of subshell in the main topology and put its index in ListOfID */
1113   int size = 0 ;
1114   for ( TopExp_Explorer Exp2(S, TopAbs_FACE); Exp2.More();  Exp2.Next() ) {
1115     
1116     const TopoDS_Face& F = TopoDS::Face( Exp2.Current() ) ;
1117
1118     if( mapFaces.Contains(F) )  {
1119       int n = GetIndexTopology( F, S ) ;
1120       if( n<=0 ) {
1121         THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::IndexOfFacesOfSubShell() : no index found", SALOME::BAD_PARAM);    
1122       }
1123       size++;
1124       ListOfID->length(size) ;
1125       ListOfID[size-1] = n ;
1126     }
1127   }
1128
1129   return ListOfID._retn() ;
1130 }
1131
1132
1133
1134 //================================================================================
1135 // function : ListOfIDIntoMapOfShapes()
1136 // purpose  : Define a MapOfShapes from a main topology 'S' a 'subShapeType'
1137 //          : and a list of indices 'L'.
1138 //          : Return true if 'aMap' is not empty
1139 //================================================================================
1140 bool GEOM_Gen_i::ListOfIDIntoMapOfShapes( const TopoDS_Shape& S,
1141                                           const GEOM::GEOM_Shape::ListOfSubShapeID& L,
1142                                           const int subShapeType,
1143                                           TopTools_MapOfShape& aMap )
1144 {
1145   if( L.length() < 1 || S.IsNull() ) {
1146     return false ;
1147   }
1148   
1149   aMap.Clear() ; 
1150   for( int k=0; k<L.length(); k++ ) {
1151     /* indices start at 1 in list L */
1152     int j = 1 ;
1153     TopExp_Explorer exp ;
1154     TopTools_MapOfShape M; 
1155     for(  exp.Init( S, TopAbs_ShapeEnum(subShapeType) ); exp.More(); exp.Next() ) {
1156       if ( M.Add(exp.Current()) ) 
1157         { 
1158           if( L[k] == j ) {
1159             aMap.Add( exp.Current() ) ;
1160           }
1161           j++ ;
1162         }
1163     }
1164   }
1165   return true ;
1166 }
1167
1168
1169
1170 //================================================================================
1171 // function : ListOfIDIntoSequenceOfShapes()
1172 // purpose  : Define 'aSequenceOfShapes' from a main topology 'S' a 'subShapeType'
1173 //          : and a list of indices 'L'.
1174 //          : Return true if 'aSequenceOfShapes' is not empty
1175 //================================================================================
1176 bool GEOM_Gen_i::ListOfIDIntoSequenceOfShapes( const TopoDS_Shape& S,
1177                                                const GEOM::GEOM_Shape::ListOfSubShapeID& L,
1178                                                const int subShapeType,
1179                                                TopTools_SequenceOfShape& aSequenceOfShapes )
1180 {
1181   if( L.length() < 1 || S.IsNull() ) {
1182     return false ;
1183   }
1184   
1185   aSequenceOfShapes.Clear() ; 
1186   for( int k=0; k<L.length(); k++ ) {
1187     /* indices start at 1 in list L */
1188     int j = 1 ;
1189     TopExp_Explorer exp ;
1190     for(  exp.Init( S, TopAbs_ShapeEnum(subShapeType) ); exp.More(); exp.Next() ) {
1191       if( L[k] == j ) {
1192         aSequenceOfShapes.Append( exp.Current() ) ;
1193       }
1194       j++ ;
1195     }
1196   }
1197   return true ;
1198 }
1199
1200
1201
1202 //================================================================================
1203 // function : SuppressFaces()
1204 // purpose  : Suppress faces contained in ListOfID from 'shape'.
1205 //          : Return a list of Geom shapes each one is a main shape GEOM::FACE or GEOM::SHELL
1206 //================================================================================
1207 GEOM::GEOM_Gen::ListOfGeomShapes* GEOM_Gen_i::SuppressFaces( GEOM::GEOM_Shape_ptr shape,
1208                                                                 const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID ) 
1209   throw (SALOME::SALOME_Exception)
1210 {
1211   GEOM::GEOM_Gen::ListOfGeomShapes_var listOfGeomShapes = new GEOM::GEOM_Gen::ListOfGeomShapes;
1212   listOfGeomShapes->length(0) ;
1213
1214   TopoDS_Shape mainShape = GetTopoShape(shape);
1215   if( mainShape.IsNull() )
1216     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::SuppressFaces() : null argument shape", SALOME::BAD_PARAM);
1217   
1218   if( ListOfID.length() < 1 )
1219     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::SuppressFaces() : empty ListOfID", SALOME::BAD_PARAM);
1220   
1221   /* Define 'mapFaces' a map of faces to be suppressed in mainShape */
1222   TopTools_MapOfShape mapFaces ;
1223   if( !ListOfIDIntoMapOfShapes(mainShape, ListOfID, TopAbs_FACE, mapFaces ) ) {
1224     return listOfGeomShapes._retn();
1225   }
1226     
1227   /* Call algorithm to calculate a compound of shells resulting of face suppression */
1228   int numberOfShells = 0 ;
1229   TopoDS_Shape aCompoundOfShells ;
1230   numberOfShells = SuppressFacesGlue(mainShape, mapFaces, aCompoundOfShells) ;
1231   if(numberOfShells < 1) {
1232     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::Suppressfaces() : no shells", SALOME::BAD_PARAM);
1233   }
1234  
1235   /* Create a shell for each shell contained in 'aCompoundOfShells' and             */ 
1236   /* put it in the list of GeomShapes to be returned.                               */
1237   /* But if the shell is composed of only a face we create a face and not a shell   */
1238   int i = 0 ;
1239   for( TopExp_Explorer exp(aCompoundOfShells, TopAbs_SHELL); exp.More(); exp.Next() ) {
1240
1241     const TopoDS_Shell& aShell = TopoDS::Shell( exp.Current() );    
1242     if( aShell.IsNull() ) {
1243       THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::Suppressfaces() : null shell", SALOME::BAD_PARAM);
1244     }
1245     
1246     GEOM::GEOM_Shape::ListOfSubShapeID_var aList = new GEOM::GEOM_Shape::ListOfSubShapeID;
1247     aList = IndexOfFacesOfSubShell(mainShape, aShell) ;
1248
1249      if( aList->length() < 1 ) {
1250       THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::SuppressFaces() : aList is empty", SALOME::BAD_PARAM);
1251     }
1252
1253     TopoDS_Shape aShellOrFace ;
1254     /* Only a face into the shell : we create a single face instead of a shell : 'aList' is unchanged  */
1255     if( aList->length() == 1 ) {
1256       TopExp_Explorer exp ;
1257       exp.Init( aShell, TopAbs_FACE ) ;
1258       exp.More() ;
1259       aShellOrFace = exp.Current() ;
1260     }
1261     else {
1262       aShellOrFace = aShell ;
1263     }
1264     
1265     /* Create CORBA object */
1266     GEOM::GEOM_Shape_var result = CreateObject(aShellOrFace) ;
1267     if( CORBA::is_nil(result) ) {
1268       THROW_SALOME_CORBA_EXCEPTION("Suppress Faces aborted : null result", SALOME::BAD_PARAM);
1269     }
1270
1271     InsertInLabelOneArgument(mainShape, shape, aShellOrFace, result, myCurrentOCAFDoc) ;
1272     i++ ;
1273     listOfGeomShapes->length(i) ;
1274     listOfGeomShapes[i-1] = result ;
1275   }
1276
1277   return listOfGeomShapes._retn() ;
1278 }
1279
1280
1281
1282 //================================================================================
1283 // function : IsShapeInSequence()
1284 // purpose  : return true is aShape is in SS. The test method is 'IsSame()'
1285 //================================================================================
1286 bool GEOM_Gen_i::IsShapeInSequence(const TopTools_SequenceOfShape& SS, const TopoDS_Shape& aShape)
1287 {
1288   if( aShape.IsNull() || SS.IsEmpty() ) 
1289     return false ;  
1290   for( int i=1; i<=SS.Length(); i++) {
1291     if( SS.Value(i).IsSame(aShape) )
1292       return true ;
1293   }
1294   return false ;
1295 }
1296
1297
1298 //================================================================================
1299 // function : FreeEdgesFromMapOfFace()
1300 // purpose  : Define MS a map of all edges of faces of 'MSfaces'
1301 //          : All multiple edges are removed !
1302 //================================================================================
1303 void GEOM_Gen_i::FreeEdgesFromMapOfFace( const TopTools_MapOfShape& MSfaces,
1304                                          TopTools_MapOfShape& MS )
1305 {
1306   MS.Clear() ;
1307   TopTools_MapOfShape Multiple ;
1308   TopTools_MapIteratorOfMapOfShape it ;
1309   for( it.Initialize(MSfaces); it.More(); it.Next() ) {
1310     TopoDS_Shape aFace = it.Key() ;
1311     TopExp_Explorer exp ;
1312     for( exp.Init( aFace, TopAbs_EDGE); exp.More(); exp.Next() ) {
1313       if( !Multiple.Contains( exp.Current() ) && !MS.Add( exp.Current() ) ) {
1314         MS.Remove( exp.Current() ) ;
1315         Multiple.Add( exp.Current() ) ;
1316       }
1317     }
1318   }
1319   return ;
1320 }
1321
1322
1323 //================================================================================
1324 // function : MapRemoveSequence()
1325 // purpose  : In term of shapes ST = MS - SSRemove
1326 //          :
1327 //================================================================================
1328 void GEOM_Gen_i::MapRemoveSequence( const TopTools_MapOfShape& MS,
1329                                     const TopTools_SequenceOfShape& SSRemove,
1330                                     TopTools_SequenceOfShape& ST ) 
1331 {
1332   ST.Clear() ;
1333   TopTools_MapIteratorOfMapOfShape it ;
1334   for( it.Initialize(MS); it.More(); it.Next() ) {
1335     TopoDS_Shape aShape = it.Key() ;
1336     if( !IsShapeInSequence( SSRemove, aShape ) )
1337       ST.Append( aShape ) ;
1338   }
1339   return ;
1340 }
1341
1342
1343
1344 //================================================================================
1345 // function : SuppressHoleSubRoutine()
1346 // purpose  : Define recursively 'MSfacesSuppress' a list of faces to suppress in a hole
1347 //================================================================================
1348 void GEOM_Gen_i::SuppressHoleSubRoutine( const TopoDS_Shape& mainShape,
1349                                          const TopoDS_Face& aFace,
1350                                          const TopTools_SequenceOfShape& SSedgesOfWire, 
1351                                          const TopTools_IndexedDataMapOfShapeListOfShape& aMapEdgesFaces,
1352                                          const TopTools_MapOfShape& MSfaces,                                     
1353                                          TopTools_MapOfShape& MSfacesSuppress,
1354                                          const Standard_Boolean withEndFace,
1355                                          const TopoDS_Face& endFace,
1356                                          TopTools_MapOfShape& MSwireEndEdges )
1357   throw (SALOME::SALOME_Exception)
1358 {  
1359   TopTools_MapOfShape MS ;
1360   TopTools_SequenceOfShape SU ;
1361   FreeEdgesFromMapOfFace(MSfaces, MS) ;        /* MS = free edges of MSfaces */
1362   MapRemoveSequence(MS, SSedgesOfWire, SU) ;   /* SU = MS - SSedgesOfWire    */
1363
1364   if( SU.IsEmpty() ) {
1365     return ;
1366   }
1367
1368   /* Here SU contains new edges to find new faces to suppress                                          */
1369   /* Define the list of faces of SU edges that aren't in faces of MSfacesSuppress in order to add into */
1370   /* For each edge we have a map of all its faces : it's in 'aMapEdgesFaces'                           */
1371   TopTools_MapOfShape MSfacesTmp ;
1372   for( int v=1; v<=SU.Length(); v++ ) {
1373     TopoDS_Shape E = SU.Value(v) ;
1374     TopoDS_Shape F ;
1375     TopTools_ListOfShape LF ;
1376     int ind = aMapEdgesFaces.FindIndex(E) ;
1377     
1378     /* LF is the list of faces for an edge of SU : may be empty no matter */
1379     LF = aMapEdgesFaces.FindFromIndex(ind) ;
1380     
1381     TopTools_ListIteratorOfListOfShape it ;
1382     for( it.Initialize(LF); it.More(); it.Next() ) {
1383       F = it.Value() ;
1384       if( withEndFace == false ) {
1385         if( F.IsSame(aFace) )
1386           THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHoleSubRoutine() : hole traversing or ?", SALOME::BAD_PARAM);
1387         if( !MSfacesSuppress.Contains(F) ) {
1388           MSfacesSuppress.Add(F) ;
1389           MSfacesTmp.Add(F) ; // Dont remove the 'if' !
1390         }
1391       }
1392       else { /* withEndFace == true */
1393         if( F.IsSame(aFace) && !F.IsSame(endFace) )
1394           THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHoleSubRoutine() : hole traversing incoherent ?", SALOME::BAD_PARAM);
1395         
1396         if( F.IsSame(endFace) ) {
1397           /* We have reached endFace if selection was correct so we add  */
1398           /* edge in a map to find later the corresponding endWire (hole */
1399           MSwireEndEdges.Add(E) ;
1400         }
1401         else {
1402           if( !MSfacesSuppress.Contains(F) ) {
1403             MSfacesSuppress.Add(F) ;
1404             MSfacesTmp.Add(F) ;
1405           }
1406         }
1407       }
1408     }
1409   }
1410   /* Call recursively this routine */
1411   SuppressHoleSubRoutine( mainShape, aFace, SSedgesOfWire, aMapEdgesFaces, MSfacesTmp, MSfacesSuppress, withEndFace, endFace, MSwireEndEdges ) ; 
1412 }
1413
1414
1415
1416 //================================================================================
1417 // function : GetShapeFromIndex()
1418 // purpose  : Find 'tds' a sub shape of 'aShape' according to 'aList' that contains
1419 //          : a unique index !
1420 //          : Warning : index must be setted with the same exploration logic !
1421 //          : So 'index' is calculated with no shape doublons !
1422 //================================================================================
1423 bool GEOM_Gen_i::GetShapeFromIndex( const TopoDS_Shape& aShape,
1424                                     const TopAbs_ShapeEnum aType, 
1425                                     const int index,
1426                                     TopoDS_Shape& tds )
1427
1428 {
1429   if (aShape.IsNull() || index < 1) 
1430     return false ;
1431   /* Indices start at 1 */
1432   int j = 1 ;
1433   bool found = false ;
1434   TopExp_Explorer exp ;
1435   TopTools_MapOfShape M;
1436   for( exp.Init( aShape, aType ); exp.More(); exp.Next() ) {
1437     if( M.Add(exp.Current()) ) { /* if not a doublon : we compare */
1438       if( index == j ) {
1439         tds =  exp.Current() ;
1440         return true ;
1441       }
1442       j++ ;
1443     }
1444   }
1445   return false ;
1446 }
1447
1448
1449
1450 //================================================================================
1451 // function : SuppressHolesInFaceOrShell()  Main method.
1452 // purpose  : Suppress holes identified by wires in a single face or shell
1453 //
1454 //================================================================================
1455 GEOM::GEOM_Shape_ptr GEOM_Gen_i::SuppressHolesInFaceOrShell( GEOM::GEOM_Shape_ptr shapeFaceShell,
1456                                                        const GEOM::GEOM_Shape::ListOfSubShapeID& ListIdWires )
1457   throw (SALOME::SALOME_Exception)
1458 {
1459   GEOM::GEOM_Shape_var result;
1460
1461   if( ListIdWires.length() < 1 )
1462     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHolesInFace : no holes selected", SALOME::BAD_PARAM);
1463   
1464   const TopoDS_Shape tds = GetTopoShape(shapeFaceShell) ;
1465   if( tds.IsNull() || !IsValid(tds) )
1466     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHolesInFace() : non valid main argument", SALOME::BAD_PARAM);
1467   
1468   /* Create a map of wires/holes to suppress */
1469   TopTools_MapOfShape MapHoles ;
1470   for ( int i = 0; i < ListIdWires.length(); i++ ) {
1471     TopoDS_Shape W ;    
1472     if( !GetShapeFromIndex( tds, TopAbs_WIRE, ListIdWires[i], W ) )
1473       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHolesInFace() : bad index ?", SALOME::BAD_PARAM);
1474     MapHoles.Add( W ) ;
1475   }
1476
1477   /* Test if argument is a face or shell */
1478   bool isFace ;
1479   if( tds.ShapeType() == TopAbs_FACE )
1480     isFace = true ;
1481   else if ( tds.ShapeType() == TopAbs_SHELL )
1482     isFace = false ;
1483   else
1484     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHolesInFace() : not a face or a shell", SALOME::BAD_PARAM); 
1485
1486   /* Define two maps : all faces and faces to that will be modified */
1487   TopTools_MapOfShape MapFacesToModify ;
1488   TopTools_MapOfShape MapFacesAll ;
1489   TopExp_Explorer expF ;
1490   for( expF.Init( tds, TopAbs_FACE);  expF.More(); expF.Next() ) {
1491     TopoDS_Face F = TopoDS::Face( expF.Current() ) ;
1492     MapFacesAll.Add(F) ;
1493     TopExp_Explorer expW ;
1494     for( expW.Init( F, TopAbs_WIRE);  expW.More(); expW.Next() ) {
1495       TopoDS_Wire W = TopoDS::Wire( expW.Current() ) ;
1496       if( MapHoles.Contains(W) ) {
1497         MapFacesToModify.Add(F) ;
1498       }
1499     }
1500   }
1501
1502   /* Define faces not modified */
1503   TopTools_MapOfShape MapFacesNotModified ;
1504   TopTools_MapIteratorOfMapOfShape it ;
1505   for( it.Initialize(MapFacesAll); it.More(); it.Next() ) {
1506     TopoDS_Face FF = TopoDS::Face( it.Key() ) ;
1507     if( !MapFacesToModify.Contains(FF) )
1508       MapFacesNotModified.Add(FF) ;
1509   }
1510
1511   if( MapFacesToModify.IsEmpty() )
1512     THROW_SALOME_CORBA_EXCEPTION("Error : empty map of faces", SALOME::BAD_PARAM); 
1513   
1514   if( isFace && MapFacesToModify.Extent() != 1 )
1515     THROW_SALOME_CORBA_EXCEPTION("Incoherent", SALOME::BAD_PARAM);
1516   
1517   /* Main argument is a face */
1518   if( isFace && MapFacesToModify.Extent() == 1 ) {
1519     TopoDS_Face resultFace ;
1520     if( !RebuildFaceRemovingHoles( TopoDS::Face(tds), MapHoles, resultFace ) )
1521       THROW_SALOME_CORBA_EXCEPTION(" Problem : !RebuildFaceRemovingHoles()", SALOME::BAD_PARAM);
1522     /* Creation of CORBA object : face topology */
1523     result = CreateObject(resultFace);
1524     InsertInLabelOneArgument(tds, shapeFaceShell, resultFace, result, myCurrentOCAFDoc) ;
1525     return result ;
1526   }
1527
1528   /* Main argument is a shell : rebuild faces modified */
1529   TopTools_MapOfShape MapFacesModified ;
1530   for( it.Initialize(MapFacesToModify); it.More(); it.Next() ) {
1531     TopoDS_Face FF = TopoDS::Face( it.Key() ) ;
1532     TopoDS_Face resF ;
1533     if( !RebuildFaceRemovingHoles( FF, MapHoles, resF ) )
1534       THROW_SALOME_CORBA_EXCEPTION(" Problem shell : !RebuildFaceRemovingHoles()", SALOME::BAD_PARAM);
1535     MapFacesModified.Add(resF) ;
1536   }
1537
1538   /* Rebuild the shell with faces modified and non modified */
1539   TopoDS_Shell resultShell ;
1540   BRepPrim_Builder B;
1541   B.MakeShell(resultShell) ;
1542   TopTools_MapIteratorOfMapOfShape it1 ;
1543   for( it1.Initialize(MapFacesModified); it1.More(); it1.Next() )
1544     B.AddShellFace( resultShell,TopoDS::Face( it1.Key() ) ) ;
1545   for( it1.Initialize(MapFacesNotModified); it1.More(); it1.Next() )
1546     B.AddShellFace( resultShell,TopoDS::Face( it1.Key() ) ) ;
1547   
1548   B.CompleteShell(resultShell) ;
1549   
1550   if( resultShell.IsNull() )
1551     THROW_SALOME_CORBA_EXCEPTION("Null or not valid result Shell", SALOME::BAD_PARAM) ;
1552   
1553   /* Creation of CORBA object : shell topology */
1554   result = CreateObject(resultShell);
1555   InsertInLabelOneArgument(tds, shapeFaceShell, resultShell, result, myCurrentOCAFDoc) ;
1556   return result ;
1557 }
1558
1559
1560 //================================================================================
1561 // function : RebuildFaceRemovingHoles()
1562 // purpose  : Rebuild a face removing holes that are in 'mapHoles'.
1563 //          : NB : 'mapHoles' may content more holes than necessary
1564 //================================================================================
1565 bool GEOM_Gen_i::RebuildFaceRemovingHoles( const TopoDS_Face& aFace,
1566                                            const TopTools_MapOfShape& mapHoles,
1567                                            TopoDS_Shape& resultFace )
1568 {
1569   /* Get the outer wire of the face 'aFace' */
1570   TopoDS_Wire outW = BRepTools::OuterWire( aFace ) ;
1571   if( outW.IsNull() || !IsValid(outW) )
1572     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHolesInFace : bad outer wire of 'aFace'", SALOME::BAD_PARAM);
1573   
1574   /* Rebuild a face avoiding holes in the map 'mapHoles' */  
1575   Handle(Geom_Surface) Surface = BRep_Tool::Surface(aFace) ;
1576   TopoDS_Face F2 = BRepBuilderAPI_MakeFace( Surface, outW, true ) ;
1577   
1578   if( F2.Orientation() != aFace.Orientation() )
1579     F2.Orientation( aFace.Orientation() ) ;
1580   
1581   BRepBuilderAPI_MakeFace aBuilder( F2 ) ;
1582   bool foundAndKeepHoles = false ;
1583   TopExp_Explorer exp ;
1584   
1585   for( exp.Init( aFace, TopAbs_WIRE);  exp.More(); exp.Next() ) {
1586     TopoDS_Wire hole = TopoDS::Wire( exp.Current() ) ;
1587     if( !mapHoles.Contains(hole) && !exp.Current().IsEqual(outW) ) {
1588       aBuilder.Add( hole) ;      
1589       if( !aBuilder.IsDone() )
1590         THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHolesInFace : builder problem !", SALOME::BAD_PARAM);      
1591       
1592       resultFace = TopoDS::Face(aBuilder) ;
1593       foundAndKeepHoles = true ;
1594     }
1595   }
1596   
1597   if( !foundAndKeepHoles )
1598     resultFace = F2 ;
1599   else
1600     resultFace = TopoDS::Face(aBuilder) ;
1601   
1602   return true ;
1603 }
1604
1605
1606
1607
1608 //================================================================================
1609 // function : SuppressHole() Main method.
1610 // purpose  : Suppress an hole identified by a wire in a face of shape
1611 //          : ListIdFace contains a unique index of face in shape
1612 //          : ListIdWire contains a unique index of wire in face !!!
1613 //          : ListIdEndFace is used only when hole traverse.
1614 //================================================================================
1615 GEOM::GEOM_Shape_ptr GEOM_Gen_i::SuppressHole( GEOM::GEOM_Shape_ptr shape,
1616                                          const GEOM::GEOM_Shape::ListOfSubShapeID& ListIdFace,
1617                                          const GEOM::GEOM_Shape::ListOfSubShapeID& ListIdWire,
1618                                          const GEOM::GEOM_Shape::ListOfSubShapeID& ListIdEndFace )
1619   throw (SALOME::SALOME_Exception)
1620 {
1621   GEOM::GEOM_Shape_var result;
1622   TopoDS_Face aFace ;
1623   TopoDS_Wire aWire ;  
1624   TopoDS_Face endFace ;
1625   bool withEndFace ;
1626   TopoDS_Shape tmp ;
1627   
1628   /* Retrieve 'aShape' the initial main shape selection */
1629   const TopoDS_Shape aShape = GetTopoShape(shape);
1630   
1631   if( !IsValid(aShape) )
1632     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : non valid main shape", SALOME::BAD_PARAM);
1633   
1634   if( ListIdFace.length() != 1 || ListIdWire.length() != 1 )
1635     THROW_SALOME_CORBA_EXCEPTION("bad list", SALOME::BAD_PARAM);  
1636   
1637   /* Retrieve 'aFace' selection */
1638   if( !GetShapeFromIndex( aShape, TopAbs_FACE, ListIdFace[0], tmp ) ) {
1639     THROW_SALOME_CORBA_EXCEPTION("face not found", SALOME::BAD_PARAM);
1640   }
1641   else {
1642     aFace = TopoDS::Face(tmp) ;
1643   }
1644   if( !IsValid(aFace) )
1645     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : face shape not valid", SALOME::BAD_PARAM);  
1646   
1647    /* Retrieve 'aWire' selection : Warning : index of wire refers to the face ! */
1648   TopoDS_Shape aTmp ;
1649   if( !GetShapeFromIndex( aFace, TopAbs_WIRE, ListIdWire[0], aTmp ) ) {
1650     THROW_SALOME_CORBA_EXCEPTION("wire not found", SALOME::BAD_PARAM);
1651   }
1652   else {
1653     aWire = TopoDS::Wire(aTmp) ;
1654   }
1655   if( !IsValid(aWire) )
1656     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : bad wire" , SALOME::BAD_PARAM);
1657
1658   /* Get the outer wire of aFace */
1659   TopoDS_Wire outerW = BRepTools::OuterWire( aFace ) ;
1660   if( outerW.IsNull() || !IsValid(outerW) ) 
1661     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : bad outer wire", SALOME::BAD_PARAM);
1662   
1663   /* Test bad user selection aWire */
1664   if( aWire.IsSame(outerW) )
1665     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : outerW = aWire", SALOME::BAD_PARAM);
1666    
1667   /* Test if 'endFace' is used  as argument and seems to be a valid one          */
1668   /* NB : 'endFace' is optional and used when hole to suppress traverse 'aShape' */
1669   if( ListIdEndFace.length() == 0 ) {
1670     withEndFace = false ;
1671   }
1672   else {
1673     TopoDS_Shape aTemp ;
1674     if( !GetShapeFromIndex( aShape, TopAbs_FACE, ListIdEndFace[0], aTemp ) || tmp.IsNull() || !IsValid(aTemp) )
1675       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : non valid endFace", SALOME::BAD_PARAM);
1676
1677     /* Test if 'endFace' as at least one hole */    
1678     endFace = TopoDS::Face(aTemp) ;  
1679
1680     TopExp_Explorer fExp ;
1681     int nbWires = 0 ;
1682     for( fExp.Init(endFace, TopAbs_WIRE);  fExp.More(); fExp.Next() ) {
1683       TopoDS_Wire W = TopoDS::Wire( fExp.Current() ) ;
1684       if( !W.IsNull() && IsValid(W) )
1685         nbWires++ ;
1686     }
1687     if(nbWires > 1)
1688       withEndFace = true ; /* at least 2 wires : outer wire plus an hole or more */
1689     else
1690       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SuppressHole() : end face selection ?", SALOME::BAD_PARAM);
1691   }
1692   
1693   /* Find edges of aWire and test if degenerated */
1694   TopTools_SequenceOfShape SSedgesOfWire ;
1695   TopExp_Explorer wireExp ;  
1696   for( wireExp.Init(aWire, TopAbs_EDGE);  wireExp.More(); wireExp.Next() ) {
1697     TopoDS_Edge E = TopoDS::Edge( wireExp.Current() ) ;
1698     if( E.IsNull() || BRep_Tool::Degenerated(E) ) {
1699       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : found bad edge", SALOME::BAD_PARAM);
1700     }
1701     else {
1702       SSedgesOfWire.Append( wireExp.Current() ) ;
1703     }
1704   }
1705   if( SSedgesOfWire.Length() < 1 )
1706     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : no edge(s) for aWire", SALOME::BAD_PARAM); 
1707   
1708   /* Retrieve face ancestors of all edges of 'aWire' but avoiding 'aFace' */ 
1709   
1710   TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesFaces;
1711   TopTools_MapIteratorOfMapOfShape anIt ;
1712   TopTools_MapOfShape MFSuppress ;
1713   TopTools_MapOfShape MFSuppressTmp ;
1714   bool wireOnFace = false ;
1715  
1716   TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgesFaces) ;  
1717   for( int h=1; h<=SSedgesOfWire.Length(); h++ ) {
1718     
1719     TopoDS_Shape anEdgeOfWire = SSedgesOfWire.Value(h) ;
1720     int ind = aMapEdgesFaces.FindIndex(anEdgeOfWire) ;
1721     if(ind < 1)
1722       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : index of edge", SALOME::BAD_PARAM);
1723     
1724     TopTools_ListOfShape LF;
1725     LF = aMapEdgesFaces.FindFromIndex(ind) ; /* Contains all faces ancestors of an edge of wire */
1726     if( LF.IsEmpty() )
1727       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : no face for an edge", SALOME::BAD_PARAM);
1728     
1729     /* Filter faces avoiding 'aFace' */
1730     TopTools_ListIteratorOfListOfShape it ;
1731     for( it.Initialize(LF); it.More(); it.Next() ) {
1732       TopoDS_Face F = TopoDS::Face( it.Value() ) ;
1733       if( !F.IsSame(aFace) ) { 
1734         MFSuppressTmp.Add(F) ;
1735         MFSuppress.Add(F) ;
1736       }
1737       else {
1738         wireOnFace = true ;
1739       }
1740     }
1741   }
1742   
1743   if( !wireOnFace ) {
1744     THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : wire not on selected face", SALOME::BAD_PARAM);  
1745   }
1746
1747   /* Call routine to define faces to suppress and and optional endWire on endFace */
1748   TopTools_MapOfShape MSwireEndEdges ; /* will contain edges of final wire (hole) */
1749   SuppressHoleSubRoutine( aShape, aFace, SSedgesOfWire, aMapEdgesFaces, MFSuppressTmp, MFSuppress, withEndFace, endFace, MSwireEndEdges ) ;
1750
1751   TopoDS_Wire endWire ;
1752   if( withEndFace ) {
1753     
1754     if( MSwireEndEdges.Extent() < 1 )
1755       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : MSwireEndEdges.Extent() < 1", SALOME::BAD_PARAM);
1756
1757     if( !FindCompareWireHoleOnFace( endFace, MSwireEndEdges, endWire ) )           
1758       THROW_SALOME_CORBA_EXCEPTION("in GEOM_Gen_i::SupressHole() : no endWire found", SALOME::BAD_PARAM);    
1759    }
1760    
1761   /* Build 'resTds' : a shape containing a compound of faces */
1762   TopoDS_Shape resTds;
1763   if( !withEndFace && !BuildShapeHoleNotTraversing( aShape, aFace, aWire, MFSuppress, resTds ) )
1764     THROW_SALOME_CORBA_EXCEPTION("Rebuild result shape has aborted", SALOME::BAD_PARAM);
1765   
1766   if( withEndFace && !BuildShapeHoleTraversing( aShape, aFace, aWire, MFSuppress, endFace, endWire, resTds ) )
1767     THROW_SALOME_CORBA_EXCEPTION("Rebuild result shape has aborted (end hole)", SALOME::BAD_PARAM); 
1768   
1769   /* Reconstruction of final shape with 'resTds' : a compound of faces after hole suppressing */
1770   /* Actual limitation is : 'aShape' must not contain more than a solid or a shell !          */
1771   TopoDS_Shape finalShape ;
1772  
1773   TopExp_Explorer exp ;
1774   unsigned int nbSolid = 0 ;
1775   TopoDS_Solid aSolid ;
1776   for( exp.Init(aShape, TopAbs_SOLID); exp.More(); exp.Next() ) {
1777     aSolid = TopoDS::Solid( exp.Current() ) ;
1778     nbSolid++ ;
1779     if( nbSolid > 1 )
1780       THROW_SALOME_CORBA_EXCEPTION("Limitation : main shape contents more than one solid", SALOME::BAD_PARAM);  
1781   }
1782    
1783   unsigned int nbShell = 0 ;
1784   TopoDS_Shell aShell ;
1785   for( exp.Init(aShape, TopAbs_SHELL); exp.More(); exp.Next() ) {
1786     aShell = TopoDS::Shell( exp.Current() ) ;
1787     nbShell++ ;
1788     if( nbShell > 1 )
1789       THROW_SALOME_CORBA_EXCEPTION("Limitation : main shape contents more than one shell", SALOME::BAD_PARAM);
1790   }
1791   
1792   /* No shells and no solids : can send a compound even for a single face, see GUI ! */
1793   if( nbShell == 0 ) {
1794     finalShape = resTds ;
1795   }
1796   
1797   /* a shell */
1798   TopoDS_Shell shellResult ;
1799   if( nbShell == 1 ) {
1800     if ( !BuildShellWithFaceCompound( TopoDS::Compound(resTds), shellResult ) ) 
1801       THROW_SALOME_CORBA_EXCEPTION("Error after BuildShellWithFaceCompound()", SALOME::BAD_PARAM);
1802     finalShape = shellResult ;
1803   }
1804   
1805   /* a solid with a shell */
1806   if( nbSolid == 1 && nbShell == 1) {
1807     BRepBuilderAPI_MakeSolid B;
1808     B.Add(shellResult) ;
1809     if( !B.IsDone() )
1810       THROW_SALOME_CORBA_EXCEPTION("Error : !B.IsDone()", SALOME::BAD_PARAM);  
1811     finalShape = B.Solid() ;
1812   }
1813
1814   result = CreateObject(finalShape);
1815   InsertInLabelOneArgument(aShape, shape, finalShape, result, myCurrentOCAFDoc) ;
1816   return result ;
1817 }
1818
1819
1820
1821 //================================================================================
1822 // function : BuildShellWithFaceCompound()
1823 // purpose  : Build a shell with a compound of faces.
1824 //================================================================================
1825 bool GEOM_Gen_i::BuildShellWithFaceCompound( const TopoDS_Compound Comp,
1826                                              TopoDS_Shell& resultShell )
1827 {
1828   resultShell.Nullify() ;
1829   BRepPrim_Builder B ;
1830   B.MakeShell(resultShell) ;  
1831   TopExp_Explorer ex ;
1832   int i = 0 ;
1833   for( ex.Init( Comp, TopAbs_FACE); ex.More(); ex.Next() ) {
1834     TopoDS_Face F = TopoDS::Face( ex.Current() ) ;
1835     if( !IsValid(F) ) {
1836       return false ;
1837     }
1838     B.AddShellFace( resultShell, F ) ;
1839   }
1840   B.CompleteShell(resultShell) ;
1841   if( resultShell.IsNull() ) {
1842     return false ;
1843   }  
1844   return true ;
1845 }
1846
1847
1848 //================================================================================
1849 // function : FindCompareWireHoleOnFace()
1850 // purpose  : Try to find a wire on 'aFace' which edges are same than those
1851 //          : into 'MSwireEdges' map. 'aFoundWire' is defined and 'true' returned.
1852 //          : 'MSwireEdges' represents generally an hole an 'aFace'.
1853 //          : The outer wire of 'aFace' is avoided !
1854 //================================================================================
1855 bool GEOM_Gen_i::FindCompareWireHoleOnFace( const TopoDS_Face& F,
1856                                             const TopTools_MapOfShape& MSwireEdges,
1857                                             TopoDS_Wire& aFoundWire )
1858 {
1859   aFoundWire.Nullify() ;
1860   
1861   if( F.IsNull() )
1862     return false ;
1863   
1864   /* Get the outer wire of aFace */
1865   TopoDS_Wire outerW = BRepTools::OuterWire(F) ;
1866   if( outerW.IsNull() || !IsValid(outerW) ) {
1867     return false ;
1868   }
1869   
1870   int nbEdges = MSwireEdges.Extent() ;
1871   if( nbEdges < 1 ) {
1872     return false ;
1873   }
1874   
1875   TopExp_Explorer exp1 ;
1876   TopExp_Explorer exp2 ;
1877   for ( exp1.Init(F, TopAbs_WIRE) ; exp1.More(); exp1.Next() ) {
1878     TopoDS_Wire W = TopoDS::Wire( exp1.Current() ) ;
1879     int i = 0 ;
1880     if( !W.IsSame(outerW) ) {
1881       for ( exp2.Init( W, TopAbs_EDGE) ; exp2.More(); exp2.Next() ) {
1882         TopoDS_Edge E = TopoDS::Edge( exp2.Current() ) ;
1883         if( MSwireEdges.Contains(E) ) {
1884           i++ ;
1885           if( i == nbEdges ) {
1886             aFoundWire = W ;
1887             return true ;
1888           }
1889         }
1890       }
1891     }
1892   }
1893   return false ;
1894 }
1895
1896
1897 //================================================================================
1898 // function : BuildShapeHoleNotTraversing()
1899 // purpose  : Define 'resultTds' a reconstruction of 'aShape' after modification
1900 //          : on 'aFace' where 'aWire is removed' and suppression of faces 'MFSuppress'
1901 //          : ( Used as a sub routine of SuppressHole() )
1902 //================================================================================
1903 bool GEOM_Gen_i::BuildShapeHoleNotTraversing( const TopoDS_Shape& aShape,
1904                                               const TopoDS_Face& aFace,
1905                                               const TopoDS_Wire& aWire,
1906                                               const TopTools_MapOfShape& MFSuppress,
1907                                               TopoDS_Shape& resultTds )
1908   throw (SALOME::SALOME_Exception)
1909 {
1910   BRep_Builder B;
1911   TopExp_Explorer exp ;
1912   TopoDS_Face newFace ;
1913   
1914   resultTds.Nullify() ;
1915   TopoDS_Compound Comp ;
1916   B.MakeCompound (Comp);
1917   
1918   try  {
1919     
1920     /* Explore all faces of 'aShape' to rebuild a compound */
1921     for ( exp.Init(aShape, TopAbs_FACE) ; exp.More(); exp.Next() ) {
1922       
1923       TopoDS_Face F1 = TopoDS::Face( exp.Current() );
1924       /* Rebuild face(s) not suppressed */
1925       if( !MFSuppress.Contains(F1) ) {
1926         
1927         if( F1.IsEqual( aFace ) ) {
1928           TopTools_MapOfShape MSwire ;
1929           MSwire.Add(aWire) ;
1930           if( !RebuildFaceRemovingHoles(aFace, MSwire, newFace) ) {
1931             return false ;
1932           }
1933           B.Add( Comp, newFace ) ;
1934         }
1935         else {
1936           /* For any other face not suppressed */
1937           B.Add( Comp, F1 ) ;
1938         }
1939       }
1940     }
1941   }
1942   catch(Standard_Failure) {
1943     THROW_SALOME_CORBA_EXCEPTION("in BuildShapeHoleNotTraversing() : Exception catched", SALOME::BAD_PARAM);
1944   }  
1945   resultTds = Comp ;
1946   return true ;
1947 }
1948
1949
1950
1951 //================================================================================
1952 // function : BuildShapeHoleTraversing()
1953 // purpose  : Define 'resultTds' a reconstruction of 'aShape' after modifications.
1954 //          : On 'aFace'    'aWire    is removed'
1955 //          : On 'endFace'  'endWire' is removed.
1956 //          : Faces of 'MFSuppress' are removed.
1957 //          : ( Used as a sub routine of SuppressHole() )
1958 //================================================================================
1959 bool GEOM_Gen_i::BuildShapeHoleTraversing( const TopoDS_Shape& aShape,
1960                                            const TopoDS_Face& aFace,
1961                                            const TopoDS_Wire& aWire,
1962                                            const TopTools_MapOfShape& MFSuppress,
1963                                            const TopoDS_Face& endFace,
1964                                            const TopoDS_Wire& endWire,
1965                                            TopoDS_Shape& resultTds )
1966   throw (SALOME::SALOME_Exception)
1967 {
1968   BRep_Builder B;
1969   TopExp_Explorer exp ;
1970   TopoDS_Face newFace ;
1971
1972   resultTds.Nullify() ;
1973   TopoDS_Compound Comp ;
1974   B.MakeCompound (Comp);
1975   
1976   /* Necessary to use general method */
1977   TopTools_MapOfShape MSwire1  ;
1978   MSwire1.Add(aWire) ; 
1979   TopTools_MapOfShape MSwire2 ;
1980   MSwire2.Add(endWire) ;
1981
1982   try  {    
1983     
1984     /* Explore all faces of 'aShape' to rebuild a compound */
1985     for ( exp.Init(aShape, TopAbs_FACE) ; exp.More(); exp.Next() ) {
1986       TopoDS_Face F1 = TopoDS::Face( exp.Current() );      
1987       
1988       /* Rebuild face(s) not suppressed */
1989       if( !MFSuppress.Contains(F1) ) {
1990         
1991         /* Rebuild 'aFace' */
1992         if( F1.IsEqual( aFace ) && !F1.IsEqual( endFace ) ) {
1993           if( !RebuildFaceRemovingHoles(aFace, MSwire1, newFace) ) {
1994             return false ;
1995           }
1996           B.Add(Comp, newFace) ;
1997         }       
1998         
1999         /* Rebuild 'endFace' */
2000         if( !F1.IsEqual( aFace ) && F1.IsEqual( endFace ) ) {
2001           if( !RebuildFaceRemovingHoles(endFace, MSwire2, newFace) ) {
2002             return false ;
2003           }
2004           B.Add(Comp, newFace) ;
2005         }
2006         
2007         /* Hole in the same face : aFace = endFace */
2008         if( F1.IsEqual( aFace ) && F1.IsEqual( endFace ) ) {
2009           TopoDS_Face FF ;
2010           if( !RebuildFaceRemovingHoles(aFace, MSwire1, newFace) || !RebuildFaceRemovingHoles(newFace, MSwire2, FF) ) {
2011             return false ;
2012           }
2013           B.Add( Comp, FF ) ;
2014         }
2015         
2016         /* For any other face not suppressed */
2017         if( !F1.IsEqual(aFace) && !F1.IsEqual( endFace ) ) {      
2018           B.Add( Comp, F1 ) ;
2019         }
2020         
2021       }
2022     }
2023   }
2024   catch(Standard_Failure) {
2025     THROW_SALOME_CORBA_EXCEPTION("in BuildShapeHoleTraversing() : Exception catched", SALOME::BAD_PARAM);
2026   }  
2027   resultTds = Comp ;
2028   return true ;
2029 }
2030
2031 //=======================================================================
2032 //function : SortShapes
2033 //purpose  : 
2034 //=======================================================================
2035
2036 static void SortShapes(TopTools_ListOfShape& SL)
2037 {
2038   Standard_Integer MaxShapes = SL.Extent();
2039   TopTools_Array1OfShape  aShapes (1,MaxShapes);
2040   TColStd_Array1OfInteger OrderInd(1,MaxShapes);
2041   TColStd_Array1OfReal    MidXYZ  (1,MaxShapes); //X,Y,Z;
2042   TColStd_Array1OfReal    Length  (1,MaxShapes); //X,Y,Z;
2043   
2044   // Computing of CentreOfMass
2045   Standard_Integer Index;
2046   GProp_GProps GPr;
2047   gp_Pnt GPoint;
2048   TopTools_ListIteratorOfListOfShape it(SL);
2049   for (Index=1;  it.More();  Index++)
2050   {
2051     TopoDS_Shape S = it.Value();
2052     SL.Remove( it ); // == it.Next()
2053     aShapes(Index) = S;
2054     OrderInd.SetValue (Index, Index);
2055     if (S.ShapeType() == TopAbs_VERTEX)
2056     {
2057       GPoint = BRep_Tool::Pnt( TopoDS::Vertex( S ));
2058       Length.SetValue( Index, (Standard_Real) S.Orientation());
2059     }
2060     else
2061     {
2062       BRepGProp::LinearProperties (S, GPr);
2063       GPoint = GPr.CentreOfMass();
2064       Length.SetValue( Index, GPr.Mass() );
2065     }
2066     MidXYZ.SetValue(Index,
2067                     GPoint.X()*999 + GPoint.Y()*99 + GPoint.Z()*0.9);
2068   }
2069   // Sorting
2070   Standard_Integer aTemp;
2071   Standard_Boolean exchange, Sort = Standard_True;
2072   while (Sort)
2073   {
2074     Sort = Standard_False;
2075     for (Index=1; Index < MaxShapes; Index++)
2076     {
2077       if (MidXYZ(OrderInd(Index)) > MidXYZ(OrderInd(Index+1)))
2078         exchange = Standard_True;
2079       else if (MidXYZ(OrderInd(Index)) == MidXYZ(OrderInd(Index+1)) &&
2080                Length(OrderInd(Index)) >  Length(OrderInd(Index+1)) )
2081         exchange = Standard_True;
2082       else
2083         exchange = Standard_False;
2084       if (exchange)
2085       {
2086         aTemp = OrderInd(Index);
2087         OrderInd(Index) = OrderInd(Index+1);
2088         OrderInd(Index+1) = aTemp;
2089         Sort = Standard_True;
2090       }
2091     }
2092   }
2093   for (Index=1; Index <= MaxShapes; Index++)
2094     SL.Append( aShapes( OrderInd(Index) ));
2095 }
2096
2097 //================================================================================
2098 // function : SubShape()
2099 // purpose  : Method for GUI or TUI
2100 //================================================================================
2101
2102 GEOM::GEOM_Shape_ptr GEOM_Gen_i::SubShape(GEOM::GEOM_Shape_ptr shape,
2103                                           CORBA::Short ShapeType, 
2104                                           const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID)
2105      throw (SALOME::SALOME_Exception)
2106 {
2107   return SubShapesOne(shape, (TopAbs_ShapeEnum) ShapeType, ListOfID);
2108 }
2109
2110 //================================================================================
2111 // function : SubShapeSorted()
2112 // purpose  : Method for GUI or TUI
2113 //================================================================================
2114
2115 GEOM::GEOM_Shape_ptr GEOM_Gen_i::SubShapeSorted(GEOM::GEOM_Shape_ptr shape,
2116                                                 CORBA::Short ShapeType, 
2117                                                 const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID)
2118      throw (SALOME::SALOME_Exception)
2119 {
2120   return SubShapesOne(shape, (TopAbs_ShapeEnum) ShapeType, ListOfID, Standard_True);
2121 }
2122
2123 //================================================================================
2124 // function : SubShapesOne()
2125 // purpose  :
2126 //================================================================================
2127
2128 GEOM::GEOM_Shape_ptr GEOM_Gen_i::SubShapesOne( GEOM::GEOM_Shape_ptr shape,
2129                                               const TopAbs_ShapeEnum ShapeType, 
2130                                               const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID,
2131                                               const Standard_Boolean Sort)
2132   throw (SALOME::SALOME_Exception)
2133 {
2134   GEOM::GEOM_Shape_var result;
2135   TopoDS_Shape mainShape;
2136   TopoDS_Shape mainTopo = GetTopoShape(shape);
2137
2138   bool main = false;
2139   while ( !main ) {
2140     if ( shape->IsMainShape() ) {
2141       mainShape = GetTopoShape(shape);
2142       main = true;
2143     } else
2144       shape = GetIORFromString( shape->MainName() );
2145   }
2146   
2147   if(ListOfID.length() < 1) {
2148     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::SubShape() : bad list of shapes",
2149                                  SALOME::BAD_PARAM);
2150   }
2151   
2152   /* Create a sequence of all sub shapes */
2153   TopTools_ListOfShape listShape;
2154
2155   TopTools_MapOfShape mapShape;
2156   TopExp_Explorer exp ;
2157   for ( exp.Init( mainShape, TopAbs_ShapeEnum(ShapeType)) ; exp.More(); exp.Next() ) {
2158     if ( mapShape.Add( exp.Current() ) )
2159       listShape.Append ( exp.Current() );
2160   }
2161
2162   if (listShape.IsEmpty()) return shape;
2163
2164   if (Sort)
2165     SortShapes(listShape);
2166
2167   TopTools_SequenceOfShape SS;
2168   TopTools_ListIteratorOfListOfShape it (listShape);
2169   for (; it.More(); it.Next())
2170     SS.Append( it.Value() );
2171
2172   TopoDS_Shape SubShape;
2173   if (ListOfID.length() == 1)
2174     SubShape = SS.Value(ListOfID[0]);
2175   else
2176   {
2177     BRep_Builder B;
2178     TopoDS_Compound Comp;
2179     B.MakeCompound (Comp);
2180     unsigned int ind;
2181     for ( ind = 0; ind < ListOfID.length(); ind++ )
2182       B.Add( Comp, SS.Value(ListOfID[ind]) );
2183     SubShape = Comp;
2184   }
2185
2186   if ( !TNaming_Tool::HasLabel( myCurrentOCAFDoc->Main(), SubShape ) ) 
2187     result = CreateSubObject( SubShape, shape, ListOfID);
2188   else {
2189     int TransDef;
2190     TDF_Label Lab = TNaming_Tool::Label( myCurrentOCAFDoc->Main(), SubShape, TransDef );
2191     Handle(TDataStd_Name) Att;
2192     if ( Lab.FindAttribute( TDataStd_Name::GetID(), Att ) ) {
2193       TCollection_AsciiString nameIOR( Att->Get() );
2194       result = GEOM::GEOM_Shape::_narrow(_orb->string_to_object( nameIOR.ToCString() ));
2195       if ( strcmp( result->MainName(), _orb->object_to_string(shape) ) != 0 ) {
2196         result = CreateSubObject( SubShape, shape, ListOfID);
2197       }
2198     }
2199   }
2200
2201   return result;
2202 }
2203
2204
2205 //================================================================================
2206 // function : SubShapeAll()
2207 // purpose  : Explode a shape in all sub shapes with a type (Method for TUI or GUI)
2208 //================================================================================
2209
2210 GEOM::GEOM_Gen::ListOfGeomShapes* GEOM_Gen_i::SubShapeAll(GEOM::GEOM_Shape_ptr shape,
2211                                                           CORBA::Short ShapeType)
2212      throw (SALOME::SALOME_Exception)
2213 {
2214   return SubShapesAll(shape, (TopAbs_ShapeEnum) ShapeType);
2215 }
2216
2217 //================================================================================
2218 // function : SubShapeAllSorted()
2219 // purpose  : Explode a shape in all sub shapes with a type (Method for TUI or GUI)
2220 //================================================================================
2221
2222 GEOM::GEOM_Gen::ListOfGeomShapes* GEOM_Gen_i::SubShapeAllSorted(GEOM::GEOM_Shape_ptr shape,
2223                                                                 CORBA::Short ShapeType)
2224      throw (SALOME::SALOME_Exception)
2225 {
2226   return SubShapesAll(shape, (TopAbs_ShapeEnum) ShapeType, Standard_True);
2227 }
2228
2229 //================================================================================
2230 // function : SubShapeAllSorted()
2231 // purpose  :
2232 //================================================================================
2233
2234 GEOM::GEOM_Gen::ListOfGeomShapes* GEOM_Gen_i::SubShapesAll(GEOM::GEOM_Shape_ptr shape,
2235                                                            const TopAbs_ShapeEnum ShapeType,
2236                                                            const Standard_Boolean Sort)
2237   throw (SALOME::SALOME_Exception)
2238 {
2239   /* List of sub shapes returned */
2240   GEOM::GEOM_Gen::ListOfGeomShapes_var listOfGeomShapes = new GEOM::GEOM_Gen::ListOfGeomShapes;
2241   listOfGeomShapes->length(0) ;
2242   
2243   TopoDS_Shape mainTopo = GetTopoShape(shape);
2244   TopoDS_Shape mainShape;
2245   bool main = false;
2246   while ( !main ) {
2247     if ( shape->IsMainShape() ) {
2248       mainShape = GetTopoShape(shape);
2249       main = true;
2250     } else
2251       shape = GetIORFromString( shape->MainName() );
2252   }
2253
2254   if( mainTopo.IsNull() ) {
2255     THROW_SALOME_CORBA_EXCEPTION("In GEOM_Gen_i::SubShapeAll() : null main shape",
2256                                  SALOME::BAD_PARAM);
2257   }
2258   
2259   /* List/index : field set for any sub shape */
2260   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
2261   ListOfID->length(1) ;
2262
2263     // retrieve all subshapes
2264   TopTools_MapOfShape mapShape;
2265   TopTools_ListOfShape listShape;
2266     
2267   if (mainTopo.ShapeType()==TopAbs_COMPOUND  &&  ShapeType==TopAbs_SHAPE)
2268   {
2269     TopoDS_Iterator It(mainTopo,Standard_True,Standard_True );  
2270     for ( ; It.More(); It.Next() ) 
2271       if (mapShape.Add( It.Value() ))
2272         listShape.Append( It.Value() );
2273   }
2274   else
2275   {
2276     TopExp_Explorer exp ( mainTopo, ShapeType);
2277     for ( ; exp.More(); exp.Next() ) 
2278       if (mapShape.Add( exp.Current() ))
2279         listShape.Append( exp.Current() );
2280   }
2281
2282   if (Sort)
2283     SortShapes(listShape);
2284     
2285   /* Create all sub shapes */
2286   int index;
2287   GEOM::GEOM_Shape_var result;
2288     
2289   TopTools_ListIteratorOfListOfShape itSub (listShape);
2290   for (index = 1; itSub.More(); itSub.Next(), ++index)
2291   {
2292     const TopoDS_Shape& SubShape = itSub.Value();
2293     // check if SubShape is already in OCAFDS and ...
2294     if ( TNaming_Tool::HasLabel( myCurrentOCAFDoc->Main(), SubShape ) )
2295     {
2296       int TransDef;
2297       TDF_Label Lab = TNaming_Tool::Label( myCurrentOCAFDoc->Main(), SubShape, TransDef );
2298       Handle(TDataStd_Name) Att;
2299       if ( Lab.FindAttribute( TDataStd_Name::GetID(), Att ) )
2300       {
2301         TCollection_AsciiString nameIOR( Att->Get() );
2302         result = GEOM::GEOM_Shape::_narrow(_orb->string_to_object( nameIOR.ToCString() ));
2303         // ... it is subshape of <shape>
2304         if ( strcmp( result->MainName(), _orb->object_to_string(shape) ) == 0 ) 
2305         {
2306           listOfGeomShapes->length(index) ;
2307           listOfGeomShapes[index-1] = result ;
2308           continue;
2309         }
2310       }
2311     }
2312
2313 //      if (Sort)
2314 //        ListOfID[0] = index;
2315 //      else
2316     ListOfID[0] = GetIndexTopology( SubShape, mainShape ) ;
2317     result = CreateSubObject( SubShape, shape, ListOfID);
2318     /* Add each sub shape in the list returned */
2319     listOfGeomShapes->length(index) ;
2320     listOfGeomShapes[index-1] = result ;      
2321   }
2322
2323   return listOfGeomShapes._retn() ;
2324 }
2325
2326
2327 //=================================================================================
2328 // function : MakeBoolean()
2329 // purpose  : Boolean operation according to the type 'operation'
2330 //=================================================================================
2331 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeBoolean(GEOM::GEOM_Shape_ptr shape1,
2332                                        GEOM::GEOM_Shape_ptr shape2,
2333                                        CORBA::Long operation) 
2334   throw (SALOME::SALOME_Exception)
2335 {
2336   GEOM::GEOM_Shape_var result;
2337   TopoDS_Shape shape ;
2338   TopoDS_Shape aShape1  ;
2339   TopoDS_Shape aShape2  ;
2340   
2341   try {
2342     aShape1 = GetTopoShape(shape1) ;
2343     aShape2 = GetTopoShape(shape2) ;
2344     
2345     if( aShape1.IsNull() || aShape2.IsNull() ) {
2346       THROW_SALOME_CORBA_EXCEPTION("Boolean aborted : argument shape is null", SALOME::BAD_PARAM);
2347     }
2348     
2349     switch (operation)
2350       {
2351       case 1 :   /* Common */
2352         shape = BRepAlgoAPI_Common(aShape1, aShape2).Shape();
2353         break ;
2354       case 2 :   /* Cut */
2355         shape = BRepAlgoAPI_Cut(aShape1, aShape2).Shape();
2356         break ;
2357       case 3 :   /* Fuse */
2358         shape = BRepAlgoAPI_Fuse(aShape1, aShape2).Shape();
2359         break ;
2360       case 4 :   /* Section */
2361         shape = BRepAlgoAPI_Section(aShape1, aShape2).Shape();
2362         break ;
2363       default :
2364         MESSAGE("Boolean operation not known : " << operation ) ;
2365         return result ;
2366       }
2367   }
2368   catch(Standard_Failure) {
2369     THROW_SALOME_CORBA_EXCEPTION("Exception catched in boolean operation", SALOME::BAD_PARAM);
2370   }
2371     
2372   /* We test the validity of resulting shape */
2373   if( !IsValid(shape) ) {
2374     THROW_SALOME_CORBA_EXCEPTION("Boolean aborted : non valid shape result", SALOME::BAD_PARAM);
2375   }
2376         
2377   result = CreateObject(shape) ;
2378       
2379   /* put shape and name into geom/OCAF doc */
2380   GEOMDS_Commands GC(myCurrentOCAFDoc->Main());
2381   /* add attributs 'shape' and' name_io'r in a new label */
2382   TDF_Label Lab = GC.Generated(aShape1, shape, result->Name() );
2383   TCollection_AsciiString entry;
2384   TDF_Tool::Entry(Lab, entry);
2385   result->ShapeId( entry.ToCString() ) ;
2386       
2387   /* Create a new label */
2388   TDF_Label NewLab = Lab.NewChild();
2389   TCollection_ExtendedString Value("Arguments");
2390   TDataStd_Name::Set(NewLab,Value);
2391   
2392   TDF_Label NewLab1 = NewLab.NewChild();
2393   TDF_Label RefLab;
2394   TDF_Tool::Label(myCurrentOCAFDoc->GetData(), shape1->ShapeId(), RefLab);
2395   TDF_Reference::Set(NewLab1, RefLab);
2396   
2397   TDF_Label NewLab2 = NewLab.NewChild();
2398   TDF_Tool::Label(myCurrentOCAFDoc->GetData(), shape2->ShapeId(), RefLab);
2399   TDF_Reference::Set(NewLab2, RefLab);
2400   return result ;
2401 }
2402
2403
2404 //=================================================================================
2405 // function : MakeFuse()
2406 // purpose  : Special purpose !
2407 //=================================================================================
2408 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeFuse(GEOM::GEOM_Shape_ptr shape1,
2409                                     GEOM::GEOM_Shape_ptr shape2)
2410   throw (SALOME::SALOME_Exception)
2411 {
2412   GEOM::GEOM_Shape_var result;
2413   TopoDS_Shape aShape1 = GetTopoShape(shape1) ;
2414   TopoDS_Shape aShape2 = GetTopoShape(shape2) ;
2415   if( aShape1.IsNull() || aShape2.IsNull() ) {
2416     THROW_SALOME_CORBA_EXCEPTION("Fuse aborted : shape in argument is null", SALOME::BAD_PARAM);
2417   }
2418   TopoDS_Shape shape;
2419   try {
2420     shape  = BRepAlgoAPI_Fuse(aShape1, aShape2).Shape();
2421   }
2422   catch(Standard_Failure) {
2423     THROW_SALOME_CORBA_EXCEPTION("Exception catched in Fuse operation", SALOME::BAD_PARAM);
2424   }
2425
2426   /* We test the validity of resulting shape */
2427   if( !IsValid(shape) ) {
2428     THROW_SALOME_CORBA_EXCEPTION("Fuse aborted : non valid shape result", SALOME::BAD_PARAM);
2429   }
2430
2431   result = CreateObject(shape) ;
2432
2433   /* put shape and name into geom/OCAF doc */
2434   GEOMDS_Commands GC(myCurrentOCAFDoc->Main());
2435   /* add attributs 'shape' and' name_io'r in a new label */
2436   TDF_Label Lab = GC.Generated(aShape1, shape, result->Name() );
2437   TCollection_AsciiString entry;
2438   TDF_Tool::Entry(Lab, entry);
2439   result->ShapeId( entry.ToCString() ) ;
2440
2441   /* Create a new label */
2442   TDF_Label NewLab = Lab.NewChild();
2443   TCollection_ExtendedString Value("Arguments");
2444   TDataStd_Name::Set(NewLab,Value);
2445
2446   TDF_Label NewLab1 = NewLab.NewChild();
2447   TDF_Label RefLab;
2448   TDF_Tool::Label(myCurrentOCAFDoc->GetData(), shape1->ShapeId(), RefLab);
2449   TDF_Reference::Set(NewLab1, RefLab);
2450
2451   TDF_Label NewLab2 = NewLab.NewChild();
2452   TDF_Tool::Label(myCurrentOCAFDoc->GetData(), shape2->ShapeId(), RefLab);
2453   TDF_Reference::Set(NewLab2, RefLab);
2454   return result ;
2455 }
2456
2457
2458 //================================================================================
2459 // function : MakeAxisStruct()
2460 // purpose  : Create a structure GEOM::AxisStruct (see IDL file)
2461 //================================================================================
2462 GEOM::AxisStruct GEOM_Gen_i::MakeAxisStruct(CORBA::Double x,
2463                                       CORBA::Double y,
2464                                       CORBA::Double z,
2465                                       CORBA::Double vx,
2466                                       CORBA::Double vy,
2467                                       CORBA::Double vz) 
2468 {
2469   GEOM::AxisStruct A ;
2470   A.x  = x ;   A.y  = y ;   A.z  = z ;
2471   A.vx = vx ;  A.vy = vy ;  A.vz = vz ;
2472   return A ;
2473 }
2474
2475
2476 //================================================================================
2477 // function : MakePointStruct()
2478 // purpose  : Create a structure GEOM::PointStruct (see IDL file)
2479 //================================================================================
2480 GEOM::PointStruct GEOM_Gen_i::MakePointStruct(CORBA::Double x,
2481                                         CORBA::Double y,
2482                                         CORBA::Double z)
2483 {
2484   beginService( "GEOM_Gen_i::MakePointStruct" );
2485   GEOM::PointStruct p ;
2486   p.x = x ;   p.y = y ;   p.z = z ;
2487   endService( "GEOM_Gen_i::MakePointStruct" );
2488   return p ;
2489 }
2490
2491 //================================================================================
2492 // function : MakeDirection()
2493 // purpose  : Create a structure GEOM::DirStruct (see IDL file)
2494 //================================================================================
2495 GEOM::DirStruct GEOM_Gen_i::MakeDirection(const GEOM::PointStruct& p)
2496
2497   GEOM::DirStruct d ;
2498     d.PS.x = p.x ;  d.PS.y = p.y ;  d.PS.z = p.z ;
2499     return d ;
2500   }
2501
2502 //=================================================================================
2503 // function : MakeBox()
2504 // purpose  : Create a box topology.
2505 //=================================================================================
2506 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeBox(CORBA::Double x1,
2507                                          CORBA::Double y1,
2508                                          CORBA::Double z1,
2509                                          CORBA::Double x2,
2510                                          CORBA::Double y2,
2511                                          CORBA::Double z2) 
2512      throw (SALOME::SALOME_Exception)
2513 {
2514   gp_Pnt P1(x1,y1,z1);
2515   gp_Pnt P2(x2,y2,z2);
2516   GEOM::GEOM_Shape_var result ;
2517   TopoDS_Shape tds ;
2518   try {
2519     tds = BRepPrimAPI_MakeBox(P1,P2).Shape();
2520   }
2521   catch(Standard_Failure) {
2522     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeBox", SALOME::BAD_PARAM);
2523   }
2524
2525   if (tds.IsNull()) {
2526     THROW_SALOME_CORBA_EXCEPTION("Make Box aborted : null shape", SALOME::BAD_PARAM);
2527   } 
2528
2529   result = CreateObject(tds);
2530   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
2531   result->ShapeId(entry) ;
2532   return result;  
2533 }
2534
2535
2536 //================================================================================
2537 // function : MakeCylinder
2538 // purpose  : Create a cylinder topology
2539 //================================================================================
2540 GEOM::GEOM_Shape_ptr  GEOM_Gen_i::MakeCylinder(const GEOM::PointStruct& pstruct,
2541                                                const GEOM::DirStruct& dstruct,
2542                                                CORBA::Double radius,
2543                                                CORBA::Double height) 
2544   throw (SALOME::SALOME_Exception)
2545 {
2546   GEOM::GEOM_Shape_var result;
2547   TopoDS_Shape tds ;
2548   gp_Pnt p(pstruct.x, pstruct.y, pstruct.z) ;
2549   gp_Dir d(dstruct.PS.x, dstruct.PS.y, dstruct.PS.z) ;
2550   gp_Ax2 axis(p, d) ;
2551
2552   try {
2553     tds = BRepPrimAPI_MakeCylinder(axis, radius, height).Shape();
2554   }
2555   catch(Standard_Failure) {
2556     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeCylinder", SALOME::BAD_PARAM);
2557   }
2558   
2559   if (tds.IsNull()) {
2560     THROW_SALOME_CORBA_EXCEPTION("Make Cylinder aborted", SALOME::BAD_PARAM);
2561   } 
2562   result = CreateObject(tds);
2563   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
2564   result->ShapeId(entry);
2565   return result ;  
2566 }
2567
2568
2569
2570 //================================================================================
2571 // function : MakeSphere()
2572 // purpose  : Make a sphere topology
2573 //================================================================================
2574 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeSphere(CORBA::Double x1,
2575                                       CORBA::Double y1,
2576                                       CORBA::Double z1,
2577                                       CORBA::Double radius) 
2578   throw (SALOME::SALOME_Exception)
2579 {
2580   GEOM::GEOM_Shape_var result ;
2581   TopoDS_Shape tds ;
2582   try {
2583     tds = BRepPrimAPI_MakeSphere(gp_Pnt(x1,y1,z1), radius).Shape();
2584   }
2585   catch(Standard_Failure) {
2586     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeSphere", SALOME::BAD_PARAM);
2587   }
2588   
2589   if (tds.IsNull()) {
2590     THROW_SALOME_CORBA_EXCEPTION("Make Sphere aborted", SALOME::BAD_PARAM);
2591   } 
2592   result = CreateObject(tds) ;  
2593   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
2594   result->ShapeId(entry);
2595   return result;  
2596 }
2597
2598
2599
2600 //================================================================================
2601 // function : MakeTorus()
2602 // purpose  : Create a torus topology
2603 //================================================================================
2604 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeTorus( const GEOM::PointStruct& pstruct,
2605                                       const GEOM::DirStruct& dstruct,
2606                                       CORBA::Double major_radius,
2607                                       CORBA::Double minor_radius )
2608   throw (SALOME::SALOME_Exception)
2609 {
2610   GEOM::GEOM_Shape_var result;
2611   TopoDS_Shape tds ;
2612   gp_Pnt p(pstruct.x, pstruct.y, pstruct.z) ;
2613   gp_Dir d(dstruct.PS.x, dstruct.PS.y, dstruct.PS.z) ;
2614   gp_Ax2 axis(p, d) ;
2615
2616   try {
2617   tds = BRepPrimAPI_MakeTorus(axis, major_radius, minor_radius).Shape();
2618   }
2619   catch(Standard_Failure) {
2620     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeTorus", SALOME::BAD_PARAM);
2621   }
2622
2623   if (tds.IsNull()) {
2624     THROW_SALOME_CORBA_EXCEPTION("Make torus aborted", SALOME::BAD_PARAM);
2625   }
2626   result = CreateObject(tds);
2627   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
2628   result->ShapeId(entry);
2629   return result ;
2630 }
2631
2632
2633 //================================================================================
2634 // function : MakeCone()
2635 // purpose  : Create a cone topology
2636 //================================================================================
2637 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeCone(const GEOM::PointStruct& pstruct,
2638                                     const GEOM::DirStruct& dstruct,
2639                                     CORBA::Double radius1,
2640                                     CORBA::Double radius2,
2641                                     CORBA::Double height)
2642   throw (SALOME::SALOME_Exception)
2643 {
2644   GEOM::GEOM_Shape_var result;
2645   TopoDS_Shape tds ;
2646   gp_Pnt p(pstruct.x, pstruct.y, pstruct.z) ;
2647   gp_Dir d(dstruct.PS.x, dstruct.PS.y, dstruct.PS.z) ;
2648   gp_Ax2 axis(p, d) ;
2649   
2650   try {
2651     /* Cone doesn't work if same radius */
2652     if( fabs(radius1-radius2) <= Precision::Confusion() ) {
2653       tds = BRepPrimAPI_MakeCylinder(axis, (radius1+radius2)/2.0, height).Shape();
2654     }
2655     else {
2656       tds = BRepPrimAPI_MakeCone(axis, radius1, radius2, height).Shape();
2657     }
2658   }
2659   catch(Standard_Failure) {
2660     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeCone", SALOME::BAD_PARAM);
2661   }
2662
2663   if (tds.IsNull()) {
2664     THROW_SALOME_CORBA_EXCEPTION("Make Cone aborted", SALOME::BAD_PARAM);
2665   }
2666   result = CreateObject(tds);
2667   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
2668   result->ShapeId(entry);
2669   return result ;
2670 }
2671
2672
2673
2674 //==================================================================================
2675 // function : ImportIGES()
2676 // purpose  : Import shape from an IGES (IGS) file
2677 //          : LPN modified 7 mai 2002
2678 //==================================================================================
2679 GEOM::GEOM_Shape_ptr GEOM_Gen_i::ImportIGES(const char* filename)
2680   throw (SALOME::SALOME_Exception)
2681 {
2682   GEOM::GEOM_Shape_var result ;
2683   //VRV: OCC 4.0 migration
2684   IGESControl_Reader aReader;  
2685   //VRV: OCC 4.0 migration
2686   try {
2687     IFSelect_ReturnStatus stat = aReader.ReadFile((char*)filename);
2688     if ( stat != IFSelect_RetDone ) {
2689       THROW_SALOME_CORBA_EXCEPTION("Error in reading import file", SALOME::BAD_PARAM);    }
2690     
2691     MESSAGE("ImportIGES : all Geometry Transfer" << endl ) ;
2692     aReader.Clear();
2693     aReader.TransferRoots(false);
2694
2695     MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes() << endl );    
2696     TopoDS_Shape shape = aReader.OneShape();
2697
2698     if ( !shape.IsNull() ) {      
2699       /* Final CORBA object creation */
2700       result = CreateObject(shape) ;
2701       const char *entry = InsertInLabel( shape, result->Name(), myCurrentOCAFDoc ) ;
2702       result->ShapeId(entry);
2703       return result ;
2704     }
2705   }
2706   catch(Standard_Failure) {
2707     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportIGES()", SALOME::BAD_PARAM);
2708   } 
2709   
2710   THROW_SALOME_CORBA_EXCEPTION("Import IGES aborted : internal error", SALOME::BAD_PARAM);
2711 }
2712
2713
2714
2715 //==================================================================================
2716 // function : ImportSTEP()
2717 // purpose  : Import shape from an STEP (stp) file
2718 //          : 'result' is a compound of shapes if file contains more entities.
2719 //==================================================================================
2720 GEOM::GEOM_Shape_ptr GEOM_Gen_i::ImportSTEP(const char* filename)
2721   throw (SALOME::SALOME_Exception)
2722 {
2723   GEOM::GEOM_Shape_var result ;
2724   //VRV: OCC 4.0 migration
2725   STEPControl_Reader aReader;
2726   //VRV: OCC 4.0 migration
2727
2728   TopoDS_Compound compound;
2729   BRep_Builder B;
2730   B.MakeCompound( compound );
2731   
2732   try {
2733     IFSelect_ReturnStatus status = aReader.ReadFile((char*)filename);
2734     
2735     if (status == IFSelect_RetDone) {
2736       Standard_Boolean failsonly = Standard_False ;
2737       aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
2738       /* Root transfers */
2739       Standard_Integer nbr = aReader.NbRootsForTransfer();
2740       aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
2741       
2742       for ( Standard_Integer n=1; n <= nbr; n++) {
2743         
2744         Standard_Boolean ok = aReader.TransferRoot(n);
2745         /* Collecting resulting entities */
2746         Standard_Integer nbs = aReader.NbShapes();
2747         if (nbs == 0)
2748           THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;      
2749         
2750         for ( Standard_Integer i=1; i<=nbs; i++ ) {       
2751           TopoDS_Shape aShape = aReader.Shape(i);
2752           if ( aShape.IsNull() )
2753             THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;   
2754           
2755           /* For a single entity */
2756           if(nbr == 1 && nbs == 1) {      
2757             result = CreateObject(aShape) ;
2758             const char *entry = InsertInLabel(aShape, result->Name(), myCurrentOCAFDoc) ;
2759             result->ShapeId(entry);
2760             return result ;
2761           }
2762           else {
2763             B.Add( compound, aShape ) ;
2764           }
2765         }
2766       }
2767
2768      TopoDS_Shape tds = compound ; 
2769      result = CreateObject(tds) ;
2770      if( CORBA::is_nil(result) )
2771        THROW_SALOME_CORBA_EXCEPTION("Translation aborted : null result", SALOME::BAD_PARAM);
2772      const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
2773      result->ShapeId(entry);
2774      return result ;
2775     }
2776     
2777   }
2778   catch(Standard_Failure) {
2779     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
2780   }  
2781   return result ;
2782 }
2783
2784
2785 //==================================================================================
2786 // function : Partition()
2787 // purpose  :
2788 //==================================================================================
2789
2790 GEOM::GEOM_Shape_ptr
2791   GEOM_Gen_i::Partition(const GEOM::GEOM_Gen::ListOfIOR& ListShapes, 
2792                         const GEOM::GEOM_Gen::ListOfIOR& ListTools,
2793                         const GEOM::GEOM_Gen::ListOfIOR& ListKeepInside,
2794                         const GEOM::GEOM_Gen::ListOfIOR& ListRemoveInside,
2795                         const CORBA::Short               Limit)
2796 throw (SALOME::SALOME_Exception)
2797 {
2798   GEOM::GEOM_Shape_var aResult;  
2799   TopoDS_Shape tds ;
2800   //MESSAGE ("In Partition");
2801   try {
2802
2803     unsigned int ind, nbshapes = 0;
2804     nbshapes += ListShapes.length() + ListTools.length();
2805     nbshapes += ListKeepInside.length() + ListRemoveInside.length();
2806     
2807     Partition_Spliter PS;
2808     TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
2809     
2810     // add object shapes that are in ListShapes;
2811     for ( ind = 0; ind < ListShapes.length(); ind++) {
2812       
2813       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListShapes[ind] );
2814       TopoDS_Shape Shape = GetTopoShape(aShape);
2815       if(Shape.IsNull() ) {
2816         //MESSAGE ( "In Partition a shape is null" );
2817         THROW_SALOME_CORBA_EXCEPTION("In Partition a shape is null", SALOME::BAD_PARAM);
2818       }
2819       if ( ShapesMap.Add( Shape ))
2820         PS.AddShape(Shape);
2821     }
2822     
2823     // add tool shapes that are in ListTools and not in ListShapes;
2824     for (ind = 0; ind < ListTools.length(); ind++) {
2825       
2826       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListTools[ind] );
2827       TopoDS_Shape Shape = GetTopoShape(aShape);
2828       if(Shape.IsNull() ) {
2829         //MESSAGE ( "In Partition a tool shape is null" );
2830         THROW_SALOME_CORBA_EXCEPTION("In Partition a shape is null", SALOME::BAD_PARAM);
2831       }
2832       if ( !ShapesMap.Contains( Shape ) && ToolsMap.Add( Shape ))
2833         PS.AddTool(Shape);
2834     }
2835     
2836     // add shapes that are in ListKeepInside, as object shapes;
2837     for (ind = 0; ind < ListKeepInside.length(); ind++) {
2838       
2839       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListKeepInside[ind] );
2840       TopoDS_Shape Shape = GetTopoShape(aShape);
2841       if(Shape.IsNull() ) {
2842         //MESSAGE ( "In Partition a Keep Inside shape is null" );
2843         THROW_SALOME_CORBA_EXCEPTION("In Partition a shape is null", SALOME::BAD_PARAM);
2844       }
2845       if (!ToolsMap.Contains( Shape ) &&
2846           ShapesMap.Add( Shape ))
2847         PS.AddShape(Shape);
2848     }
2849     
2850     // add shapes that are in ListRemoveInside, as object shapes;
2851     for (ind = 0; ind < ListRemoveInside.length(); ind++) {
2852       
2853       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListRemoveInside[ind] );
2854       TopoDS_Shape Shape = GetTopoShape(aShape);
2855       if(Shape.IsNull() ) {
2856         //MESSAGE ( "In Partition a Remove Inside shape is null" );
2857         THROW_SALOME_CORBA_EXCEPTION("In Partition a shape is null", SALOME::BAD_PARAM);
2858       }
2859       if (!ToolsMap.Contains( Shape ) &&
2860           ShapesMap.Add( Shape ) )
2861         PS.AddShape(Shape);
2862     }
2863     
2864     PS.Compute ((TopAbs_ShapeEnum) Limit);
2865
2866     // suppress result outside of shapes in KInsideMap
2867     for (ind = 0; ind < ListKeepInside.length(); ind++) {
2868       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListKeepInside[ind] );
2869       TopoDS_Shape Shape = GetTopoShape(aShape);
2870       PS.KeepShapesInside( Shape );
2871     }
2872     
2873     // suppress result inside of shapes in RInsideMap
2874     for (ind = 0; ind < ListRemoveInside.length(); ind++) {
2875       
2876       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListRemoveInside[ind] );
2877       TopoDS_Shape Shape = GetTopoShape(aShape);
2878       PS.RemoveShapesInside( Shape );
2879     }
2880     
2881     tds = PS.Shape();
2882     
2883     if( !IsValid(tds) ) {
2884       //MESSAGE ( "In Partition: non valid shape result" );
2885       THROW_SALOME_CORBA_EXCEPTION("Partition aborted : non valid shape result", SALOME::BAD_PARAM);
2886     }
2887   }
2888   catch (Standard_Failure) {
2889     //MESSAGE ( "In Partition: Exception catched in GEOM_Gen_i::Partition()" );
2890     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::Partition", SALOME::BAD_PARAM);
2891   }
2892   
2893   aResult = CreateObject(tds) ;
2894
2895   /* add attributs S and mystr in a new label */
2896   GEOMDS_Commands GC(myCurrentOCAFDoc->Main());
2897   TDF_Label Lab = GC.AddShape(tds, aResult->Name() );
2898   TCollection_AsciiString entry;
2899   TDF_Tool::Entry(Lab,entry);
2900   aResult->ShapeId( entry.ToCString() ) ;
2901   
2902   // add arguments
2903   
2904   /* Create a new label */
2905   TDF_Label NewLab = Lab.NewChild();
2906   TCollection_ExtendedString Value("Arguments");
2907   TDataStd_Name::Set(NewLab,Value);
2908
2909   // object shapes
2910   for (unsigned int ind = 0; ind < ListShapes.length(); ind++) {
2911     TDF_Label NewLab1 = NewLab.NewChild();
2912     
2913     GEOM::GEOM_Shape_var aShape = GetIORFromString( ListShapes[ind] );
2914     Standard_CString anEntry = aShape->ShapeId();
2915     TDF_Label RefLab;
2916     TDF_Tool::Label(myCurrentOCAFDoc->GetData(), anEntry, RefLab);
2917     TDF_Reference::Set(NewLab1,RefLab);
2918   }
2919   // tool shapes
2920   for (unsigned int ind = 0; ind < ListTools.length(); ind++) {
2921     TDF_Label NewLab1 = NewLab.NewChild();
2922     
2923     GEOM::GEOM_Shape_var aShape = GetIORFromString( ListTools[ind] );
2924     Standard_CString anEntry = aShape->ShapeId();
2925     TDF_Label RefLab;
2926     TDF_Tool::Label(myCurrentOCAFDoc->GetData(), anEntry, RefLab);
2927     TDF_Reference::Set(NewLab1,RefLab);
2928   }
2929   // limit shapes 1
2930   for (unsigned int ind = 0; ind < ListKeepInside.length(); ind++) {
2931     TDF_Label NewLab1 = NewLab.NewChild();
2932     
2933     GEOM::GEOM_Shape_var aShape = GetIORFromString( ListKeepInside[ind] );
2934     Standard_CString anEntry = aShape->ShapeId();
2935     TDF_Label RefLab;
2936     TDF_Tool::Label(myCurrentOCAFDoc->GetData(), anEntry, RefLab);
2937     TDF_Reference::Set(NewLab1,RefLab);
2938   }
2939   // limit shapes 2
2940   for (unsigned int ind = 0; ind < ListRemoveInside.length(); ind++) {
2941     TDF_Label NewLab1 = NewLab.NewChild();
2942     
2943     GEOM::GEOM_Shape_var aShape = GetIORFromString( ListRemoveInside[ind] );
2944     Standard_CString anEntry = aShape->ShapeId();
2945     TDF_Label RefLab;
2946     TDF_Tool::Label(myCurrentOCAFDoc->GetData(), anEntry, RefLab);
2947     TDF_Reference::Set(NewLab1,RefLab);
2948   }
2949   
2950   return aResult;    
2951 }
2952
2953
2954
2955 //==================================================================================
2956 // function : MakeFilling()
2957 // purpose  : Create a surface from section curves filling
2958 //==================================================================================
2959 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeFilling(GEOM::GEOM_Shape_ptr myShape,
2960                                        CORBA::Short mindeg,
2961                                        CORBA::Short maxdeg,
2962                                        CORBA::Double tol3d,
2963                                        CORBA::Double tol2d,
2964                                        CORBA::Short nbiter)
2965   throw (SALOME::SALOME_Exception)
2966 {
2967   GEOM::GEOM_Shape_var result ;
2968   TopoDS_Face tds ;
2969   TopoDS_Shape aShape = GetTopoShape(myShape) ;
2970   if( aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND ) {
2971     THROW_SALOME_CORBA_EXCEPTION("MakeFilling aborted : null shape or not a compound", SALOME::BAD_PARAM);
2972   } 
2973
2974   try {
2975     /* we verify the contents of the shape */
2976     TopExp_Explorer Ex ;
2977     TopoDS_Shape Scurrent ;     
2978     Standard_Real First, Last ;
2979     Handle(Geom_Curve) C ;
2980     GeomFill_SectionGenerator Section ;
2981     
2982     Standard_Integer i = 0 ;
2983     for(Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
2984       Scurrent = Ex.Current() ;
2985       if( Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE)  {
2986         THROW_SALOME_CORBA_EXCEPTION("Initial shape doesn't contain only edges !", SALOME::BAD_PARAM);
2987       }
2988       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
2989       C = new Geom_TrimmedCurve(C, First, Last);
2990       Section.AddCurve(C) ;
2991       i++ ;
2992     }
2993     
2994     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
2995     /* We set 'tolerance' = tol3d                                            */
2996     // Section.Perform( tol3d ) ; NRI */
2997     Section.Perform( Precision::Confusion() ) ;
2998     Handle(GeomFill_Line) Line = new GeomFill_Line(i) ;
2999     
3000     GeomFill_AppSurf App(mindeg, maxdeg, tol3d, tol2d, nbiter) ; /* user parameters */
3001     App.Perform(Line, Section) ;
3002     
3003     if (!App.IsDone()) {
3004       THROW_SALOME_CORBA_EXCEPTION("Filling aborted : non valid shape result", SALOME::BAD_PARAM);
3005     }
3006     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
3007     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);    
3008     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface(App.SurfPoles(),
3009                                                               App.SurfWeights(),
3010                                                               App.SurfUKnots(),
3011                                                               App.SurfVKnots(),
3012                                                               App.SurfUMults(),
3013                                                               App.SurfVMults(),
3014                                                               App.UDegree(),
3015                                                               App.VDegree());
3016     
3017     if( GBS.IsNull() )  {
3018       THROW_SALOME_CORBA_EXCEPTION("Make Filling aborted", SALOME::BAD_PARAM);
3019     }
3020     tds  = BRepBuilderAPI_MakeFace(GBS) ;    
3021   }
3022   catch(Standard_Failure) {
3023     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeFilling", SALOME::BAD_PARAM);
3024   } 
3025   
3026   /* We test the validity of resulting shape */
3027   if( !IsValid(tds) ) {
3028     THROW_SALOME_CORBA_EXCEPTION("Filling aborted : non valid shape result", SALOME::BAD_PARAM);
3029   } 
3030   else {
3031     result = CreateObject(tds) ;
3032     InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
3033   }
3034  
3035   return result ;
3036 }
3037
3038
3039 //=================================================================================
3040 // function : MakeGlueFaces()
3041 // purpose  :
3042 //=================================================================================
3043
3044 TopoDS_Face GEOM_Gen_i::FindSameFace(const TopoDS_Shape& aShape, 
3045                                      const TopoDS_Face& F,
3046                                      double tol3d)
3047 {
3048   TopoDS_Face aFace;
3049   bool isSame = false;
3050   for (TopExp_Explorer exf(aShape,TopAbs_FACE); exf.More(); exf.Next())
3051     {
3052       //MESSAGE("--- test a face");
3053       int nbFound = 0;
3054       aFace = TopoDS::Face(exf.Current());
3055       TopTools_ListOfShape liste1;
3056       TopTools_ListOfShape liste2;
3057       for (TopExp_Explorer exp(aFace,TopAbs_VERTEX); exp.More(); exp.Next())
3058         {
3059           const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
3060           liste1.Append(V);
3061         }
3062       for (TopExp_Explorer exp(F,TopAbs_VERTEX); exp.More(); exp.Next())
3063         {
3064           const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
3065           liste2.Append(V);
3066         }
3067       isSame = false;
3068       if (liste1.Extent() == liste2.Extent())
3069         {
3070           TopTools_ListIteratorOfListOfShape it1(liste1);
3071           isSame = true;
3072           for (; it1.More(); it1.Next())
3073             {
3074               bool foundSamePoint = false;
3075               gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(it1.Value()));
3076               TopTools_ListIteratorOfListOfShape it2(liste2);
3077               for (it2; it2.More(); it2.Next())
3078                 {
3079                   gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(it2.Value()));
3080                   double d = P1.Distance(P2);
3081                   if (d < tol3d)
3082                     {
3083                       nbFound++;
3084                       //MESSAGE("    found Same Point : "<<nbFound<<" - "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
3085                       foundSamePoint = true;
3086                       break;
3087                     }
3088                 }
3089               isSame = isSame && foundSamePoint;
3090               if (! isSame) break; // a vertex does not correspond : not same face
3091             }
3092         }
3093       if (isSame)
3094         {
3095           //MESSAGE("    --- Found Same Face");
3096           break; // a face corresponding to F is found
3097         }
3098     }
3099   if (! isSame) aFace.Nullify(); // return null face
3100   return aFace;
3101 }
3102
3103 TopoDS_Edge GEOM_Gen_i::FindSameEdge(const TopoDS_Face& nf, 
3104                                      TopoDS_Edge& Eold,
3105                                      double tol3d)
3106 {
3107   TopoDS_Face newFace = TopoDS::Face(nf.Oriented(TopAbs_REVERSED));
3108   TopoDS_Vertex VFirst, VLast;
3109   TopExp::Vertices(Eold, VFirst, VLast);
3110   gp_Pnt Pf = BRep_Tool::Pnt(VFirst);
3111   gp_Pnt Pl = BRep_Tool::Pnt(VLast);
3112   TopoDS_Edge Enew;
3113   for (TopExp_Explorer ee(newFace,TopAbs_EDGE); ee.More(); ee.Next())
3114     {
3115       const TopoDS_Edge& E = TopoDS::Edge(ee.Current());
3116       TopoDS_Vertex VFn, VLn;
3117       TopExp::Vertices(E, VFn, VLn);
3118       gp_Pnt Pfn = BRep_Tool::Pnt(VFn);
3119       gp_Pnt Pln = BRep_Tool::Pnt(VLn);
3120       double dff = Pf.Distance(Pfn);
3121       double dfl = Pf.Distance(Pln);
3122       double dlf = Pl.Distance(Pfn);
3123       double dll = Pl.Distance(Pln);
3124       if ((dff < tol3d) && (dll <tol3d))
3125         {
3126           //MESSAGE("--- edge forward " <<Pf.X()<<" "<<Pf.Y()<<" "<<Pf.Z()<<" "<<Pl.X()<<" "<<Pl.Y()<<" "<<Pl.Z());
3127           Enew = TopoDS::Edge(E.Oriented(TopAbs_FORWARD));
3128           Eold = TopoDS::Edge(Eold.Oriented(TopAbs_FORWARD));
3129           break;
3130         } 
3131       if ((dfl < tol3d) && (dlf <tol3d))
3132         {
3133           //MESSAGE("--- edge reversed " <<Pf.X()<<" "<<Pf.Y()<<" "<<Pf.Z()<<" "<<Pl.X()<<" "<<Pl.Y()<<" "<<Pl.Z());
3134           Enew = TopoDS::Edge(E.Oriented(TopAbs_REVERSED));
3135           Eold = TopoDS::Edge(Eold.Oriented(TopAbs_FORWARD));
3136           break;
3137         } 
3138     }
3139   return Enew;
3140 }
3141
3142 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeGlueFaces(GEOM::GEOM_Shape_ptr myShape,
3143                                                double tol3d)
3144   throw (SALOME::SALOME_Exception)
3145 {
3146
3147   // prendre un premier shell dans la liste des shells
3148   // initialiser un compshell avec ce shell
3149   // tant qu'il reste des shells dans la liste
3150   //   chercher un shell qui a des faces en  commun avec le compshell
3151   //   creer un BRepTools_Quilt
3152   //   recenser les faces communes issues du compshell, les ajouter au quilt 
3153   //   recenser les faces restantes du shell a inclure, les ajouter au quilt
3154   //   recenser les edges en double, a remplacer
3155   //   pour chaque paire d'edge
3156   //     tester l'orientation relative des aretes
3157   //     bind dans le quilt de Eold.Forward et Enew.Forward (ou reverse)
3158   //   recuperer le nouveau shell
3159   // l'incorporer dans le compshell
3160   // appliquer BRepTools_SameParameter au compshell
3161   // (rendre parametres 2D des edges identiques aux parametres 3D)
3162
3163   GEOM::GEOM_Shape_var result ;
3164   TopoDS_Shape tds ;
3165   TopoDS_Shape aShape = GetTopoShape(myShape) ;
3166   TopoDS_Compound C;
3167   BRep_Builder bu;
3168   bu.MakeCompound(C); // empty compound;
3169   TopTools_ListOfShape shellList;
3170   for (TopExp_Explorer exp(aShape,TopAbs_SHELL); exp.More(); exp.Next())
3171     {
3172       const TopoDS_Shell& S = TopoDS::Shell(exp.Current());
3173       shellList.Append(S);
3174     }
3175   TopTools_ListIteratorOfListOfShape its(shellList);
3176   if ( ! its.More())
3177     {
3178       THROW_SALOME_CORBA_EXCEPTION("glue aborted : no shell in shape", SALOME::BAD_PARAM);
3179     }
3180   TopoDS_Shell S = TopoDS::Shell(its.Value());
3181   bu.Add(C, S); // add first shell to compound
3182   shellList.Remove(its);
3183   its.Initialize(shellList);
3184   bool shellAdded = true;
3185   while ((shellList.Extent() > 0) && shellAdded)
3186     {
3187       //MESSAGE("more shells : "<< shellList.Extent());
3188       shellAdded = false;
3189       its.Initialize(shellList);
3190       for(; its.More(); its.Next())
3191         {
3192           //MESSAGE("one more shell to try");
3193           TopTools_ListOfShape newFaces; // common faces from new compound
3194           TopTools_ListOfShape oldFaces; // common faces from shell to add
3195           TopTools_ListOfShape addFaces; // not common faces from shell to add
3196           TopTools_ListOfShape newEdges; // common edges from new compound
3197           TopTools_ListOfShape oldEdges; // common edges from face to add
3198           TopoDS_Compound CFN;
3199           TopoDS_Compound CFO;
3200           bu.MakeCompound(CFN);       // empty compound for new faces
3201           bu.MakeCompound(CFO);       // empty compound for old faces
3202           S = TopoDS::Shell(its.Value());
3203           for (TopExp_Explorer exp(S,TopAbs_FACE); exp.More(); exp.Next())
3204             {
3205               //MESSAGE("--- try to find corresponding face in new compound");
3206               TopoDS_Face F = TopoDS::Face(exp.Current());
3207               TopoDS_Face newFace = FindSameFace(C,F,tol3d);
3208               if (! newFace.IsNull())
3209                 {
3210                   //MESSAGE("--- face found");
3211                   newFaces.Append(newFace); 
3212                   bu.Add(CFN, newFace); // common faces from new compound
3213                   oldFaces.Append(F);
3214                   for (TopExp_Explorer ee(F,TopAbs_EDGE);ee.More();ee.Next())
3215                     {
3216                       //MESSAGE("--- find edge pair");
3217                       TopoDS_Edge Eold = TopoDS::Edge(ee.Current());
3218                       const TopoDS_Edge& Enew = FindSameEdge(newFace, Eold, tol3d);
3219                       oldEdges.Append(Eold);
3220                       newEdges.Append(Enew);
3221                     }
3222                 }
3223               else
3224                 {
3225                   //MESSAGE("---");
3226                   addFaces.Append(F);
3227                   bu.Add(CFO, F); // not common faces from shell to add
3228                 }
3229             }
3230           if ( !newFaces.IsEmpty())
3231             {
3232               //MESSAGE("--- some faces found ---");
3233               shellAdded = true;
3234               BRepTools_Quilt glue;
3235               glue.Add(CFN);
3236               TopTools_ListIteratorOfListOfShape ito(oldEdges);
3237               TopTools_ListIteratorOfListOfShape itn(newEdges);
3238               for (; ito.More(); ito.Next())
3239                 {                 
3240                   //MESSAGE("--- bind");
3241                   glue.Bind(TopoDS::Edge(ito.Value()), TopoDS::Edge(itn.Value()));
3242                   itn.Next();
3243                 }
3244               glue.Add(CFO);
3245               TopoDS_Compound newc = TopoDS::Compound(glue.Shells());
3246               for (TopExp_Explorer exs(newc,TopAbs_SHELL); exs.More(); exs.Next())
3247                 {
3248                   TopoDS_Shell NS = TopoDS::Shell(exs.Current());
3249                   bu.Add(C, NS);
3250                 }
3251               shellList.Remove(its);
3252               //MESSAGE("--- remove shell from list");
3253               break;
3254             }
3255         }
3256     }
3257   //MESSAGE("---" << shellList.Extent() << " " << shellAdded);
3258
3259   TopExp_Explorer  exp(C,TopAbs_SHELL);
3260   Standard_Integer ish=0;
3261   TopoDS_Compound  Res;
3262   TopoDS_Solid     Sol;
3263   BRep_Builder     B;
3264   B.MakeCompound(Res);
3265   TopoDS_Shape theShape;
3266
3267   for (; exp.More(); exp.Next())
3268     {
3269       TopoDS_Shape Sh = exp.Current();
3270       B.MakeSolid(Sol);
3271       B.Add(Sol,Sh);
3272       BRepClass3d_SolidClassifier SC(Sol);
3273       SC.PerformInfinitePoint(1.E-6); // cf. BRepFill_Confusion() - BRepFill_Evolved.cxx
3274       if (SC.State() == TopAbs_IN)
3275         {
3276           B.MakeSolid(Sol);
3277           B.Add(Sol,Sh.Reversed());
3278         }
3279       B.Add(Res,Sol);
3280       ish++;
3281     }
3282   if (ish == 1) { theShape = Sol;}
3283   else          { theShape = Res;}
3284
3285   BRepLib::SameParameter(theShape, 1.E-5, Standard_True);
3286   tds = theShape;
3287   result = CreateObject(tds);    
3288   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
3289   //MESSAGE("---");
3290   return result;
3291 }
3292
3293 //=================================================================================
3294 // function : MakeSewing()
3295 // purpose  :
3296 //=================================================================================
3297 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeSewing( const GEOM::GEOM_Gen::ListOfIOR& ListShapes,
3298                                        CORBA::Double precision )
3299   throw (SALOME::SALOME_Exception)
3300 {
3301   GEOM::GEOM_Shape_var result ;
3302   TopoDS_Shape tds ;
3303   BRepOffsetAPI_Sewing aMethod ;
3304
3305   try {
3306     /* default OCC is 1.0e-06 */
3307     aMethod.Init(precision, Standard_False);  
3308     for ( unsigned int i = 0; i < ListShapes.length(); i++) {
3309       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListShapes[i] ); 
3310       TopoDS_Shape Shape = GetTopoShape(aShape) ;
3311       if( Shape.IsNull() ) {
3312         THROW_SALOME_CORBA_EXCEPTION("MakeSewing aborted : null shape during operation", SALOME::BAD_PARAM);
3313       } 
3314       aMethod.Add(Shape) ;  
3315     }  
3316     
3317     aMethod.Perform() ;
3318     tds = aMethod.SewedShape() ;
3319     if( !IsValid(tds) ) {
3320       THROW_SALOME_CORBA_EXCEPTION("Make Sewing aborted : non valid shape", SALOME::BAD_PARAM);
3321     }
3322     if( tds.IsNull() ) {
3323       THROW_SALOME_CORBA_EXCEPTION("Make Sewing aborted : null shape", SALOME::BAD_PARAM);
3324     }
3325   }
3326   catch (Standard_Failure) {
3327     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeSewing", SALOME::BAD_PARAM);
3328   }
3329   
3330   result = CreateObject(tds);
3331   InsertInLabelMoreArguments(tds, result, ListShapes, myCurrentOCAFDoc) ;
3332   return result;
3333 }
3334
3335 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeSewingShape( GEOM::GEOM_Shape_ptr aShape,
3336                                             CORBA::Double precision )
3337   throw (SALOME::SALOME_Exception)
3338 {
3339   GEOM::GEOM_Shape_var result ;
3340   TopoDS_Shape tds, S ;
3341   BRepOffsetAPI_Sewing aMethod ;
3342
3343   try {
3344     S = GetTopoShape(aShape) ;
3345     if(S.IsNull() ) {
3346       THROW_SALOME_CORBA_EXCEPTION("In Sewing a Shape is null", SALOME::BAD_PARAM);
3347     }
3348
3349     /* default OCC is 1.0e-06 */
3350     aMethod.Init(precision, Standard_False);  
3351     for ( TopExp_Explorer exp( S, TopAbs_FACE); exp.More(); exp.Next() ) {
3352       const TopoDS_Face& F = TopoDS::Face(exp.Current());
3353       aMethod.Add(F) ;  
3354     }  
3355     
3356     aMethod.Perform() ;
3357     tds = aMethod.SewedShape() ;
3358     if( !IsValid(tds) ) {
3359       THROW_SALOME_CORBA_EXCEPTION("Make Sewing aborted : non valid shape", SALOME::BAD_PARAM);
3360     }
3361   }
3362   catch (Standard_Failure) {
3363     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeSewing", SALOME::BAD_PARAM);
3364   }
3365   
3366   result = CreateObject(tds);
3367   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3368   result->ShapeId(entry) ;
3369   return result;
3370 }
3371
3372 //==================================================================================
3373 // function : OrientationChange()
3374 // purpose  : Change the orientation of a new shape
3375 //          : TopAbs_FORWARD < -- > TopAbs_REVERSED
3376 //
3377 //          : WARNING : for the moment we make a new shape !
3378 //==================================================================================
3379 GEOM::GEOM_Shape_ptr GEOM_Gen_i::OrientationChange(GEOM::GEOM_Shape_ptr aShape)
3380   throw (SALOME::SALOME_Exception)
3381 {
3382   GEOM::GEOM_Shape_var result ;  
3383   BRep_Builder aBuilder;  
3384
3385   TopoDS_Shape shape = GetTopoShape(aShape) ;
3386   if( shape.IsNull() ) {
3387     THROW_SALOME_CORBA_EXCEPTION("Shape is null", SALOME::BAD_PARAM);
3388   }
3389
3390   BRepBuilderAPI_Copy Copy(shape);
3391   if( Copy.IsDone() ) {
3392     TopoDS_Shape tds = Copy.Shape();
3393     if( tds.IsNull() ) {
3394       THROW_SALOME_CORBA_EXCEPTION("Orientation aborted : null shape", SALOME::BAD_PARAM);
3395     } 
3396   
3397     if( tds.Orientation() == TopAbs_FORWARD)
3398       tds.Orientation(TopAbs_REVERSED) ;
3399     else
3400       tds.Orientation(TopAbs_FORWARD) ;
3401     
3402     result = CreateObject(tds); 
3403     InsertInLabelOneArgument(shape, aShape, tds, result, myCurrentOCAFDoc) ;
3404   }
3405   return result ;
3406 }
3407
3408
3409 //==================================================================================
3410 // function : GetReferencedObjects()
3411 // purpose  :
3412 //==================================================================================
3413 GEOM::GEOM_Gen::ListOfIOR* GEOM_Gen_i::GetReferencedObjects(GEOM::GEOM_Shape_ptr shape)
3414 {
3415   GEOM::GEOM_Gen::ListOfIOR_var aList = new GEOM::GEOM_Gen::ListOfIOR;
3416   aList->length(0);
3417
3418   if (shape->_is_nil()) return aList._retn();
3419
3420   Standard_CString entry = shape->ShapeId();
3421   TDF_Label Lab;
3422   TDF_Tool::Label(myCurrentOCAFDoc->GetData(), entry, Lab);
3423
3424   Handle(TDataStd_Name) Att;
3425   Lab.FindAttribute(TDataStd_Name::GetID(),Att);
3426
3427   TDF_ChildIterator ChildIterator(Lab);
3428   if (ChildIterator.More()) {
3429     TDF_Label L = ChildIterator.Value();
3430     Handle(TDataStd_Name) Att;
3431     L.FindAttribute(TDataStd_Name::GetID(),Att);
3432     if (Att->Get().IsEqual(TCollection_ExtendedString("Arguments")) ) {
3433
3434       TDF_ChildIterator ChildIterator1(L);
3435       unsigned int i = 0;
3436
3437       while (ChildIterator1.More()) {
3438         TDF_Label L = ChildIterator1.Value();
3439
3440         Handle(TDF_Reference) Ref;
3441         if (L.FindAttribute(TDF_Reference::GetID(),Ref)) {
3442           i++;
3443         }
3444         ChildIterator1.Next();
3445       }
3446       aList->length(i);
3447       i = 0;
3448       TDF_ChildIterator ChildIterator2(L);
3449       while (ChildIterator2.More()) {    
3450         TDF_Label L = ChildIterator2.Value();
3451         Handle(TDF_Reference) Ref;
3452         if (L.FindAttribute(TDF_Reference::GetID(),Ref)) {
3453           TDF_Label L = Ref->Get();
3454           
3455           Handle(TDataStd_Name) Att;
3456           L.FindAttribute(TDataStd_Name::GetID(),Att);
3457           TCollection_AsciiString nameIOR (Att->Get()) ;
3458           aList[i] = strdup( nameIOR.ToCString() );
3459           i++;
3460         }
3461    
3462         ChildIterator2.Next();
3463       }
3464     }
3465   }
3466   return aList._retn();
3467 }
3468
3469 //==================================================================================
3470 // function : GetObjects()
3471 // purpose  :
3472 //==================================================================================
3473 GEOM::GEOM_Gen::ListOfIOR* GEOM_Gen_i::GetObjects(GEOM::GEOM_Shape_ptr shape)
3474 {
3475   GEOM::GEOM_Gen::ListOfIOR_var aList = new GEOM::GEOM_Gen::ListOfIOR;
3476   aList->length(0);
3477   
3478   Standard_CString entry = shape->ShapeId();
3479   TDF_Label Lab;
3480   TDF_Tool::Label(myCurrentOCAFDoc->GetData(), entry, Lab);
3481   
3482   Handle(TDataStd_Name) Att;
3483   Lab.FindAttribute(TDataStd_Name::GetID(),Att);
3484
3485   TDF_ChildIterator ChildIterator(Lab);
3486   unsigned int i = 0;
3487   while (ChildIterator.More()) {
3488     TDF_Label L = ChildIterator.Value();
3489     Handle(TDataStd_Name) Att;
3490     L.FindAttribute(TDataStd_Name::GetID(),Att);
3491
3492     if (!Att->Get().IsEqual(TCollection_ExtendedString("Arguments")) ) {
3493       i++;
3494     }
3495     ChildIterator.Next();
3496   }
3497
3498   aList->length(i);
3499   i = 0;
3500   TDF_ChildIterator ChildIterator1(Lab);
3501   while (ChildIterator1.More()) {
3502     TDF_Label L = ChildIterator1.Value();
3503     Handle(TDataStd_Name) Att;
3504     L.FindAttribute(TDataStd_Name::GetID(),Att);
3505
3506     if (!Att->Get().IsEqual(TCollection_ExtendedString("Arguments")) ) {
3507       TCollection_AsciiString nameIOR (Att->Get());
3508       aList[i] = strdup( nameIOR.ToCString() );
3509       i++;
3510     }
3511     ChildIterator1.Next();
3512   }
3513   return aList._retn();
3514 }
3515
3516
3517 //==================================================================================
3518 // function : Import
3519 // purpose  : Import shape from a BREP file
3520 //==================================================================================
3521 GEOM::GEOM_Shape_ptr GEOM_Gen_i::ImportBREP(const char* filename)
3522   throw (SALOME::SALOME_Exception)
3523 {
3524   TopoDS_Shape tds ;
3525   GEOM::GEOM_Shape_var result ;
3526   
3527   try {
3528     BRep_Builder aBuilder;  
3529     BRepTools::Read(tds, strdup(filename), aBuilder) ;    
3530     if (tds.IsNull()) {
3531       THROW_SALOME_CORBA_EXCEPTION("Import BRep aborted", SALOME::BAD_PARAM);
3532     } 
3533   }
3534   catch(Standard_Failure) {
3535     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportBREP", SALOME::BAD_PARAM);
3536   }
3537
3538   result = CreateObject(tds) ;
3539   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3540   result->ShapeId(entry);
3541   return result; 
3542 }
3543
3544
3545 //================================================================================
3546 // function : MakePlane()
3547 // purpose  : Make a plane topology (non infinite)
3548 //================================================================================
3549 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakePlane(const GEOM::PointStruct& pstruct,
3550                                      const GEOM::DirStruct& dstruct,
3551                                      CORBA::Double trimsize) 
3552   throw (SALOME::SALOME_Exception)
3553 {
3554   GEOM::GEOM_Shape_var result ;
3555   TopoDS_Shape tds ;
3556
3557   try {
3558     gp_Pnt aPoint(pstruct.x, pstruct.y, pstruct.z) ;
3559     gp_Dir aDirection(dstruct.PS.x, dstruct.PS.y, dstruct.PS.z) ;
3560     /*  we make a trimmed plane */
3561     gp_Pln gplane(aPoint, aDirection) ;    
3562     tds = BRepBuilderAPI_MakeFace(gplane, -trimsize, +trimsize, -trimsize, +trimsize) ;
3563   }
3564   catch(Standard_Failure) {
3565     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakePlane", SALOME::BAD_PARAM);
3566   }
3567
3568   if (tds.IsNull()) {
3569     THROW_SALOME_CORBA_EXCEPTION("Make Plane aborted : null shape", SALOME::BAD_PARAM);
3570    } 
3571   
3572   result = CreateObject(tds) ;
3573   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3574   result->ShapeId(entry);
3575   return result ;
3576 }
3577
3578 //=================================================================================
3579 // function : MakeVertex()
3580 // purpose  : Create a Vertex topology.
3581 //=================================================================================
3582 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeVertex(CORBA::Double x,
3583                                       CORBA::Double y,
3584                                       CORBA::Double z) 
3585   throw (SALOME::SALOME_Exception)
3586 {
3587
3588   GEOM::GEOM_Shape_var result ; 
3589   gp_Pnt P(x,y,z);
3590   TopoDS_Shape tds = BRepBuilderAPI_MakeVertex(P).Shape();
3591   if (tds.IsNull()) {
3592     THROW_SALOME_CORBA_EXCEPTION("Make Vertex/Point aborted", SALOME::BAD_PARAM);
3593   }
3594   result = CreateObject(tds) ;
3595   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3596   result->ShapeId(entry);
3597   return result ;
3598 }
3599
3600
3601 //=================================================================================
3602 // function : MakeFace()
3603 // purpose  : 
3604 //=================================================================================
3605 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeFace( GEOM::GEOM_Shape_ptr wire,
3606                                      CORBA::Boolean wantplanarface ) 
3607   throw (SALOME::SALOME_Exception) 
3608 {
3609   GEOM::GEOM_Shape_var result ;
3610   TopoDS_Shape aShape;
3611   TopoDS_Shape tds;
3612
3613   try {
3614     aShape = GetTopoShape(wire) ;
3615     if( aShape.IsNull() || aShape.ShapeType() != TopAbs_WIRE ) {
3616       THROW_SALOME_CORBA_EXCEPTION("MakeFace aborted : null or inappropriate shape", SALOME::BAD_PARAM);
3617     }
3618     TopoDS_Wire W = TopoDS::Wire(aShape) ;
3619     tds = BRepBuilderAPI_MakeFace(W, wantplanarface).Shape() ;
3620     if( !tds.IsNull() ) {
3621       result = CreateObject(tds) ;
3622       InsertInLabelOneArgument(aShape, wire, tds, result, myCurrentOCAFDoc) ;
3623     }
3624     else {
3625       THROW_SALOME_CORBA_EXCEPTION("Null result in GEOM_Gen_i::MakeFace", SALOME::BAD_PARAM);
3626     }
3627   }
3628   catch (Standard_Failure) {
3629     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeFace", SALOME::BAD_PARAM);
3630   }  
3631   return result ;
3632 }
3633
3634
3635 //================================================================================
3636 // function : MakeLine
3637 // purpose  : Make a Line topology
3638 //================================================================================
3639 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeLine(const GEOM::PointStruct& pstruct,
3640                                     const GEOM::DirStruct& dstruct)
3641   throw (SALOME::SALOME_Exception)
3642 {
3643   GEOM::GEOM_Shape_var result  ;
3644   gp_Pnt P1(pstruct.x, pstruct.y, pstruct.z);
3645   gp_Pnt P2(dstruct.PS.x, dstruct.PS.y, dstruct.PS.z) ;  
3646   TopoDS_Shape tds ;
3647   
3648   try {
3649     tds = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
3650   }
3651   catch(Standard_Failure) {
3652     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeLine", SALOME::BAD_PARAM);
3653   }
3654
3655   if ( tds.IsNull() ) {
3656     THROW_SALOME_CORBA_EXCEPTION("Make Line aborted : null shape", SALOME::BAD_PARAM);
3657   }
3658   else {
3659     result = CreateObject(tds) ;
3660     const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3661     result->ShapeId(entry);
3662   }
3663   return result ;  
3664 }
3665
3666
3667 //================================================================================
3668 // function : MakeVector()
3669 // purpose  : Make a vector
3670 //================================================================================
3671 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeVector(const GEOM::PointStruct& pstruct1,
3672                                       const GEOM::PointStruct& pstruct2)
3673   throw (SALOME::SALOME_Exception)
3674 {
3675   GEOM::GEOM_Shape_var result  ;
3676   TopoDS_Shape tds ;
3677   
3678   try {
3679     gp_Pnt P1(pstruct1.x, pstruct1.y, pstruct1.z);
3680     gp_Pnt P2(pstruct2.x, pstruct2.y, pstruct2.z) ;
3681     tds = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
3682   }
3683   catch(Standard_Failure) {
3684     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeVector", SALOME::BAD_PARAM);
3685   }
3686
3687   if ( tds.IsNull() ) {
3688     THROW_SALOME_CORBA_EXCEPTION("Make Vector aborted : null shape", SALOME::BAD_PARAM);
3689   }
3690   else {
3691     result = CreateObject(tds) ;
3692     const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3693     result->ShapeId(entry);
3694   }
3695   return result ;  
3696 }
3697
3698
3699 //================================================================================
3700 // function : MakeCircle()
3701 // purpose  : 
3702 //================================================================================
3703 GEOM::GEOM_Shape_ptr  GEOM_Gen_i::MakeCircle(const GEOM::PointStruct& pstruct,
3704                                        const GEOM::DirStruct& dstruct,
3705                                        CORBA::Double radius)
3706   throw (SALOME::SALOME_Exception) 
3707 {
3708   GEOM::GEOM_Shape_var result;
3709   TopoDS_Shape tds ;
3710
3711   try {
3712     gp_Pnt p(pstruct.x, pstruct.y, pstruct.z) ;
3713     gp_Dir d(dstruct.PS.x, dstruct.PS.y, dstruct.PS.z) ;
3714     gp_Ax2 axis(p, d) ;
3715     gp_Circ circ( axis, radius);    
3716     BRepBuilderAPI_MakeEdge MakeEdge( circ );
3717     tds = MakeEdge.Edge();
3718   }
3719   catch(Standard_Failure) {
3720     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeCircle", SALOME::BAD_PARAM);
3721   }
3722   if (tds.IsNull()) {
3723     THROW_SALOME_CORBA_EXCEPTION("Make Circle aborted", SALOME::BAD_PARAM);
3724   } 
3725   result = CreateObject(tds);
3726   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3727   result->ShapeId(entry);
3728   return result ;  
3729 }
3730
3731 //================================================================================
3732 // function : MakeArc()
3733 // purpose  : make an arc of circle from pInit to pEnd and passing on pCircle
3734 //================================================================================
3735 GEOM::GEOM_Shape_ptr  GEOM_Gen_i::MakeArc(const GEOM::PointStruct& pInit,
3736                                     const GEOM::PointStruct& pCircle,
3737                                     const GEOM::PointStruct& pEnd)
3738   throw (SALOME::SALOME_Exception) 
3739 {
3740   GEOM::GEOM_Shape_var result;
3741   try {
3742     gp_Pnt pI(pInit.x, pInit.y, pInit.z) ;
3743     gp_Pnt pC(pCircle.x, pCircle.y, pCircle.z) ;
3744     gp_Pnt pE(pEnd.x, pEnd.y, pEnd.z) ;
3745     
3746     GC_MakeArcOfCircle arc( pI, pC, pE ) ;
3747     if( !arc.IsDone() ) {
3748       THROW_SALOME_CORBA_EXCEPTION("Arc not done", SALOME::BAD_PARAM);
3749     }
3750     BRepBuilderAPI_MakeEdge MakeEdge( arc );
3751     TopoDS_Shape tds = MakeEdge.Edge();
3752     if (tds.IsNull()) {
3753       THROW_SALOME_CORBA_EXCEPTION("Null result : arc not done", SALOME::BAD_PARAM);
3754     } 
3755     else {
3756       result = CreateObject(tds);
3757       const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
3758       result->ShapeId(entry);
3759     }
3760   }
3761   catch(Standard_Failure) {
3762     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeArc", SALOME::BAD_PARAM);
3763   }
3764   return result ;
3765 }
3766
3767
3768
3769 //=================================================================================
3770 // function : MakeTranslation()
3771 // purpose  : Translate a 3D shape
3772 //=================================================================================
3773 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeTranslation( GEOM::GEOM_Shape_ptr myShape,
3774                                             CORBA::Double x,
3775                                             CORBA::Double y,
3776                                             CORBA::Double z)
3777   throw (SALOME::SALOME_Exception)
3778 {
3779   GEOM::GEOM_Shape_var result ;
3780   TopoDS_Shape aShape = GetTopoShape(myShape) ;
3781   if( aShape.IsNull() ) {
3782     THROW_SALOME_CORBA_EXCEPTION("Translation aborted : null shape", SALOME::BAD_PARAM);
3783   }
3784   gp_Vec theVector(x,y,z) ;
3785   gp_Trsf theTransformation ;
3786   theTransformation.SetTranslation(theVector) ;
3787   BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
3788   TopoDS_Shape tds = myBRepTransformation.Shape() ;
3789
3790   result = CreateObject(tds) ;
3791   if( CORBA::is_nil(result) ) {
3792     THROW_SALOME_CORBA_EXCEPTION("Translation aborted : null result", SALOME::BAD_PARAM);
3793   }
3794   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
3795   return result;
3796 }
3797
3798
3799 //=================================================================================
3800 // function : MakeMultiTranslation1D()
3801 // purpose  : Multi-Translate a 3D shape
3802 //=================================================================================
3803 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeMultiTranslation1D( GEOM::GEOM_Shape_ptr myShape,
3804                                                    const GEOM::DirStruct& dir,
3805                                                    CORBA::Double step,
3806                                                    CORBA::Short nbtimes )
3807   throw (SALOME::SALOME_Exception)
3808 {
3809   GEOM::GEOM_Shape_var result ;
3810   TopoDS_Shape tds ;
3811
3812   TopoDS_Shape aShape = GetTopoShape(myShape) ;
3813   if( aShape.IsNull() )
3814     THROW_SALOME_CORBA_EXCEPTION("MakeMultiTranslation1D aborted : null shape", SALOME::BAD_PARAM);
3815   
3816   try {
3817     int i ;
3818     double DX, DY, DZ ;
3819     gp_Trsf theTransformation ;
3820     gp_Vec myVec ;
3821     gp_Vec Vec( dir.PS.x, dir.PS.y, dir.PS.z ) ;
3822     Vec.Normalize();
3823     TopoDS_Compound compound;
3824     BRep_Builder B;
3825     B.MakeCompound( compound );
3826     
3827     for ( i = 0; i < nbtimes; i++ ) {
3828       DX = i * step * Vec.X() ;
3829       DY = i * step * Vec.Y() ;
3830       DZ = i * step * Vec.Z() ;
3831       myVec.SetCoord( DX, DY, DZ ) ;
3832       theTransformation.SetTranslation(myVec) ;
3833       BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
3834       B.Add( compound, myBRepTransformation.Shape() );
3835     }
3836     tds = compound ;
3837     result = CreateObject(tds) ;
3838   }
3839   catch (Standard_Failure) {
3840     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeMultiTranslation1D", SALOME::BAD_PARAM);
3841   }
3842     
3843   if( CORBA::is_nil(result) ) {
3844     THROW_SALOME_CORBA_EXCEPTION("MakeMultiTranslation1D aborted : null result", SALOME::BAD_PARAM);
3845   }
3846   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
3847   return result;
3848 }
3849
3850
3851 //=================================================================================
3852 // function : MakeMultiTranslation2D()
3853 // purpose  : Multi-Translate a 3D shape
3854 //=================================================================================
3855 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeMultiTranslation2D( GEOM::GEOM_Shape_ptr myShape,
3856                                                    const GEOM::DirStruct& dir1,
3857                                                    CORBA::Double step1,
3858                                                    CORBA::Short nbtimes1,
3859                                                    const GEOM::DirStruct& dir2,
3860                                                    CORBA::Double step2,
3861                                                    CORBA::Short nbtimes2 )
3862   throw (SALOME::SALOME_Exception)
3863 {
3864   GEOM::GEOM_Shape_var result ;
3865   TopoDS_Shape tds ;
3866   
3867   TopoDS_Shape aShape = GetTopoShape(myShape) ;
3868   if( aShape.IsNull() ) {
3869     THROW_SALOME_CORBA_EXCEPTION("MakeMultiTranslation2D aborted : null shape", SALOME::BAD_PARAM);
3870   }
3871   
3872   try {
3873     int i, j ;
3874     double DX, DY, DZ ;
3875     gp_Trsf theTransformation ;
3876     gp_Vec myVec ;
3877     gp_Vec Vec1( dir1.PS.x, dir1.PS.y, dir1.PS.z ) ;
3878     Vec1.Normalize();
3879     gp_Vec Vec2( dir2.PS.x, dir2.PS.y, dir2.PS.z ) ;
3880     Vec2.Normalize();
3881     TopoDS_Compound compound;
3882     BRep_Builder B;
3883     B.MakeCompound( compound );
3884     
3885     for ( i = 0; i < nbtimes1; i++ ) {
3886       for ( j = 0; j < nbtimes2; j++ ) {
3887         DX = i * step1 * Vec1.X() + j * step2 * Vec2.X() ;
3888         DY = i * step1 * Vec1.Y() + j * step2 * Vec2.Y() ;
3889         DZ = i * step1 * Vec1.Z() + j * step2 * Vec2.Z() ;
3890         myVec.SetCoord( DX, DY, DZ ) ;
3891         theTransformation.SetTranslation(myVec) ;
3892         BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
3893         B.Add( compound, myBRepTransformation.Shape() );
3894       }
3895     }
3896     tds = compound ;
3897     result = CreateObject(tds) ;
3898   }
3899   catch(Standard_Failure) {
3900     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeMultiTranslation2D", SALOME::BAD_PARAM);
3901   }
3902    
3903   if( CORBA::is_nil(result) ) {
3904     THROW_SALOME_CORBA_EXCEPTION("MakeMultiTranslation2D aborted : null result", SALOME::BAD_PARAM);
3905   }
3906   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
3907   return result;
3908 }
3909
3910
3911 //=================================================================================
3912 // function : MakeMultiRotation1D()
3913 // purpose  : Multi-Rotate a 3D shape
3914 //=================================================================================
3915 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeMultiRotation1D( GEOM::GEOM_Shape_ptr myShape,
3916                                                 const GEOM::DirStruct& dir,
3917                                                 const GEOM::PointStruct& loc,
3918                                                 CORBA::Short nbtimes)
3919   throw (SALOME::SALOME_Exception)
3920 {
3921   GEOM::GEOM_Shape_var result ;
3922   TopoDS_Shape tds ;
3923   TopoDS_Shape aShape = GetTopoShape(myShape) ;
3924   if( aShape.IsNull() ) {
3925     THROW_SALOME_CORBA_EXCEPTION("MakeMultiRotation1D aborted : null shape", SALOME::BAD_PARAM);
3926   }
3927  
3928   try {
3929
3930     int i ;
3931     gp_Pnt P(loc.x, loc.y, loc.z) ;
3932     gp_Dir D(dir.PS.x, dir.PS.y, dir.PS.z) ;
3933     gp_Ax1 AX1(P, D) ;
3934     
3935     double angle = 360.0/nbtimes ;
3936     gp_Trsf theTransformation ;
3937     TopoDS_Compound compound;
3938     BRep_Builder B;
3939     B.MakeCompound( compound );
3940     
3941     for ( i = 0; i < nbtimes; i++ ) {
3942       theTransformation.SetRotation(AX1, i*angle*PI180) ;
3943       BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
3944       B.Add( compound, myBRepTransformation.Shape() );
3945     }
3946     tds = compound ;
3947     result = CreateObject(tds) ;
3948   }
3949   catch(Standard_Failure) {
3950     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeMultiRotation1D", SALOME::BAD_PARAM);
3951   }
3952   
3953   if( CORBA::is_nil(result) ) {
3954     THROW_SALOME_CORBA_EXCEPTION("MakeMultiRotation1D aborted : null result", SALOME::BAD_PARAM);
3955   }
3956   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
3957   return result;
3958 }
3959
3960
3961 //=================================================================================
3962 // function : MakeMultiRotation2D()
3963 // purpose  : Multi-Rotate a 3D shape
3964 //=================================================================================
3965 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeMultiRotation2D( GEOM::GEOM_Shape_ptr myShape,
3966                                                 const GEOM::DirStruct& dir,
3967                                                 const GEOM::PointStruct& loc,
3968                                                 CORBA::Double ang,
3969                                                 CORBA::Short nbtimes1,
3970                                                 CORBA::Double step,
3971                                                 CORBA::Short nbtimes2 )
3972   throw (SALOME::SALOME_Exception)
3973 {
3974   GEOM::GEOM_Shape_var result ;
3975   TopoDS_Shape tds ;
3976   TopoDS_Shape aShape = GetTopoShape(myShape) ;
3977   if( aShape.IsNull() ) {
3978     THROW_SALOME_CORBA_EXCEPTION("MakeMultiRotation2D aborted : null shape", SALOME::BAD_PARAM);
3979   }
3980
3981   try {
3982    
3983     int i, j ;
3984     double DX, DY, DZ ;
3985     gp_Pnt P(loc.x, loc.y, loc.z) ;
3986     gp_Dir D(dir.PS.x, dir.PS.y, dir.PS.z) ;
3987     gp_Ax1 AX1(P, D) ;
3988     gp_Trsf theTransformation1 ;
3989     gp_Trsf theTransformation2 ;
3990     gp_Pnt P1 ;
3991     GProp_GProps System ;
3992     
3993     if ( aShape.ShapeType() == TopAbs_VERTEX) {
3994       P1 = BRep_Tool::Pnt(TopoDS::Vertex( aShape ));
3995     } 
3996     else if ( aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE ) {
3997       BRepGProp::LinearProperties(aShape, System);
3998       P1 = System.CentreOfMass() ;
3999     }
4000     else if ( aShape.ShapeType() == TopAbs_FACE || aShape.ShapeType() == TopAbs_SHELL ) {
4001       BRepGProp::SurfaceProperties(aShape, System);
4002       P1 = System.CentreOfMass() ;
4003     }
4004     else {
4005       BRepGProp::VolumeProperties(aShape, System);
4006       P1 = System.CentreOfMass() ;
4007     }
4008     
4009     Handle(Geom_Line) Line = new Geom_Line(AX1);
4010     gp_Pnt P2 = GeomAPI_ProjectPointOnCurve( P1, Line ) ;
4011     
4012     if ( P1.IsEqual(P2, Precision::Confusion() ) )
4013       THROW_SALOME_CORBA_EXCEPTION("Points are confused", SALOME::BAD_PARAM);
4014     
4015     gp_Vec Vec(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z()) ;
4016     Vec.Normalize();
4017     
4018     gp_Vec myVec ;
4019     TopoDS_Compound compound;
4020     BRep_Builder B;
4021     B.MakeCompound( compound );
4022     
4023     for ( i = 0; i < nbtimes2; i++ ) {
4024       for ( j = 0; j < nbtimes1; j++ ) {
4025         DX = i * step * Vec.X() ;
4026         DY = i * step * Vec.Y() ;
4027         DZ = i * step * Vec.Z() ;
4028         myVec.SetCoord( DX, DY, DZ ) ;
4029         theTransformation1.SetTranslation(myVec) ;
4030         theTransformation2.SetRotation(AX1, j*ang*PI180) ;
4031         BRepBuilderAPI_Transform myBRepTransformation1(aShape, theTransformation1, Standard_False) ;
4032         BRepBuilderAPI_Transform myBRepTransformation2(myBRepTransformation1.Shape(), theTransformation2, Standard_False) ;
4033         B.Add( compound, myBRepTransformation2.Shape() );
4034       }
4035     }
4036     tds = compound ;
4037     result = CreateObject(tds) ;  
4038   }
4039   catch(Standard_Failure) {
4040     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeMultiRotation2D", SALOME::BAD_PARAM);
4041   }
4042   
4043   if( CORBA::is_nil(result) ) {
4044     THROW_SALOME_CORBA_EXCEPTION("MakeMultiRotation2D aborted : null result", SALOME::BAD_PARAM);
4045   }
4046   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
4047   return result;
4048 }
4049
4050
4051 //=================================================================================
4052 // function : MakeCopy()
4053 // purpose  : Copy a 3D shape
4054 //=================================================================================
4055 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeCopy( GEOM::GEOM_Shape_ptr Shape)
4056   throw (SALOME::SALOME_Exception) 
4057 {
4058   GEOM::GEOM_Shape_var result ;
4059   TopoDS_Shape tds ;
4060   TopoDS_Shape aShape = GetTopoShape(Shape) ;
4061   if(aShape.IsNull() ) {
4062     THROW_SALOME_CORBA_EXCEPTION("Copy aborted : null shape during operation", SALOME::BAD_PARAM);
4063   }  
4064   BRepBuilderAPI_Copy Copy(aShape);
4065   if( Copy.IsDone() ) {   
4066     tds = Copy.Shape();
4067     result = CreateObject(tds);    
4068     InsertInLabelOneArgument(aShape, Shape, tds, result, myCurrentOCAFDoc) ;
4069   }
4070
4071   return result;
4072 }
4073
4074
4075 //=================================================================================
4076 // function : MakeMirrorByPlane()
4077 // purpose  : build a shape by symmetry of 'myShape' with 'shapePlane' in argument
4078 //=================================================================================
4079 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeMirrorByPlane(GEOM::GEOM_Shape_ptr myShape,
4080                                              GEOM::GEOM_Shape_ptr shapePlane) 
4081   throw (SALOME::SALOME_Exception)
4082 {
4083   GEOM::GEOM_Shape_var result ; 
4084   TopoDS_Shape tds ;
4085   TopoDS_Shape aShape      = GetTopoShape(myShape) ;
4086   TopoDS_Shape aShapePlane = GetTopoShape(shapePlane) ;
4087   if( aShape.IsNull()  || aShapePlane.IsNull() ) {
4088     THROW_SALOME_CORBA_EXCEPTION("Mirror aborted : null shape argument", SALOME::BAD_PARAM);
4089   }  
4090   
4091   try {
4092     Handle(Geom_Surface) surf = BRep_Tool::Surface(TopoDS::Face(aShapePlane)) ; 
4093     Handle(Geom_Plane) myPlane = Handle(Geom_Plane)::DownCast(surf) ;
4094     const gp_Ax3 pos = myPlane->Position() ;
4095     const gp_Pnt loc = pos.Location() ;  /* location of the plane */
4096     const gp_Dir dir = pos.Direction() ; /* Main direction of the plane (Z axis) */  
4097     
4098     /* plane used for mirroring */
4099     gp_Ax2 pln(loc, dir) ;
4100     gp_Trsf theTransformation ;
4101     theTransformation.SetMirror(pln) ;
4102     BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
4103
4104     tds = myBRepTransformation.Shape() ;
4105     if(tds.IsNull() ) {
4106       THROW_SALOME_CORBA_EXCEPTION("Mirror aborted", SALOME::BAD_PARAM);
4107     }
4108   }
4109   catch(Standard_Failure) {
4110     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeMirrorByPlane", SALOME::BAD_PARAM);    
4111   }
4112   
4113   result = CreateObject(tds) ;
4114
4115   /* Insert arguments in ocaf */
4116   GEOM::GEOM_Gen::ListOfIOR_var ListShapes = new GEOM::GEOM_Gen::ListOfIOR;
4117   ListShapes->length(2);
4118   ListShapes[0] = GetStringFromIOR(GEOM::GEOM_Shape::_duplicate(myShape)) ;
4119   ListShapes[1] = GetStringFromIOR(GEOM::GEOM_Shape::_duplicate(shapePlane)) ;
4120   InsertInLabelMoreArguments(tds, result, ListShapes, myCurrentOCAFDoc) ;
4121   return result ;
4122 }
4123
4124
4125
4126 //=================================================================================
4127 // function : MakeRotation()
4128 // purpose  : Rotation of a 3D shape around an axis
4129 //=================================================================================
4130 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeRotation( GEOM::GEOM_Shape_ptr myShape,
4131                                          const GEOM::AxisStruct& axis,
4132                                          CORBA::Double angle)
4133   throw (SALOME::SALOME_Exception)
4134 {
4135   GEOM::GEOM_Shape_var result ;
4136   TopoDS_Shape tds ;
4137   TopoDS_Shape aShape = GetTopoShape(myShape) ;
4138   if( aShape.IsNull() ) {
4139     THROW_SALOME_CORBA_EXCEPTION("Rotation aborted : null shape during operation", SALOME::BAD_PARAM);
4140   }
4141   
4142   try {
4143     gp_Pnt P(axis.x, axis.y, axis.z) ;
4144     gp_Dir D(axis.vx, axis.vy, axis.vz) ;
4145     gp_Ax1 AX(P, D) ;
4146     
4147     gp_Trsf theTransformation ;
4148     theTransformation.SetRotation(AX, angle) ;
4149     BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
4150     tds = myBRepTransformation.Shape() ;
4151   }
4152   catch(Standard_Failure) {
4153     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeRotation", SALOME::BAD_PARAM);
4154   }
4155   
4156   if ( !tds.IsNull() ) {
4157     result = CreateObject(tds) ;
4158     InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
4159   }
4160   return result ;
4161 }
4162
4163
4164 //=================================================================================
4165 // function : MakeScaleTransform()
4166 // purpose  : Make a shape multipling another by a scale factor
4167 //=================================================================================
4168 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeScaleTransform(GEOM::GEOM_Shape_ptr myShape,
4169                                               const GEOM::PointStruct& theCenterOfScale,
4170                                               CORBA::Double factor)
4171   throw (SALOME::SALOME_Exception)
4172 {
4173   GEOM::GEOM_Shape_var result ;
4174   TopoDS_Shape tds ;
4175   TopoDS_Shape aShape = GetTopoShape(myShape) ;
4176   if( aShape.IsNull() ) {
4177     THROW_SALOME_CORBA_EXCEPTION("Scale aborted : null shape during operation", SALOME::BAD_PARAM);
4178   }
4179   
4180   try {
4181     gp_Pnt Pcenter(theCenterOfScale.x, theCenterOfScale.y, theCenterOfScale.z) ;
4182     gp_Trsf theTransformation ;  
4183     theTransformation.SetScale(Pcenter, factor) ;
4184     BRepBuilderAPI_Transform myBRepTransformation(aShape, theTransformation, Standard_False) ;
4185     tds = myBRepTransformation.Shape() ;
4186   }
4187   catch(Standard_Failure) {
4188     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeScaleTransform", SALOME::BAD_PARAM);
4189   }
4190   
4191   if ( !tds.IsNull() ) {
4192     result = CreateObject(tds) ; 
4193     InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
4194   }
4195   return result ;
4196 }
4197
4198
4199 //=================================================================================
4200 // function : MakeCompound()
4201 // purpose  : Make a compound from a list containing one or more shapes
4202 //=================================================================================
4203 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeCompound( const GEOM::GEOM_Gen::ListOfIOR& ListShapes )
4204   throw (SALOME::SALOME_Exception)
4205 {
4206   GEOM::GEOM_Shape_var result ;
4207   TopoDS_Compound C;
4208   BRep_Builder aBuilder;
4209   aBuilder.MakeCompound(C) ;
4210
4211   for ( unsigned int i = 0; i < ListShapes.length(); i++) {
4212     GEOM::GEOM_Shape_var aShape = GetIORFromString( ListShapes[i] );    
4213     TopoDS_Shape Shape = GetTopoShape(aShape) ;
4214     if( Shape.IsNull() ) {
4215        THROW_SALOME_CORBA_EXCEPTION("Compound aborted : null shape during operation", SALOME::BAD_PARAM);
4216     }
4217     aBuilder.Add(C, Shape) ;
4218   }
4219   
4220   if ( C.IsNull() ) {
4221     THROW_SALOME_CORBA_EXCEPTION("Null result : Compound operation aborted", SALOME::BAD_PARAM);
4222   }
4223   else {
4224     result = CreateObject(C) ;
4225     InsertInLabelMoreArguments(C, result, ListShapes, myCurrentOCAFDoc) ;
4226   }
4227   return result;
4228 }
4229
4230
4231
4232 //================================================================================
4233 // function : MakeEdge()
4234 // purpose  : Make a linear edge with 2 points
4235 //================================================================================
4236 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeEdge(const GEOM::PointStruct& pstruct1,
4237                                     const GEOM::PointStruct& pstruct2)
4238   throw (SALOME::SALOME_Exception)
4239 {
4240   GEOM::GEOM_Shape_var result  ;
4241   TopoDS_Shape tds ;
4242   
4243   try {
4244     gp_Pnt P1(pstruct1.x, pstruct1.y, pstruct1.z);
4245     gp_Pnt P2(pstruct2.x, pstruct2.y, pstruct2.z) ;  
4246     tds = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
4247     if ( tds.IsNull() )
4248       THROW_SALOME_CORBA_EXCEPTION("MakeEdge aborted : null result", SALOME::BAD_PARAM);
4249   }
4250   catch (Standard_Failure) {
4251     THROW_SALOME_CORBA_EXCEPTION("Exception catched in MakeEdge", SALOME::BAD_PARAM);
4252   }
4253   
4254   result = CreateObject(tds) ;
4255   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
4256   result->ShapeId(entry);
4257   return result ;  
4258 }
4259
4260
4261
4262 //=================================================================================
4263 // function : MakeWire()
4264 // purpose  : Make a wire from a list containing one or more edges or wires that can
4265 // be connected
4266 //=================================================================================
4267 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeWire( const GEOM::GEOM_Gen::ListOfIOR& ListShapes )
4268   throw (SALOME::SALOME_Exception)
4269 {
4270   GEOM::GEOM_Shape_var result ;
4271   BRepBuilderAPI_MakeWire MW ;
4272   TopoDS_Shape tds, Shape ; 
4273   
4274   try {
4275     for ( unsigned int i = 0; i < ListShapes.length(); i++) {
4276       GEOM::GEOM_Shape_var aShape = GetIORFromString( ListShapes[i] );    
4277       Shape = GetTopoShape(aShape) ;
4278       if( Shape.IsNull() ) {
4279         THROW_SALOME_CORBA_EXCEPTION("MakeWire aborted : null shape during operation", SALOME::BAD_PARAM);
4280       }
4281       if( Shape.ShapeType() == TopAbs_EDGE )
4282         MW.Add( TopoDS::Edge(Shape) ) ;
4283       if (Shape.ShapeType() == TopAbs_WIRE ) 
4284         MW.Add( TopoDS::Wire(Shape) ) ;
4285     }    
4286     tds = MW  ;
4287
4288   }
4289   catch(Standard_Failure) {
4290     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeWire", SALOME::BAD_PARAM);
4291   }
4292   
4293   if( tds.IsNull() ) {
4294     THROW_SALOME_CORBA_EXCEPTION("Make Wire operation aborted : null result", SALOME::BAD_PARAM);
4295   }
4296   else {
4297     result = CreateObject(tds) ;
4298     InsertInLabelMoreArguments(tds, result, ListShapes, myCurrentOCAFDoc) ;   
4299   }
4300   return result;
4301 }
4302
4303
4304
4305 //=================================================================================
4306 // function : MakeRevolution()
4307 // purpose  : 
4308 //=================================================================================
4309 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeRevolution(GEOM::GEOM_Shape_ptr myShape,
4310                                           const GEOM::AxisStruct& axis,
4311                                           double angle)
4312   throw (SALOME::SALOME_Exception)
4313
4314   GEOM::GEOM_Shape_var result ;
4315   TopoDS_Shape tds ;
4316   TopoDS_Shape aShape = GetTopoShape(myShape) ;
4317   if( aShape.IsNull() ) {
4318     THROW_SALOME_CORBA_EXCEPTION("Revolution aborted : null shape", SALOME::BAD_PARAM);
4319   }
4320   try {
4321     gp_Pnt P(axis.x, axis.y, axis.z) ;
4322     gp_Dir D(axis.vx, axis.vy, axis.vz);
4323     gp_Ax1 AX(P,D);
4324     tds = BRepPrimAPI_MakeRevol(aShape, AX, angle);
4325   }
4326   catch(Standard_Failure) {
4327     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeRevolution", SALOME::BAD_PARAM);
4328   }
4329   
4330   if( tds.IsNull() ) {
4331     THROW_SALOME_CORBA_EXCEPTION("Revolution aborted", SALOME::BAD_PARAM);
4332   }
4333   result = CreateObject(tds) ;
4334   InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
4335   return result ;     
4336 }
4337
4338
4339 //=================================================================================
4340 // function : MakePipe()
4341 // purpose  : Create a shape by sweeping a baseShape along a pathShape
4342 //=================================================================================
4343 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakePipe( GEOM::GEOM_Shape_ptr pathShape,
4344                                      GEOM::GEOM_Shape_ptr baseShape )
4345   throw (SALOME::SALOME_Exception)
4346 {
4347   GEOM::GEOM_Shape_var result ;
4348   TopoDS_Shape tds ;
4349   TopoDS_Wire aWire ;
4350   TopoDS_Shape pathTds = GetTopoShape(pathShape) ;
4351   TopoDS_Shape baseTds = GetTopoShape(baseShape) ;
4352
4353   if( baseTds.IsNull() || pathTds.IsNull() ) {
4354     THROW_SALOME_CORBA_EXCEPTION("MakePipe aborted : null shape argument", SALOME::BAD_PARAM);
4355   }
4356
4357   if( pathTds.ShapeType() == TopAbs_WIRE ) {
4358     aWire = TopoDS::Wire(pathTds) ;
4359   }
4360   else {
4361     if ( pathTds.ShapeType() == TopAbs_EDGE ) {
4362       TopoDS_Edge aEdge = TopoDS::Edge(pathTds) ;
4363       aWire = BRepBuilderAPI_MakeWire(aEdge);
4364     }
4365     else {
4366       THROW_SALOME_CORBA_EXCEPTION("MakePipe aborted : bad shape type", SALOME::BAD_PARAM);
4367     }
4368   }
4369
4370   try {
4371     tds = BRepOffsetAPI_MakePipe(aWire, baseTds) ;
4372   }
4373   catch(Standard_Failure) {
4374     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakePipe", SALOME::BAD_PARAM);
4375   }
4376   
4377   if (  !IsValid(tds) ) {
4378     THROW_SALOME_CORBA_EXCEPTION("MakePipe aborted : non valid shape result", SALOME::BAD_PARAM);
4379   }
4380   else {
4381     result = CreateObject(tds) ;
4382
4383     /* Insert arguments in ocaf */
4384     GEOM::GEOM_Gen::ListOfIOR_var ListShapes = new GEOM::GEOM_Gen::ListOfIOR;
4385     ListShapes->length(2);
4386     ListShapes[0] = GetStringFromIOR(GEOM::GEOM_Shape::_duplicate(pathShape)) ;
4387     ListShapes[1] = GetStringFromIOR(GEOM::GEOM_Shape::_duplicate(baseShape)) ;    
4388     InsertInLabelMoreArguments(tds, result, ListShapes, myCurrentOCAFDoc) ;
4389   }
4390   return result ;
4391 }
4392
4393
4394 //=================================================================================
4395 // function : MakePrism()
4396 // purpose  : uses myShape as base and the vector P1 to P2
4397 //=================================================================================
4398 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakePrism( GEOM::GEOM_Shape_ptr myShape,
4399                                            const GEOM::PointStruct& P1,
4400                                            const GEOM::PointStruct& P2 )
4401   throw (SALOME::SALOME_Exception)
4402 {               
4403   GEOM::GEOM_Shape_var result ;
4404   TopoDS_Shape tds ;
4405   TopoDS_Shape aShape = GetTopoShape(myShape) ;
4406   if( aShape.IsNull() ) {
4407     THROW_SALOME_CORBA_EXCEPTION("Prism aborted : null shape operation", SALOME::BAD_PARAM);
4408   }
4409   
4410   try {
4411     gp_Vec Vector (P2.x - P1.x, P2.y - P1.y, P2.z - P1.z) ;
4412     tds = BRepPrimAPI_MakePrism(aShape, Vector, Standard_False).Shape() ;
4413   }
4414   catch(Standard_Failure) {
4415     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakePipe", SALOME::BAD_PARAM);
4416   }
4417   
4418   if ( tds.IsNull() ) {
4419     THROW_SALOME_CORBA_EXCEPTION("Prism aborted", SALOME::BAD_PARAM);
4420   }
4421   else {
4422     result = CreateObject(tds) ;     
4423     InsertInLabelOneArgument(aShape, myShape, tds, result, myCurrentOCAFDoc) ;
4424   }
4425   return result ;
4426 }
4427
4428
4429 //=================================================================================
4430 // function : MakeCDG()
4431 // purpose  : Create a CDG topology.
4432 //=================================================================================
4433 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeCDG(GEOM::GEOM_Shape_ptr aShape) 
4434   throw (SALOME::SALOME_Exception)
4435 {
4436   GEOM::GEOM_Shape_var result ;
4437   TopoDS_Shape tds ;
4438   TopoDS_Shape shape = GetTopoShape(aShape) ;
4439   GProp_GProps System;
4440   gp_Pnt myCenterMass ;
4441
4442   if( shape.IsNull() ) {
4443     THROW_SALOME_CORBA_EXCEPTION("MakeCDG aborted : null shape argument", SALOME::BAD_PARAM);
4444   }
4445   
4446   try {
4447     if ( shape.ShapeType() == TopAbs_VERTEX) {
4448       myCenterMass = BRep_Tool::Pnt(TopoDS::Vertex( shape ));
4449     } 
4450     else if ( shape.ShapeType() == TopAbs_EDGE || shape.ShapeType() == TopAbs_WIRE ) {
4451       BRepGProp::LinearProperties(shape, System);
4452       myCenterMass = System.CentreOfMass() ;
4453     }
4454     else if ( shape.ShapeType() == TopAbs_FACE || shape.ShapeType() == TopAbs_SHELL ) {
4455       BRepGProp::SurfaceProperties(shape, System);
4456       myCenterMass = System.CentreOfMass() ;
4457     }
4458     else {
4459       BRepGProp::VolumeProperties(shape, System);
4460       myCenterMass = System.CentreOfMass() ;
4461     }
4462
4463     tds = BRepBuilderAPI_MakeVertex(myCenterMass).Shape() ;
4464   }
4465   catch(Standard_Failure) {
4466     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeCDG", SALOME::BAD_PARAM);
4467   }
4468   
4469   if ( tds.IsNull() ) {
4470     THROW_SALOME_CORBA_EXCEPTION("Make CDG aborted : null shape result", SALOME::BAD_PARAM);
4471   }
4472   else {
4473     result = CreateObject(tds) ;     
4474     InsertInLabelOneArgument(shape, aShape, tds, result, myCurrentOCAFDoc) ;
4475   }
4476   return result ; 
4477 }
4478
4479
4480 //=================================================================================
4481 // function : Archimede()
4482 // purpose  :
4483 //=================================================================================
4484 GEOM::GEOM_Shape_ptr GEOM_Gen_i::Archimede(GEOM::GEOM_Shape_ptr aShape,
4485                                      CORBA::Double aWeight,
4486                                      CORBA::Double aWaterDensity,
4487                                      CORBA::Double aMeshingDeflection)
4488   throw (SALOME::SALOME_Exception)
4489 {
4490   GEOM::GEOM_Shape_var result;
4491
4492   double cste = -1;
4493   if (aWaterDensity != 0.)
4494     cste = aWeight/aWaterDensity;
4495   else
4496     THROW_SALOME_CORBA_EXCEPTION("Water density is null", SALOME::BAD_PARAM);
4497
4498   TopoDS_Shape shape = GetTopoShape(aShape) ;
4499   if( shape.IsNull() ) {
4500     THROW_SALOME_CORBA_EXCEPTION("Shape is null", SALOME::BAD_PARAM);
4501   }
4502
4503   gp_Dir direct(0.0,0.0,1.0);
4504   gp_Pnt PosPlan(0.0,0.0,0.0);
4505   Geom_Plane PP (PosPlan,direct);
4506   Handle(Geom_Geometry) G = PP.Copy();
4507   Handle(Geom_Plane) P = Handle(Geom_Plane)::DownCast(G);
4508
4509   gp_Dir Zdirection(0.0,0.0,1.0);
4510   VolumeSection VOL( shape, aMeshingDeflection);
4511   VOL.SetPlane(P);
4512   Handle (Geom_RectangularTrimmedSurface) SurfaceTrimmee;
4513   
4514   if(Zdirection.IsEqual(direct,Precision::Angular()) == Standard_False) { 
4515     VOL.MakeRotation(direct);
4516   }
4517   
4518   VOL.CenterOfGravity();
4519   SurfaceTrimmee = VOL.TrimSurf();
4520   Standard_Real Cote = VOL.Archimede( cste, aMeshingDeflection );
4521   
4522   if ( Cote == -1 ) {
4523     double Zmin,Zmax;
4524     VOL.getZ(Zmin,Zmax);
4525     double volume = VOL.CalculateVolume( Zmax ) * aWaterDensity;
4526
4527     char msg[100]="";  
4528     sprintf(msg, "shape sinks to the bottom : Weigth max = %.1f", volume);
4529
4530     THROW_SALOME_CORBA_EXCEPTION(msg, SALOME::BAD_PARAM);
4531   }
4532   
4533   SurfaceTrimmee=VOL.AjustePlan(SurfaceTrimmee,Cote,PosPlan);
4534   if(Zdirection.IsEqual(direct,Precision::Angular()) == Standard_False) { 
4535     SurfaceTrimmee=VOL.InvMakeRotation(direct,SurfaceTrimmee);
4536   }
4537   
4538   Standard_Real u1,u2,v1,v2;
4539   SurfaceTrimmee->Bounds(u1,u2,v1,v2);
4540   TopoDS_Face tirant = BRepBuilderAPI_MakeFace(SurfaceTrimmee, u1, u2, v1, v2);
4541   
4542   if (tirant.IsNull()) {
4543     THROW_SALOME_CORBA_EXCEPTION("Result is null", SALOME::BAD_PARAM);
4544   }
4545
4546   result = CreateObject(tirant);
4547   InsertInLabelOneArgument(shape, aShape, tirant, result, myCurrentOCAFDoc) ;
4548
4549   return result;  
4550 }
4551
4552
4553 //================================================================================
4554 // function : MakeFillet()
4555 // purpose  : Create a cylinder topology
4556 //================================================================================
4557 GEOM::GEOM_Shape_ptr  GEOM_Gen_i::MakeFillet( GEOM::GEOM_Shape_ptr shape,
4558                                               CORBA::Double radius,
4559                                               CORBA::Short ShapeType,
4560                                               const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID ) 
4561   throw (SALOME::SALOME_Exception)
4562 {
4563   GEOM::GEOM_Shape_var result;
4564   TopoDS_Shape tds ;
4565
4566   const TopoDS_Shape aShape = GetTopoShape(shape) ;
4567   if( aShape.IsNull() ) {
4568     THROW_SALOME_CORBA_EXCEPTION("Shape is null", SALOME::BAD_PARAM);
4569   }
4570
4571   BRepFilletAPI_MakeFillet fill(aShape);
4572
4573   try {
4574     /* case all */
4575     if(ListOfID.length() == 0) {
4576       TopExp_Explorer Exp ( aShape, TopAbs_EDGE );
4577       for (Exp; Exp.More(); Exp.Next()) {
4578         TopoDS_Edge E =TopoDS::Edge(Exp.Current());
4579         fill.Add(E);
4580       }
4581       for (int i = 1;i<=fill.NbContours();i++) {
4582         fill.SetRadius(radius,i);
4583       }
4584       tds = fill.Shape();
4585       
4586     } else {
4587
4588       /* case selection */               
4589       for ( unsigned int ind = 0; ind < ListOfID.length(); ind++ ) {
4590         TopoDS_Shape ss ;
4591         if( GetShapeFromIndex( aShape, (TopAbs_ShapeEnum)ShapeType, ListOfID[ind], ss ) ) {
4592           TopoDS_Edge E = TopoDS::Edge(ss) ;
4593           fill.Add( E );
4594         }
4595       }
4596       for (int i = 1;i<=fill.NbContours();i++) {
4597         fill.SetRadius(radius,i);
4598       }
4599       tds = fill.Shape();
4600     }
4601   }
4602   catch(Standard_Failure) {
4603     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeFillet", SALOME::BAD_PARAM);
4604   }
4605   
4606   if (tds.IsNull()) {
4607     THROW_SALOME_CORBA_EXCEPTION("Make Fillet aborted", SALOME::BAD_PARAM);
4608   } 
4609   result = CreateObject(tds);
4610   InsertInLabelOneArgument(aShape, shape, tds, result, myCurrentOCAFDoc) ;
4611
4612   return result ;  
4613 }
4614
4615
4616 //================================================================================
4617 // function : MakeChamfer
4618 // purpose  : Create a Chamfer topology
4619 //================================================================================
4620 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakeChamfer( GEOM::GEOM_Shape_ptr shape,
4621                                               CORBA::Double d1,
4622                                               CORBA::Double d2,
4623                                               CORBA::Short ShapeType,
4624                                               const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfID ) 
4625   throw (SALOME::SALOME_Exception)
4626 {
4627   GEOM::GEOM_Shape_var result;
4628   TopoDS_Shape tds ;
4629
4630   const TopoDS_Shape aShape = GetTopoShape(shape) ;
4631   if( aShape.IsNull() ) {
4632     THROW_SALOME_CORBA_EXCEPTION("Shape is null", SALOME::BAD_PARAM);
4633   }
4634   
4635   BRepFilletAPI_MakeChamfer MC(aShape);
4636
4637   try {
4638     /* case all */
4639     TopTools_IndexedDataMapOfShapeListOfShape M;
4640     TopExp::MapShapesAndAncestors(aShape,TopAbs_EDGE,TopAbs_FACE,M);
4641     if(ListOfID.length() == 0) {
4642       for (int i = 1;i<=M.Extent();i++) {
4643         TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
4644         TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
4645         if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
4646           MC.Add(d1,d2,E,F);
4647       }
4648       tds = MC.Shape();
4649
4650     } else {
4651
4652       /* case selection */  
4653       for ( unsigned int ind = 0; ind < ListOfID.length(); ind++ ) {
4654         TopoDS_Shape ss ;
4655         if( GetShapeFromIndex( aShape, (TopAbs_ShapeEnum)ShapeType, ListOfID[ind], ss ) ) {
4656           TopoDS_Edge E = TopoDS::Edge( ss ) ;
4657           TopoDS_Face F = TopoDS::Face(M.FindFromKey(E).First());
4658           if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
4659             MC.Add(d1,d2,E,F);
4660         }
4661       }
4662       tds = MC.Shape();
4663     }
4664   }
4665   catch(Standard_Failure) {
4666     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeChamfer", SALOME::BAD_PARAM);
4667   }
4668   
4669   if (tds.IsNull()) {
4670     THROW_SALOME_CORBA_EXCEPTION("Make Chamfer aborted", SALOME::BAD_PARAM);
4671   } 
4672   result = CreateObject(tds);
4673   InsertInLabelOneArgument(aShape, shape, tds, result, myCurrentOCAFDoc) ;
4674
4675   return result ;
4676 }
4677
4678 //=================================================================================
4679 // function : CheckShape()
4680 // purpose  :
4681 //=================================================================================
4682 CORBA::Boolean GEOM_Gen_i::CheckShape(GEOM::GEOM_Shape_ptr shape) 
4683   throw (SALOME::SALOME_Exception)
4684 {
4685   TopoDS_Shape S = GetTopoShape(shape) ;
4686   if( S.IsNull() ) {
4687     THROW_SALOME_CORBA_EXCEPTION("Shape is null", SALOME::BAD_PARAM);
4688   }
4689
4690   BRepCheck_Analyzer ana(S,false);
4691   if (ana.IsValid()) 
4692     return 1;
4693
4694   return 0;
4695 }
4696
4697 //=================================================================================
4698 // function : MakePlacedBox()
4699 // purpose  :
4700 //=================================================================================
4701 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakePlacedBox(CORBA::Double x1,  CORBA::Double y1,  CORBA::Double z1,
4702                                          CORBA::Double delta1, CORBA::Double delta2, CORBA::Double delta3)
4703   throw (SALOME::SALOME_Exception)
4704 {
4705   GEOM::GEOM_Shape_var result ;
4706   TopoDS_Shape tds ;
4707
4708   CORBA::Double x2, y2, z2 ;
4709
4710   try {
4711     x2 = x1 + delta1 ;
4712     y2 = y1 + delta2 ;
4713     z2 = z1 + delta3 ;
4714     
4715     gp_Pnt P1(x1,y1,z1);
4716     gp_Pnt P2(x2,y2,z2);
4717     
4718     tds = BRepPrimAPI_MakeBox(P1,P2).Shape();
4719   }
4720   catch(Standard_Failure) {
4721     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakeBox", SALOME::BAD_PARAM);
4722   }
4723   
4724   if (tds.IsNull()) {
4725     THROW_SALOME_CORBA_EXCEPTION("Make Box aborted : null shape", SALOME::BAD_PARAM);
4726   } 
4727   
4728   result = CreateObject(tds);
4729   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
4730   result->ShapeId(entry) ;
4731
4732   return result;  
4733 }
4734
4735 //=================================================================================
4736 // function : MakePanel()
4737 // purpose  :
4738 //=================================================================================
4739 GEOM::GEOM_Shape_ptr GEOM_Gen_i::MakePanel(GEOM::GEOM_Shape_ptr shape,
4740                                      CORBA::Short directiontype,
4741                                      CORBA::Double delta)
4742   throw (SALOME::SALOME_Exception)
4743 {
4744   GEOM::GEOM_Shape_var result ;
4745   TopoDS_Shape tds ;
4746   TopoDS_Shape aShape = GetTopoShape(shape) ;
4747   Bnd_Box B ;
4748   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax ;
4749   GEOM::PointStruct pstruct1, pstruct2, pstruct3, pstruct4 ;
4750
4751   if(aShape.IsNull() ) {
4752     THROW_SALOME_CORBA_EXCEPTION("MakePanel aborted : null shape during operation", SALOME::BAD_PARAM);
4753   }  
4754
4755   try {
4756     BRepBndLib::Add(aShape,B);
4757     B.Enlarge(10.);
4758     B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
4759     
4760     switch (directiontype)
4761       {
4762       case 1 :   /* X */
4763         pstruct1 = MakePointStruct( delta, aymin, azmin ) ;
4764         pstruct2 = MakePointStruct( delta, aymin, azmax ) ;
4765         pstruct3 = MakePointStruct( delta, aymax, azmax ) ;
4766         pstruct4 = MakePointStruct( delta, aymax, azmin ) ;
4767         break ;
4768       case 2 :   /* Y */
4769         pstruct1 = MakePointStruct( axmin, delta, azmin ) ;
4770         pstruct2 = MakePointStruct( axmin, delta, azmax ) ;
4771         pstruct3 = MakePointStruct( axmax, delta, azmax ) ;
4772         pstruct4 = MakePointStruct( axmax, delta, azmin ) ;
4773         break ;
4774       case 3 :   /* Z */
4775         pstruct1 = MakePointStruct( axmin, aymin, delta ) ;
4776         pstruct2 = MakePointStruct( axmin, aymax, delta ) ;
4777         pstruct3 = MakePointStruct( axmax, aymax, delta ) ;
4778         pstruct4 = MakePointStruct( axmax, aymin, delta ) ;
4779         break ;
4780       default :
4781         return result ;
4782       }
4783     
4784     GEOM::GEOM_Shape_ptr Edge1 = MakeEdge(pstruct1, pstruct2);
4785     GEOM::GEOM_Shape_ptr Edge2 = MakeEdge(pstruct2, pstruct3);
4786     GEOM::GEOM_Shape_ptr Edge3 = MakeEdge(pstruct3, pstruct4);
4787     GEOM::GEOM_Shape_ptr Edge4 = MakeEdge(pstruct4, pstruct1);
4788     
4789     GEOM::GEOM_Gen::ListOfIOR_var aList = new GEOM::GEOM_Gen::ListOfIOR;
4790     aList->length(4);
4791     aList[0]=strdup(Edge1->Name());
4792     aList[1]=strdup(Edge2->Name());
4793     aList[2]=strdup(Edge3->Name());
4794     aList[3]=strdup(Edge4->Name());
4795     
4796     GEOM::GEOM_Shape_ptr aWire = MakeWire( aList );
4797     GEOM::GEOM_Shape_ptr aFace = MakeFace( aWire, true ) ;
4798     tds = GetTopoShape(aFace);
4799     
4800   }
4801   catch(Standard_Failure) {
4802     THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::MakePanel", SALOME::BAD_PARAM);
4803   }
4804   
4805   if (tds.IsNull()) {
4806     THROW_SALOME_CORBA_EXCEPTION("Make PanelsPartition aborted : null shape", SALOME::BAD_PARAM);
4807   } 
4808   
4809   result = CreateObject(tds); 
4810   const char *entry = InsertInLabel(tds, result->Name(), myCurrentOCAFDoc) ;
4811   result->ShapeId(entry) ;
4812   
4813   return result;
4814 }
4815
4816
4817 void GEOM_Gen_i::ExportIGES(const char* filename,GEOM::GEOM_Shape_ptr theShape) 
4818   throw (SALOME::SALOME_Exception)
4819 {
4820   if (theShape->_is_nil()) 
4821     {
4822       THROW_SALOME_CORBA_EXCEPTION("Export IGES aborted", SALOME::BAD_PARAM);
4823     } 
4824   TopoDS_Shape tds = GetTopoShape(theShape);
4825   if (tds.IsNull()) 
4826     {
4827       THROW_SALOME_CORBA_EXCEPTION("Export IGES aborted", SALOME::BAD_PARAM);
4828     } 
4829   try 
4830     {
4831       //VRV: OCC 4.0 migration
4832       IGESControl_Controller::Init();
4833       IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
4834                                    Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
4835       //VRV: OCC 4.0 migration
4836         
4837       ICW.AddShape (tds);
4838       ICW.ComputeModel();
4839       char * aname = strdup(filename);
4840       Standard_Boolean result = ICW.Write( aname );
4841       free(aname);
4842     }
4843   catch(Standard_Failure) 
4844     {
4845       THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ExportIGES", SALOME::BAD_PARAM);
4846     }
4847 }
4848
4849 void GEOM_Gen_i::ExportBREP(const char* filename,GEOM::GEOM_Shape_ptr theShape)
4850   throw (SALOME::SALOME_Exception)
4851 {
4852   if (theShape->_is_nil()) 
4853     {
4854       THROW_SALOME_CORBA_EXCEPTION("Export BRep aborted", SALOME::BAD_PARAM);
4855     } 
4856   TopoDS_Shape tds = GetTopoShape(theShape);
4857   if (tds.IsNull()) 
4858     {
4859       THROW_SALOME_CORBA_EXCEPTION("Export BRep aborted", SALOME::BAD_PARAM);
4860     } 
4861   try 
4862     {
4863       char * aname = strdup(filename);
4864       Standard_Boolean result = BRepTools::Write(tds,aname);
4865       free(aname);
4866     }
4867   catch(Standard_Failure) 
4868     {
4869       THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ExportBREP", SALOME::BAD_PARAM);
4870     }
4871 }
4872
4873 void GEOM_Gen_i::ExportSTEP(const char* filename,GEOM::GEOM_Shape_ptr theShape) 
4874   throw (SALOME::SALOME_Exception)
4875 {
4876   if (theShape->_is_nil()) 
4877     {
4878       THROW_SALOME_CORBA_EXCEPTION("Export STEP aborted", SALOME::BAD_PARAM);
4879     } 
4880   TopoDS_Shape tds = GetTopoShape(theShape);
4881   if (tds.IsNull()) 
4882     {
4883       THROW_SALOME_CORBA_EXCEPTION("Export STEP aborted", SALOME::BAD_PARAM);
4884     } 
4885   try 
4886     {
4887       IFSelect_ReturnStatus status ;
4888       //VRV: OCC 4.0 migration
4889       STEPControl_Writer aWriter;
4890       status = aWriter.Transfer( tds, STEPControl_ManifoldSolidBrep ) ;
4891       //VRV: OCC 4.0 migration
4892       if ( status == IFSelect_RetDone ) 
4893         {
4894           char * aname = strdup(filename);
4895           status = aWriter.Write( aname ) ;
4896           free(aname);
4897         }
4898     }
4899   catch(Standard_Failure) 
4900     {
4901       THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ExportBREP", SALOME::BAD_PARAM);
4902     }
4903 }
4904
4905
4906 //=====================================================================================
4907 // EXPORTED METHODS
4908 //=====================================================================================
4909 extern "C"
4910 {
4911   PortableServer::ObjectId * GeometryEngine_factory(CORBA::ORB_ptr orb,
4912                                                     PortableServer::POA_ptr poa, 
4913                                                     PortableServer::ObjectId * contId,
4914                                                     const char *instanceName, 
4915                                                     const char * interfaceName)
4916   {
4917 MESSAGE("mygeom")
4918    GEOM_Gen_i * myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
4919 MESSAGE("mygeom")
4920    myGEOM_Gen_i->register_name("/myGEOM_Gen"); // NRI : 11/07/2002 : Add for Supervision example 
4921 MESSAGE("mygeom")
4922    return myGEOM_Gen_i->getId() ;
4923   }
4924 }
4925