b311480e |
1 | // Created on: 2003-08-15 |
2 | // Created by: Sergey ZARITCHNY |
3 | // Copyright (c) 2003-2012 OPEN CASCADE SAS |
4 | // |
5 | // The content of this file is subject to the Open CASCADE Technology Public |
6 | // License Version 6.5 (the "License"). You may not use the content of this file |
7 | // except in compliance with the License. Please obtain a copy of the License |
8 | // at http://www.opencascade.org and read it completely before using this file. |
9 | // |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
12 | // |
13 | // The Original Code and all software distributed under the License is |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
15 | // Initial Developer hereby disclaims all such warranties, including without |
16 | // limitation, any warranties of merchantability, fitness for a particular |
17 | // purpose or non-infringement. Please see the License for the specific terms |
18 | // and conditions governing the rights and limitations under the License. |
7fd59977 |
19 | |
20 | #include <XDEDRAW_Common.ixx> |
21 | #include <TCollection_ExtendedString.hxx> |
22 | |
23 | #include <Interface_Macros.hxx> |
24 | #include <XSControl_WorkSession.hxx> |
25 | #include <IGESControl_Controller.hxx> |
26 | #include <STEPControl_Controller.hxx> |
27 | |
28 | #include <Draw.hxx> |
29 | #include <Draw_Interpretor.hxx> |
30 | #include <SWDRAW.hxx> |
31 | #include <XSDRAW.hxx> |
32 | #include <XDEDRAW.hxx> |
33 | #include <XSDRAWIGES.hxx> |
34 | #include <XSDRAWSTEP.hxx> |
35 | |
36 | #include <TDocStd_Document.hxx> |
37 | #include <DDocStd_DrawDocument.hxx> |
38 | #include <DDocStd.hxx> |
39 | #include <TDocStd_Application.hxx> |
40 | #include <TDataStd_Name.hxx> |
41 | #include <TDF_Data.hxx> |
42 | |
43 | #include <IGESCAFControl_Reader.hxx> |
44 | #include <IGESCAFControl_Writer.hxx> |
45 | #include <STEPCAFControl_Reader.hxx> |
46 | #include <STEPCAFControl_Writer.hxx> |
47 | |
48 | #include <Dico_DictionaryOfTransient.hxx> |
49 | #include <STEPCAFControl_DictionaryOfExternFile.hxx> |
50 | #include <STEPCAFControl_ExternFile.hxx> |
51 | #include <IFSelect_SessionPilot.hxx> |
52 | #include <Dico_IteratorOfDictionaryOfTransient.hxx> |
53 | #include <STEPCAFControl_IteratorOfDictionaryOfExternFile.hxx> |
54 | #include <XSDRAW_Vars.hxx> |
55 | #include <stdio.h> |
56 | |
57 | //============================================================ |
58 | // Support for several models in DRAW |
59 | //============================================================ |
60 | |
61 | static Handle(Dico_DictionaryOfTransient) thedictws = new Dico_DictionaryOfTransient; |
62 | |
63 | static Standard_Boolean ClearDicWS() |
64 | { |
65 | thedictws->Clear(); |
66 | return Standard_True; |
67 | } |
68 | |
69 | static void AddWS(TCollection_AsciiString filename, |
70 | const Handle(XSControl_WorkSession)& WS) |
71 | { |
72 | WS->SetVars ( new XSDRAW_Vars ); // support of DRAW variables |
73 | thedictws->SetItem( filename, WS ); |
74 | } |
75 | |
76 | |
77 | static Standard_Boolean FillDicWS(Handle(STEPCAFControl_DictionaryOfExternFile)& dicFile) |
78 | { |
79 | ClearDicWS(); |
80 | if ( dicFile->IsEmpty() ) { |
81 | return Standard_False; |
82 | } |
83 | Handle(STEPCAFControl_ExternFile) EF; |
84 | STEPCAFControl_IteratorOfDictionaryOfExternFile DicEFIt ( dicFile ); |
85 | for (; DicEFIt.More(); DicEFIt.Next() ) { |
86 | TCollection_AsciiString filename = DicEFIt.Name(); |
87 | EF = DicEFIt.Value(); |
88 | AddWS ( filename, EF->GetWS() ); |
89 | } |
90 | return Standard_True; |
91 | } |
92 | |
93 | static Standard_Boolean SetCurrentWS (TCollection_AsciiString filename) |
94 | { |
95 | if ( !thedictws->HasItem(filename) ) return Standard_False; |
96 | Handle(XSControl_WorkSession) CurrentWS = |
97 | Handle(XSControl_WorkSession)::DownCast( thedictws->Item(filename) ); |
98 | XSDRAW::Pilot()->SetSession( CurrentWS ); |
99 | |
100 | return Standard_True; |
101 | } |
102 | |
103 | |
104 | //======================================================================= |
105 | //function : SetCurWS |
106 | //purpose : Set current file if many files are read |
107 | //======================================================================= |
108 | |
109 | static Standard_Integer SetCurWS (Draw_Interpretor& di , Standard_Integer argc, const char** argv) |
110 | { |
111 | if (argc <2) { |
112 | di<<"Use: "<<argv[0]<<" filename "<<"\n"; |
113 | return 1; |
114 | } |
115 | TCollection_AsciiString filename (argv[1]); |
116 | SetCurrentWS( filename ); |
117 | return 0; |
118 | } |
119 | |
120 | |
121 | //======================================================================= |
122 | //function : GetDicWSList |
123 | //purpose : List all files recorded after translation |
124 | //======================================================================= |
125 | |
126 | static Standard_Integer GetDicWSList (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/) |
127 | { |
128 | Handle(Dico_DictionaryOfTransient) DictWS = thedictws; |
129 | if ( DictWS->IsEmpty() ) return 1; |
130 | Dico_IteratorOfDictionaryOfTransient DicIt ( DictWS ); |
131 | di << " The list of last translated files:" << "\n"; |
132 | Standard_Integer num = 0; |
133 | for (; DicIt.More() ; DicIt.Next(), num++ ) { |
134 | TCollection_AsciiString strng ( DicIt.Name() ); |
135 | if ( num ) di << "\n"; |
136 | di << "\"" << strng.ToCString() << "\""; |
137 | } |
138 | return 0; |
139 | } |
140 | |
141 | //======================================================================= |
142 | //function : GetCurWS |
143 | //purpose : Return name of file which is current |
144 | //======================================================================= |
145 | |
146 | static Standard_Integer GetCurWS (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/) |
147 | { |
148 | Handle(XSControl_WorkSession) WS = XSDRAW::Session(); |
149 | di << "\"" << WS->LoadedFile() << "\""; |
150 | return 0; |
151 | } |
152 | |
153 | //======================================================================= |
154 | //function : FromShape |
155 | //purpose : Apply fromshape command to all the loaded WSs |
156 | //======================================================================= |
157 | |
158 | static Standard_Integer FromShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
159 | { |
160 | if ( argc <2 ) { |
161 | di << argv[0] << " shape: search for shape origin among all last tranalated files" << "\n"; |
162 | return 0; |
163 | } |
164 | |
165 | char command[256]; |
91322f44 |
166 | Sprintf ( command, "fromshape %.200s -1", argv[1] ); |
7fd59977 |
167 | Handle(Dico_DictionaryOfTransient) DictWS = thedictws; |
168 | if ( DictWS->IsEmpty() ) return di.Eval ( command ); |
169 | |
170 | Handle(XSControl_WorkSession) WS = XSDRAW::Session(); |
171 | |
172 | Dico_IteratorOfDictionaryOfTransient DicIt ( DictWS ); |
173 | // di << "Searching for shape among all the loaded files:" << "\n"; |
174 | Standard_Integer num = 0; |
175 | for (; DicIt.More() ; DicIt.Next(), num++ ) { |
176 | Handle(XSControl_WorkSession) CurrentWS = |
177 | Handle(XSControl_WorkSession)::DownCast( DicIt.Value() ); |
178 | XSDRAW::Pilot()->SetSession( CurrentWS ); |
179 | di.Eval ( command ); |
180 | } |
181 | |
182 | XSDRAW::Pilot()->SetSession( WS ); |
183 | return 0; |
184 | } |
185 | |
186 | //======================================================================= |
187 | //function : ReadIges |
188 | //purpose : Read IGES to DECAF document |
189 | //======================================================================= |
190 | |
191 | static Standard_Integer ReadIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
192 | { |
193 | if ( argc <3 ) { |
194 | di << "Use: " << argv[0] << " Doc filename [mode]: read IGES file to a document" << "\n"; |
195 | return 0; |
196 | } |
197 | |
198 | DeclareAndCast(IGESControl_Controller,ctl,XSDRAW::Controller()); |
199 | if (ctl.IsNull()) XSDRAW::SetNorm("IGES"); |
200 | |
201 | IGESCAFControl_Reader reader ( XSDRAW::Session(),Standard_True); |
202 | |
203 | if (argc == 4) { |
204 | Standard_Boolean mode = Standard_True; |
205 | for ( Standard_Integer i = 0; argv[3][i] ; i++ ) |
206 | switch (argv[3][i]) { |
207 | case '-' : mode = Standard_False; break; |
208 | case '+' : mode = Standard_True; break; |
209 | case 'c' : reader.SetColorMode (mode); break; |
210 | case 'n' : reader.SetNameMode (mode); break; |
211 | case 'l' : reader.SetLayerMode (mode); break; |
212 | } |
213 | } |
214 | TCollection_AsciiString fnom,rnom; |
215 | Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"IGES",fnom,rnom); |
216 | if (modfic) di<<" File IGES to read : "<<fnom.ToCString()<<"\n"; |
217 | else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n"; |
218 | // di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n"; |
219 | IFSelect_ReturnStatus readstat = IFSelect_RetVoid; |
220 | if (modfic) readstat = reader.ReadFile (fnom.ToCString()); |
221 | else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone; |
222 | if (readstat != IFSelect_RetDone) { |
223 | if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n"; |
224 | else di<<"No model loaded"<<"\n"; |
225 | return 1; |
226 | } |
227 | |
228 | Handle(TDocStd_Document) doc; |
229 | if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) { |
230 | Handle(TDocStd_Application) A; |
231 | if (!DDocStd::Find(A)) {di<<"No application found"<<"\n";return 1;} |
232 | A->NewDocument("MDTV-XCAF",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]; |
237 | } |
238 | if ( ! reader.Transfer ( doc ) ) { |
239 | di << "Cannot read any relevant data from the IGES file" << "\n"; |
240 | return 1; |
241 | } |
242 | |
243 | // Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc); |
244 | // Draw::Set(argv[1],DD); |
245 | di << "Document saved with name " << argv[1]; |
246 | |
247 | return 0; |
248 | } |
249 | |
250 | //======================================================================= |
251 | //function : WriteIges |
252 | //purpose : Write DECAF document to IGES |
253 | //======================================================================= |
254 | |
255 | static Standard_Integer WriteIges (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
256 | { |
257 | if ( argc <3 ) { |
258 | di << "Use: " << argv[0] << " Doc filename [mode]: write document to IGES file" << "\n"; |
259 | return 0; |
260 | } |
261 | |
262 | Handle(TDocStd_Document) Doc; |
263 | DDocStd::GetDocument(argv[1], Doc); |
264 | if ( Doc.IsNull() ) { |
265 | di << argv[1] << " is not a document" << "\n"; |
266 | return 1; |
267 | } |
268 | |
269 | XSDRAW::SetNorm ("IGES"); |
270 | |
271 | // IGESControl_Writer ICW (Interface_Static::CVal("write.iges.unit"), |
272 | // Interface_Static::IVal("write.iges.brep.mode")); |
273 | |
274 | IGESCAFControl_Writer writer ( XSDRAW::Session(), Standard_True ); |
275 | if (argc == 4) { |
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; |
284 | } |
285 | } |
286 | writer.Transfer ( Doc ); |
287 | |
288 | di << "Writig IGES model to file " << argv[2] << "\n"; |
289 | if ( writer.Write ( argv[2] ) ) di<<" Write OK"<<"\n"; |
290 | else di<<" Write failed"<<"\n"; |
291 | |
292 | return 0; |
293 | } |
294 | |
295 | //======================================================================= |
296 | //function : ReadStep |
297 | //purpose : Read STEP file to DECAF document |
298 | //======================================================================= |
299 | |
300 | static Standard_Integer ReadStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
301 | { |
302 | if ( argc <3 ) { |
303 | di << "Use: " << argv[0] << " Doc filename [mode]: read STEP file to a document" << "\n"; |
304 | return 0; |
305 | } |
306 | |
307 | DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller()); |
308 | if (ctl.IsNull()) XSDRAW::SetNorm("STEP"); |
309 | |
310 | STEPCAFControl_Reader reader ( XSDRAW::Session(),Standard_True); |
311 | |
312 | if (argc == 4) { |
313 | Standard_Boolean mode = Standard_True; |
314 | for ( Standard_Integer i = 0; argv[3][i] ; i++ ) |
315 | switch (argv[3][i]) { |
316 | case '-' : mode = Standard_False; break; |
317 | case '+' : mode = Standard_True; break; |
318 | case 'c' : reader.SetColorMode (mode); break; |
319 | case 'n' : reader.SetNameMode (mode); break; |
320 | case 'l' : reader.SetLayerMode (mode); break; |
321 | case 'v' : reader.SetPropsMode (mode); break; |
322 | } |
323 | } |
324 | |
325 | TCollection_AsciiString fnom,rnom; |
326 | Standard_Boolean modfic = XSDRAW::FileAndVar (argv[2],argv[1],"STEP",fnom,rnom); |
327 | if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n"; |
328 | else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n"; |
329 | // di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n"; |
330 | IFSelect_ReturnStatus readstat = IFSelect_RetVoid; |
331 | if (modfic) readstat = reader.ReadFile (fnom.ToCString()); |
332 | else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone; |
333 | if (readstat != IFSelect_RetDone) { |
334 | if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n"; |
335 | else di<<"No model loaded"<<"\n"; |
336 | return 1; |
337 | } |
338 | |
339 | Handle(TDocStd_Document) doc; |
340 | if (!DDocStd::GetDocument(argv[1],doc,Standard_False)) { |
341 | Handle(TDocStd_Application) A; |
342 | if (!DDocStd::Find(A)) {di<<"No application found"<<"\n";return 1;} |
343 | A->NewDocument("MDTV-XCAF",doc); |
344 | TDataStd_Name::Set(doc->GetData()->Root(),argv[1]); |
345 | Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc); |
346 | Draw::Set(argv[1],DD); |
347 | // di << "Document saved with name " << argv[1]; |
348 | } |
349 | if ( ! reader.Transfer ( doc ) ) { |
350 | di << "Cannot read any relevant data from the STEP file" << "\n"; |
351 | return 1; |
352 | } |
353 | |
354 | Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc); |
355 | Draw::Set(argv[1],DD); |
356 | di << "Document saved with name " << argv[1]; |
357 | |
358 | Handle(STEPCAFControl_DictionaryOfExternFile) DicFile = reader.ExternFiles(); |
359 | FillDicWS( DicFile ); |
360 | AddWS ( fnom , XSDRAW::Session() ); |
361 | |
362 | return 0; |
363 | } |
364 | |
365 | //======================================================================= |
366 | //function : WriteStep |
367 | //purpose : Write DECAF document to STEP |
368 | //======================================================================= |
369 | |
370 | static Standard_Integer WriteStep (Draw_Interpretor& di, Standard_Integer argc, const char** argv) |
371 | { |
372 | if ( argc <3 ) { |
373 | di << "Use: " << argv[0] << " Doc filename [mode=a [multifile_prefix]]: write document to the STEP file" << "\n"; |
374 | di << "Parameter mode can be: a or 0 : AsIs (default)" << "\n"; |
375 | di << "f or 1 : FacettedBRep s or 2 : ShellBasedSurfaceModel" << "\n"; |
376 | di << "m or 3 : ManifoldSolidBrep w or 4 : GeometricCurveSet/WireFrame" << "\n"; |
377 | return 0; |
378 | } |
379 | |
380 | Handle(TDocStd_Document) Doc; |
381 | DDocStd::GetDocument(argv[1], Doc); |
382 | if ( Doc.IsNull() ) { |
383 | di << argv[1] << " is not a document" << "\n"; |
384 | return 1; |
385 | } |
386 | |
387 | Standard_CString multifile = ( argc >4 ? argv[4] : 0 ); |
388 | |
389 | DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller()); |
390 | if (ctl.IsNull()) XSDRAW::SetNorm("STEP"); |
391 | STEPCAFControl_Writer writer ( XSDRAW::Session(), Standard_True ); |
392 | |
393 | STEPControl_StepModelType mode = STEPControl_AsIs; |
394 | if ( argc >3 ) { |
395 | switch (argv[3][0]) { |
396 | case 'a' : |
397 | case '0' : mode = STEPControl_AsIs; break; |
398 | case 'f' : |
399 | case '1' : mode = STEPControl_FacetedBrep; break; |
400 | case 's' : |
401 | case '2' : mode = STEPControl_ShellBasedSurfaceModel; break; |
402 | case 'm' : |
403 | case '3' : mode = STEPControl_ManifoldSolidBrep; break; |
404 | case 'w' : |
405 | case '4' : mode = STEPControl_GeometricCurveSet; break; |
406 | default : di<<"3st arg = mode, incorrect [give fsmw]"<<"\n"; return 1; |
407 | } |
408 | Standard_Boolean wrmode = Standard_True; |
409 | for ( Standard_Integer i = 0; argv[3][i] ; i++ ) |
410 | switch (argv[3][i]) { |
411 | case '-' : wrmode = Standard_False; break; |
412 | case '+' : wrmode = Standard_True; break; |
413 | case 'c' : writer.SetColorMode (wrmode); break; |
414 | case 'n' : writer.SetNameMode (wrmode); break; |
415 | case 'l' : writer.SetLayerMode (wrmode); break; |
416 | case 'v' : writer.SetPropsMode (wrmode); break; |
417 | } |
418 | |
419 | } |
420 | |
421 | di << "Translating document " << argv[1] << " to STEP" << "\n"; |
422 | if ( ! writer.Transfer ( Doc, mode, multifile ) ) { |
423 | di << "The document cannot be translated or gives no result" << "\n"; |
424 | } |
425 | |
426 | di << "Writing STEP file " << argv[2] << "\n"; |
427 | IFSelect_ReturnStatus stat = writer.Write(argv[2]); |
428 | switch (stat) { |
429 | case IFSelect_RetVoid : di<<"No file written"<<"\n"; break; |
430 | case IFSelect_RetDone : { |
431 | di<<"File "<<argv[2]<<" written"<<"\n"; |
432 | |
433 | Handle(STEPCAFControl_DictionaryOfExternFile) DicFile = writer.ExternFiles(); |
434 | FillDicWS( DicFile ); |
435 | AddWS( argv[2], XSDRAW::Session() ); |
436 | break; |
437 | } |
438 | default : di<<"Error on writing file"<<"\n"; break; |
439 | } |
440 | return 0; |
441 | } |
442 | |
443 | void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) { |
444 | |
445 | static Standard_Boolean initactor = Standard_False; |
446 | if (initactor) return; initactor = Standard_True; |
447 | |
448 | Standard_CString g = "XDE translation commands"; |
449 | |
450 | di.Add("ReadIges" , "Doc filename: Read IGES file to DECAF document" ,__FILE__, ReadIges, g); |
451 | di.Add("WriteIges" , "Doc filename: Write DECAF document to IGES file" ,__FILE__, WriteIges, g); |
452 | di.Add("ReadStep" , "Doc filename: Read STEP file to DECAF document" ,__FILE__, ReadStep, g); |
453 | di.Add("WriteStep" , "Doc filename: Write DECAF document to STEP file" ,__FILE__, WriteStep, g); |
454 | |
455 | di.Add("XFileList","Print list of files that was transfered by the last transfer" ,__FILE__, GetDicWSList , g); |
456 | di.Add("XFileCur", ": returns name of file which is set as current",__FILE__, GetCurWS, g); |
457 | di.Add("XFileSet", "filename: Set the specified file to be the current one",__FILE__, SetCurWS, g); |
458 | di.Add("XFromShape", "shape: do fromshape command for all the files",__FILE__, FromShape, g); |
459 | |
460 | } |