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