Salome HOME
0021147: EDF GEOM: Difference between compound of line and compound of edges
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifdef WNT
24 #pragma warning( disable:4786 )
25 #endif
26
27 #include <Standard_Stream.hxx>
28
29 #include "GEOM_IInsertOperations_i.hh"
30
31 #include "utilities.h"
32 #include "OpUtil.hxx"
33 #include "Utils_ExceptHandlers.hxx"
34
35 #include "GEOM_Engine.hxx"
36 #include "GEOM_Object.hxx"
37
38 #include <TColStd_HSequenceOfAsciiString.hxx>
39 #include <TDataStd_HArray1OfByte.hxx>
40
41 //=============================================================================
42 /*!
43  *   constructor:
44  */
45 //=============================================================================
46 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
47                                                     GEOM::GEOM_Gen_ptr theEngine,
48                                                     ::GEOMImpl_IInsertOperations* theImpl)
49      :GEOM_IOperations_i(thePOA, theEngine, theImpl)
50 {
51   MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
52 }
53
54 //=============================================================================
55 /*!
56  *  destructor
57  */
58 //=============================================================================
59 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
60 {
61   MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
62 }
63
64
65 //=============================================================================
66 /*!
67  *  MakeCopy
68  */
69 //=============================================================================
70 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
71 {
72   GEOM::GEOM_Object_var aGEOMObject;
73
74   //Set a not done flag
75   GetOperations()->SetNotDone();
76
77   //Get the reference shape
78   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
79   if (anOriginal.IsNull()) return aGEOMObject._retn();
80
81   //Create the copy
82   Handle(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
83
84   if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
85
86   return GetObject(anObject);
87 }
88
89 //=============================================================================
90 /*!
91  *  Export
92  */
93 //=============================================================================
94 void GEOM_IInsertOperations_i::Export
95                    (GEOM::GEOM_Object_ptr theOriginal,
96                     const char*           theFileName,
97                     const char*           theFormatName)
98 {
99   GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
100
101   //Set a not done flag
102   GetOperations()->SetNotDone();
103
104   //Get the reference shape
105   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
106   if (anOriginal.IsNull()) return;
107
108   //Export the shape to the file
109   char* aFileName   = strdup(theFileName);
110   char* aFormatName = strdup(theFormatName);
111   GetOperations()->Export(anOriginal, aFileName, aFormatName);
112   free(aFileName);
113   free(aFormatName);
114 }
115
116 //=============================================================================
117 /*!
118  *  Import
119  */
120 //=============================================================================
121 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::Import
122                    (const char* theFileName,
123                     const char* theFormatName)
124 {
125   GEOM::GEOM_Object_var aGEOMObject;
126
127   //Set a not done flag
128   GetOperations()->SetNotDone();
129
130   //Import the shape from the file
131   char* aFileName   = strdup(theFileName);
132   char* aFormatName = strdup(theFormatName);
133   Handle(GEOM_Object) anObject = GetOperations()->Import(aFileName, aFormatName);
134
135   if( strcmp(aFormatName,"IGES_UNIT")==0 && !anObject.IsNull() ) {
136     free(aFileName);
137     free(aFormatName);
138     return GetObject(anObject);
139   }
140
141   free(aFileName);
142   free(aFormatName);
143
144   if (!GetOperations()->IsDone() || anObject.IsNull()) {
145     return aGEOMObject._retn();
146   }
147
148   return GetObject(anObject);
149 }
150
151 //=============================================================================
152 /*!
153  *  ImportTranslators
154  */
155 //=============================================================================
156 void GEOM_IInsertOperations_i::ImportTranslators
157   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
158 {
159   // allocate the CORBA arrays
160   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
161   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
162
163   // Get sequences of available formats
164   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
165   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
166   if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
167     const int formSize = aFormats->Length();
168     if (formSize == aPatterns->Length()) {
169       aFormatsArray->length(formSize);
170       aPatternsArray->length(formSize);
171
172       // fill the local CORBA arrays with values from sequences
173       CORBA::Long i = 1;
174       for (; i <= formSize; i++) {
175         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
176         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
177       }
178     }
179   }
180
181   // initialize out-parameters with local arrays
182   theFormats  = aFormatsArray._retn();
183   thePatterns = aPatternsArray._retn();
184 }
185
186 //=============================================================================
187 /*!
188  *  ExportTranslators
189  */
190 //=============================================================================
191 void GEOM_IInsertOperations_i::ExportTranslators
192   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
193 {
194   // allocate the CORBA arrays
195   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
196   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
197
198   // Get sequences of available formats
199   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
200   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
201   if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
202     const int formSize = aFormats->Length();
203     if (formSize == aPatterns->Length()) {
204       aFormatsArray->length(formSize);
205       aPatternsArray->length(formSize);
206
207       // fill the local CORBA arrays with values from sequences
208       CORBA::Long i = 1;
209       for (; i <= formSize; i++) {
210         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
211         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
212       }
213     }
214   }
215
216   // initialize out-parameters with local arrays
217   theFormats  = aFormatsArray._retn();
218   thePatterns = aPatternsArray._retn();
219 }
220
221 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
222 {
223   GetOperations()->SetNotDone();
224   return GetOperations()->LoadTexture( theTextureFile );
225 }
226
227 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
228                                                  const SALOMEDS::TMPFile& theTexture)
229 {
230   GetOperations()->SetNotDone();
231   Handle(TDataStd_HArray1OfByte) aTexture;
232   if ( theTexture.length() > 0 ) {
233     aTexture = new TDataStd_HArray1OfByte( 1, theTexture.length() );
234     for ( int i = 0; i < theTexture.length(); i++ )
235       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
236   }
237   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
238 }
239
240 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID, 
241                                                         CORBA::Long& theWidth,
242                                                         CORBA::Long& theHeight)
243 {
244   int aWidth, aHeight;
245   Handle(TDataStd_HArray1OfByte) aTextureImpl = GetOperations()->GetTexture( theID, aWidth, aHeight );
246   theWidth  = aWidth;
247   theHeight = aHeight;
248   SALOMEDS::TMPFile_var aTexture;
249   if ( !aTextureImpl.IsNull() ) {
250     aTexture = new SALOMEDS::TMPFile;
251     aTexture->length( aTextureImpl->Length() );
252     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
253       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
254   }
255   return aTexture._retn();
256 }
257
258 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
259 {
260   std::list<int> localIDs = GetOperations()->GetAllTextures();
261   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
262   anIDs->length(localIDs.size());
263   std::list<int>::const_iterator anIt;
264   int i = 0;
265   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
266     anIDs[i] = *anIt;
267   return anIDs._retn();
268 }