Warnings on vc14 were eliminated
[occt.git] / src / XDEDRAW / XDEDRAW_Common.cxx
1 // Created on: 2003-08-15
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <DDocStd.hxx>
18 #include <DDocStd_DrawDocument.hxx>
19 #include <Draw.hxx>
20 #include <Draw_Interpretor.hxx>
21 #include <IFSelect_SessionPilot.hxx>
22 #include <IGESCAFControl_Reader.hxx>
23 #include <IGESCAFControl_Writer.hxx>
24 #include <IGESControl_Controller.hxx>
25 #include <Interface_Macros.hxx>
26 #include <STEPCAFControl_ExternFile.hxx>
27 #include <STEPCAFControl_Reader.hxx>
28 #include <STEPCAFControl_Writer.hxx>
29 #include <STEPControl_Controller.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TDataStd_Name.hxx>
32 #include <TDF_Data.hxx>
33 #include <TDocStd_Application.hxx>
34 #include <TDocStd_Document.hxx>
35 #include <XDEDRAW.hxx>
36 #include <XDEDRAW_Common.hxx>
37 #include <XSControl_WorkSession.hxx>
38 #include <XSDRAW.hxx>
39 #include <XSDRAW_Vars.hxx>
40 #include <XSDRAWIGES.hxx>
41 #include <XSDRAWSTEP.hxx>
42 #include <DDF.hxx>
43
44 #include <DBRep.hxx>
45 #include <XCAFDoc_DocumentTool.hxx>
46 #include <XCAFDoc_ShapeTool.hxx>
47 #include <XCAFDoc_Editor.hxx>
48 #include <TDF_Tool.hxx>
49 #include <TopoDS_Shape.hxx>
50
51 #include <stdio.h>
52 //============================================================
53 // Support for several models in DRAW
54 //============================================================
55 static NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> thedictws;
56
57 static Standard_Boolean ClearDicWS()
58 {
59   thedictws.Clear();
60   return Standard_True;
61 }
62
63 static void AddWS(TCollection_AsciiString filename,
64                   const Handle(XSControl_WorkSession)& WS)
65 {
66   WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables
67   thedictws.Bind( filename, WS );
68 }
69
70
71 static Standard_Boolean FillDicWS(NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>& dicFile)
72 {
73   ClearDicWS();
74   if ( dicFile.IsEmpty() ) {
75     return Standard_False;
76   }
77   Handle(STEPCAFControl_ExternFile) EF;
78   NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>::Iterator DicEFIt(dicFile);
79   for (; DicEFIt.More(); DicEFIt.Next() ) {
80     TCollection_AsciiString filename = DicEFIt.Key();
81     EF = DicEFIt.Value();
82     AddWS ( filename, EF->GetWS() );
83   }
84   return Standard_True;  
85 }
86
87 static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename)
88 {
89   if ( !thedictws.IsBound(filename) ) return Standard_False;
90   Handle(XSControl_WorkSession) CurrentWS = 
91     Handle(XSControl_WorkSession)::DownCast( thedictws.ChangeFind(filename) );
92   XSDRAW::Pilot()->SetSession( CurrentWS );
93   
94   return Standard_True;
95 }
96
97
98 //=======================================================================
99 //function : SetCurWS
100 //purpose  : Set current file if many files are read
101 //=======================================================================
102
103 static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv)
104 {
105   if (argc <2) {
106     di<<"Use: "<<argv[0]<<" filename \n";
107     return 1;
108   }
109   TCollection_AsciiString filename (argv[1]);
110   SetCurrentWS( filename );
111   return 0;
112 }
113
114
115 //=======================================================================
116 //function : GetDicWSList
117 //purpose  : List all files recorded after translation
118 //=======================================================================
119
120 static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
121 {
122   NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> DictWS = thedictws;
123   if ( DictWS.IsEmpty() ) return 1;
124   NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator DicIt(DictWS);
125   di << " The list of last translated files:\n";
126   Standard_Integer num = 0;
127   for (; DicIt.More() ; DicIt.Next(), num++ ) {
128     TCollection_AsciiString strng ( DicIt.Key() );
129     if ( num ) di << "\n";
130     di << "\"" << strng.ToCString() << "\"";
131   }
132   return 0;
133 }
134
135 //=======================================================================
136 //function : GetCurWS
137 //purpose  : Return name of file which is current
138 //=======================================================================
139
140 static Standard_Integer GetCurWS (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
141 {
142   Handle(XSControl_WorkSession) WS = XSDRAW::Session();
143   di << "\"" << WS->LoadedFile() << "\"";
144   return 0;
145 }
146
147 //=======================================================================
148 //function : FromShape
149 //purpose  : Apply fromshape command to all the loaded WSs
150 //=======================================================================
151
152 static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
153 {
154   if ( argc <2 ) {
155     di << argv[0] << " shape: search for shape origin among all last tranalated files\n";
156     return 0;
157   }
158   
159   char command[256];
160   Sprintf ( command, "fromshape %.200s -1", argv[1] );
161   NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> DictWS = thedictws;
162   if ( DictWS.IsEmpty() ) return di.Eval ( command );
163   
164   Handle(XSControl_WorkSession) WS = XSDRAW::Session();
165
166   NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator DicIt ( DictWS );
167 //  di << "Searching for shape among all the loaded files:\n";
168   Standard_Integer num = 0;
169   for (; DicIt.More() ; DicIt.Next(), num++ ) {
170     Handle(XSControl_WorkSession) CurrentWS = 
171       Handle(XSControl_WorkSession)::DownCast( DicIt.Value() );
172     XSDRAW::Pilot()->SetSession( CurrentWS );
173     di.Eval ( command );
174   }
175
176   XSDRAW::Pilot()->SetSession( WS );
177   return 0;
178 }
179
180 //=======================================================================
181 //function : ReadIges
182 //purpose  : Read IGES to DECAF document
183 //=======================================================================
184
185 static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
186 {
187   if ( argc <3 ) {
188     di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document\n";
189     return 0;
190   }
191   
192   DeclareAndCast(IGESControl_Controller,ctl,XSDRAW::Controller());
193   if (ctl.IsNull()) XSDRAW::SetNorm("IGES");
194
195   IGESCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
196   
197   if (argc == 4) {
198     Standard_Boolean mode = Standard_True;
199     for ( Standard_Integer i = 0; argv[3][i] ; i++ ) 
200       switch (argv[3][i]) {
201       case '-' : mode = Standard_False; break;
202       case '+' : mode = Standard_True; break;
203       case 'c' : reader.SetColorMode (mode); break;
204       case 'n' : reader.SetNameMode (mode); break;
205       case 'l' : reader.SetLayerMode (mode); break;
206       }
207   }
208   TCollection_AsciiString fnom,rnom;
209   Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"IGES",fnom,rnom);
210   if (modfic) di<<" File IGES to read : "<<fnom.ToCString()<<"\n";
211   else        di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
212 //  di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
213   IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
214   if (modfic) readstat = reader.ReadFile (fnom.ToCString());
215   else  if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
216   if (readstat != IFSelect_RetDone) {
217     if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
218     else di<<"No model loaded\n";
219     return 1;
220   }
221
222   Handle(TDocStd_Document) doc;
223   if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {  
224     Handle(TDocStd_Application) A = DDocStd::GetApplication();
225     A->NewDocument("BinXCAF",doc);  
226     TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);  
227     Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);  
228     Draw::Set(argv[1],DD);       
229 //     di << "Document saved with name " << argv[1];
230   }
231   if ( ! reader.Transfer ( doc ) ) {
232     di << "Cannot read any relevant data from the IGES file\n";
233     return 1;
234   }
235   
236 //  Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);  
237 //  Draw::Set(argv[1],DD);       
238   di << "Document saved with name " << argv[1];
239   
240   return 0;
241 }
242
243 //=======================================================================
244 //function : WriteIges
245 //purpose  : Write DECAF document to IGES
246 //=======================================================================
247
248 static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv) 
249 {
250   if ( argc <3 ) {
251     di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file\n";
252     return 0;
253   }
254   
255   Handle(TDocStd_Document) Doc;
256   DDocStd::GetDocument(argv[1], Doc);
257   if ( Doc.IsNull() ) {
258     di << argv[1] << " is not a document\n";
259     return 1;
260   }
261   
262   XSDRAW::SetNorm ("IGES");
263   
264 //  IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
265 //                        Interface_Static::IVal("write.iges.brep.mode"));
266     
267   IGESCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
268   if (argc == 4) {
269     Standard_Boolean mode = Standard_True;
270     for ( Standard_Integer i = 0; argv[3][i] ; i++ ) 
271       switch (argv[3][i]) {
272       case '-' : mode = Standard_False; break;
273       case '+' : mode = Standard_True; break;
274       case 'c' : writer.SetColorMode (mode); break;
275       case 'n' : writer.SetNameMode (mode); break;
276       case 'l' : writer.SetLayerMode (mode); break;
277       }
278   }
279   writer.Transfer ( Doc );
280
281   di << "Writig IGES model to file " << argv[2] << "\n";
282   if ( writer.Write ( argv[2] ) ) di<<" Write OK\n";
283   else di<<" Write failed\n";
284
285   return 0;
286 }
287
288 //=======================================================================
289 //function : ReadStep
290 //purpose  : Read STEP file to DECAF document 
291 //=======================================================================
292
293 static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
294 {
295   if ( argc <3 ) {
296     di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document\n";
297     return 0;
298   }
299   
300   DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
301   if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
302
303   STEPCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
304   
305   if (argc == 4) {
306     Standard_Boolean mode = Standard_True;
307     for ( Standard_Integer i = 0; argv[3][i] ; i++ ) 
308       switch (argv[3][i]) {
309       case '-' : mode = Standard_False; break;
310       case '+' : mode = Standard_True; break;
311       case 'c' : reader.SetColorMode (mode); break;
312       case 'n' : reader.SetNameMode (mode); break;
313       case 'l' : reader.SetLayerMode (mode); break;
314       case 'v' : reader.SetPropsMode (mode); break;
315       }
316   }
317   
318   TCollection_AsciiString fnom,rnom;
319   Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"STEP",fnom,rnom);
320   if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
321   else        di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
322 //  di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
323   IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
324   if (modfic) readstat = reader.ReadFile (fnom.ToCString());
325   else  if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
326   if (readstat != IFSelect_RetDone) {
327     if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
328     else di<<"No model loaded\n";
329     return 1;
330   }
331
332   Handle(TDocStd_Document) doc;
333   if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {  
334     Handle(TDocStd_Application) A = DDocStd::GetApplication();
335     A->NewDocument("BinXCAF",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   NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> 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
362 static 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 [multifile_prefix [label]]]: write document to the STEP file\n";
366     di << "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     di << "multifile_prefix: triggers writing assembly components as separate files,\n";
370     di << "                  and defines common prefix for their names\n";
371     di << "label: tag of the sub-assembly label to save only that sub-assembly\n";
372     return 0;
373   }
374   
375   Handle(TDocStd_Document) Doc;   
376   DDocStd::GetDocument(argv[1], Doc);
377   if ( Doc.IsNull() ) {
378     di << argv[1] << " is not a document\n";
379     return 1;
380   }
381   Standard_CString multifile = 0;
382   
383   Standard_Integer k = 3;
384   DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
385   if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
386   STEPCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
387    
388   STEPControl_StepModelType mode = STEPControl_AsIs;
389   if ( argc > k ) {
390     switch (argv[k][0]) {
391     case 'a' :
392     case '0' : mode = STEPControl_AsIs;                    break;
393     case 'f' :
394     case '1' : mode = STEPControl_FacetedBrep;             break;
395     case 's' :
396     case '2' : mode = STEPControl_ShellBasedSurfaceModel;  break;
397     case 'm' :
398     case '3' : mode = STEPControl_ManifoldSolidBrep;       break;
399     case 'w' :
400     case '4' : mode = STEPControl_GeometricCurveSet;       break;
401     default :  di<<"3st arg = mode, incorrect [give fsmw]\n"; return 1;
402     }
403     Standard_Boolean wrmode = Standard_True;
404     for ( Standard_Integer i = 0; argv[k][i] ; i++ ) 
405       switch (argv[3][i]) {
406       case '-' : wrmode = Standard_False; break;
407       case '+' : wrmode = Standard_True; break;
408       case 'c' : writer.SetColorMode (wrmode); break;
409       case 'n' : writer.SetNameMode (wrmode); break;
410       case 'l' : writer.SetLayerMode (wrmode); break;
411       case 'v' : writer.SetPropsMode (wrmode); break;
412       }
413     k++;
414   }
415
416   TDF_Label label;
417   if( argc > k)
418   {
419     TCollection_AsciiString aStr(argv[k]);
420     if( aStr.Search(":") ==-1)
421       multifile = argv[k++];
422     
423   }
424   if( argc > k)
425   {
426       
427     if( !DDF::FindLabel(Doc->Main().Data(), argv[k], label) || label.IsNull()) {  
428       di << "No label for entry"  << "\n";
429       return 1; 
430       
431     }
432   }
433   if( !label.IsNull())
434   {  
435     di << "Translating label "<< argv[k]<<" of document " << argv[1] << " to STEP\n";
436     if(!writer.Transfer ( label, mode, multifile )) 
437     {
438       di << "The label of document cannot be translated or gives no result\n";
439       return 1;
440     }
441
442   }
443   else
444   {
445     di << "Translating document " << argv[1] << " to STEP\n";
446     if ( ! writer.Transfer ( Doc, mode, multifile ) ) {
447       di << "The document cannot be translated or gives no result\n";
448     }
449   }
450   
451
452   di << "Writing STEP file " << argv[2] << "\n";
453   IFSelect_ReturnStatus stat = writer.Write(argv[2]);
454   switch (stat) {
455     case IFSelect_RetVoid : di<<"No file written\n"; break;
456     case IFSelect_RetDone : {
457       di<<"File "<<argv[2]<<" written\n";
458
459       NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = writer.ExternFiles();
460       FillDicWS( DicFile );
461       AddWS( argv[2], XSDRAW::Session() );
462       break;
463     }
464     default : di<<"Error on writing file\n"; break;
465   }
466   return 0;
467 }
468
469 static Standard_Integer Expand (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
470 {
471   if (argc < 3) {
472     di<<"Use: "<<argv[0]<<" Doc recurs(0/1) or Doc recurs(0/1) label1 label2 ... or Doc recurs(0/1 shape1 shape2 ...\n";
473     return 1;
474   }
475   Handle(TDocStd_Document) Doc;   
476   DDocStd::GetDocument(argv[1], Doc);
477   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
478
479   Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
480   Standard_Boolean recurs = Standard_False;
481   if(atoi(argv[2]) != 0)
482     recurs = Standard_True;
483
484   if (argc == 3)
485   {
486     if(!XCAFDoc_Editor::Expand(Doc->Main(), recurs)){
487       di << "The shape is assembly or not compaund\n";
488       return 1;
489     }
490   }
491   else 
492   {
493     for (Standard_Integer i = 3; i < argc; i++)
494     {
495       TDF_Label aLabel;
496       TDF_Tool::Label(Doc->GetData(), argv[i], aLabel);
497       if(aLabel.IsNull()){
498         TopoDS_Shape aShape;
499         aShape = DBRep::Get(argv[i]);
500         aLabel = aShapeTool->FindShape(aShape);
501       }
502
503       if (!aLabel.IsNull()){
504         if(!XCAFDoc_Editor::Expand(Doc->Main(), aLabel, recurs)){
505           di << "The shape is assembly or not compaund\n";
506           return 1;
507         }
508       }
509       else
510       { di << argv[i] << " is not a shape\n"; return 1; }
511     }
512   }
513   return 0;
514 }
515
516 void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
517
518   static Standard_Boolean initactor = Standard_False;
519   if (initactor) return;  initactor = Standard_True;
520
521   Standard_CString g = "XDE translation commands";
522
523   di.Add("ReadIges" , "Doc filename: Read IGES file to DECAF document" ,__FILE__, ReadIges, g);
524   di.Add("WriteIges" , "Doc filename: Write DECAF document to IGES file" ,__FILE__, WriteIges, g);
525   di.Add("ReadStep" , "Doc filename: Read STEP file to DECAF document" ,__FILE__, ReadStep, g);
526   di.Add("WriteStep" , "Doc filename [mode=a [multifile_prefix] [label]]: Write DECAF document to STEP file" ,__FILE__, WriteStep, g);  
527   
528   di.Add("XFileList","Print list of files that was transfered by the last transfer" ,__FILE__, GetDicWSList , g);
529   di.Add("XFileCur", ": returns name of file which is set as current",__FILE__, GetCurWS, g);
530   di.Add("XFileSet", "filename: Set the specified file to be the current one",__FILE__, SetCurWS, g);
531   di.Add("XFromShape", "shape: do fromshape command for all the files",__FILE__, FromShape, g);
532
533   di.Add("XExpand", "XExpand Doc recursively(0/1) or XExpand Doc recursively(0/1) label1 abel2 ..."  
534           "or XExpand Doc recursively(0/1) shape1 shape2 ...",__FILE__, Expand, g);
535
536 }