0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / XSDRAWIGES / XSDRAWIGES.cxx
CommitLineData
bd651bbb 1// Copyright (c) 2023 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
bd651bbb 14#include <XSDRAWIGES.hxx>
7fd59977 15
b0d96eb7 16#include <BRepTools.hxx>
42cf5bc1 17#include <DBRep.hxx>
bd651bbb 18#include <DDocStd.hxx>
19#include <DDocStd_DrawDocument.hxx>
20#include <Draw.hxx>
21#include <Draw_Interpretor.hxx>
22#include <Draw_PluginMacro.hxx>
42cf5bc1 23#include <Draw_ProgressIndicator.hxx>
24#include <DrawTrSurf.hxx>
bd651bbb 25#include <IGESCAFControl_Reader.hxx>
26#include <IGESCAFControl_Writer.hxx>
7fd59977 27#include <IGESControl_Controller.hxx>
42cf5bc1 28#include <IGESControl_Reader.hxx>
29#include <IGESControl_Writer.hxx>
42cf5bc1 30#include <IGESData_IGESEntity.hxx>
7fd59977 31#include <IGESData_IGESModel.hxx>
42cf5bc1 32#include <IGESSelect_Activator.hxx>
7fd59977 33#include <Interface_Macros.hxx>
42cf5bc1 34#include <Interface_Static.hxx>
bd651bbb 35#include <Message.hxx>
36#include <OSD_OpenFile.hxx>
37#include <OSD_Path.hxx>
38#include <TColStd_Array1OfAsciiString.hxx>
39#include <TColStd_MapIteratorOfMapOfTransient.hxx>
40#include <TDataStd_Name.hxx>
41#include <TDocStd_Application.hxx>
42cf5bc1 42#include <TopoDS_Shape.hxx>
42cf5bc1 43#include <Transfer_IteratorOfProcessForTransient.hxx>
42cf5bc1 44#include <Transfer_TransientProcess.hxx>
bd651bbb 45#include <XSAlgo.hxx>
46#include <XSAlgo_AlgoContainer.hxx>
7f56eba8 47#include <XSControl_TransferReader.hxx>
bd651bbb 48#include <XSControl_WorkSession.hxx>
42cf5bc1 49#include <XSDRAW.hxx>
7fd59977 50
bd651bbb 51//=======================================================================
b0d96eb7 52//function : WriteShape
53//purpose : Creates a file Shape_'number'
54//=======================================================================
bd651bbb 55void WriteShape(const TopoDS_Shape& shape,
56 const Standard_Integer number)
b0d96eb7 57{
58 char fname[110];
bd651bbb 59 sprintf(fname, "Shape_%d", number);
60 std::ofstream f(fname, std::ios::out | std::ios::binary);
04232180 61 std::cout << "Output file name : " << fname << std::endl;
b0d96eb7 62 f << "DBRep_DrawableShape\n";
bd651bbb 63
b0d96eb7 64 BRepTools::Write(shape, f);
65 f.close();
66}
67
bd651bbb 68//=======================================================================
69//function : XSDRAW_CommandPart
70//purpose :
71//=======================================================================
72TCollection_AsciiString XSDRAW_CommandPart(Standard_Integer argc,
73 const char** argv,
74 const Standard_Integer argf)
b0d96eb7 75{
76 TCollection_AsciiString res;
bd651bbb 77 for (Standard_Integer i = argf; i < argc; i++)
78 {
b0d96eb7 79 if (i > argf) res.AssignCat(" ");
bd651bbb 80 res.AssignCat(argv[i]);
b0d96eb7 81 }
82 return res;
83}
bd651bbb 84
85//=======================================================================
86//function : GiveEntityNumber
87//purpose :
88//=======================================================================
89static Standard_Integer GiveEntityNumber(const Handle(XSControl_WorkSession)& WS,
90 const Standard_CString name)
91{
92 Standard_Integer num = 0;
93 if (!name || name[0] == '\0')
94 {
95 char ligne[80]; ligne[0] = '\0';
96 std::cin >> ligne;
97 // std::cin.clear(); std::cin.getline (ligne,79);
98 if (ligne[0] == '\0') return 0;
99 num = WS->NumberFromLabel(ligne);
100 }
101 else num = WS->NumberFromLabel(name);
102 return num;
b0d96eb7 103}
104
bd651bbb 105//=======================================================================
106//function : FileAndVar
107//purpose :
108//=======================================================================
109Standard_Boolean FileAndVar(const Handle(XSControl_WorkSession)& session,
110 const Standard_CString file,
111 const Standard_CString var,
112 const Standard_CString def,
113 TCollection_AsciiString& resfile,
114 TCollection_AsciiString& resvar)
7fd59977 115{
bd651bbb 116 Standard_Boolean iafic = Standard_True;
117 resfile.Clear(); resvar.Clear();
118 if (file)
119 if (file[0] == '\0' ||
120 (file[0] == '.' && file[1] == '\0')) iafic = Standard_False;
121 if (!iafic) resfile.AssignCat(session->LoadedFile());
122 else resfile.AssignCat(file);
123
124 if (var && var[0] != '\0' && (var[0] != '.' || var[1] != '\0'))
125 resvar.AssignCat(var);
126 else if (resfile.Length() == 0) resvar.AssignCat(def);
127 else
128 {
129 Standard_Integer nomdeb, nomfin;
130 nomdeb = resfile.SearchFromEnd("/");
131 if (nomdeb <= 0) nomdeb = resfile.SearchFromEnd("\\"); // pour NT
132 if (nomdeb < 0) nomdeb = 0;
133 nomfin = resfile.SearchFromEnd(".");
134 if (nomfin < nomdeb) nomfin = resfile.Length() + 1;
135 resvar = resfile.SubString(nomdeb + 1, nomfin - 1);
136 }
137 return iafic;
138}
139
140//=======================================================================
141//function : igesbrep
142//purpose :
143//=======================================================================
144static Standard_Integer igesbrep(Draw_Interpretor& theDI,
145 Standard_Integer theNbArgs,
146 const char** theArgVec)
147{
148 Handle(XSControl_WorkSession) aWS = XSDRAW::Session();
149 Handle(IGESControl_Controller) aCtl =
150 Handle(IGESControl_Controller)::DownCast(aWS->NormAdaptor());
151 if (aCtl.IsNull())
152 {
153 aWS->SelectNorm("IGES");
154 }
7fd59977 155
156 // Progress indicator
bd651bbb 157 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator(theDI, 1);
158 Message_ProgressScope aPSRoot(progress->Start(), "Reading", 100);
159
160 IGESControl_Reader Reader(XSDRAW::Session(), Standard_False);
bc650d41
G
161 Standard_Boolean aFullMode = Standard_True;
162 Reader.WS()->SetModeStat(aFullMode);
7fd59977 163
bd651bbb 164 TCollection_AsciiString fnom, rnom;
7fd59977 165
bd651bbb 166 Standard_Boolean modfic = FileAndVar
167 (aWS, theArgVec[1], theArgVec[2], "IGESBREP", fnom, rnom);
168 if (modfic) theDI << " File IGES to read : " << fnom.ToCString() << "\n";
169 else theDI << " Model taken from the session : " << fnom.ToCString() << "\n";
170 theDI << " -- Names of variables BREP-DRAW prefixed by : " << rnom.ToCString() << "\n";
7fd59977 171 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
172
173#ifdef CHRONOMESURE
174 OSD_Timer Chr; Chr.Reset();
175 IDT_SetLevel(3);
176#endif
177
bd651bbb 178 // Reading the file
7e785937 179 aPSRoot.SetName("Loading");
180 progress->Show(aPSRoot);
7fd59977 181
bd651bbb 182 if (modfic) readstat = Reader.ReadFile(fnom.ToCString());
7fd59977 183 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
184
7e785937 185 aPSRoot.Next(20); // On average loading takes 20%
186 if (aPSRoot.UserBreak())
187 return 1;
7fd59977 188
bd651bbb 189 if (readstat != IFSelect_RetDone)
190 {
191 if (modfic) theDI << "Could not read file " << fnom.ToCString() << " , abandon\n";
192 else theDI << "No model loaded\n";
7fd59977 193 return 1;
194 }
bd651bbb 195 // Choice of treatment
196 Standard_Boolean fromtcl = (theNbArgs > 3);
7fd59977 197 Standard_Integer modepri = 1, nent, nbs;
198 if (fromtcl) modepri = 4;
199
bd651bbb 200 while (modepri)
201 {
7fd59977 202 //Roots for transfer are defined before setting mode ALL or OnlyVisible - gka
203 //mode OnlyVisible does not work.
204 // nent = Reader.NbRootsForTransfer();
bd651bbb 205 if (!fromtcl)
206 {
207 std::cout << "Mode (0 End, 1 Visible Roots, 2 All Roots, 3 Only One Entity, 4 Selection) :" << std::flush;
7fd59977 208 modepri = -1;
bd651bbb 209
210 // amv 26.09.2003 : this is used to avoid error of enter's simbol
211 char str[80];
212 std::cin >> str;
213 modepri = Draw::Atoi(str);
7fd59977 214 }
215
bd651bbb 216 if (modepri == 0)
217 { //fin
218 theDI << "Bye and good luck! \n";
7fd59977 219 break;
bd651bbb 220 }
7fd59977 221
bd651bbb 222 else if (modepri <= 2)
223 { // 1 : Visible Roots, 2 : All Roots
224 theDI << "All Geometry Transfer\n";
225 theDI << "spline_continuity (read) : " << Interface_Static::IVal("read.iges.bspline.continuity") << " (0 : no modif, 1 : C1, 2 : C2)\n";
226 theDI << " To modify : command param read.iges.bspline.continuity\n";
227 const Handle(XSControl_WorkSession)& thesession = Reader.WS();
228 thesession->TransferReader()->Context().Clear();
7e785937 229
230 aPSRoot.SetName("Translation");
231 progress->Show(aPSRoot);
bd651bbb 232
233 if (modepri == 1) Reader.SetReadVisible(Standard_True);
7e785937 234 Reader.TransferRoots(aPSRoot.Next(80));
bd651bbb 235
7e785937 236 if (aPSRoot.UserBreak())
237 return 1;
238
7fd59977 239 // result in only one shape for all the roots
240 // or in one shape for one root.
bd651bbb 241 theDI << "Count of shapes produced : " << Reader.NbShapes() << "\n";
7fd59977 242 Standard_Integer answer = 1;
bd651bbb 243 if (Reader.NbShapes() > 1)
244 {
245 std::cout << " pass(0) one shape for all (1)\n or one shape per root (2)\n + WriteBRep (one for all : 3) (one per root : 4) : " << std::flush;
7fd59977 246 answer = -1;
247 //amv 26.09.2003
bd651bbb 248 char str_a[80];
249 std::cin >> str_a;
250 answer = Draw::Atoi(str_a);
7fd59977 251 }
bd651bbb 252 if (answer == 0) continue;
253 if (answer == 1 || answer == 3)
254 {
255 TopoDS_Shape shape = Reader.OneShape();
256 // save the shape
257 if (shape.IsNull()) { theDI << "No Shape produced\n"; continue; }
258 char fname[110];
259 Sprintf(fname, "%s", rnom.ToCString());
260 theDI << "Saving shape in variable Draw : " << fname << "\n";
261 if (answer == 3) WriteShape(shape, 1);
262 try
263 {
264 OCC_CATCH_SIGNALS
265 DBRep::Set(fname, shape);
266 }
267 catch (Standard_Failure const& anException)
268 {
269 theDI << "** Exception : ";
270 theDI << anException.GetMessageString();
271 theDI << " ** Skip\n";
272 theDI << "Saving shape in variable Draw : " << fname << "\n";
273 WriteShape(shape, 1);
274 }
7fd59977 275 }
bd651bbb 276
277 else if (answer == 2 || answer == 4)
278 {
279 Standard_Integer numshape = Reader.NbShapes();
280 for (Standard_Integer inum = 1; inum <= numshape; inum++)
281 {
282 // save all the shapes
283 TopoDS_Shape shape = Reader.Shape(inum);
284 if (shape.IsNull()) { theDI << "No Shape produced\n"; continue; }
285 char fname[110];
286 Sprintf(fname, "%s_%d", rnom.ToCString(), inum);
287 theDI << "Saving shape in variable Draw : " << fname << "\n";
288 if (answer == 4) WriteShape(shape, inum);
289 try
290 {
291 OCC_CATCH_SIGNALS
292 DBRep::Set(fname, shape);
293 }
294 catch (Standard_Failure const& anException)
295 {
296 theDI << "** Exception : ";
297 theDI << anException.GetMessageString();
298 theDI << " ** Skip\n";
299 }
300 }
7fd59977 301 }
302 else return 0;
303 }
304
bd651bbb 305 else if (modepri == 3)
306 { // One Entity
307 std::cout << "Only One Entity" << std::endl;
308 std::cout << "spline_continuity (read) : " << Interface_Static::IVal("read.iges.bspline.continuity") << " (0 : no modif, 1 : C1, 2 : C2)" << std::endl;
309 std::cout << " To modify : command param read.iges.bspline.continuity" << std::endl;
04232180 310 std::cout << " give the number of the Entity : " << std::flush;
bd651bbb 311 nent = GiveEntityNumber(aWS, "");
312
313 if (!Reader.TransferOne(nent))
314 theDI << "Transfer entity n0 " << nent << " : no result\n";
315 else
316 {
317 nbs = Reader.NbShapes();
318 char shname[30]; Sprintf(shname, "%s_%d", rnom.ToCString(), nent);
319 theDI << "Transfer entity n0 " << nent << " OK -> DRAW Shape: " << shname << "\n";
320 theDI << "Now, " << nbs << " Shapes produced\n";
321 TopoDS_Shape sh = Reader.Shape(nbs);
322 DBRep::Set(shname, sh);
7fd59977 323 }
324 }
325
bd651bbb 326 else if (modepri == 4)
327 { // Selection
7fd59977 328 Standard_Integer answer = 1;
329 Handle(TColStd_HSequenceOfTransient) list;
330
bd651bbb 331 // Selection, nommee ou via tcl. tcl : raccourcis admis
332 // * donne iges-visible + xst-transferrable-roots
333 // *r donne xst-model-roots (TOUTES racines)
7fd59977 334
bd651bbb 335 if (fromtcl && theArgVec[3][0] == '*' && theArgVec[3][1] == '\0')
336 {
337 theDI << "All Geometry Transfer\n";
338 theDI << "spline_continuity (read) : " << Interface_Static::IVal("read.iges.bspline.continuity") << " (0 : no modif, 1 : C1, 2 : C2)\n";
339 theDI << " To modify : command param read.iges.bspline.continuity\n";
340 const Handle(XSControl_WorkSession)& thesession = Reader.WS();
341 thesession->TransferReader()->Context().Clear();
7e785937 342
343 aPSRoot.SetName("Translation");
344 progress->Show(aPSRoot);
bd651bbb 345
346 Reader.SetReadVisible(Standard_True);
7e785937 347 Reader.TransferRoots(aPSRoot.Next(80));
bd651bbb 348
7e785937 349 if (aPSRoot.UserBreak())
350 return 1;
351
7fd59977 352 // result in only one shape for all the roots
353 TopoDS_Shape shape = Reader.OneShape();
354 // save the shape
355 char fname[110];
91322f44 356 Sprintf(fname, "%s", rnom.ToCString());
bd651bbb 357 theDI << "Saving shape in variable Draw : " << fname << "\n";
358 try
359 {
7fd59977 360 OCC_CATCH_SIGNALS
bd651bbb 361 DBRep::Set(fname, shape);
362 }
363 catch (Standard_Failure const& anException)
364 {
365 theDI << "** Exception : ";
366 theDI << anException.GetMessageString();
367 theDI << " ** Skip\n";
368 theDI << "Saving shape in variable Draw : " << fname << "\n";
369 WriteShape(shape, 1);
7fd59977 370 }
7fd59977 371 return 0;
372 }
bd651bbb 373
374 if (fromtcl)
375 {
376 modepri = 0; // d office, une seule passe
377 if (theArgVec[3][0] == '*' && theArgVec[3][1] == 'r' && theArgVec[3][2] == '\0')
378 {
379 theDI << "All Roots : ";
380 list = XSDRAW::Session()->GiveList("xst-model-roots");
381 }
382 else
383 {
384 TCollection_AsciiString compart = XSDRAW_CommandPart(theNbArgs, theArgVec, 3);
385 theDI << "List given by " << compart.ToCString() << " : ";
386 list = XSDRAW::Session()->GiveList(compart.ToCString());
387 }
388 if (list.IsNull())
389 {
390 theDI << "No list defined. Give a selection name or * for all visible transferrable roots\n";
7fd59977 391 continue;
392 }
393 }
bd651bbb 394 else
395 {
396 std::cout << "Name of Selection :" << std::flush;
397 list = XSDRAW::Session()->GiveList("");
398 if (list.IsNull()) { std::cout << "No list defined" << std::endl; continue; }
7fd59977 399 }
400
401 Standard_Integer nbl = list->Length();
bd651bbb 402 theDI << "Nb entities selected : " << nbl << "\n";
7fd59977 403 if (nbl == 0) continue;
bd651bbb 404 while (answer)
405 {
406 if (!fromtcl)
407 {
408 std::cout << "Choice: 0 abandon 1 transfer all 2 with confirmation 3 list n0s ents :" << std::flush;
7fd59977 409 answer = -1;
410 // anv 26.09.2003
bd651bbb 411 char str_answer[80];
412 std::cin >> str_answer;
413 answer = Draw::Atoi(str_answer);
414 }
415 if (answer <= 0 || answer > 3) continue;
416 if (answer == 3)
417 {
418 for (Standard_Integer ill = 1; ill <= nbl; ill++)
419 {
420 Handle(Standard_Transient) ent = list->Value(ill);
421 theDI << " ";// model->Print(ent,theDI);
422 }
423 theDI << "\n";
424 }
425 if (answer == 1 || answer == 2)
426 {
427 Standard_Integer nbt = 0;
428 Handle(XSControl_WorkSession) thesession = Reader.WS();
429
7e785937 430 aPSRoot.SetName("Translation");
431 progress->Show(aPSRoot);
7fd59977 432
7e785937 433 Message_ProgressScope aPS(aPSRoot.Next(80), "Root", nbl);
434 for (Standard_Integer ill = 1; ill <= nbl && aPS.More(); ill++)
435 {
bd651bbb 436 nent = Reader.Model()->Number(list->Value(ill));
437 if (nent == 0) continue;
438 if (!Reader.TransferOne(nent, aPS.Next()))
439 theDI << "Transfer entity n0 " << nent << " : no result\n";
440 else
441 {
442 nbs = Reader.NbShapes();
443 char shname[30]; Sprintf(shname, "%s_%d", rnom.ToCString(), nbs);
444 theDI << "Transfer entity n0 " << nent << " OK -> DRAW Shape: " << shname << "\n";
445 theDI << "Now, " << nbs << " Shapes produced\n";
446 TopoDS_Shape sh = Reader.Shape(nbs);
447 DBRep::Set(shname, sh);
7fd59977 448 nbt++;
bd651bbb 449 }
450 }
7e785937 451 if (aPSRoot.UserBreak())
452 return 1;
bd651bbb 453 theDI << "Nb Shapes successfully produced : " << nbt << "\n";
454 answer = 0; // on ne reboucle pas
455 }
7fd59977 456 }
457 }
bd651bbb 458 else theDI << "Unknown mode n0 " << modepri << "\n";
7fd59977 459 }
460 return 0;
461}
462
bd651bbb 463//=======================================================================
464//function : testread
465//purpose :
466//=======================================================================
467static Standard_Integer testread(Draw_Interpretor& theDI,
468 Standard_Integer theNbArgs,
469 const char** theArgVec)
7fd59977 470{
bd651bbb 471 if (theNbArgs != 3)
472 {
473 theDI << "ERROR in " << theArgVec[0] << "Wrong Number of Arguments.\n";
474 theDI << " Usage : " << theArgVec[0] << " file_name shape_name\n";
475 return 1;
476 }
7fd59977 477 IGESControl_Reader Reader;
bd651bbb 478 Standard_CString filename = theArgVec[1];
479 IFSelect_ReturnStatus readstat = Reader.ReadFile(filename);
480 theDI << "Status from reading IGES file " << filename << " : ";
481 switch (readstat)
482 {
483 case IFSelect_RetVoid: { theDI << "empty file\n"; return 1; }
484 case IFSelect_RetDone: { theDI << "file read\n"; break; }
485 case IFSelect_RetError: { theDI << "file not found\n"; return 1; }
486 case IFSelect_RetFail: { theDI << "error during read\n"; return 1; }
487 default: { theDI << "failure\n"; return 1; }
488 }
7fd59977 489 Reader.TransferRoots();
490 TopoDS_Shape shape = Reader.OneShape();
bd651bbb 491 DBRep::Set(theArgVec[2], shape);
492 theDI << "Count of shapes produced : " << Reader.NbShapes() << "\n";
493 return 0;
7fd59977 494}
7fd59977 495
bd651bbb 496//=======================================================================
497//function : brepiges
498//purpose :
499//=======================================================================
500static Standard_Integer brepiges(Draw_Interpretor& theDI,
501 Standard_Integer theNbArgs,
502 const char** theArgVec)
7fd59977 503{
bd651bbb 504 Handle(XSControl_WorkSession) aWorkSession = XSDRAW::Session();
505 aWorkSession->SelectNorm("IGES");
7fd59977 506
bd651bbb 507 IGESControl_Writer anIgesWriter(Interface_Static::CVal("write.iges.unit"),
508 Interface_Static::IVal("write.iges.brep.mode"));
509 theDI << "unit (write) : " << Interface_Static::CVal("write.iges.unit") << "\n";
510 theDI << "mode write : " << Interface_Static::CVal("write.iges.brep.mode") << "\n";
511 theDI << " To modify : command param\n";
512
513 const char* aFileName = nullptr;
514 Standard_Integer aNumShapesProcessed = 0;
515
516 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(theDI, 1);
517 Message_ProgressScope aRootProgress(aProgress->Start(), "Translating", 100);
518 aProgress->Show(aRootProgress);
519
520 Message_ProgressScope aStepProgress(aRootProgress.Next(90), NULL, theNbArgs);
521 for (Standard_Integer i = 1; i < theNbArgs && aStepProgress.More(); i++)
522 {
523 const char* aVariableName = theArgVec[i];
524 if (theArgVec[i][0] == '+') aVariableName = &(theArgVec[i])[1];
525 else if (i > 1) {
526 aFileName = theArgVec[i]; break;
527 }
528
529 TopoDS_Shape aShape = DBRep::Get(aVariableName);
530 if (anIgesWriter.AddShape(aShape, aStepProgress.Next())) aNumShapesProcessed++;
531 else if (anIgesWriter.AddGeom(DrawTrSurf::GetCurve(aVariableName))) aNumShapesProcessed++;
532 else if (anIgesWriter.AddGeom(DrawTrSurf::GetSurface(aVariableName))) aNumShapesProcessed++;
533 }
534 anIgesWriter.ComputeModel();
535
536 if (aRootProgress.UserBreak())
7e785937 537 return 1;
7fd59977 538
bd651bbb 539 aRootProgress.SetName("Writing");
540 aProgress->Show(aRootProgress);
541
542 theDI << aNumShapesProcessed << " Shapes written, giving " << anIgesWriter.Model()->NbEntities() << " Entities\n";
7fd59977 543
bd651bbb 544 if (!aFileName) // delayed write
2857a8ac 545 {
bd651bbb 546 theDI << " Now, to write a file, command : writeall filename\n";
2857a8ac 547 return 0;
7fd59977 548 }
549
2857a8ac 550 // write file
bd651bbb 551 if (!anIgesWriter.Write(aFileName))
552 {
553 theDI << " Error: could not write file " << aFileName << "\n";
554 return 1;
555 }
556 theDI << " File " << aFileName << " written\n";
557 aWorkSession->SetModel(anIgesWriter.Model());
7fd59977 558
7fd59977 559 return 0;
560}
561
bd651bbb 562//=======================================================================
563//function : testwrite
564//purpose :
565//=======================================================================
566static Standard_Integer testwrite(Draw_Interpretor& theDI,
567 Standard_Integer theNbArgs,
568 const char** theArgVec)
7fd59977 569{
bd651bbb 570 if (theNbArgs != 3)
571 {
572 theDI << "ERROR in " << theArgVec[0] << "Wrong Number of Arguments.\n";
573 theDI << " Usage : " << theArgVec[0] << " file_name shape_name\n";
574 return 1;
575 }
7fd59977 576 IGESControl_Writer Writer;
bd651bbb 577 Standard_CString filename = theArgVec[1];
578 TopoDS_Shape shape = DBRep::Get(theArgVec[2]);
7fd59977 579 Standard_Boolean ok = Writer.AddShape(shape);
bd651bbb 580 if (!ok)
581 {
582 theDI << "Shape not add\n";
7fd59977 583 return 1;
584 }
bd651bbb 585
586 if (!(Writer.Write(filename)))
587 {
588 theDI << "Error on writing file\n";
7fd59977 589 return 1;
590 }
bd651bbb 591 theDI << "File Is Written\n";
7fd59977 592 return 0;
593}
7fd59977 594
bd651bbb 595//=======================================================================
596//function : igesparam
597//purpose :
598//=======================================================================
599static Standard_Integer igesparam(Draw_Interpretor& theDI,
600 Standard_Integer,
601 const char**)
7fd59977 602{
bd651bbb 603 // liste des parametres
604 theDI << "List of parameters which control IGES :\n";
605 theDI << " unit : write.iges.unit\n mode write : write.iges.brep.mode\n spline_continuity (read) : read.iges.bspline.continuity\nSee definition by defparam, read/edit value by param\n";
606 theDI << "unit (write) : " << Interface_Static::CVal("write.iges.unit") << "\n";
607 theDI << "mode write : " << Interface_Static::CVal("write.iges.brep.mode") << "\n";
608 theDI << "spline_continuity (read) : " << Interface_Static::IVal("read.iges.bspline.continuity") << " (0 : no modif, 1 : C1, 2 : C2)\n";
609 theDI << "\n To modifier, param nom_param new_val\n";
7fd59977 610 return 0;
611}
612
bd651bbb 613//=======================================================================
614//function : XSDRAWIGES_tplosttrim
615//purpose :
616//=======================================================================
617static Standard_Integer XSDRAWIGES_tplosttrim(Draw_Interpretor& theDI,
618 Standard_Integer theNbArgs,
619 const char** theArgVec)
620{
621 Handle(XSControl_WorkSession) aWorkSession = XSDRAW::Session();
622 const Handle(Transfer_TransientProcess)& anTransientProcess = aWorkSession->TransferReader()->TransientProcess();
623 TColStd_Array1OfAsciiString aTypeStrings(1, 3);
624 TColStd_Array1OfAsciiString aKindStrings(1, 3);
625 aTypeStrings.SetValue(1, "xst-type(CurveOnSurface)");
626 aTypeStrings.SetValue(2, "xst-type(Boundary)");
627 aTypeStrings.SetValue(3, "xst-type(Loop)");
628 aKindStrings.SetValue(1, "IGESGeom_TrimmedSurface");
629 aKindStrings.SetValue(2, "IGESGeom_BoundedSurface");
630 aKindStrings.SetValue(3, "IGESSolid_Face");
631 if (anTransientProcess.IsNull())
632 {
633 theDI << "No Transfer Read\n";
634 return 1;
635 }
636 Standard_Integer anNumFaces = 0, aTotalFaces = 0;
637 Transfer_IteratorOfProcessForTransient anIterator = anTransientProcess->AbnormalResult();
638 Standard_Integer anIndex = 0;
639 if (theNbArgs > 1)
640 {
641 TCollection_AsciiString anArg(theArgVec[1]);
642 for (anIndex = 1; anIndex <= 3; anIndex++)
643 {
644 if (aKindStrings.Value(anIndex).Location(anArg, 1, aKindStrings.Value(anIndex).Length()) != 0)
645 {
646 break;
647 }
648 }
649 }
7fd59977 650
bd651bbb 651 if (anIndex == 4)
652 {
653 theDI << "Invalid argument\n";
654 return 0;
655 }
7fd59977 656
bd651bbb 657 for (Standard_Integer j = 1; j <= 3; j++)
658 {
659 TColStd_MapOfTransient aFaceMap;
660
661 if (theNbArgs == 1)
662 {
663 anIndex = j;
7fd59977 664 }
bd651bbb 665 Handle(TColStd_HSequenceOfTransient) aFaceList = aWorkSession->GiveList(aTypeStrings.Value(anIndex).ToCString());
666 if (!aFaceList.IsNull())
667 {
668 anIterator.Filter(aFaceList);
669 }
670 else
671 {
672 theDI << "No untrimmed faces\n";
7fd59977 673 return 0;
674 }
bd651bbb 675 for (anIterator.Start(); anIterator.More(); anIterator.Next())
676 {
677 Handle(Standard_Transient) anEntity = anIterator.Starting();
678 Handle(TColStd_HSequenceOfTransient) aSharingEntities = aWorkSession->Sharings(anEntity);
679 if (!aSharingEntities.IsNull())
680 {
681 Standard_Integer aNumSharingEntities = aSharingEntities->Length();
682 if (aNumSharingEntities > 0)
683 {
684 for (Standard_Integer i = 1; i <= aNumSharingEntities; i++)
685 {
686 if (aSharingEntities->Value(i)->IsKind(aKindStrings.Value(anIndex).ToCString()))
687 {
688 if (aFaceMap.Add(aSharingEntities->Value(i)))
689 {
690 anNumFaces++;
691 }
692 }
693 }
694 }
7fd59977 695 }
696 }
bd651bbb 697 if (anNumFaces != 0)
698 {
699 if (j == 1)
700 {
701 theDI << "Number of untrimmed faces: \n";
702 }
703 switch (anIndex)
704 {
705 case 1:
706 theDI << "Trimmed Surface: \n";
707 break;
7fd59977 708 case 2:
bd651bbb 709 theDI << "Bounded Surface: \n";
710 break;
7fd59977 711 case 3:
bd651bbb 712 theDI << "Face: \n";
713 break;
7fd59977 714 }
bd651bbb 715 TColStd_MapIteratorOfMapOfTransient anMapIterator;
0ebe5b0a 716 Standard_SStream aTmpStream;
bd651bbb 717 for (anMapIterator.Initialize(aFaceMap); anMapIterator.More(); anMapIterator.Next())
718 {
719 aWorkSession->Model()->Print(anMapIterator.Key(), aTmpStream);
0ebe5b0a 720 aTmpStream << " ";
7fd59977 721 }
bd651bbb 722 theDI << aTmpStream.str().c_str();
723 theDI << "\n";
724 theDI << "\nNumber:" << anNumFaces << "\n";
725 aTotalFaces += anNumFaces;
726 }
727 if (theNbArgs > 1)
728 {
729 break;
7fd59977 730 }
bd651bbb 731 anNumFaces = 0;
732 }
733 if (aTotalFaces == 0)
734 {
735 theDI << "No untrimmed faces\n";
736 }
737 else
738 {
739 theDI << "Total number :" << aTotalFaces << "\n";
7fd59977 740 }
7fd59977 741 return 0;
742}
bd651bbb 743
744
745//=======================================================================
746//function : XSDRAWIGES_TPSTAT
747//purpose :
748//=======================================================================
749static Standard_Integer XSDRAWIGES_TPSTAT(Draw_Interpretor& theDI,
750 Standard_Integer theNbArgs,
751 const char** theArgVec)
7fd59977 752{
bd651bbb 753 Handle(XSControl_WorkSession) aWorkSession = XSDRAW::Session();
754 const Standard_CString anArg1 = theArgVec[1];
755 const Handle(Transfer_TransientProcess)& aTransientProcess = aWorkSession->TransferReader()->TransientProcess();
756 IGESControl_Reader aReader;
757 Handle(Interface_InterfaceModel) aModel = aTransientProcess->Model();
758 if (aModel.IsNull()) { theDI << "No Transfer Read\n"; return -1; }
759 Handle(XSControl_WorkSession) aSession = aReader.WS();
760 aSession->SetMapReader(aTransientProcess);
761 Standard_Integer aMode = 0;
762 if (theNbArgs > 1)
763 {
764 char a2 = anArg1[1]; if (a2 == '\0') a2 = '!';
765 switch (anArg1[0])
766 {
767 case 'g': aReader.PrintTransferInfo(IFSelect_FailAndWarn, IFSelect_GeneralInfo); break;
768 case 'c': aReader.PrintTransferInfo(IFSelect_FailAndWarn, IFSelect_CountByItem); break;
769 case 'C': aReader.PrintTransferInfo(IFSelect_FailAndWarn, IFSelect_ListByItem); break;
770 case 'r': aReader.PrintTransferInfo(IFSelect_FailAndWarn, IFSelect_ResultCount); break;
771 case 's': aReader.PrintTransferInfo(IFSelect_FailAndWarn, IFSelect_Mapping); break;
772 case '?': aMode = -1; break;
773 default: aMode = -2; break;
7fd59977 774 }
775 }
bd651bbb 776 if (aMode < -1) theDI << "Unknown Mode\n";
777 if (aMode < 0)
778 {
779 theDI << "Modes available :\n"
780 << "g : general c : checks (count) C (list)\n"
781 << "r : number of CasCade resulting shapes\n"
782 << "s : mapping between IGES entities and CasCade shapes\n";
783 if (aMode < -1) return -1;
7fd59977 784 return 0;
785 }
786 return 0;
787}
788
bd651bbb 789
790//=======================================================================
791//function : etest
792//purpose :
793//=======================================================================
794static Standard_Integer etest(Draw_Interpretor& theDI,
795 Standard_Integer theNbArgs,
796 const char** theArgVec)
7fd59977 797{
bd651bbb 798 if (theNbArgs < 3)
799 {
800 theDI << "etest igesfile shape\n";
7fd59977 801 return 0;
802 }
803 IGESControl_Reader aReader;
bd651bbb 804 aReader.ReadFile(theArgVec[1]);
7fd59977 805 aReader.SetReadVisible(Standard_True);
806 aReader.TransferRoots();
807 TopoDS_Shape shape = aReader.OneShape();
bd651bbb 808 DBRep::Set(theArgVec[2], shape);
7fd59977 809 return 0;
810}
811
bd651bbb 812//=======================================================================
813//function : ReadIges
814//purpose : Read IGES to DECAF document
815//=======================================================================
816static Standard_Integer ReadIges(Draw_Interpretor& theDI,
817 Standard_Integer theNbArgs,
818 const char** theArgVec)
7fd59977 819{
bd651bbb 820 if (theNbArgs < 3)
821 {
822 theDI << "Use: " << theArgVec[0] << " Doc filename [mode]: read IGES file to a document\n";
823 return 0;
824 }
7fd59977 825
bd651bbb 826 DeclareAndCast(IGESControl_Controller, aController, XSDRAW::Controller());
827 if (aController.IsNull()) XSDRAW::SetNorm("IGES");
7fd59977 828
bd651bbb 829 TCollection_AsciiString aFileName, aModelName;
830 Standard_Boolean isModified = XSDRAW::FileAndVar(theArgVec[2], theArgVec[1], "IGES", aFileName, aModelName);
831 if (isModified) theDI << " File IGES to read : " << aFileName.ToCString() << "\n";
832 else theDI << " Model taken from the session : " << aModelName.ToCString() << "\n";
7fd59977 833
bd651bbb 834 IGESCAFControl_Reader aReader(XSDRAW::Session(), isModified);
835 Standard_Integer onlyVisible = Interface_Static::IVal("read.iges.onlyvisible");
836 aReader.SetReadVisible(onlyVisible == 1);
837
838 if (theNbArgs == 4)
839 {
840 Standard_Boolean mode = Standard_True;
841 for (Standard_Integer i = 0; theArgVec[3][i]; i++)
842 switch (theArgVec[3][i])
843 {
844 case '-': mode = Standard_False; break;
845 case '+': mode = Standard_True; break;
846 case 'c': aReader.SetColorMode(mode); break;
847 case 'n': aReader.SetNameMode(mode); break;
848 case 'l': aReader.SetLayerMode(mode); break;
849 }
850 }
851 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(theDI);
852 Message_ProgressScope aRootScope(aProgress->Start(), "IGES import", isModified ? 2 : 1);
853
854 IFSelect_ReturnStatus aReadStatus = IFSelect_RetVoid;
855 if (isModified)
856 {
857 Message_ProgressScope aReadScope(aRootScope.Next(), "File reading", 1);
858 aReadScope.Show();
859 aReadStatus = aReader.ReadFile(aFileName.ToCString());
860 }
861 else if (XSDRAW::Session()->NbStartingEntities() > 0)
862 {
863 aReadStatus = IFSelect_RetDone;
864 }
865 if (aReadStatus != IFSelect_RetDone)
866 {
867 if (isModified)
868 {
869 theDI << "Could not read file " << aFileName.ToCString() << " , abandon\n";
870 }
871 else
872 {
873 theDI << "No model loaded\n";
874 }
875 return 1;
876 }
877
878 Handle(TDocStd_Document) aDocument;
879 if (!DDocStd::GetDocument(theArgVec[1], aDocument, Standard_False))
880 {
881 Handle(TDocStd_Application) anApplication = DDocStd::GetApplication();
882 anApplication->NewDocument("BinXCAF", aDocument);
883 TDataStd_Name::Set(aDocument->GetData()->Root(), theArgVec[1]);
884 Handle(DDocStd_DrawDocument) aDrawDocument = new DDocStd_DrawDocument(aDocument);
885 Draw::Set(theArgVec[1], aDrawDocument);
886 }
887 if (!aReader.Transfer(aDocument, aRootScope.Next()))
888 {
889 theDI << "Cannot read any relevant data from the IGES file\n";
890 return 1;
891 }
892 theDI << "Document saved with name " << theArgVec[1];
7fd59977 893
bd651bbb 894 return 0;
895}
7fd59977 896
897//=======================================================================
bd651bbb 898//function : WriteIges
899//purpose : Write DECAF document to IGES
7fd59977 900//=======================================================================
bd651bbb 901static Standard_Integer WriteIges(Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
7fd59977 902{
bd651bbb 903 if (theNbArgs < 3)
904 {
905 theDI << "Use: " << theArgVec[0] << " Doc filename [mode]: write document to IGES file\n";
906 return 0;
907 }
7fd59977 908
bd651bbb 909 Handle(TDocStd_Document) aDocument;
910 DDocStd::GetDocument(theArgVec[1], aDocument);
911 if (aDocument.IsNull())
912 {
913 theDI << theArgVec[1] << " is not a document\n";
914 return 1;
915 }
7fd59977 916
bd651bbb 917 XSDRAW::SetNorm("IGES");
918
919 TCollection_AsciiString aFileName, aModelName;
920 const Standard_Boolean isModified = XSDRAW::FileAndVar(theArgVec[2], theArgVec[1], "IGES", aFileName, aModelName);
921
922 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(theDI);
923 Message_ProgressScope aRootScope(aProgress->Start(), "IGES export", isModified ? 2 : 1);
924
925 IGESCAFControl_Writer aWriter(XSDRAW::Session(), Standard_True);
926 if (theNbArgs == 4)
927 {
928 Standard_Boolean mode = Standard_True;
929 for (Standard_Integer i = 0; theArgVec[3][i]; i++)
930 switch (theArgVec[3][i])
931 {
932 case '-': mode = Standard_False; break;
933 case '+': mode = Standard_True; break;
934 case 'c': aWriter.SetColorMode(mode); break;
935 case 'n': aWriter.SetNameMode(mode); break;
936 case 'l': aWriter.SetLayerMode(mode); break;
937 }
938 }
939 aWriter.Transfer(aDocument, aRootScope.Next());
940
941 if (isModified)
942 {
943 Message_ProgressScope aWriteScope(aRootScope.Next(), "File writing", 1);
944 aWriteScope.Show();
945 theDI << "Writing IGES model to file " << theArgVec[2] << "\n";
946 if (aWriter.Write(aFileName.ToCString()))
947 {
948 theDI << " Write OK\n";
949 }
950 else
951 {
952 theDI << " Write failed\n";
953 }
954 }
955 else
956 {
957 theDI << "Document has been translated into the session";
958 }
959 return 0;
960}
7fd59977 961
962//=======================================================================
bd651bbb 963//function : Factory
964//purpose :
7fd59977 965//=======================================================================
bd651bbb 966void XSDRAWIGES::Factory(Draw_Interpretor& theDI)
7fd59977 967{
bd651bbb 968 static Standard_Boolean aIsActivated = Standard_False;
969 if (aIsActivated)
970 {
971 return;
972 }
973 aIsActivated = Standard_True;
974
975 IGESControl_Controller::Init();
976
977 const char* aGroup = "DE: IGES";
978
979 theDI.Add("tplosttrim", "number of untrimmed faces during last transfer", __FILE__, XSDRAWIGES_tplosttrim, aGroup);
980 theDI.Add("igesbrep", "igesbrep [file else already loaded model] [name DRAW]", __FILE__, igesbrep, aGroup);
981 theDI.Add("testreadiges", "testreadiges [file else already loaded model] [name DRAW]", __FILE__, testread, aGroup);
982 theDI.Add("igesparam", "igesparam ->list, + name ->one param, + name val->change", __FILE__, igesparam, aGroup);
983 theDI.Add("TPSTAT", " ", __FILE__, XSDRAWIGES_TPSTAT, aGroup);
984 theDI.Add("etest", "test of eviewer", __FILE__, etest, aGroup);
985
986 theDI.Add("ReadIges", "Doc filename: Read IGES file to DECAF document", __FILE__, ReadIges, aGroup);
987 theDI.Add("WriteIges", "Doc filename: Write DECAF document to IGES file", __FILE__, WriteIges, aGroup);
988 theDI.Add("igesread", "igesread [file else already loaded model] [name DRAW]", __FILE__, igesbrep, aGroup);
989 theDI.Add("igeswrite", "igesread [file else already loaded model] [name DRAW]", __FILE__, brepiges, aGroup);
990 theDI.Add("brepiges", "brepiges sh1 [+sh2 [+sh3 ..]] filename.igs", __FILE__, brepiges, aGroup);
991 theDI.Add("testwriteiges", "testwriteiges filename.igs shape", __FILE__, testwrite, aGroup);
992
993 // Load XSDRAW session for pilot activation
994 XSDRAW::LoadDraw(theDI);
7fd59977 995}
bd651bbb 996
997// Declare entry point PLUGINFACTORY
998DPLUGIN(XSDRAWIGES)