1 // Created on: 2003-08-15
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <DDocStd.hxx>
18 #include <DDocStd_DrawDocument.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>
39 #include <XSDRAW_Vars.hxx>
40 #include <XSDRAWIGES.hxx>
41 #include <XSDRAWSTEP.hxx>
42 #include <VrmlAPI_Writer.hxx>
46 #include <XCAFDoc_DocumentTool.hxx>
47 #include <XCAFDoc_ShapeTool.hxx>
48 #include <XCAFDoc_Editor.hxx>
49 #include <TDF_Tool.hxx>
50 #include <TopoDS_Shape.hxx>
51 #include <Interface_Static.hxx>
52 #include <UnitsMethods.hxx>
56 //============================================================
57 // Support for several models in DRAW
58 //============================================================
59 static NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> thedictws;
61 static Standard_Boolean ClearDicWS()
67 static void AddWS(TCollection_AsciiString filename,
68 const Handle(XSControl_WorkSession)& WS)
70 WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables
71 thedictws.Bind( filename, WS );
75 static Standard_Boolean FillDicWS(NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>& dicFile)
78 if ( dicFile.IsEmpty() ) {
79 return Standard_False;
81 Handle(STEPCAFControl_ExternFile) EF;
82 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>::Iterator DicEFIt(dicFile);
83 for (; DicEFIt.More(); DicEFIt.Next() ) {
84 TCollection_AsciiString filename = DicEFIt.Key();
86 AddWS ( filename, EF->GetWS() );
91 static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename)
93 if ( !thedictws.IsBound(filename) ) return Standard_False;
94 Handle(XSControl_WorkSession) CurrentWS =
95 Handle(XSControl_WorkSession)::DownCast( thedictws.ChangeFind(filename) );
96 XSDRAW::Pilot()->SetSession( CurrentWS );
102 //=======================================================================
103 //function : SetCurWS
104 //purpose : Set current file if many files are read
105 //=======================================================================
107 static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv)
110 di<<"Use: "<<argv[0]<<" filename \n";
113 TCollection_AsciiString filename (argv[1]);
114 SetCurrentWS( filename );
119 //=======================================================================
120 //function : GetDicWSList
121 //purpose : List all files recorded after translation
122 //=======================================================================
124 static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
126 NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> DictWS = thedictws;
127 if ( DictWS.IsEmpty() ) return 1;
128 NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator DicIt(DictWS);
129 di << " The list of last translated files:\n";
130 Standard_Integer num = 0;
131 for (; DicIt.More() ; DicIt.Next(), num++ ) {
132 TCollection_AsciiString strng ( DicIt.Key() );
133 if ( num ) di << "\n";
134 di << "\"" << strng.ToCString() << "\"";
139 //=======================================================================
140 //function : GetCurWS
141 //purpose : Return name of file which is current
142 //=======================================================================
144 static Standard_Integer GetCurWS (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
146 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
147 di << "\"" << WS->LoadedFile() << "\"";
151 //=======================================================================
152 //function : FromShape
153 //purpose : Apply fromshape command to all the loaded WSs
154 //=======================================================================
156 static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
159 di << argv[0] << " shape: search for shape origin among all last tranalated files\n";
164 Sprintf ( command, "fromshape %.200s -1", argv[1] );
165 NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> DictWS = thedictws;
166 if ( DictWS.IsEmpty() ) return di.Eval ( command );
168 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
170 NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator DicIt ( DictWS );
171 // di << "Searching for shape among all the loaded files:\n";
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 );
180 XSDRAW::Pilot()->SetSession( WS );
184 //=======================================================================
185 //function : ReadIges
186 //purpose : Read IGES to DECAF document
187 //=======================================================================
189 static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
192 di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document\n";
196 DeclareAndCast(IGESControl_Controller,ctl,XSDRAW::Controller());
197 if (ctl.IsNull()) XSDRAW::SetNorm("IGES");
199 TCollection_AsciiString fnom, rnom;
200 Standard_Boolean modfic = XSDRAW::FileAndVar(argv[2], argv[1], "IGES", fnom, rnom);
201 if (modfic) di << " File IGES to read : " << fnom.ToCString() << "\n";
202 else di << " Model taken from the session : " << fnom.ToCString() << "\n";
203 // di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
205 IGESCAFControl_Reader reader ( XSDRAW::Session(),modfic);
206 Standard_Integer onlyvisible = Interface_Static::IVal("read.iges.onlyvisible");
207 reader.SetReadVisible(onlyvisible == 1);
210 Standard_Boolean mode = Standard_True;
211 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
212 switch (argv[3][i]) {
213 case '-' : mode = Standard_False; break;
214 case '+' : mode = Standard_True; break;
215 case 'c' : reader.SetColorMode (mode); break;
216 case 'n' : reader.SetNameMode (mode); break;
217 case 'l' : reader.SetLayerMode (mode); break;
220 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
221 if (modfic) readstat = reader.ReadFile (fnom.ToCString());
222 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
223 if (readstat != IFSelect_RetDone) {
224 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
225 else di<<"No model loaded\n";
229 Handle(TDocStd_Document) doc;
230 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
231 Handle(TDocStd_Application) A = DDocStd::GetApplication();
232 A->NewDocument("BinXCAF",doc);
233 TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);
234 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
235 Draw::Set(argv[1],DD);
236 // di << "Document saved with name " << argv[1];
238 if ( ! reader.Transfer ( doc ) ) {
239 di << "Cannot read any relevant data from the IGES file\n";
243 // Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
244 // Draw::Set(argv[1],DD);
245 di << "Document saved with name " << argv[1];
250 //=======================================================================
251 //function : WriteIges
252 //purpose : Write DECAF document to IGES
253 //=======================================================================
255 static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
258 di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file\n";
262 Handle(TDocStd_Document) Doc;
263 DDocStd::GetDocument(argv[1], Doc);
264 if ( Doc.IsNull() ) {
265 di << argv[1] << " is not a document\n";
269 XSDRAW::SetNorm ("IGES");
271 // IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
272 // Interface_Static::IVal("write.iges.brep.mode"));
274 IGESCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
276 Standard_Boolean mode = Standard_True;
277 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
278 switch (argv[3][i]) {
279 case '-' : mode = Standard_False; break;
280 case '+' : mode = Standard_True; break;
281 case 'c' : writer.SetColorMode (mode); break;
282 case 'n' : writer.SetNameMode (mode); break;
283 case 'l' : writer.SetLayerMode (mode); break;
286 writer.Transfer ( Doc );
288 TCollection_AsciiString fnom, rnom;
289 Standard_Boolean modfic = XSDRAW::FileAndVar(argv[2], argv[1], "IGES", fnom, rnom);
292 di << "Writig IGES model to file " << argv[2] << "\n";
293 if ( writer.Write ( argv[2] ) ) di<<" Write OK\n";
294 else di<<" Write failed\n";
298 di << "Document has been translated into the session";
303 //=======================================================================
304 //function : ReadStep
305 //purpose : Read STEP file to DECAF document
306 //=======================================================================
308 static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
311 di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document\n";
315 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
316 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
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";
324 STEPCAFControl_Reader reader ( XSDRAW::Session(),modfic);
327 Standard_Boolean mode = Standard_True;
328 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
329 switch (argv[3][i]) {
330 case '-' : mode = Standard_False; break;
331 case '+' : mode = Standard_True; break;
332 case 'c' : reader.SetColorMode (mode); break;
333 case 'n' : reader.SetNameMode (mode); break;
334 case 'l' : reader.SetLayerMode (mode); break;
335 case 'v' : reader.SetPropsMode (mode); break;
339 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
340 if (modfic) readstat = reader.ReadFile (fnom.ToCString());
341 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
342 if (readstat != IFSelect_RetDone) {
343 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
344 else di<<"No model loaded\n";
348 Handle(TDocStd_Document) doc;
349 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
350 Handle(TDocStd_Application) A = DDocStd::GetApplication();
351 A->NewDocument("BinXCAF",doc);
352 TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);
353 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
354 Draw::Set(argv[1],DD);
355 // di << "Document saved with name " << argv[1];
357 if ( ! reader.Transfer ( doc ) ) {
358 di << "Cannot read any relevant data from the STEP file\n";
362 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
363 Draw::Set(argv[1],DD);
364 di << "Document saved with name " << argv[1];
366 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = reader.ExternFiles();
367 FillDicWS( DicFile );
368 AddWS ( fnom , XSDRAW::Session() );
373 //=======================================================================
374 //function : WriteStep
375 //purpose : Write DECAF document to STEP
376 //=======================================================================
378 static Standard_Integer WriteStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
381 di << "Use: " << argv[0] << " Doc filename [mode [multifile_prefix [label]]]: write document to the STEP file\n";
382 di << "mode can be: a or 0 : AsIs (default)\n";
383 di << " f or 1 : FacettedBRep s or 2 : ShellBasedSurfaceModel\n";
384 di << " m or 3 : ManifoldSolidBrep w or 4 : GeometricCurveSet/WireFrame\n";
385 di << "multifile_prefix: triggers writing assembly components as separate files,\n";
386 di << " and defines common prefix for their names\n";
387 di << "label: tag of the sub-assembly label to save only that sub-assembly\n";
391 Handle(TDocStd_Document) Doc;
392 DDocStd::GetDocument(argv[1], Doc);
393 if ( Doc.IsNull() ) {
394 di << argv[1] << " is not a document\n";
397 Standard_CString multifile = 0;
399 Standard_Integer k = 3;
400 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
401 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
402 STEPCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
404 STEPControl_StepModelType mode = STEPControl_AsIs;
406 switch (argv[k][0]) {
408 case '0' : mode = STEPControl_AsIs; break;
410 case '1' : mode = STEPControl_FacetedBrep; break;
412 case '2' : mode = STEPControl_ShellBasedSurfaceModel; break;
414 case '3' : mode = STEPControl_ManifoldSolidBrep; break;
416 case '4' : mode = STEPControl_GeometricCurveSet; break;
417 default : di<<"3st arg = mode, incorrect [give fsmw]\n"; return 1;
419 Standard_Boolean wrmode = Standard_True;
420 for ( Standard_Integer i = 0; argv[k][i] ; i++ )
421 switch (argv[3][i]) {
422 case '-' : wrmode = Standard_False; break;
423 case '+' : wrmode = Standard_True; break;
424 case 'c' : writer.SetColorMode (wrmode); break;
425 case 'n' : writer.SetNameMode (wrmode); break;
426 case 'l' : writer.SetLayerMode (wrmode); break;
427 case 'v' : writer.SetPropsMode (wrmode); break;
435 TCollection_AsciiString aStr(argv[k]);
436 if( aStr.Search(":") ==-1)
437 multifile = argv[k++];
443 if( !DDF::FindLabel(Doc->Main().Data(), argv[k], label) || label.IsNull()) {
444 di << "No label for entry" << "\n";
451 di << "Translating label "<< argv[k]<<" of document " << argv[1] << " to STEP\n";
452 if(!writer.Transfer ( label, mode, multifile ))
454 di << "The label of document cannot be translated or gives no result\n";
461 di << "Translating document " << argv[1] << " to STEP\n";
462 if ( ! writer.Transfer ( Doc, mode, multifile ) ) {
463 di << "The document cannot be translated or gives no result\n";
467 TCollection_AsciiString fnom, rnom;
468 Standard_Boolean modfic = XSDRAW::FileAndVar(argv[2], argv[1], "STEP", fnom, rnom);
471 di << "Writing STEP file " << argv[2] << "\n";
472 IFSelect_ReturnStatus stat = writer.Write(argv[2]);
474 case IFSelect_RetVoid : di<<"No file written\n"; break;
475 case IFSelect_RetDone : {
476 di<<"File "<<argv[2]<<" written\n";
478 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = writer.ExternFiles();
479 FillDicWS( DicFile );
480 AddWS( argv[2], XSDRAW::Session() );
483 default : di<<"Error on writing file\n"; break;
488 di << "Document has been translated into the session";
493 static Standard_Integer Expand (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
496 di<<"Use: "<<argv[0]<<" Doc recurs(0/1) or Doc recurs(0/1) label1 label2 ... or Doc recurs(0/1 shape1 shape2 ...\n";
499 Handle(TDocStd_Document) Doc;
500 DDocStd::GetDocument(argv[1], Doc);
501 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
503 Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
504 Standard_Boolean recurs = Standard_False;
505 if(atoi(argv[2]) != 0)
506 recurs = Standard_True;
510 if(!XCAFDoc_Editor::Expand(Doc->Main(), recurs)){
511 di << "No suitable labels to expand\n";
517 for (Standard_Integer i = 3; i < argc; i++)
520 TDF_Tool::Label(Doc->GetData(), argv[i], aLabel);
523 aShape = DBRep::Get(argv[i]);
524 aLabel = aShapeTool->FindShape(aShape);
527 if (!aLabel.IsNull()){
528 if(!XCAFDoc_Editor::Expand(Doc->Main(), aLabel, recurs)){
529 di << "The shape is assembly or not compound\n";
534 { di << argv[i] << " is not a shape\n"; return 1; }
541 //=======================================================================
542 //function : WriteVrml
543 //purpose : Write DECAF document to Vrml
544 //=======================================================================
546 static Standard_Integer WriteVrml(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
549 di << "Use: " << argv[0] << " Doc filename: write document to Vrml file\n";
553 Handle(TDocStd_Document) aDoc;
554 DDocStd::GetDocument(argv[1], aDoc);
556 di << argv[1] << " is not a document\n";
560 if (argc < 3 || argc > 5)
562 di << "wrong number of parameters\n";
566 VrmlAPI_Writer writer;
567 writer.SetRepresentation(VrmlAPI_ShadedRepresentation);
568 Standard_Real anOCCLengthUnit =
569 UnitsMethods::GetLengthFactorValue(Interface_Static::IVal("xstep.cascade.unit"));
570 Standard_Real aScale = 0.001*anOCCLengthUnit;
571 writer.WriteDoc(aDoc, argv[2], aScale);
577 void XDEDRAW_Common::InitCommands(Draw_Interpretor& di)
579 static Standard_Boolean initactor = Standard_False;
584 initactor = Standard_True;
586 Standard_CString g = "XDE translation commands";
588 di.Add("ReadIges" , "Doc filename: Read IGES file to DECAF document" ,__FILE__, ReadIges, g);
589 di.Add("WriteIges" , "Doc filename: Write DECAF document to IGES file" ,__FILE__, WriteIges, g);
590 di.Add("ReadStep" , "Doc filename: Read STEP file to DECAF document" ,__FILE__, ReadStep, g);
591 di.Add("WriteStep" , "Doc filename [mode=a [multifile_prefix] [label]]: Write DECAF document to STEP file" ,__FILE__, WriteStep, g);
593 di.Add("XFileList","Print list of files that was transfered by the last transfer" ,__FILE__, GetDicWSList , g);
594 di.Add("XFileCur", ": returns name of file which is set as current",__FILE__, GetCurWS, g);
595 di.Add("XFileSet", "filename: Set the specified file to be the current one",__FILE__, SetCurWS, g);
596 di.Add("XFromShape", "shape: do fromshape command for all the files",__FILE__, FromShape, g);
598 di.Add("XExpand", "XExpand Doc recursively(0/1) or XExpand Doc recursively(0/1) label1 label2 ..."
599 "or XExpand Doc recursively(0/1) shape1 shape2 ...",__FILE__, Expand, g);
601 di.Add("WriteVrml", "Doc filename [version VRML#1.0/VRML#2.0 (1/2): 2 by default] [representation shaded/wireframe/both (0/1/2): 0 by default]", __FILE__, WriteVrml, g);