8e791ca8408916c8406223c4bd6531d58bc90261
[occt.git] / src / IGESToBRep / IGESToBRep_Actor.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <BRepLib.hxx>
16 #include <IGESData_GlobalSection.hxx>
17 #include <IGESData_IGESEntity.hxx>
18 #include <IGESData_IGESModel.hxx>
19 #include <IGESToBRep.hxx>
20 #include <IGESToBRep_Actor.hxx>
21 #include <IGESToBRep_CurveAndSurface.hxx>
22 #include <Interface_InterfaceModel.hxx>
23 #include <Interface_Macros.hxx>
24 #include <Interface_Static.hxx>
25 #include <Message_ProgressSentry.hxx>
26 #include <ShapeExtend_Explorer.hxx>
27 #include <ShapeFix_ShapeTolerance.hxx>
28 #include <Standard_ErrorHandler.hxx>
29 #include <Standard_Failure.hxx>
30 #include <Standard_Transient.hxx>
31 #include <Standard_Type.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <Transfer_Binder.hxx>
34 #include <Transfer_TransientProcess.hxx>
35 #include <TransferBRep.hxx>
36 #include <TransferBRep_ShapeBinder.hxx>
37 #include <XSAlgo.hxx>
38 #include <XSAlgo_AlgoContainer.hxx>
39
40 IMPLEMENT_STANDARD_RTTIEXT(IGESToBRep_Actor,Transfer_ActorOfTransientProcess)
41
42 //=======================================================================
43 //function : IGESToBRep_Actor
44 //purpose  : 
45 //=======================================================================
46 IGESToBRep_Actor::IGESToBRep_Actor ()
47 {  thecontinuity = 0;  theeps = 0.0001;  }
48
49
50 //=======================================================================
51 //function : SetModel
52 //purpose  : 
53 //=======================================================================
54 void IGESToBRep_Actor::SetModel (const Handle(Interface_InterfaceModel)& model)
55 {
56   themodel = model;
57   theeps = Handle(IGESData_IGESModel)::DownCast(themodel)->GlobalSection().Resolution();
58 }
59
60
61 //=======================================================================
62 //function : SetContinuity
63 //purpose  : 
64 //=======================================================================
65 void  IGESToBRep_Actor::SetContinuity (const Standard_Integer continuity)
66 {
67   thecontinuity = continuity;
68 }
69
70
71 //=======================================================================
72 //function : GetContinuity
73 //purpose  : 
74 //=======================================================================
75 Standard_Integer  IGESToBRep_Actor::GetContinuity () const
76 {
77   return thecontinuity;
78 }
79
80 //=======================================================================
81 //function : Recognize
82 //purpose  : 
83 //=======================================================================
84 Standard_Boolean  IGESToBRep_Actor::Recognize
85 (const Handle(Standard_Transient)& start)
86 {
87   DeclareAndCast(IGESData_IGESModel,mymodel,themodel);
88   DeclareAndCast(IGESData_IGESEntity,ent,start);
89   if (ent.IsNull()) return Standard_False;
90
91 //   Cas reconnus
92   Standard_Integer typnum = ent->TypeNumber();
93   Standard_Integer fornum = ent->FormNumber();
94   if (IGESToBRep::IsCurveAndSurface(ent) ||
95       ((typnum == 402 && (fornum == 1 || fornum == 7 || 
96                          fornum == 14 || fornum == 15))  ||
97        (typnum == 408 ) || (typnum == 308)))
98     return Standard_True;
99
100 //  Cas restants : non reconnus
101   return Standard_False;
102 }
103 //=======================================================================
104 //function : EncodeRegul
105 //purpose  : INTERNAL to encode regularity on edges
106 //=======================================================================
107
108 static Standard_Boolean  EncodeRegul (const TopoDS_Shape& sh)
109 {
110   Standard_Real tolang = Interface_Static::RVal("read.encoderegularity.angle");
111   if (sh.IsNull()) return Standard_True;
112   if (tolang <= 0) return Standard_True;
113   try {
114     OCC_CATCH_SIGNALS
115     BRepLib::EncodeRegularity (sh,tolang);
116   }
117   catch(Standard_Failure) {
118     return Standard_False;
119   }
120   return Standard_True;
121 }
122 //=======================================================================
123 //function : TrimTolerances
124 //purpose  : Trims tolerances of the shape according to static parameters
125 //          
126 //=======================================================================
127
128 static void TrimTolerances (const TopoDS_Shape& shape,
129                             const Standard_Real tol)
130 {
131   if( Interface_Static::IVal("read.maxprecision.mode")==1) {
132     ShapeFix_ShapeTolerance SFST;
133     SFST.LimitTolerance (shape, 0, Max(tol,Interface_Static::RVal ("read.maxprecision.val")));
134   }
135 }
136
137
138
139 //=======================================================================
140 //function : Transfer
141 //purpose  : 
142 //=======================================================================
143 Handle(Transfer_Binder) IGESToBRep_Actor::Transfer
144 (const Handle(Standard_Transient)& start, const Handle(Transfer_TransientProcess)& TP)
145 {
146   DeclareAndCast(IGESData_IGESModel,mymodel,themodel);
147   DeclareAndCast(IGESData_IGESEntity,ent,start);
148   if (mymodel.IsNull() || ent.IsNull()) return NullResult();
149   Standard_Integer anum = mymodel->Number(start);
150   
151   if (Interface_Static::IVal("read.iges.faulty.entities") == 0 && mymodel->IsErrorEntity(anum)) 
152     return NullResult();
153   TopoDS_Shape shape;
154
155 //   appeler le transfert seulement si type OK
156   Standard_Integer typnum = ent->TypeNumber();
157   Standard_Integer fornum = ent->FormNumber();
158   Standard_Real eps;
159   if (IGESToBRep::IsCurveAndSurface(ent) ||
160       (typnum == 402 && (fornum == 1 || fornum == 7|| 
161                          fornum == 14 || fornum == 15)) || 
162       (typnum == 408) || (typnum == 308)) {
163
164     // Start progress scope (no need to check if progress exists -- it is safe)
165     Message_ProgressSentry aPSentry(TP->GetProgress(), "Transfer stage", 0, 2, 1);
166
167     XSAlgo::AlgoContainer()->PrepareForTransfer();
168     IGESToBRep_CurveAndSurface CAS;
169     CAS.SetModel(mymodel);
170     CAS.SetContinuity(thecontinuity);
171     CAS.SetTransferProcess(TP);
172     Standard_Integer Ival = Interface_Static::IVal("read.precision.mode");
173     if ( Ival == 0)
174       eps = mymodel->GlobalSection().Resolution();
175     else
176       eps = Interface_Static::RVal("read.precision.val"); //:10 ABV 11 Nov 97
177 //:10      eps = BRepAPI::Precision(); 
178     Ival = Interface_Static::IVal("read.iges.bspline.approxd1.mode");
179     CAS.SetModeApprox ( (Ival > 0) );
180     Ival = Interface_Static::IVal("read.surfacecurve.mode");
181     CAS.SetSurfaceCurve (Ival);
182
183     if (eps > 1.E-08) {
184       CAS.SetEpsGeom(eps);
185       theeps = eps*CAS.GetUnitFactor();
186 //      Interface_Static::SetRVal("lastpreci",theeps);
187     }
188     Standard_Integer nbTPitems = TP->NbMapped();
189     {
190       try {
191         OCC_CATCH_SIGNALS
192         shape = CAS.TransferGeometry(ent);
193       }
194       catch(Standard_Failure) {
195         shape.Nullify();
196       }
197     }
198
199     // Switch to fix stage.
200     aPSentry.Next();
201     
202     // fixing shape
203     Handle(Standard_Transient) info;
204     shape = XSAlgo::AlgoContainer()->ProcessShape( shape, theeps, CAS.GetMaxTol(), 
205                                                    "read.iges.resource.name", 
206                                                    "read.iges.sequence", info,
207                                                    TP->GetProgress() );
208     XSAlgo::AlgoContainer()->MergeTransferInfo(TP, info, nbTPitems);
209   }
210
211   ShapeExtend_Explorer SBE;
212   if (SBE.ShapeType(shape,Standard_True) != TopAbs_SHAPE) {
213     if (!shape.IsNull()) {
214       EncodeRegul (shape);
215       //#74 rln 03.03.99 S4135
216       TrimTolerances (shape, UsedTolerance());
217    //   Shapes().Append(shape);
218     }
219   }
220   Handle(TransferBRep_ShapeBinder) binder;
221   if (!shape.IsNull()) binder = new TransferBRep_ShapeBinder(shape);
222   return binder;
223 }
224
225
226     Standard_Real  IGESToBRep_Actor::UsedTolerance () const
227       {  return theeps;  }