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