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.
16 #include <XDEDRAW_Common.ixx>
17 #include <TCollection_ExtendedString.hxx>
19 #include <Interface_Macros.hxx>
20 #include <XSControl_WorkSession.hxx>
21 #include <IGESControl_Controller.hxx>
22 #include <STEPControl_Controller.hxx>
25 #include <Draw_Interpretor.hxx>
27 #include <XDEDRAW.hxx>
28 #include <XSDRAWIGES.hxx>
29 #include <XSDRAWSTEP.hxx>
31 #include <TDocStd_Document.hxx>
32 #include <DDocStd_DrawDocument.hxx>
33 #include <DDocStd.hxx>
34 #include <TDocStd_Application.hxx>
35 #include <TDataStd_Name.hxx>
36 #include <TDF_Data.hxx>
38 #include <IGESCAFControl_Reader.hxx>
39 #include <IGESCAFControl_Writer.hxx>
40 #include <STEPCAFControl_Reader.hxx>
41 #include <STEPCAFControl_Writer.hxx>
43 #include <Dico_DictionaryOfTransient.hxx>
44 #include <STEPCAFControl_DictionaryOfExternFile.hxx>
45 #include <STEPCAFControl_ExternFile.hxx>
46 #include <IFSelect_SessionPilot.hxx>
47 #include <Dico_IteratorOfDictionaryOfTransient.hxx>
48 #include <STEPCAFControl_IteratorOfDictionaryOfExternFile.hxx>
49 #include <XSDRAW_Vars.hxx>
52 //============================================================
53 // Support for several models in DRAW
54 //============================================================
56 static Handle(Dico_DictionaryOfTransient) thedictws = new Dico_DictionaryOfTransient;
58 static Standard_Boolean ClearDicWS()
64 static void AddWS(TCollection_AsciiString filename,
65 const Handle(XSControl_WorkSession)& WS)
67 WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables
68 thedictws->SetItem( filename, WS );
72 static Standard_Boolean FillDicWS(Handle(STEPCAFControl_DictionaryOfExternFile)& dicFile)
75 if ( dicFile->IsEmpty() ) {
76 return Standard_False;
78 Handle(STEPCAFControl_ExternFile) EF;
79 STEPCAFControl_IteratorOfDictionaryOfExternFile DicEFIt ( dicFile );
80 for (; DicEFIt.More(); DicEFIt.Next() ) {
81 TCollection_AsciiString filename = DicEFIt.Name();
83 AddWS ( filename, EF->GetWS() );
88 static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename)
90 if ( !thedictws->HasItem(filename) ) return Standard_False;
91 Handle(XSControl_WorkSession) CurrentWS =
92 Handle(XSControl_WorkSession)::DownCast( thedictws->Item(filename) );
93 XSDRAW::Pilot()->SetSession( CurrentWS );
99 //=======================================================================
100 //function : SetCurWS
101 //purpose : Set current file if many files are read
102 //=======================================================================
104 static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv)
107 di<<"Use: "<<argv[0]<<" filename "<<"\n";
110 TCollection_AsciiString filename (argv[1]);
111 SetCurrentWS( filename );
116 //=======================================================================
117 //function : GetDicWSList
118 //purpose : List all files recorded after translation
119 //=======================================================================
121 static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
123 Handle(Dico_DictionaryOfTransient) DictWS = thedictws;
124 if ( DictWS->IsEmpty() ) return 1;
125 Dico_IteratorOfDictionaryOfTransient DicIt ( DictWS );
126 di << " The list of last translated files:" << "\n";
127 Standard_Integer num = 0;
128 for (; DicIt.More() ; DicIt.Next(), num++ ) {
129 TCollection_AsciiString strng ( DicIt.Name() );
130 if ( num ) di << "\n";
131 di << "\"" << strng.ToCString() << "\"";
136 //=======================================================================
137 //function : GetCurWS
138 //purpose : Return name of file which is current
139 //=======================================================================
141 static Standard_Integer GetCurWS (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
143 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
144 di << "\"" << WS->LoadedFile() << "\"";
148 //=======================================================================
149 //function : FromShape
150 //purpose : Apply fromshape command to all the loaded WSs
151 //=======================================================================
153 static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
156 di << argv[0] << " shape: search for shape origin among all last tranalated files" << "\n";
161 Sprintf ( command, "fromshape %.200s -1", argv[1] );
162 Handle(Dico_DictionaryOfTransient) DictWS = thedictws;
163 if ( DictWS->IsEmpty() ) return di.Eval ( command );
165 Handle(XSControl_WorkSession) WS = XSDRAW::Session();
167 Dico_IteratorOfDictionaryOfTransient DicIt ( DictWS );
168 // di << "Searching for shape among all the loaded files:" << "\n";
169 Standard_Integer num = 0;
170 for (; DicIt.More() ; DicIt.Next(), num++ ) {
171 Handle(XSControl_WorkSession) CurrentWS =
172 Handle(XSControl_WorkSession)::DownCast( DicIt.Value() );
173 XSDRAW::Pilot()->SetSession( CurrentWS );
177 XSDRAW::Pilot()->SetSession( WS );
181 //=======================================================================
182 //function : ReadIges
183 //purpose : Read IGES to DECAF document
184 //=======================================================================
186 static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
189 di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document" << "\n";
193 DeclareAndCast(IGESControl_Controller,ctl,XSDRAW::Controller());
194 if (ctl.IsNull()) XSDRAW::SetNorm("IGES");
196 IGESCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
199 Standard_Boolean mode = Standard_True;
200 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
201 switch (argv[3][i]) {
202 case '-' : mode = Standard_False; break;
203 case '+' : mode = Standard_True; break;
204 case 'c' : reader.SetColorMode (mode); break;
205 case 'n' : reader.SetNameMode (mode); break;
206 case 'l' : reader.SetLayerMode (mode); break;
209 TCollection_AsciiString fnom,rnom;
210 Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"IGES",fnom,rnom);
211 if (modfic) di<<" File IGES to read : "<<fnom.ToCString()<<"\n";
212 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
213 // di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
214 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
215 if (modfic) readstat = reader.ReadFile (fnom.ToCString());
216 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
217 if (readstat != IFSelect_RetDone) {
218 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
219 else di<<"No model loaded"<<"\n";
223 Handle(TDocStd_Document) doc;
224 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
225 Handle(TDocStd_Application) A;
226 if (!DDocStd::Find(A)) {di<<"No application found"<<"\n";return 1;}
227 A->NewDocument("MDTV-XCAF",doc);
228 TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);
229 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
230 Draw::Set(argv[1],DD);
231 // di << "Document saved with name " << argv[1];
233 if ( ! reader.Transfer ( doc ) ) {
234 di << "Cannot read any relevant data from the IGES file" << "\n";
238 // Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
239 // Draw::Set(argv[1],DD);
240 di << "Document saved with name " << argv[1];
245 //=======================================================================
246 //function : WriteIges
247 //purpose : Write DECAF document to IGES
248 //=======================================================================
250 static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
253 di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file" << "\n";
257 Handle(TDocStd_Document) Doc;
258 DDocStd::GetDocument(argv[1], Doc);
259 if ( Doc.IsNull() ) {
260 di << argv[1] << " is not a document" << "\n";
264 XSDRAW::SetNorm ("IGES");
266 // IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"),
267 // Interface_Static::IVal("write.iges.brep.mode"));
269 IGESCAFControl_Writer writer ( XSDRAW::Session(), Standard_True );
271 Standard_Boolean mode = Standard_True;
272 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
273 switch (argv[3][i]) {
274 case '-' : mode = Standard_False; break;
275 case '+' : mode = Standard_True; break;
276 case 'c' : writer.SetColorMode (mode); break;
277 case 'n' : writer.SetNameMode (mode); break;
278 case 'l' : writer.SetLayerMode (mode); break;
281 writer.Transfer ( Doc );
283 di << "Writig IGES model to file " << argv[2] << "\n";
284 if ( writer.Write ( argv[2] ) ) di<<" Write OK"<<"\n";
285 else di<<" Write failed"<<"\n";
290 //=======================================================================
291 //function : ReadStep
292 //purpose : Read STEP file to DECAF document
293 //=======================================================================
295 static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
298 di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document" << "\n";
302 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
303 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
305 STEPCAFControl_Reader reader ( XSDRAW::Session(),Standard_True);
308 Standard_Boolean mode = Standard_True;
309 for ( Standard_Integer i = 0; argv[3][i] ; i++ )
310 switch (argv[3][i]) {
311 case '-' : mode = Standard_False; break;
312 case '+' : mode = Standard_True; break;
313 case 'c' : reader.SetColorMode (mode); break;
314 case 'n' : reader.SetNameMode (mode); break;
315 case 'l' : reader.SetLayerMode (mode); break;
316 case 'v' : reader.SetPropsMode (mode); break;
320 TCollection_AsciiString fnom,rnom;
321 Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"STEP",fnom,rnom);
322 if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
323 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
324 // di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
325 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
326 if (modfic) readstat = reader.ReadFile (fnom.ToCString());
327 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
328 if (readstat != IFSelect_RetDone) {
329 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
330 else di<<"No model loaded"<<"\n";
334 Handle(TDocStd_Document) doc;
335 if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) {
336 Handle(TDocStd_Application) A;
337 if (!DDocStd::Find(A)) {di<<"No application found"<<"\n";return 1;}
338 A->NewDocument("MDTV-XCAF",doc);
339 TDataStd_Name::Set(doc->GetData()->Root(),argv[1]);
340 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
341 Draw::Set(argv[1],DD);
342 // di << "Document saved with name " << argv[1];
344 if ( ! reader.Transfer ( doc ) ) {
345 di << "Cannot read any relevant data from the STEP file" << "\n";
349 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
350 Draw::Set(argv[1],DD);
351 di << "Document saved with name " << argv[1];
353 Handle(STEPCAFControl_DictionaryOfExternFile) DicFile = reader.ExternFiles();
354 FillDicWS( DicFile );
355 AddWS ( fnom , XSDRAW::Session() );
360 //=======================================================================
361 //function : WriteStep
362 //purpose : Write DECAF document to STEP
363 //=======================================================================
365 static Standard_Integer WriteStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
368 di << "Use: " << argv[0] << " Doc filename [mode=a [multifile_prefix]]: write document to the STEP file" << "\n";
369 di << "Parameter mode can be: a or 0 : AsIs (default)" << "\n";
370 di << "f or 1 : FacettedBRep s or 2 : ShellBasedSurfaceModel" << "\n";
371 di << "m or 3 : ManifoldSolidBrep w or 4 : GeometricCurveSet/WireFrame" << "\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";
382 Standard_CString multifile = ( argc >4 ? argv[4] : 0 );
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[3][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[3][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;
416 di << "Translating document " << argv[1] << " to STEP" << "\n";
417 if ( ! writer.Transfer ( Doc, mode, multifile ) ) {
418 di << "The document cannot be translated or gives no result" << "\n";
421 di << "Writing STEP file " << argv[2] << "\n";
422 IFSelect_ReturnStatus stat = writer.Write(argv[2]);
424 case IFSelect_RetVoid : di<<"No file written"<<"\n"; break;
425 case IFSelect_RetDone : {
426 di<<"File "<<argv[2]<<" written"<<"\n";
428 Handle(STEPCAFControl_DictionaryOfExternFile) DicFile = writer.ExternFiles();
429 FillDicWS( DicFile );
430 AddWS( argv[2], XSDRAW::Session() );
433 default : di<<"Error on writing file"<<"\n"; break;
438 void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
440 static Standard_Boolean initactor = Standard_False;
441 if (initactor) return; initactor = Standard_True;
443 Standard_CString g = "XDE translation commands";
445 di.Add("ReadIges" , "Doc filename: Read IGES file to DECAF document" ,__FILE__, ReadIges, g);
446 di.Add("WriteIges" , "Doc filename: Write DECAF document to IGES file" ,__FILE__, WriteIges, g);
447 di.Add("ReadStep" , "Doc filename: Read STEP file to DECAF document" ,__FILE__, ReadStep, g);
448 di.Add("WriteStep" , "Doc filename: Write DECAF document to STEP file" ,__FILE__, WriteStep, g);
450 di.Add("XFileList","Print list of files that was transfered by the last transfer" ,__FILE__, GetDicWSList , g);
451 di.Add("XFileCur", ": returns name of file which is set as current",__FILE__, GetCurWS, g);
452 di.Add("XFileSet", "filename: Set the specified file to be the current one",__FILE__, SetCurWS, g);
453 di.Add("XFromShape", "shape: do fromshape command for all the files",__FILE__, FromShape, g);