0026515: Exponential memory usage problems with BOPDS_IndexedMapOfPaveBlock and NColl...
[occt.git] / src / XDEDRAW / XDEDRAW_Common.cxx
CommitLineData
b311480e 1// Created on: 2003-08-15
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2003-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
7fd59977 16
42cf5bc1 17#include <DDocStd.hxx>
18#include <DDocStd_DrawDocument.hxx>
19#include <Dico_DictionaryOfTransient.hxx>
20#include <Dico_IteratorOfDictionaryOfTransient.hxx>
7fd59977 21#include <Draw.hxx>
22#include <Draw_Interpretor.hxx>
42cf5bc1 23#include <IFSelect_SessionPilot.hxx>
7fd59977 24#include <IGESCAFControl_Reader.hxx>
25#include <IGESCAFControl_Writer.hxx>
42cf5bc1 26#include <IGESControl_Controller.hxx>
27#include <Interface_Macros.hxx>
7fd59977 28#include <STEPCAFControl_DictionaryOfExternFile.hxx>
29#include <STEPCAFControl_ExternFile.hxx>
7fd59977 30#include <STEPCAFControl_IteratorOfDictionaryOfExternFile.hxx>
42cf5bc1 31#include <STEPCAFControl_Reader.hxx>
32#include <STEPCAFControl_Writer.hxx>
33#include <STEPControl_Controller.hxx>
34#include <TCollection_ExtendedString.hxx>
35#include <TDataStd_Name.hxx>
36#include <TDF_Data.hxx>
37#include <TDocStd_Application.hxx>
38#include <TDocStd_Document.hxx>
39#include <XDEDRAW.hxx>
40#include <XDEDRAW_Common.hxx>
41#include <XSControl_WorkSession.hxx>
42#include <XSDRAW.hxx>
7fd59977 43#include <XSDRAW_Vars.hxx>
42cf5bc1 44#include <XSDRAWIGES.hxx>
45#include <XSDRAWSTEP.hxx>
7fd59977 46
42cf5bc1 47#include <stdio.h>
7fd59977 48//============================================================
49// Support for several models in DRAW
50//============================================================
7fd59977 51static Handle(Dico_DictionaryOfTransient) thedictws = new Dico_DictionaryOfTransient;
52
53static Standard_Boolean ClearDicWS()
54{
55 thedictws->Clear();
56 return Standard_True;
57}
58
59static void AddWS(TCollection_AsciiString filename,
60 const Handle(XSControl_WorkSession)& WS)
61{
62 WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables
63 thedictws->SetItem( filename, WS );
64}
65
66
67static Standard_Boolean FillDicWS(Handle(STEPCAFControl_DictionaryOfExternFile)& dicFile)
68{
69 ClearDicWS();
70 if ( dicFile->IsEmpty() ) {
71 return Standard_False;
72 }
73 Handle(STEPCAFControl_ExternFile) EF;
74 STEPCAFControl_IteratorOfDictionaryOfExternFile DicEFIt ( dicFile );
75 for (; DicEFIt.More(); DicEFIt.Next() ) {
76 TCollection_AsciiString filename = DicEFIt.Name();
77 EF = DicEFIt.Value();
78 AddWS ( filename, EF->GetWS() );
79 }
80 return Standard_True;
81}
82
83static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename)
84{
85 if ( !thedictws->HasItem(filename) ) return Standard_False;
86 Handle(XSControl_WorkSession) CurrentWS =
87 Handle(XSControl_WorkSession)::DownCast( thedictws->Item(filename) );
88 XSDRAW::Pilot()->SetSession( CurrentWS );
89
90 return Standard_True;
91}
92
93
94//=======================================================================
95//function : SetCurWS
96//purpose : Set current file if many files are read
97//=======================================================================
98
99static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv)
100{
101 if (argc <2) {
102 di<<"Use: "<<argv[0]<<" filename "<<"\n";
103 return 1;
104 }
105 TCollection_AsciiString filename (argv[1]);
106 SetCurrentWS( filename );
107 return 0;
108}
109
110
111//=======================================================================
112//function : GetDicWSList
113//purpose : List all files recorded after translation
114//=======================================================================
115
116static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
117{
118 Handle(Dico_DictionaryOfTransient) DictWS = thedictws;
119 if ( DictWS->IsEmpty() ) return 1;
120 Dico_IteratorOfDictionaryOfTransient DicIt ( DictWS );
121 di << " The list of last translated files:" << "\n";
122 Standard_Integer num = 0;
123 for (; DicIt.More() ; DicIt.Next(), num++ ) {
124 TCollection_AsciiString strng ( DicIt.Name() );
125 if ( num ) di << "\n";
126 di << "\"" << strng.ToCString() << "\"";
127 }
128 return 0;
129}
130
131//=======================================================================
132//function : GetCurWS
133//purpose : Return name of file which is current
134//=======================================================================
135
136static Standard_Integer GetCurWS (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
137{
138 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
139 di << "\"" << WS->LoadedFile() << "\"";
140 return 0;
141}
142
143//=======================================================================
144//function : FromShape
145//purpose : Apply fromshape command to all the loaded WSs
146//=======================================================================
147
148static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
149{
150 if ( argc <2 ) {
151 di << argv[0] << " shape: search for shape origin among all last tranalated files" << "\n";
152 return 0;
153 }
154
155 char command[256];
91322f44 156 Sprintf ( command, "fromshape %.200s -1", argv[1] );
7fd59977 157 Handle(Dico_DictionaryOfTransient) DictWS = thedictws;
158 if ( DictWS->IsEmpty() ) return di.Eval ( command );
159
160 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
161
162 Dico_IteratorOfDictionaryOfTransient DicIt ( DictWS );
163// di << "Searching for shape among all the loaded files:" << "\n";
164 Standard_Integer num = 0;
165 for (; DicIt.More() ; DicIt.Next(), num++ ) {
166 Handle(XSControl_WorkSession) CurrentWS =
167 Handle(XSControl_WorkSession)::DownCast( DicIt.Value() );
168 XSDRAW::Pilot()->SetSession( CurrentWS );
169 di.Eval ( command );
170 }
171
172 XSDRAW::Pilot()->SetSession( WS );
173 return 0;
174}
175
176//=======================================================================
177//function : ReadIges
178//purpose : Read IGES to DECAF document
179//=======================================================================
180
181static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
182{
183 if ( argc <3 ) {
184 di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document" << "\n";
185 return 0;
186 }
187
188 DeclareAndCast(IGESControl_Controller,ctl,XSDRAW::Controller());
189 if (ctl.IsNull()) XSDRAW::SetNorm("IGES");
190
191 IGESCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
192
193 if (argc == 4) {
194 Standard_Boolean mode = Standard_True;
195 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
196 switch (argv[3][i]) {
197 case '-' : mode = Standard_False; break;
198 case '+' : mode = Standard_True; break;
199 case 'c' : reader.SetColorMode (mode); break;
200 case 'n' : reader.SetNameMode (mode); break;
201 case 'l' : reader.SetLayerMode (mode); break;
202 }
203 }
204 TCollection_AsciiString fnom,rnom;
205 Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"IGES",fnom,rnom);
206 if (modfic) di<<" File IGES to read : "<<fnom.ToCString()<<"\n";
207 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
208// di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
209 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
210 if (modfic) readstat = reader.ReadFile (fnom.ToCString());
211 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
212 if (readstat != IFSelect_RetDone) {
213 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
214 else di<<"No model loaded"<<"\n";
215 return 1;
216 }
217
218 Handle(TDocStd_Document) doc;
219 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
220 Handle(TDocStd_Application) A;
221 if (!DDocStd::Find(A)) {di<<"No application found"<<"\n";return 1;}
fff263bd 222 A->NewDocument("BinXCAF",doc);
7fd59977 223 TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);
224 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
225 Draw::Set(argv[1],DD);
226// di << "Document saved with name " << argv[1];
227 }
228 if ( ! reader.Transfer ( doc ) ) {
229 di << "Cannot read any relevant data from the IGES file" << "\n";
230 return 1;
231 }
232
233// Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
234// Draw::Set(argv[1],DD);
235 di << "Document saved with name " << argv[1];
236
237 return 0;
238}
239
240//=======================================================================
241//function : WriteIges
242//purpose : Write DECAF document to IGES
243//=======================================================================
244
245static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
246{
247 if ( argc <3 ) {
248 di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file" << "\n";
249 return 0;
250 }
251
252 Handle(TDocStd_Document) Doc;
253 DDocStd::GetDocument(argv[1], Doc);
254 if ( Doc.IsNull() ) {
255 di << argv[1] << " is not a document" << "\n";
256 return 1;
257 }
258
259 XSDRAW::SetNorm ("IGES");
260
261// IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
262// Interface_Static::IVal("write.iges.brep.mode"));
263
264 IGESCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
265 if (argc == 4) {
266 Standard_Boolean mode = Standard_True;
267 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
268 switch (argv[3][i]) {
269 case '-' : mode = Standard_False; break;
270 case '+' : mode = Standard_True; break;
271 case 'c' : writer.SetColorMode (mode); break;
272 case 'n' : writer.SetNameMode (mode); break;
273 case 'l' : writer.SetLayerMode (mode); break;
274 }
275 }
276 writer.Transfer ( Doc );
277
278 di << "Writig IGES model to file " << argv[2] << "\n";
279 if ( writer.Write ( argv[2] ) ) di<<" Write OK"<<"\n";
280 else di<<" Write failed"<<"\n";
281
282 return 0;
283}
284
285//=======================================================================
286//function : ReadStep
287//purpose : Read STEP file to DECAF document
288//=======================================================================
289
290static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
291{
292 if ( argc <3 ) {
293 di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document" << "\n";
294 return 0;
295 }
296
297 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
298 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
299
300 STEPCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
301
302 if (argc == 4) {
303 Standard_Boolean mode = Standard_True;
304 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
305 switch (argv[3][i]) {
306 case '-' : mode = Standard_False; break;
307 case '+' : mode = Standard_True; break;
308 case 'c' : reader.SetColorMode (mode); break;
309 case 'n' : reader.SetNameMode (mode); break;
310 case 'l' : reader.SetLayerMode (mode); break;
311 case 'v' : reader.SetPropsMode (mode); break;
312 }
313 }
314
315 TCollection_AsciiString fnom,rnom;
316 Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"STEP",fnom,rnom);
317 if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
318 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
319// di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
320 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
321 if (modfic) readstat = reader.ReadFile (fnom.ToCString());
322 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
323 if (readstat != IFSelect_RetDone) {
324 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
325 else di<<"No model loaded"<<"\n";
326 return 1;
327 }
328
329 Handle(TDocStd_Document) doc;
330 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
331 Handle(TDocStd_Application) A;
332 if (!DDocStd::Find(A)) {di<<"No application found"<<"\n";return 1;}
fff263bd 333 A->NewDocument("BinXCAF",doc);
7fd59977 334 TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);
335 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
336 Draw::Set(argv[1],DD);
337// di << "Document saved with name " << argv[1];
338 }
339 if ( ! reader.Transfer ( doc ) ) {
340 di << "Cannot read any relevant data from the STEP file" << "\n";
341 return 1;
342 }
343
344 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
345 Draw::Set(argv[1],DD);
346 di << "Document saved with name " << argv[1];
347
348 Handle(STEPCAFControl_DictionaryOfExternFile) DicFile = reader.ExternFiles();
349 FillDicWS( DicFile );
350 AddWS ( fnom , XSDRAW::Session() );
351
352 return 0;
353}
354
355//=======================================================================
356//function : WriteStep
357//purpose : Write DECAF document to STEP
358//=======================================================================
359
360static Standard_Integer WriteStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
361{
362 if ( argc <3 ) {
363 di << "Use: " << argv[0] << " Doc filename [mode=a [multifile_prefix]]: write document to the STEP file" << "\n";
364 di << "Parameter mode can be: a or 0 : AsIs (default)" << "\n";
365 di << "f or 1 : FacettedBRep s or 2 : ShellBasedSurfaceModel" << "\n";
366 di << "m or 3 : ManifoldSolidBrep w or 4 : GeometricCurveSet/WireFrame" << "\n";
367 return 0;
368 }
369
370 Handle(TDocStd_Document) Doc;
371 DDocStd::GetDocument(argv[1], Doc);
372 if ( Doc.IsNull() ) {
373 di << argv[1] << " is not a document" << "\n";
374 return 1;
375 }
376
377 Standard_CString multifile = ( argc >4 ? argv[4] : 0 );
378
379 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
380 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
381 STEPCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
382
383 STEPControl_StepModelType mode = STEPControl_AsIs;
384 if ( argc >3 ) {
385 switch (argv[3][0]) {
386 case 'a' :
387 case '0' : mode = STEPControl_AsIs; break;
388 case 'f' :
389 case '1' : mode = STEPControl_FacetedBrep; break;
390 case 's' :
391 case '2' : mode = STEPControl_ShellBasedSurfaceModel; break;
392 case 'm' :
393 case '3' : mode = STEPControl_ManifoldSolidBrep; break;
394 case 'w' :
395 case '4' : mode = STEPControl_GeometricCurveSet; break;
396 default : di<<"3st arg = mode, incorrect [give fsmw]"<<"\n"; return 1;
397 }
398 Standard_Boolean wrmode = Standard_True;
399 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
400 switch (argv[3][i]) {
401 case '-' : wrmode = Standard_False; break;
402 case '+' : wrmode = Standard_True; break;
403 case 'c' : writer.SetColorMode (wrmode); break;
404 case 'n' : writer.SetNameMode (wrmode); break;
405 case 'l' : writer.SetLayerMode (wrmode); break;
406 case 'v' : writer.SetPropsMode (wrmode); break;
407 }
408
409 }
410
411 di << "Translating document " << argv[1] << " to STEP" << "\n";
412 if ( ! writer.Transfer ( Doc, mode, multifile ) ) {
413 di << "The document cannot be translated or gives no result" << "\n";
414 }
415
416 di << "Writing STEP file " << argv[2] << "\n";
417 IFSelect_ReturnStatus stat = writer.Write(argv[2]);
418 switch (stat) {
419 case IFSelect_RetVoid : di<<"No file written"<<"\n"; break;
420 case IFSelect_RetDone : {
421 di<<"File "<<argv[2]<<" written"<<"\n";
422
423 Handle(STEPCAFControl_DictionaryOfExternFile) DicFile = writer.ExternFiles();
424 FillDicWS( DicFile );
425 AddWS( argv[2], XSDRAW::Session() );
426 break;
427 }
428 default : di<<"Error on writing file"<<"\n"; break;
429 }
430 return 0;
431}
432
433void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
434
435 static Standard_Boolean initactor = Standard_False;
436 if (initactor) return; initactor = Standard_True;
437
438 Standard_CString g = "XDE translation commands";
439
440 di.Add("ReadIges" , "Doc filename: Read IGES file to DECAF document" ,__FILE__, ReadIges, g);
441 di.Add("WriteIges" , "Doc filename: Write DECAF document to IGES file" ,__FILE__, WriteIges, g);
442 di.Add("ReadStep" , "Doc filename: Read STEP file to DECAF document" ,__FILE__, ReadStep, g);
443 di.Add("WriteStep" , "Doc filename: Write DECAF document to STEP file" ,__FILE__, WriteStep, g);
444
445 di.Add("XFileList","Print list of files that was transfered by the last transfer" ,__FILE__, GetDicWSList , g);
446 di.Add("XFileCur", ": returns name of file which is set as current",__FILE__, GetCurWS, g);
447 di.Add("XFileSet", "filename: Set the specified file to be the current one",__FILE__, SetCurWS, g);
448 di.Add("XFromShape", "shape: do fromshape command for all the files",__FILE__, FromShape, g);
449
450}