1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #pragma warning( disable:4786 )
27 #include <Standard_Stream.hxx>
29 #include "GEOM_IInsertOperations_i.hh"
31 #include "utilities.h"
33 #include "Utils_ExceptHandlers.hxx"
35 #include "GEOM_Engine.hxx"
36 #include "GEOM_Object.hxx"
38 #include <Basics_OCCTVersion.hxx>
40 #include <TColStd_HSequenceOfAsciiString.hxx>
42 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
43 #include <TColStd_HArray1OfByte.hxx>
45 #include <TDataStd_HArray1OfByte.hxx>
48 //=============================================================================
52 //=============================================================================
53 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
54 GEOM::GEOM_Gen_ptr theEngine,
55 ::GEOMImpl_IInsertOperations* theImpl)
56 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
58 MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
61 //=============================================================================
65 //=============================================================================
66 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
68 MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
72 //=============================================================================
76 //=============================================================================
77 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
79 GEOM::GEOM_Object_var aGEOMObject;
82 GetOperations()->SetNotDone();
84 //Get the reference shape
85 Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
86 if (anOriginal.IsNull()) return aGEOMObject._retn();
89 Handle(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
91 if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
93 return GetObject(anObject);
96 //=============================================================================
100 //=============================================================================
101 void GEOM_IInsertOperations_i::Export
102 (GEOM::GEOM_Object_ptr theOriginal,
103 const char* theFileName,
104 const char* theFormatName)
106 GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
108 //Set a not done flag
109 GetOperations()->SetNotDone();
111 //Get the reference shape
112 Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
113 if (anOriginal.IsNull()) return;
115 //Export the shape to the file
116 char* aFileName = strdup(theFileName);
117 char* aFormatName = strdup(theFormatName);
118 GetOperations()->Export(anOriginal, aFileName, aFormatName);
123 //=============================================================================
127 //=============================================================================
128 GEOM::ListOfGO* GEOM_IInsertOperations_i::ImportFile
129 (const char* theFileName,
130 const char* theFormatName)
132 GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
134 //Set a not done flag
135 GetOperations()->SetNotDone();
137 //Import the shape from the file
138 char* aFileName = strdup(theFileName);
139 char* aFormatName = strdup(theFormatName);
140 Handle(TColStd_HSequenceOfTransient) aHSeq = GetOperations()->Import(aFileName, aFormatName);
142 if( strcmp(aFormatName,"IGES_UNIT")==0 && !aHSeq.IsNull() ) {
151 if (!GetOperations()->IsDone() || aHSeq.IsNull()) {
155 // Copy created objects.
156 Standard_Integer aLength = aHSeq->Length();
158 aSeq->length(aLength);
160 for (Standard_Integer i = 1; i <= aLength; i++) {
161 aSeq[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aHSeq->Value(i)));
167 //=============================================================================
171 //=============================================================================
172 char* GEOM_IInsertOperations_i::ReadValue(const char* theFileName,
173 const char* theFormatName,
174 const char* theParameterName)
176 //Set a not done flag
177 GetOperations()->SetNotDone();
179 char* aFileName = strdup(theFileName);
180 char* aFormatName = strdup(theFormatName);
181 char* aParameterName = strdup(theParameterName);
183 TCollection_AsciiString aUnits = GetOperations()->ReadValue
184 (aFileName, aFormatName, aParameterName);
188 free(aParameterName);
190 return CORBA::string_dup(aUnits.ToCString());
193 //=============================================================================
197 //=============================================================================
198 void GEOM_IInsertOperations_i::ImportTranslators
199 (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
201 // allocate the CORBA arrays
202 GEOM::string_array_var aFormatsArray = new GEOM::string_array();
203 GEOM::string_array_var aPatternsArray = new GEOM::string_array();
205 // Get sequences of available formats
206 Handle(TColStd_HSequenceOfAsciiString) aFormats = new TColStd_HSequenceOfAsciiString;
207 Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
208 if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
209 const int formSize = aFormats->Length();
210 if (formSize == aPatterns->Length()) {
211 aFormatsArray->length(formSize);
212 aPatternsArray->length(formSize);
214 // fill the local CORBA arrays with values from sequences
216 for (; i <= formSize; i++) {
217 aFormatsArray[i-1] = CORBA::string_dup(aFormats->Value(i).ToCString());
218 aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
223 // initialize out-parameters with local arrays
224 theFormats = aFormatsArray._retn();
225 thePatterns = aPatternsArray._retn();
228 //=============================================================================
232 //=============================================================================
233 void GEOM_IInsertOperations_i::ExportTranslators
234 (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
236 // allocate the CORBA arrays
237 GEOM::string_array_var aFormatsArray = new GEOM::string_array();
238 GEOM::string_array_var aPatternsArray = new GEOM::string_array();
240 // Get sequences of available formats
241 Handle(TColStd_HSequenceOfAsciiString) aFormats = new TColStd_HSequenceOfAsciiString;
242 Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
243 if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
244 const int formSize = aFormats->Length();
245 if (formSize == aPatterns->Length()) {
246 aFormatsArray->length(formSize);
247 aPatternsArray->length(formSize);
249 // fill the local CORBA arrays with values from sequences
251 for (; i <= formSize; i++) {
252 aFormatsArray[i-1] = CORBA::string_dup(aFormats->Value(i).ToCString());
253 aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
258 // initialize out-parameters with local arrays
259 theFormats = aFormatsArray._retn();
260 thePatterns = aPatternsArray._retn();
263 //=============================================================================
267 //=============================================================================
268 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream)
270 GEOM::GEOM_Object_var aGEOMObject;
272 //Set a not done flag
273 GetOperations()->SetNotDone();
275 if (theStream.length() < 1)
276 return aGEOMObject._retn();
278 char* buf = (char*)theStream.NP_data();
279 std::istringstream aStream (buf);
281 Handle(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream);
282 if (!GetOperations()->IsDone() || anObject.IsNull())
283 return aGEOMObject._retn();
285 return GetObject(anObject);
288 //=============================================================================
292 //=============================================================================
293 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
295 GetOperations()->SetNotDone();
296 return GetOperations()->LoadTexture( theTextureFile );
299 //=============================================================================
303 //=============================================================================
304 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
305 const SALOMEDS::TMPFile& theTexture)
307 GetOperations()->SetNotDone();
309 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
310 Handle(TColStd_HArray1OfByte) aTexture;
312 Handle(TDataStd_HArray1OfByte) aTexture;
315 if ( theTexture.length() > 0 ) {
316 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
317 aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
319 aTexture = new TDataStd_HArray1OfByte (1, theTexture.length());
322 for ( int i = 0; i < theTexture.length(); i++ )
323 aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
325 return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
328 //=============================================================================
332 //=============================================================================
333 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
334 CORBA::Long& theWidth,
335 CORBA::Long& theHeight)
338 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
339 Handle(TColStd_HArray1OfByte) aTextureImpl =
341 Handle(TDataStd_HArray1OfByte) aTextureImpl =
343 GetOperations()->GetTexture(theID, aWidth, aHeight);
346 SALOMEDS::TMPFile_var aTexture;
347 if ( !aTextureImpl.IsNull() ) {
348 aTexture = new SALOMEDS::TMPFile;
349 aTexture->length( aTextureImpl->Length() );
350 for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
351 aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
353 return aTexture._retn();
356 //=============================================================================
360 //=============================================================================
361 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
363 std::list<int> localIDs = GetOperations()->GetAllTextures();
364 GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
365 anIDs->length(localIDs.size());
366 std::list<int>::const_iterator anIt;
368 for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
370 return anIDs._retn();
373 //=============================================================================
375 * Export a shape to XAO format
376 * \param shape The shape to export
377 * \param groups The list of groups to export
378 * \param fields The list of fields to export
379 * \param author The author of the export
380 * \param fileName The name of the exported file
381 * \return boolean indicating if export was succeful.
383 //=============================================================================
384 CORBA::Boolean GEOM_IInsertOperations_i::ExportXAO(GEOM::GEOM_Object_ptr shape,
385 const GEOM::ListOfGO& groups,
386 const GEOM::ListOfFields& fields,
388 const char* fileName)
391 // Set a not done flag
392 GetOperations()->SetNotDone();
394 // Get the reference shape
395 Handle(GEOM_Object) reference = GetObjectImpl(shape);
397 // Get the reference groups
399 std::list<Handle(GEOM_Object)> groupsObj;
400 for (; ind < groups.length(); ind++)
402 Handle(GEOM_Object) gobj = GetObjectImpl(groups[ind]);
403 if (gobj.IsNull()) return false;
404 groupsObj.push_back(gobj);
407 // Get the reference fields
409 std::list<Handle(GEOM_Field)> fieldsObj;
410 for (; ind < fields.length(); ind++)
412 Handle(GEOM_Field) fobj = Handle(GEOM_Field)::DownCast(GetBaseObjectImpl(fields[ind]));
413 if (fobj.IsNull()) return false;
414 fieldsObj.push_back(fobj);
417 if (!reference.IsNull())
420 isGood = GetOperations()->ExportXAO(reference, groupsObj, fieldsObj, author, fileName);
426 //=============================================================================
428 * Import a shape from XAO format
429 * \param fileName The name of the file to import
430 * \param shape The imported shape
431 * \param subShapes The list of imported subShapes
432 * \param groups The list of imported groups
433 * \param fields The list of imported fields
434 * \return boolean indicating if import was succeful.
436 //=============================================================================
437 CORBA::Boolean GEOM_IInsertOperations_i::ImportXAO(const char* fileName,
438 GEOM::GEOM_Object_out shape,
439 GEOM::ListOfGO_out subShapes,
440 GEOM::ListOfGO_out groups,
441 GEOM::ListOfFields_out fields)
443 GEOM::GEOM_Object_var vshape;
444 shape = vshape._retn();
446 subShapes = new GEOM::ListOfGO;
447 groups = new GEOM::ListOfGO;
448 fields = new GEOM::ListOfFields;
450 // Set a not done flag
451 GetOperations()->SetNotDone();
453 Handle(TColStd_HSequenceOfTransient) importedSubShapes = new TColStd_HSequenceOfTransient();
454 Handle(TColStd_HSequenceOfTransient) importedGroups = new TColStd_HSequenceOfTransient();
455 Handle(TColStd_HSequenceOfTransient) importedFields = new TColStd_HSequenceOfTransient();
456 Handle(GEOM_Object) hshape;
457 bool res = GetOperations()->ImportXAO(fileName, hshape, importedSubShapes, importedGroups, importedFields);
459 if (!GetOperations()->IsDone() || !res)
463 int n = importedSubShapes->Length();
464 subShapes->length(n);
465 for (int i = 1; i <= n; i++)
467 (*subShapes)[i - 1] = GetObject(Handle(GEOM_Object)::DownCast(importedSubShapes->Value(i)));
471 n = importedGroups->Length();
473 for (int i = 1; i <= n; i++)
475 (*groups)[i - 1] = GetObject(Handle(GEOM_Object)::DownCast(importedGroups->Value(i)));
479 n = importedFields->Length();
481 for (int i = 1; i <= n; i++)
483 (*fields)[i - 1] = GEOM::GEOM_Field::_narrow(
484 GetBaseObject(Handle(GEOM_Field)::DownCast(importedFields->Value(i))));
487 shape = GetObject(hshape);