1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 //pdn 11.01.99 including <stdio.h> for compilation on NT
15 //#70 rln 03.03.99 syntax correction
16 // sln 11.06.2002 OCC448 : Initialize "read.onlyvisiable" parameter to control transfering invisiable sub entities which logicaly depend on the grouping entities
18 #include <BRep_Builder.hxx>
19 #include <BRepLib.hxx>
20 #include <BRepTools_Modifier.hxx>
21 #include <gp_Trsf.hxx>
22 #include <IGESAppli.hxx>
23 #include <IGESAppli_Protocol.hxx>
24 #include <IGESData_FileProtocol.hxx>
25 #include <IGESData_GlobalSection.hxx>
26 #include <IGESData_IGESEntity.hxx>
27 #include <IGESData_IGESModel.hxx>
28 #include <IGESFile_Read.hxx>
29 #include <IGESSolid.hxx>
30 #include <IGESSolid_Protocol.hxx>
31 #include <IGESToBRep.hxx>
32 #include <IGESToBRep_Actor.hxx>
33 #include <IGESToBRep_CurveAndSurface.hxx>
34 #include <IGESToBRep_Reader.hxx>
35 #include <Interface_Check.hxx>
36 #include <Interface_CheckIterator.hxx>
37 #include <Interface_CheckTool.hxx>
38 #include <Interface_Macros.hxx>
39 #include <Interface_ShareFlags.hxx>
40 #include <Interface_Static.hxx>
41 #include <Message_Messenger.hxx>
42 #include <Message_Msg.hxx>
43 #include <Message_ProgressSentry.hxx>
44 #include <OSD_Timer.hxx>
45 #include <ShapeAlgo.hxx>
46 #include <ShapeAlgo_AlgoContainer.hxx>
47 #include <ShapeExtend_Explorer.hxx>
48 #include <ShapeFix_ShapeTolerance.hxx>
49 #include <Standard_ErrorHandler.hxx>
50 #include <Standard_Failure.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <Transfer_IteratorOfProcessForTransient.hxx>
55 #include <Transfer_TransferOutput.hxx>
56 #include <Transfer_TransientProcess.hxx>
57 #include <TransferBRep.hxx>
58 #include <TransferBRep_ShapeBinder.hxx>
59 #include <TransferBRep_ShapeListBinder.hxx>
61 #include <XSAlgo_AlgoContainer.hxx>
64 //#include <ShapeCustom.hxx>
72 static Handle(IGESData_FileProtocol) protocol;
75 //=======================================================================
76 //function : IGESToBRep_Reader
78 //=======================================================================
79 IGESToBRep_Reader::IGESToBRep_Reader ()
81 theDone = Standard_False;
82 if (protocol.IsNull()) {
83 IGESAppli::Init(); IGESSolid::Init();
84 protocol = new IGESData_FileProtocol;
85 protocol->Add(IGESAppli::Protocol());
86 protocol->Add(IGESSolid::Protocol());
88 theActor = new IGESToBRep_Actor;
89 theProc = new Transfer_TransientProcess;
93 //=======================================================================
95 //purpose : loads a Model from a file
96 //=======================================================================
98 Standard_Integer IGESToBRep_Reader::LoadFile (const Standard_CString filename)
100 if ( theProc.IsNull() )
101 theProc = new Transfer_TransientProcess;
102 Handle(Message_Messenger) TF = theProc->Messenger();
104 // Message for Diagnostic file.
105 Message_Msg msg2000("IGES_2000");
106 msg2000.Arg(filename);
107 TF->Send (msg2000, Message_Info);
108 //Message_Msg msg2001("IGES_2001"); // Date
109 Message_Msg msg2005("IGES_2005");
110 msg2005.Arg(theProc->TraceLevel());
111 TF->Send (msg2005, Message_Info);
112 /////////////////////////////////////////////////////////
113 Handle(IGESData_IGESModel) model = new IGESData_IGESModel;
115 OSD_Timer c; c.Reset(); c.Start();
116 char *pfilename=(char *)filename;
117 Standard_Integer StatusFile = IGESFile_Read(pfilename,model,protocol);
118 if (StatusFile != 0) {
119 // Sending of message : IGES file opening error
120 Message_Msg Msg2("XSTEP_2");
121 TF->Send (Msg2, Message_Info);
122 //Message_Msg Msg3("XSTEP_3");
123 //Message_Msg Msg4("XSTEP_4");
124 //Message_Msg Msg5("XSTEP_5");
125 //Message_Msg Msg6("XSTEP_6");
126 //Message_Msg Msg7("XSTEP_7");
127 // Reasons of the file opening error
130 case 2 : // Sending of message : No such file or directory
132 Message_Msg Msg3("XSTEP_3");
133 TF->Send (Msg3, Message_Info);
136 case 12 : // Sending of message : Not enough space
138 Message_Msg Msg4("XSTEP_4");
139 TF->Send (Msg4, Message_Info);
142 case 13 : // Sending of message : Permission Denied
144 Message_Msg Msg5("XSTEP_5");
145 TF->Send (Msg5, Message_Info);
148 case 24 : // Sending of message : Too many open files
150 Message_Msg Msg6("XSTEP_6");
151 TF->Send (Msg6, Message_Info);
154 default : // Sending of message : No determined
156 Message_Msg Msg7("XSTEP_7");
157 TF->Send (Msg7, Message_Info);
163 Message_Msg Msg8 ("XSTEP_8");
164 Message_Msg Msg25 ("XSTEP_25");
165 Message_Msg Msg26 ("XSTEP_26");
166 // Nb warning in global section.
168 Standard_Integer nbWarn = 0,nbFail = 0;
169 // Add the number of warning on enities :
170 Interface_CheckTool cht (model,protocol);
171 Interface_CheckIterator anIter = cht.CompleteCheckList();
172 for(anIter.Start(); anIter.More(); anIter.Next()) {
173 const Handle(Interface_Check) ach = anIter.Value();
174 nbWarn += ach->NbWarnings();
175 nbFail += ach->NbFails();
177 // Messages nbWarn and nbFail;
180 TF->Send (Msg25, Message_Info);
181 TF->Send (Msg26, Message_Info);
183 // Message fin de loading iGES file (elapsed time %s)
186 Standard_Real second, cpu;
187 Standard_Integer minute, hour;
188 c.Show(second, minute, hour,cpu);
190 Sprintf(t,"%dh:%dm:%.2fs",hour,minute,second);
192 Sprintf(t,"%dm:%.2fs",minute,second);
194 Sprintf(t,"%.2fs",second);
195 // Sending of message : End of Loading
197 TF->Send (Msg8, Message_Info);
204 //=======================================================================
205 //function : SetModel
206 //purpose : Specifies a Model to work on
207 //=======================================================================
208 void IGESToBRep_Reader::SetModel (const Handle(IGESData_IGESModel)& model)
211 theDone = Standard_False;
213 if ( theProc.IsNull() )
214 theProc = new Transfer_TransientProcess (theModel->NbEntities());
220 //=======================================================================
222 //purpose : returns the Model to be worked on
223 //=======================================================================
224 Handle(IGESData_IGESModel) IGESToBRep_Reader::Model () const
228 //=======================================================================
229 //function : SetTransientProcess
230 //purpose : Specifies a TransferProcess
231 //=======================================================================
232 void IGESToBRep_Reader::SetTransientProcess
233 (const Handle(Transfer_TransientProcess)& TP)
236 //=======================================================================
237 //function : TransientProcess
238 //purpose : Returns the TransferProcess
239 //=======================================================================
240 Handle(Transfer_TransientProcess) IGESToBRep_Reader::TransientProcess () const
243 //=======================================================================
245 //purpose : returns theActor
246 //=======================================================================
247 Handle(IGESToBRep_Actor) IGESToBRep_Reader::Actor () const
251 //=======================================================================
253 //purpose : Clears the result and Done status
254 //=======================================================================
255 void IGESToBRep_Reader::Clear ()
257 theDone = Standard_False;
262 //=======================================================================
264 //purpose : Checks the Model
265 //=======================================================================
266 Standard_Boolean IGESToBRep_Reader::Check
267 (const Standard_Boolean withprint) const
269 Interface_CheckTool cht (theModel,protocol);
270 Interface_CheckIterator chl = cht.CompleteCheckList();
271 if (withprint && !theProc.IsNull())
272 cht.Print(chl, theProc->Messenger());
273 return chl.IsEmpty(Standard_True);
277 //=======================================================================
279 //purpose : returns True if the last transfert was a success
280 //=======================================================================
281 Standard_Boolean IGESToBRep_Reader::IsDone () const
285 //=======================================================================
286 //function : EncodeRegul
287 //purpose : INTERNAL to encode regularity on edges
288 //=======================================================================
290 static Standard_Boolean EncodeRegul (const TopoDS_Shape& sh)
292 Standard_Real tolang = Interface_Static::RVal("read.encoderegularity.angle");
293 if (sh.IsNull()) return Standard_True;
294 if (tolang <= 0) return Standard_True;
297 BRepLib::EncodeRegularity (sh,tolang);
299 catch(Standard_Failure const&) {
300 return Standard_False;
302 return Standard_True;
305 //=======================================================================
306 //function : UpdateMap
307 //purpose : Updates the correspondence map (Transfer_TransientProcess),
308 // setting as translation results, the shapes received after
309 // modification by modifier (BRepTools_Modifier)
310 //warning : BRepTools_Modifier raises exception when it cannot find input
311 // shape in its internal list
312 //=======================================================================
314 // coment as unused PTV 18.09.2000
315 // static void UpdateMap (const Handle(Transfer_TransientProcess)& map,
316 // const BRepTools_Modifier& modifier)
318 // Transfer_IteratorOfProcessForTransient iterator = map->CompleteResult(Standard_True);
319 // for (iterator.Start(); iterator.More(); iterator.Next()) {
320 // const Handle(Transfer_Binder) binder = iterator.Value();
321 // try { //to avoid exception in BRepTools_Modifier
323 // if (binder->IsKind (STANDARD_TYPE (TransferBRep_ShapeBinder))) {
324 // DeclareAndCast(TransferBRep_ShapeBinder, shapebinder, binder);
325 // if (shapebinder->HasResult()) {
326 // TopoDS_Shape result = shapebinder->Result();
327 // TopoDS_Shape modified = modifier.ModifiedShape (result);
328 // if (shapebinder->Status() != Transfer_StatusUsed) //to avoid exception
329 // shapebinder->SetResult (modified);
332 // else if (binder->IsKind (STANDARD_TYPE (TransferBRep_ShapeListBinder))) {
333 // DeclareAndCast(TransferBRep_ShapeListBinder, shapelistbinder, binder);
334 // for (Standard_Integer i = 1; i <= shapelistbinder->NbShapes(); i++) {
335 // TopoDS_Shape result = shapelistbinder->Shape (i);
336 // TopoDS_Shape modified = modifier.ModifiedShape (result);
337 // shapelistbinder->SetResult (i, modified);
341 // catch(Standard_Failure) {
347 //=======================================================================
348 //function : TrimTolerances
349 //purpose : Trims tolerances of the shape according to static parameters
351 //=======================================================================
353 static void TrimTolerances (const TopoDS_Shape& shape,
354 const Standard_Real tol)
356 if( Interface_Static::IVal("read.maxprecision.mode")==1) {
357 ShapeFix_ShapeTolerance SFST;
358 SFST.LimitTolerance (shape, 0, Max(tol,Interface_Static::RVal ("read.maxprecision.val")));
362 //=======================================================================
363 //function : TransferRoots
364 //purpose : Transfers all Roots Entities
365 //=======================================================================
366 void IGESToBRep_Reader::TransferRoots (const Standard_Boolean onlyvisible)
368 if (theModel.IsNull() || theProc.IsNull()) return;
370 Handle(Message_Messenger) TF = theProc->Messenger();
371 // Declaration of messages.
372 Message_Msg msg2030("IGES_2030");
373 TF->Send (msg2030, Message_Info);
374 Message_Msg msg2065("IGES_2065");
375 OSD_Timer c; c.Reset(); c.Start(); // Initialisation du CHRONO
376 theDone = Standard_False;
379 Standard_Integer level = theProc->TraceLevel();
380 theProc->SetErrorHandle(Standard_True);
381 theProc->SetRootManagement(Standard_True);
382 // PrepareTransfer(); -> protocol, actor
383 theActor->SetModel(theModel);
384 Standard_Integer continuity = Interface_Static::IVal("read.iges.bspline.continuity");
385 theActor->SetContinuity (continuity);
386 theProc->SetModel (theModel);
387 theProc->SetActor (theActor);
388 Transfer_TransferOutput TP (theProc,theModel);
390 const Handle(Interface_Protocol) aProtocol = protocol; // to avoid ambiguity
391 Interface_ShareFlags SH (theModel, aProtocol);
392 Standard_Integer nb = theModel->NbEntities();
393 ShapeExtend_Explorer SBE;
396 Standard_Integer precisionMode = Interface_Static::IVal("read.precision.mode");
397 Message_Msg msg2035("IGES_2035");
398 msg2035.Arg(precisionMode);
399 TF->Send (msg2035, Message_Info);
400 if (precisionMode==1) {
401 Message_Msg msg2040("IGES_2040");
402 msg2040.Arg(Interface_Static::RVal("read.precision.val"));//#70 rln 03.03.99
403 TF->Send (msg2040, Message_Info);
405 Message_Msg msg2045("IGES_2045");
406 msg2045.Arg(continuity);
407 TF->Send (msg2045, Message_Info);
408 Message_Msg msg2050("IGES_2050");
409 msg2050.Arg(Interface_Static::IVal("read.surfacecurve.mode"));
410 TF->Send (msg2050, Message_Info);
412 // sln 11.06.2002 OCC448
413 Interface_Static::SetIVal("read.iges.onlyvisible",onlyvisible);
415 Message_ProgressSentry PS ( theProc->GetProgress(), "Root", 0, nb, 1 );
416 for (Standard_Integer i = 1; i <= nb && PS.More(); i++, PS.Next()) {
417 Handle(IGESData_IGESEntity) ent = theModel->Entity(i);
418 if ( SH.IsShared(ent) || ! theActor->Recognize (ent) ) continue;
420 Message_Msg msg2070("IGES_2070");
422 msg2070.Arg(ent->TypeNumber());
423 TF->Send (msg2070, Message_Info);
425 // on ajoute un traitement pour ne prendre que les entites visibles
426 if ( ! onlyvisible || ent->BlankStatus() == 0 ) {
428 theDone = Standard_True;
432 shape = TransferBRep::ShapeResult (theProc,ent);
434 catch(Standard_Failure const&) {
435 Message_Msg msg1005("IGES_1005");
436 TF->Send (msg1005, Message_Info);
439 if (shape.IsNull()) {
440 Message_Msg msg2076("IGES_2076");
441 TF->Send (msg2076, Message_Info);
444 if (SBE.ShapeType(shape,Standard_True) != TopAbs_SHAPE) {
445 if (!shape.IsNull()) {
447 //#74 rln 03.03.99 S4135
448 TrimTolerances (shape, theActor->UsedTolerance());
449 theShapes.Append(shape);
457 Standard_Real second, cpu;
458 Standard_Integer minute, hour;
459 c.Show(second, minute, hour,cpu);
461 Sprintf(t,"%dh:%dm:%.2fs",hour,minute,second);
463 Sprintf(t,"%dm:%.2fs",minute,second);
465 Sprintf(t,"%.2fs",second);
466 // Sending of message : End of Loading
468 TF->Send (msg2065, Message_Info);
472 //=======================================================================
473 //function : Transfer
474 //purpose : Transfers an Entity given
475 //=======================================================================
476 Standard_Boolean IGESToBRep_Reader::Transfer(const Standard_Integer num)
478 Handle(Message_Messenger) TF = theProc->Messenger();
479 theDone = Standard_False;
480 if (theModel.IsNull()) {
481 Message_Msg msg2031("IGES_2031");
482 TF->Send (msg2031, Message_Info);
483 return Standard_False;
485 if (num <= 0 || num > theModel->NbEntities()) {
486 Message_Msg msg2032("IGES_2032");
488 TF->Send (msg2032, Message_Info);
489 return Standard_False;
491 // declaration of messages
492 Message_Msg msg2030("IGES_2030");
493 TF->Send (msg2030, Message_Info);
494 Message_Msg msg2065("IGES_2065");
495 OSD_Timer c; c.Reset(); c.Start(); // Initialisation du CHRONO
497 Handle(IGESData_IGESEntity) ent = theModel->Entity(num);
499 Message_ProgressSentry PS ( theProc->GetProgress(), "OneEnt", 0, 1, 1 ); //skl
501 XSAlgo::AlgoContainer()->PrepareForTransfer();
502 IGESToBRep_CurveAndSurface CAS;
503 CAS.SetModel(theModel);
505 Standard_Integer Ival = Interface_Static::IVal("read.precision.mode");
506 Message_Msg msg2035("IGES_2035");
508 TF->Send (msg2035, Message_Info);
510 eps = theModel->GlobalSection().Resolution();
512 //mjm : modif du 19/12/97 pour prise en compte effective du parametre
513 eps = Interface_Static::RVal("read.precision.val");
514 Message_Msg msg2040("IGES_2040");
515 msg2040.Arg(eps);//#70 rln 03.03.99
516 TF->Send (msg2040, Message_Info);
519 Ival = Interface_Static::IVal("read.iges.bspline.approxd1.mode");
520 CAS.SetModeApprox ( (Ival > 0) );
521 Message_Msg msg2045("IGES_2045");
522 Ival = Interface_Static::IVal("read.iges.bspline.continuity");
524 TF->Send (msg2045, Message_Info);
525 CAS.SetContinuity(Ival);
526 Message_Msg msg2050("IGES_2050");
527 Ival = Interface_Static::IVal("read.surfacecurve.mode");
529 TF->Send (msg2050, Message_Info);
530 CAS.SetSurfaceCurve (Ival);
532 if (eps > 1.E-08) CAS.SetEpsGeom(eps);
533 CAS.SetTransferProcess(theProc);
535 Standard_Boolean exceptionRaised = Standard_False;
537 Standard_Integer nbTPitems = theProc->NbMapped();
541 shape = CAS.TransferGeometry (ent);
543 catch(Standard_Failure const&) {
544 Message_Msg msg1015("IGES_1015");
545 TF->Send (msg1015, Message_Info);
546 exceptionRaised = Standard_True;
549 if (!exceptionRaised) {
551 // shape = XSAlgo::AlgoContainer()->PerformFixShape ( shape, theProc, eps*CAS.GetUnitFactor(), CAS.GetMaxTol() );
553 Handle(Standard_Transient) info;
554 shape = XSAlgo::AlgoContainer()->ProcessShape( shape, eps*CAS.GetUnitFactor(), CAS.GetMaxTol(),
555 "read.iges.resource.name",
556 "read.iges.sequence", info,
557 theProc->GetProgress() );
558 XSAlgo::AlgoContainer()->MergeTransferInfo(theProc, info, nbTPitems);
560 ShapeExtend_Explorer SBE;
561 if (SBE.ShapeType (shape,Standard_True) != TopAbs_SHAPE) {
562 TransferBRep::SetShapeResult (theProc,ent,shape);
563 theProc->SetRoot (ent);
564 if (!shape.IsNull()) {
565 theDone = Standard_True;
567 //#74 rln 03.03.99 S4135
568 TrimTolerances (shape, CAS.GetMaxTol());
569 theShapes.Append(shape);
578 Standard_Real second, cpu;
579 Standard_Integer minute, hour;
580 c.Show(second, minute, hour,cpu);
582 Sprintf(t,"%dh:%dm:%.2fs",hour,minute,second);
584 Sprintf(t,"%dm:%.2fs",minute,second);
586 Sprintf(t,"%.2fs",second);
587 // Sending of message : End of Loading
589 TF->Send (msg2065, Message_Info);
590 return Standard_True;
594 //=======================================================================
595 //function : UsedTolerance
596 //purpose : Returns the used tolerance (input)
597 //=======================================================================
598 Standard_Real IGESToBRep_Reader::UsedTolerance () const
599 { return theActor->UsedTolerance(); }
601 //=======================================================================
602 //function : NbShapes
603 //purpose : Returns the count of produced Shapes
604 //=======================================================================
605 Standard_Integer IGESToBRep_Reader::NbShapes () const
606 { return theShapes.Length(); }
609 //=======================================================================
611 //purpose : Returns a Shape given its rank
612 //=======================================================================
613 TopoDS_Shape IGESToBRep_Reader::Shape (const Standard_Integer num) const
616 if (num > 0 && num <= theShapes.Length()) res = theShapes.Value(num);
621 //=======================================================================
622 //function : OneShape
623 //purpose : Returns a unique Shape
624 //=======================================================================
625 TopoDS_Shape IGESToBRep_Reader::OneShape () const
628 Standard_Integer nb = theShapes.Length();
629 if (nb == 0) return res;
630 else if (nb == 1) return theShapes.Value(1);
635 for (Standard_Integer i = 1; i <= nb; i ++) B.Add (C,theShapes.Value(i));