Salome HOME
Fixed bug in method GetReferncedObject that caused a crash in case if the reference...
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #ifdef WNT
21 #pragma warning( disable:4786 )
22 #endif
23
24 #include <Standard_Stream.hxx>
25
26 #include "GEOM_IInsertOperations_i.hh"
27
28 #include "utilities.h"
29 #include "OpUtil.hxx"
30 #include "Utils_ExceptHandlers.hxx"
31
32 #include "GEOM_Engine.hxx"
33 #include "GEOM_Object.hxx"
34
35 #include <TColStd_HSequenceOfAsciiString.hxx>
36
37 //=============================================================================
38 /*!
39  *   constructor:
40  */
41 //=============================================================================
42 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
43                                                     GEOM::GEOM_Gen_ptr theEngine,
44                                                     ::GEOMImpl_IInsertOperations* theImpl)
45      :GEOM_IOperations_i(thePOA, theEngine, theImpl)
46 {
47   MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
48 }
49
50 //=============================================================================
51 /*!
52  *  destructor
53  */
54 //=============================================================================
55 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
56 {
57   MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
58 }
59
60
61 //=============================================================================
62 /*!
63  *  MakeCopy
64  */
65 //=============================================================================
66 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
67 {
68   GEOM::GEOM_Object_var aGEOMObject;
69
70   //Set a not done flag
71   GetOperations()->SetNotDone();
72
73   if (theOriginal == NULL) return aGEOMObject._retn();
74
75   //Get the reference shape
76   Handle(GEOM_Object) anOriginal =
77     GetOperations()->GetEngine()->GetObject(theOriginal->GetStudyID(),
78                                             theOriginal->GetEntry());
79
80   if (anOriginal.IsNull()) return aGEOMObject._retn();
81
82   //Create the copy
83   Handle(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
84
85   if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
86
87   return GetObject(anObject);
88 }
89
90 //=============================================================================
91 /*!
92  *  Export
93  */
94 //=============================================================================
95 void GEOM_IInsertOperations_i::Export
96                    (GEOM::GEOM_Object_ptr theOriginal,
97                     const char*           theFileName,
98                     const char*           theFormatName)
99 {
100   GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
101
102   //Set a not done flag
103   GetOperations()->SetNotDone();
104
105   if (theOriginal == NULL) return;
106
107   //Get the reference shape
108   Handle(GEOM_Object) anOriginal =
109     GetOperations()->GetEngine()->GetObject(theOriginal->GetStudyID(),
110                                             theOriginal->GetEntry());
111
112   if (anOriginal.IsNull()) return;
113
114   //Export the shape to the file
115   char* aFileName   = strdup(theFileName);
116   char* aFormatName = strdup(theFormatName);
117   GetOperations()->Export(anOriginal, aFileName, aFormatName);
118   free(aFileName);
119   free(aFormatName);
120
121   return;
122 }
123
124 //=============================================================================
125 /*!
126  *  Import
127  */
128 //=============================================================================
129 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::Import
130                    (const char* theFileName,
131                     const char* theFormatName)
132 {
133   GEOM::GEOM_Object_var aGEOMObject;
134
135   //Set a not done flag
136   GetOperations()->SetNotDone();
137
138   //Import the shape from the file
139   char* aFileName   = strdup(theFileName);
140   char* aFormatName = strdup(theFormatName);
141   Handle(GEOM_Object) anObject = GetOperations()->Import(aFileName, aFormatName);
142   free(aFileName);
143   free(aFormatName);
144
145   if (!GetOperations()->IsDone() || anObject.IsNull())
146     return aGEOMObject._retn();
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 }