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>
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>
52 //============================================================
53 // Support for several models in DRAW
54 //============================================================
55 static NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> thedictws;
57 static Standard_Boolean ClearDicWS()
63 static void AddWS(TCollection_AsciiString filename,
64 const Handle(XSControl_WorkSession)& WS)
66 WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables
67 thedictws.Bind( filename, WS );
71 static Standard_Boolean FillDicWS(NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>& dicFile)
74 if ( dicFile.IsEmpty() ) {
75 return Standard_False;
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();
82 AddWS ( filename, EF->GetWS() );
87 static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename)
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 );
98 //=======================================================================
100 //purpose : Set current file if many files are read
101 //=======================================================================
103 static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv)
106 di<<"Use: "<<argv[0]<<" filename \n";
109 TCollection_AsciiString filename (argv[1]);
110 SetCurrentWS( filename );
115 //=======================================================================
116 //function : GetDicWSList
117 //purpose : List all files recorded after translation
118 //=======================================================================
120 static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
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() << "\"";
135 //=======================================================================
136 //function : GetCurWS
137 //purpose : Return name of file which is current
138 //=======================================================================
140 static Standard_Integer GetCurWS (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
142 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
143 di << "\"" << WS->LoadedFile() << "\"";
147 //=======================================================================
148 //function : FromShape
149 //purpose : Apply fromshape command to all the loaded WSs
150 //=======================================================================
152 static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
155 di << argv[0] << " shape: search for shape origin among all last tranalated files\n";
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 );
164 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
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 );
176 XSDRAW::Pilot()->SetSession( WS );
180 //=======================================================================
181 //function : ReadIges
182 //purpose : Read IGES to DECAF document
183 //=======================================================================
185 static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
188 di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document\n";
192 DeclareAndCast(IGESControl_Controller,ctl,XSDRAW::Controller());
193 if (ctl.IsNull()) XSDRAW::SetNorm("IGES");
195 IGESCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
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;
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";
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];
231 if ( ! reader.Transfer ( doc ) ) {
232 di << "Cannot read any relevant data from the IGES file\n";
236 // Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
237 // Draw::Set(argv[1],DD);
238 di << "Document saved with name " << argv[1];
243 //=======================================================================
244 //function : WriteIges
245 //purpose : Write DECAF document to IGES
246 //=======================================================================
248 static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
251 di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file\n";
255 Handle(TDocStd_Document) Doc;
256 DDocStd::GetDocument(argv[1], Doc);
257 if ( Doc.IsNull() ) {
258 di << argv[1] << " is not a document\n";
262 XSDRAW::SetNorm ("IGES");
264 // IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
265 // Interface_Static::IVal("write.iges.brep.mode"));
267 IGESCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
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;
279 writer.Transfer ( Doc );
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";
288 //=======================================================================
289 //function : ReadStep
290 //purpose : Read STEP file to DECAF document
291 //=======================================================================
293 static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
296 di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document\n";
300 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
301 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
303 STEPCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
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;
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";
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];
341 if ( ! reader.Transfer ( doc ) ) {
342 di << "Cannot read any relevant data from the STEP file\n";
346 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
347 Draw::Set(argv[1],DD);
348 di << "Document saved with name " << argv[1];
350 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = reader.ExternFiles();
351 FillDicWS( DicFile );
352 AddWS ( fnom , XSDRAW::Session() );
357 //=======================================================================
358 //function : WriteStep
359 //purpose : Write DECAF document to STEP
360 //=======================================================================
362 static Standard_Integer WriteStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
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";
375 Handle(TDocStd_Document) Doc;
376 DDocStd::GetDocument(argv[1], Doc);
377 if ( Doc.IsNull() ) {
378 di << argv[1] << " is not a document\n";
381 Standard_CString multifile = 0;
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 );
388 STEPControl_StepModelType mode = STEPControl_AsIs;
390 switch (argv[k][0]) {
392 case '0' : mode = STEPControl_AsIs; break;
394 case '1' : mode = STEPControl_FacetedBrep; break;
396 case '2' : mode = STEPControl_ShellBasedSurfaceModel; break;
398 case '3' : mode = STEPControl_ManifoldSolidBrep; break;
400 case '4' : mode = STEPControl_GeometricCurveSet; break;
401 default : di<<"3st arg = mode, incorrect [give fsmw]\n"; return 1;
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;
419 TCollection_AsciiString aStr(argv[k]);
420 if( aStr.Search(":") ==-1)
421 multifile = argv[k++];
427 if( !DDF::FindLabel(Doc->Main().Data(), argv[k], label) || label.IsNull()) {
428 di << "No label for entry" << "\n";
435 di << "Translating label "<< argv[k]<<" of document " << argv[1] << " to STEP\n";
436 if(!writer.Transfer ( label, mode, multifile ))
438 di << "The label of document cannot be translated or gives no result\n";
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";
452 di << "Writing STEP file " << argv[2] << "\n";
453 IFSelect_ReturnStatus stat = writer.Write(argv[2]);
455 case IFSelect_RetVoid : di<<"No file written\n"; break;
456 case IFSelect_RetDone : {
457 di<<"File "<<argv[2]<<" written\n";
459 NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = writer.ExternFiles();
460 FillDicWS( DicFile );
461 AddWS( argv[2], XSDRAW::Session() );
464 default : di<<"Error on writing file\n"; break;
469 static Standard_Integer Expand (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
472 di<<"Use: "<<argv[0]<<" Doc recurs(0/1) or Doc recurs(0/1) label1 label2 ... or Doc recurs(0/1 shape1 shape2 ...\n";
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; }
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;
486 if(!XCAFDoc_Editor::Expand(Doc->Main(), recurs)){
487 di << "The shape is assembly or not compaund\n";
493 for (Standard_Integer i = 3; i < argc; i++)
496 TDF_Tool::Label(Doc->GetData(), argv[i], aLabel);
499 aShape = DBRep::Get(argv[i]);
500 aLabel = aShapeTool->FindShape(aShape);
503 if (!aLabel.IsNull()){
504 if(!XCAFDoc_Editor::Expand(Doc->Main(), aLabel, recurs)){
505 di << "The shape is assembly or not compaund\n";
510 { di << argv[i] << " is not a shape\n"; return 1; }
516 void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
518 static Standard_Boolean initactor = Standard_False;
519 if (initactor) return; initactor = Standard_True;
521 Standard_CString g = "XDE translation commands";
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);
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);
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);