0028643: Coding rules - eliminate GCC compiler warnings -Wmisleading-indentation
[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>
7fd59977 19#include <Draw.hxx>
20#include <Draw_Interpretor.hxx>
42cf5bc1 21#include <IFSelect_SessionPilot.hxx>
7fd59977 22#include <IGESCAFControl_Reader.hxx>
23#include <IGESCAFControl_Writer.hxx>
42cf5bc1 24#include <IGESControl_Controller.hxx>
25#include <Interface_Macros.hxx>
7fd59977 26#include <STEPCAFControl_ExternFile.hxx>
42cf5bc1 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>
7fd59977 39#include <XSDRAW_Vars.hxx>
42cf5bc1 40#include <XSDRAWIGES.hxx>
41#include <XSDRAWSTEP.hxx>
f5e92b04 42#include <DDF.hxx>
7fd59977 43
5f07d05d 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
42cf5bc1 51#include <stdio.h>
7fd59977 52//============================================================
53// Support for several models in DRAW
54//============================================================
997e128f 55static NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> thedictws;
7fd59977 56
57static Standard_Boolean ClearDicWS()
58{
997e128f 59 thedictws.Clear();
7fd59977 60 return Standard_True;
61}
62
63static void AddWS(TCollection_AsciiString filename,
64 const Handle(XSControl_WorkSession)& WS)
65{
66 WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables
997e128f 67 thedictws.Bind( filename, WS );
7fd59977 68}
69
70
997e128f 71static Standard_Boolean FillDicWS(NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>& dicFile)
7fd59977 72{
73 ClearDicWS();
997e128f 74 if ( dicFile.IsEmpty() ) {
7fd59977 75 return Standard_False;
76 }
77 Handle(STEPCAFControl_ExternFile) EF;
997e128f 78 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>::Iterator DicEFIt(dicFile);
7fd59977 79 for (; DicEFIt.More(); DicEFIt.Next() ) {
997e128f 80 TCollection_AsciiString filename = DicEFIt.Key();
7fd59977 81 EF = DicEFIt.Value();
82 AddWS ( filename, EF->GetWS() );
83 }
84 return Standard_True;
85}
86
87static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename)
88{
997e128f 89 if ( !thedictws.IsBound(filename) ) return Standard_False;
7fd59977 90 Handle(XSControl_WorkSession) CurrentWS =
997e128f 91 Handle(XSControl_WorkSession)::DownCast( thedictws.ChangeFind(filename) );
7fd59977 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
103static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv)
104{
105 if (argc <2) {
586db386 106 di<<"Use: "<<argv[0]<<" filename \n";
7fd59977 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
120static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
121{
997e128f 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);
586db386 125 di << " The list of last translated files:\n";
7fd59977 126 Standard_Integer num = 0;
127 for (; DicIt.More() ; DicIt.Next(), num++ ) {
997e128f 128 TCollection_AsciiString strng ( DicIt.Key() );
7fd59977 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
140static 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
152static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
153{
154 if ( argc <2 ) {
586db386 155 di << argv[0] << " shape: search for shape origin among all last tranalated files\n";
7fd59977 156 return 0;
157 }
158
159 char command[256];
91322f44 160 Sprintf ( command, "fromshape %.200s -1", argv[1] );
997e128f 161 NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> DictWS = thedictws;
162 if ( DictWS.IsEmpty() ) return di.Eval ( command );
7fd59977 163
164 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
165
997e128f 166 NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator DicIt ( DictWS );
586db386 167// di << "Searching for shape among all the loaded files:\n";
7fd59977 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
185static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
186{
187 if ( argc <3 ) {
586db386 188 di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document\n";
7fd59977 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) {
586db386 217 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
218 else di<<"No model loaded\n";
7fd59977 219 return 1;
220 }
221
222 Handle(TDocStd_Document) doc;
223 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
6fe96f84 224 Handle(TDocStd_Application) A = DDocStd::GetApplication();
fff263bd 225 A->NewDocument("BinXCAF",doc);
7fd59977 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 ) ) {
586db386 232 di << "Cannot read any relevant data from the IGES file\n";
7fd59977 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
248static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
249{
250 if ( argc <3 ) {
586db386 251 di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file\n";
7fd59977 252 return 0;
253 }
254
255 Handle(TDocStd_Document) Doc;
256 DDocStd::GetDocument(argv[1], Doc);
257 if ( Doc.IsNull() ) {
586db386 258 di << argv[1] << " is not a document\n";
7fd59977 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";
586db386 282 if ( writer.Write ( argv[2] ) ) di<<" Write OK\n";
283 else di<<" Write failed\n";
7fd59977 284
285 return 0;
286}
287
288//=======================================================================
289//function : ReadStep
290//purpose : Read STEP file to DECAF document
291//=======================================================================
292
293static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
294{
295 if ( argc <3 ) {
586db386 296 di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document\n";
7fd59977 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) {
586db386 327 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
328 else di<<"No model loaded\n";
7fd59977 329 return 1;
330 }
331
332 Handle(TDocStd_Document) doc;
333 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
6fe96f84 334 Handle(TDocStd_Application) A = DDocStd::GetApplication();
fff263bd 335 A->NewDocument("BinXCAF",doc);
7fd59977 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 ) ) {
586db386 342 di << "Cannot read any relevant data from the STEP file\n";
7fd59977 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
997e128f 350 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = reader.ExternFiles();
7fd59977 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 ) {
586db386 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";
f5e92b04 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";
7fd59977 372 return 0;
373 }
374
375 Handle(TDocStd_Document) Doc;
376 DDocStd::GetDocument(argv[1], Doc);
377 if ( Doc.IsNull() ) {
586db386 378 di << argv[1] << " is not a document\n";
7fd59977 379 return 1;
380 }
f5e92b04 381 Standard_CString multifile = 0;
7fd59977 382
f5e92b04 383 Standard_Integer k = 3;
7fd59977 384 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
385 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
386 STEPCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
f5e92b04 387
7fd59977 388 STEPControl_StepModelType mode = STEPControl_AsIs;
f5e92b04 389 if ( argc > k ) {
390 switch (argv[k][0]) {
7fd59977 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;
586db386 401 default : di<<"3st arg = mode, incorrect [give fsmw]\n"; return 1;
7fd59977 402 }
403 Standard_Boolean wrmode = Standard_True;
f5e92b04 404 for ( Standard_Integer i = 0; argv[k][i] ; i++ )
7fd59977 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 }
f5e92b04 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++];
7fd59977 422
423 }
f5e92b04 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 }
7fd59977 432 }
f5e92b04 433 if( !label.IsNull())
434 {
586db386 435 di << "Translating label "<< argv[k]<<" of document " << argv[1] << " to STEP\n";
f5e92b04 436 if(!writer.Transfer ( label, mode, multifile ))
437 {
586db386 438 di << "The label of document cannot be translated or gives no result\n";
f5e92b04 439 return 1;
440 }
441
442 }
443 else
444 {
586db386 445 di << "Translating document " << argv[1] << " to STEP\n";
f5e92b04 446 if ( ! writer.Transfer ( Doc, mode, multifile ) ) {
586db386 447 di << "The document cannot be translated or gives no result\n";
f5e92b04 448 }
449 }
450
7fd59977 451
452 di << "Writing STEP file " << argv[2] << "\n";
453 IFSelect_ReturnStatus stat = writer.Write(argv[2]);
454 switch (stat) {
586db386 455 case IFSelect_RetVoid : di<<"No file written\n"; break;
7fd59977 456 case IFSelect_RetDone : {
586db386 457 di<<"File "<<argv[2]<<" written\n";
7fd59977 458
997e128f 459 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = writer.ExternFiles();
7fd59977 460 FillDicWS( DicFile );
461 AddWS( argv[2], XSDRAW::Session() );
462 break;
463 }
586db386 464 default : di<<"Error on writing file\n"; break;
7fd59977 465 }
466 return 0;
467}
468
5f07d05d 469static Standard_Integer Expand (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
470{
471 if (argc < 3) {
586db386 472 di<<"Use: "<<argv[0]<<" Doc recurs(0/1) or Doc recurs(0/1) label1 label2 ... or Doc recurs(0/1 shape1 shape2 ...\n";
5f07d05d 473 return 1;
474 }
475 Handle(TDocStd_Document) Doc;
476 DDocStd::GetDocument(argv[1], Doc);
586db386 477 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
5f07d05d 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)){
586db386 487 di << "The shape is assembly or not compaund\n";
5f07d05d 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)){
586db386 505 di << "The shape is assembly or not compaund\n";
5f07d05d 506 return 1;
507 }
508 }
509 else
586db386 510 { di << argv[i] << " is not a shape\n"; return 1; }
5f07d05d 511 }
512 }
513 return 0;
514}
515
c48e2889 516void XDEDRAW_Common::InitCommands(Draw_Interpretor& di)
517{
7fd59977 518 static Standard_Boolean initactor = Standard_False;
c48e2889 519 if (initactor)
520 {
521 return;
522 }
523 initactor = Standard_True;
7fd59977 524
525 Standard_CString g = "XDE translation commands";
526
527 di.Add("ReadIges" , "Doc filename: Read IGES file to DECAF document" ,__FILE__, ReadIges, g);
528 di.Add("WriteIges" , "Doc filename: Write DECAF document to IGES file" ,__FILE__, WriteIges, g);
529 di.Add("ReadStep" , "Doc filename: Read STEP file to DECAF document" ,__FILE__, ReadStep, g);
f5e92b04 530 di.Add("WriteStep" , "Doc filename [mode=a [multifile_prefix] [label]]: Write DECAF document to STEP file" ,__FILE__, WriteStep, g);
7fd59977 531
532 di.Add("XFileList","Print list of files that was transfered by the last transfer" ,__FILE__, GetDicWSList , g);
533 di.Add("XFileCur", ": returns name of file which is set as current",__FILE__, GetCurWS, g);
534 di.Add("XFileSet", "filename: Set the specified file to be the current one",__FILE__, SetCurWS, g);
535 di.Add("XFromShape", "shape: do fromshape command for all the files",__FILE__, FromShape, g);
536
5f07d05d 537 di.Add("XExpand", "XExpand Doc recursively(0/1) or XExpand Doc recursively(0/1) label1 abel2 ..."
538 "or XExpand Doc recursively(0/1) shape1 shape2 ...",__FILE__, Expand, g);
539
7fd59977 540}