0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / IGESControl / IGESControl_IGESBoundary.cxx
1 // Created on: 2000-02-05
2 // Created by: data exchange team
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 //06.01.99 pdn renaming ShapeFix_Wire::FixLittle to ShapeFix_Wire::FixSmall
17 //:n1 abv 20.01.99: BUC60328.rle (loopback): call FixSmall with Max(1.,preci)
18 //smh #1  11.03.99 PRO15640/15641, szv#2 PRO17393: call FixSameParameter after BuildCurve3d
19 //#74 rln 10.03.99 S4135: handling use of BRepLib::SameParameter by new static parameter
20 // pdn 18.03.99 S4135: using mintol instead of resolution from IGES file
21 // pdn 18.03.99 S4135: projecting with resolution from IGES file.
22 //%14 pdn 06.03.99 implementing fixFace in IGES
23 // S4135 pdn improvements in using of stdsameparameter flag
24 //#21 gka 12.04.99 CSR UKI60998 entities number - 13, 24 lost of boundaries during translation of tabulated cylinders
25 //S4181 pdn 15.04.99 implementing of reading IGES elementary surfaces. (instead of shift, reverce and 
26 //scale factors the transformation matrix and scale factor used)
27
28 #include <IGESControl_IGESBoundary.ixx>
29 #include <TColStd_HSequenceOfTransient.hxx>
30 #include <Geom2d_Curve.hxx>
31 #include <Geom_Curve.hxx>
32 #include <Geom_Plane.hxx>
33 #include <GeomAdaptor_Curve.hxx>
34 #include <Precision.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Wire.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRep_Tool.hxx>
39 #include <ShapeBuild_Edge.hxx>
40 #include <ShapeAnalysis_Wire.hxx>
41 #include <ShapeAnalysis_ShapeTolerance.hxx>
42 #include <ShapeFix_Edge.hxx>
43 #include <ShapeFix_Wire.hxx>
44 #include <ShapeFix_ShapeTolerance.hxx>
45 #include <IGESToBRep.hxx>
46 #include <IGESToBRep_TopoCurve.hxx>
47 #include <Message_Msg.hxx>
48 #include <Interface_Static.hxx>
49 #include <ShapeAlgo.hxx>
50 #include <ShapeAlgo_AlgoContainer.hxx>
51 #include <ShapeAlgo_ToolContainer.hxx>
52 #include <IGESGeom_BSplineCurve.hxx>
53 #include <IGESData_IGESEntity.hxx>
54
55
56 //=======================================================================
57 //function : IGESControl_IGESBoundary
58 //purpose  : 
59 //=======================================================================
60
61 IGESControl_IGESBoundary::IGESControl_IGESBoundary() : IGESToBRep_IGESBoundary()
62 {
63 }
64
65 //=======================================================================
66 //function : IGESControl_IGESBoundary
67 //purpose  : 
68 //=======================================================================
69
70 IGESControl_IGESBoundary::IGESControl_IGESBoundary(const IGESToBRep_CurveAndSurface& CS) :
71        IGESToBRep_IGESBoundary (CS)
72 {
73 }
74
75 //=======================================================================
76 //function : Check
77 //purpose  : 
78 //=======================================================================
79
80  void IGESControl_IGESBoundary::Check(const Standard_Boolean result,const Standard_Boolean checkclosure,
81                                       const Standard_Boolean aokCurve3d, const Standard_Boolean aokCurve2d) 
82 {
83   Standard_Boolean Result = result;
84   Standard_Boolean okCurve3d = aokCurve3d, okCurve2d = aokCurve2d; 
85   Standard_Real maxtol = myCS.GetMaxTol();
86   
87   if (Result && checkclosure) {
88     //USA60022 7277 check of closure
89     Handle(ShapeAnalysis_Wire) saw = new ShapeAnalysis_Wire;
90     saw->Load (mysewd);
91     saw->SetPrecision (maxtol);
92     saw->CheckConnected (1);
93     if (saw->LastCheckStatus (ShapeExtend_FAIL)) {
94       saw->Load (mysewd3d);
95       saw->CheckConnected (1);
96       if (saw->LastCheckStatus (ShapeExtend_FAIL)) okCurve3d = Standard_False;
97       else                                         okCurve2d = Standard_False;
98       Result = Standard_False;
99     }
100   }
101   if (!Result) {
102     mysewd->Clear();
103     if (okCurve3d && mysewd3d->NbEdges() > 0) {
104       Message_Msg Msg1070("IGES_1070");//"Representations in the file are inconsistent. Recomputation from 3d"
105       Msg1070.Arg(3);
106       myCS.SendWarning(myentity,Msg1070);
107       mysewd = mysewd3d;
108     }
109     else if (okCurve2d && mysewd2d->NbEdges() > 0) {
110       Message_Msg Msg1070("IGES_1070");//"Representations in the file are inconsistent. Recomputation from 2d"
111       Msg1070.Arg(2);
112       myCS.SendWarning(myentity,Msg1070);
113       mysewd = mysewd2d;
114     }
115   }
116 }
117
118 //=======================================================================
119 //function : Connect
120 //purpose  : Connects theNextWD to theWD using theSAW. 
121 //           First, connects edges of theNextWD by calling ShapeFix_Wire::FixConnected(). This
122 //           is necessary when theNextWD was built using multiple curves from the Composite
123 //           Curve (as ShapeExtend_WireData::Wire() would otherwise produce a wrong 
124 //           disconnected TopoDS_Wire).
125 //           FixConnected() will only update the edges resulting from different composite
126 //           curve segments. Edges resulting from splitting C0 curve will already be
127 //           connected.
128 //=======================================================================
129 static Standard_Boolean Connect (const Handle(ShapeAnalysis_Wire)& theSAW,
130                                  const Handle(ShapeExtend_WireData)& theWD,
131                                  const Handle(ShapeExtend_WireData)& theNextWD,
132                                  const Standard_Boolean theConnectNextWD,
133                                  const Standard_Real theMaxTol,
134                                  Standard_Real& theDistMin,
135                                  Standard_Boolean& theReverseWD,
136                                  Standard_Boolean& theReverseNextWD)
137 {
138     theSAW->Load (theWD);
139     if (theConnectNextWD) {
140       Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
141       sfw->Load (theNextWD);
142       sfw->ClosedWireMode() = Standard_False; //internal connections are enough
143       sfw->FixConnected();
144     }
145     return ShapeAlgo::AlgoContainer()->ConnectNextWire (theSAW, theNextWD,
146       theMaxTol, theDistMin, theReverseWD, theReverseNextWD);
147 }
148
149 //=======================================================================
150 //function : Transfer
151 //purpose  : 
152 //=======================================================================
153
154  Standard_Boolean IGESControl_IGESBoundary::Transfer(Standard_Boolean& okCurve,
155                                                      Standard_Boolean& okCurve3d,
156                                                      Standard_Boolean& okCurve2d,
157                                                      const Handle(IGESData_IGESEntity)& icurve3d,
158                                                      const Handle(ShapeExtend_WireData)& scurve3d,
159                                                      const Standard_Boolean usescurve,
160                                                      const Standard_Boolean toreverse3d,
161                                                      const Handle(IGESData_HArray1OfIGESEntity)& curves2d,
162                                                      const Standard_Boolean toreverse2d,
163                                                      const Standard_Integer number,
164                                                      Handle(ShapeExtend_WireData)& Gsewd) 
165 {
166   Gsewd                                = new ShapeExtend_WireData;//local translation (for mysewd)
167   Handle(ShapeExtend_WireData) Gsewd3d = new ShapeExtend_WireData;//local translation (for mysewd3d)
168   Handle(ShapeExtend_WireData) Gsewd2d = new ShapeExtend_WireData;//local translation (for mysewd2d)
169
170   Standard_Boolean revsewd, revnextsewd;
171   Standard_Real distmin, precision = myCS.GetEpsGeom() * myCS.GetUnitFactor(), maxtol = myCS.GetMaxTol();
172   
173   Handle(ShapeAnalysis_Wire) saw   = new ShapeAnalysis_Wire,
174                              saw3d = new ShapeAnalysis_Wire,
175                              saw2d = new ShapeAnalysis_Wire;
176   saw->Load   (Gsewd);   saw->SetPrecision   (precision);
177   saw3d->Load (Gsewd3d); saw3d->SetPrecision (precision);
178   saw2d->Load (Gsewd2d); saw2d->SetPrecision (precision);
179   
180   Standard_Boolean GTranslate3d = Standard_True, GTranslate2d = Standard_True,
181                    Preferred3d  = Standard_True, Preferred2d  = Standard_True;
182
183   Standard_Integer len3d = 0, len2d = 0;
184   Handle(TColStd_HSequenceOfTransient) seq3d, seq2d;
185   if (usescurve)
186     len3d = scurve3d->NbEdges();
187   else {
188     IGESToBRep::IGESCurveToSequenceOfIGESCurve (icurve3d, seq3d);
189       len3d = seq3d->Length();
190   }
191   if (!curves2d.IsNull()) {
192     for (Standard_Integer i = 1; i <= curves2d->Length(); i++)
193       IGESToBRep::IGESCurveToSequenceOfIGESCurve (curves2d->Value (i), seq2d);
194     len2d = seq2d->Length();
195   }
196
197   Standard_Integer surfcurv = myCS.GetSurfaceCurve();
198   if ((surfcurv == -2 && len2d > 0) || len3d == 0)
199     GTranslate3d = Standard_False;
200   else if (( surfcurv == -3 && len3d > 0) || len2d == 0)
201     GTranslate2d = Standard_False;
202  
203   if (GTranslate3d && GTranslate2d) {
204     //Setting preference in the case of inconsitency between 3D and 2D
205     if      (surfcurv == 2)         Preferred3d = Standard_False;
206     else if (surfcurv == 3)         Preferred2d = Standard_False;
207     else if (myfilepreference == 2) Preferred3d = Standard_False;
208     else if (myfilepreference == 3) Preferred2d = Standard_False;
209     else                            Preferred3d = Standard_False;
210   }
211   if (GTranslate3d && GTranslate2d && len3d != len2d) {
212     GTranslate3d = Preferred3d;
213     GTranslate2d = Preferred2d;
214   }
215     
216   IGESToBRep_TopoCurve TC (myCS);
217   
218   if (GTranslate3d && !GTranslate2d) {
219     if (usescurve) {
220       Gsewd->Add(scurve3d->Wire());
221     }
222     else {
223       TopoDS_Shape Sh = TC.TransferTopoCurve (icurve3d);
224       if (!Sh.IsNull()) {
225         Gsewd3d->Add (Sh);
226         if (toreverse3d) {
227           ReverseCurves3d (Gsewd3d);
228           Gsewd->Add (Gsewd3d->Wire());
229         }
230         else Gsewd->Add (Sh);//Gsewd = Gsewd3d is impossible to avoid sharing of sewd (UK1.igs entity 7)
231       }
232     }
233   }
234   else if (!GTranslate3d && GTranslate2d) {
235     for (Standard_Integer i = curves2d->Lower(); i <= curves2d->Upper(); i++) {
236       TopoDS_Shape Sh = TC.Transfer2dTopoCurve (Handle(IGESData_IGESEntity)::DownCast (curves2d->Value (i)),
237                                                 myface, mytrsf, myuFact);
238       if (!Sh.IsNull()) Gsewd2d->Add (Sh);
239     }
240     if (toreverse2d) {
241       ReverseCurves2d (Gsewd2d, myface);
242     }
243     Gsewd->Add (Gsewd2d->Wire());
244   }
245   else if( GTranslate3d && GTranslate2d ) {
246     //Translate both curves 3D and 2D
247     //Suppose that i-th segment in 2D curve corresponds to i-th segment in 3D curve
248     for (Standard_Integer i = 1; i <= len3d; i++) {
249       Standard_Boolean LTranslate3d = Standard_True, LTranslate2d = Standard_True;
250       
251       Handle(ShapeExtend_WireData) Lsewd3d = new ShapeExtend_WireData;
252       TC.SetBadCase (Standard_False); //:27
253       if (usescurve)
254         Lsewd3d->Add (scurve3d->Edge (i));
255       else {
256         TopoDS_Shape shape3d = TC.TransferTopoCurve (Handle(IGESData_IGESEntity)::DownCast (seq3d->Value (i)));
257         if (!shape3d.IsNull()) {
258           Lsewd3d->Add (shape3d);
259           if (toreverse3d) {
260             ReverseCurves3d (Lsewd3d);
261           }
262         }
263         else LTranslate3d = Standard_False;
264       }
265       Standard_Boolean bad3d = TC.BadCase(); //:27
266       okCurve3d = okCurve3d && ShapeAlgo::AlgoContainer()->ConnectNextWire (saw3d, Lsewd3d, maxtol, distmin, revsewd, revnextsewd);
267       
268       Handle(ShapeExtend_WireData) Lsewd2d = new ShapeExtend_WireData;
269       TC.SetBadCase (Standard_False); //:27
270       TopoDS_Shape shape2d = TC.Transfer2dTopoCurve (Handle(IGESData_IGESEntity)::DownCast (seq2d->Value (i)),
271                                                      myface, mytrsf, myuFact);
272       Standard_Boolean bad2d = TC.BadCase(); //:27
273       if (!shape2d.IsNull()) {
274         Lsewd2d->Add  (shape2d);
275         if (toreverse2d) {
276           ReverseCurves2d (Lsewd2d, myface);
277         }
278         okCurve2d = okCurve2d && ShapeAlgo::AlgoContainer()->ConnectNextWire (saw2d, Lsewd2d, maxtol, distmin, revsewd, revnextsewd);
279       }
280       else LTranslate2d = Standard_False;
281       
282 //     if (LTranslate3d && LTranslate2d && (Lsewd3d->NbEdges() != Lsewd2d->NbEdges() || bad3d || bad2d)) {
283       Standard_Boolean isBSpline = Standard_False;
284       if(!usescurve && !seq3d->Value (i).IsNull() && !seq2d->Value (i).IsNull())
285         isBSpline = seq3d->Value (i)->IsKind(STANDARD_TYPE(IGESGeom_BSplineCurve)) &&
286           seq2d->Value (i)->IsKind(STANDARD_TYPE(IGESGeom_BSplineCurve));
287       
288       if (LTranslate3d && LTranslate2d && 
289           (
290            (isBSpline && (Lsewd3d->NbEdges() != Lsewd2d->NbEdges())) ||
291            (!isBSpline && (Lsewd3d->NbEdges() != 1 || Lsewd2d->NbEdges() != 1)) ||
292            bad3d || bad2d
293            )) {
294         
295         LTranslate3d = Preferred3d;
296         LTranslate2d = Preferred2d;
297       }
298       Handle(ShapeExtend_WireData) Lsewd;//Lsewd3d or Lsewd2d or Lsewd3d+pcurve
299       if      ( LTranslate3d && !LTranslate2d) Lsewd = Lsewd3d;
300       else if (!LTranslate3d &&  LTranslate2d) Lsewd = Lsewd2d;
301       else {
302         Lsewd = Lsewd3d;
303         //copying pcurve to edge with 3D curve
304         Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
305         for (Standard_Integer iedge = 1; iedge <= Lsewd3d->NbEdges(); iedge++) {
306           TopoDS_Edge edge3d = Lsewd3d->Edge (iedge), edge2d = Lsewd2d->Edge (iedge);
307           if (!IGESToBRep::TransferPCurve (edge2d, edge3d, myface)) continue;
308           if (sfe->FixReversed2d (edge3d, myface)) {
309 #ifdef OCCT_DEBUG
310             cout << "Warning: IGESToBRep_IGESBoundary: 2D curve of edge was reversed" << endl;
311 #endif
312           }
313           //#74 rln 10.03.99 S4135: handling use of BRepLib::SameParameter by new static parameter
314           if (Interface_Static::IVal ("read.stdsameparameter.mode")) {
315             Standard_Real first, last;
316             BRep_Tool::Range(edge3d,first,last);
317             // pdn 08.04.99 S4135 optimizing in computation of SPTol
318             //choosing tolerance according to Approx_SameParameter: 50 * 22
319             Standard_Real SPTol = Min (precision, Abs (last - first)/1000);
320             BRep_Builder B;
321             B.SameParameter (edge3d, Standard_False);
322             sfe->FixSameParameter (edge3d, SPTol);
323           }
324           else
325             sfe->FixSameParameter (edge3d);
326           Standard_Real maxdev = BRep_Tool::Tolerance (edge3d);
327           //pdn 08.04.99 S4135 recomputing only if deviation is greater than maxtol
328           if (maxdev > maxtol) { //:e2
329 #ifdef OCCT_DEBUG
330             cout << "Warning: IGESToBRep_IGESBoundary: Deviation = " << maxdev << endl;
331 #endif
332             ShapeFix_ShapeTolerance().SetTolerance (edge3d, Precision::Confusion());
333             for (Standard_Integer ie = 1; ie <= iedge; ie++)
334               ShapeBuild_Edge().RemovePCurve (Lsewd3d->Edge (ie), myface);
335             if (Preferred3d) {
336 #ifdef OCCT_DEBUG
337               cout << "Warning: IGESToBRep_IGESBoundary: 3D and 2D curves are inconsistent; 2D is ignored" << endl;
338 #endif
339             }
340             else {
341 #ifdef OCCT_DEBUG
342               cout << "Warning: IGESToBRep_IGESBoundary: 3D and 2D curves are inconsistent; 3D is ignored" << endl;
343 #endif
344               Lsewd = Lsewd2d;
345             }
346             break;
347           }
348         }
349       }
350       okCurve = okCurve && ShapeAlgo::AlgoContainer()->ConnectNextWire (saw, Lsewd, maxtol, distmin, revsewd, revnextsewd);
351       if (!okCurve) {
352 #ifdef OCCT_DEBUG
353         cout << "Warning: IGESToBRep_IGESBoundary: Curves " << i - 1 << " and " << i << " cannot be connected" << endl;
354 #endif
355         Gsewd3d = new ShapeExtend_WireData;
356         for (Standard_Integer i = 1; i <= len3d; i++) {
357           if (usescurve)
358             Gsewd3d->Add (scurve3d->Edge (i));
359           else {
360             TopoDS_Shape Sh =
361               TC.TransferTopoCurve (Handle(IGESData_IGESEntity)::DownCast (seq3d->Value (i)));
362             if (!Sh.IsNull()) Gsewd3d->Add (Sh);
363           }
364         }
365         if (toreverse3d) {
366           ReverseCurves3d(Gsewd3d);
367         }
368         Gsewd2d = new ShapeExtend_WireData;
369         for (Standard_Integer i = 1; i <= len2d; i++) {
370           TopoDS_Shape Sh = TC.Transfer2dTopoCurve (Handle(IGESData_IGESEntity)::DownCast (seq2d->Value (i)),
371                                                     myface, mytrsf, myuFact);
372           if (!Sh.IsNull()) Gsewd2d->Add (Sh);
373         }
374         if (toreverse2d) {
375           ReverseCurves2d (Gsewd2d, myface);
376         }
377         Handle(ShapeFix_Wire) sfw3 = new ShapeFix_Wire(Gsewd3d->Wire(),myface,precision);
378         sfw3->Perform();
379         TopoDS_Wire w3 = sfw3->Wire();
380         Handle(ShapeFix_Wire) sfw2 = new ShapeFix_Wire(Gsewd2d->Wire(),myface,precision);
381         sfw2->Perform();
382         TopoDS_Wire w2 = sfw2->Wire();
383         ShapeAnalysis_ShapeTolerance ST;
384         double tol3 = ST.Tolerance(w3,1);
385         double tol2 = ST.Tolerance(w2,1);
386         Gsewd3d = new ShapeExtend_WireData;
387         Gsewd2d = new ShapeExtend_WireData;
388         Gsewd3d->Add(w3);
389         Gsewd2d->Add(w3);
390         if(tol3<tol2)
391           Gsewd->Add(w3);
392         else
393           Gsewd->Add(w2);
394         okCurve = Standard_True;
395         okCurve2d = Standard_True;
396         okCurve3d = Standard_True;
397       }
398     }
399   }
400   
401   if (number > 1) {
402     okCurve   = okCurve && Connect (saw, mysewd, Gsewd, (len3d > 1) || (len2d > 1), maxtol,
403       distmin, revsewd, revnextsewd);
404     okCurve3d = okCurve3d && Connect (saw3d, mysewd3d, Gsewd3d, len3d > 1, maxtol,
405       distmin, revsewd, revnextsewd);
406     okCurve2d = okCurve2d && Connect (saw2d, mysewd2d, Gsewd2d, len2d > 1, maxtol,
407       distmin, revsewd, revnextsewd);
408   }
409   else {
410     mysewd   = Gsewd;
411     mysewd3d = Gsewd3d;
412     mysewd2d = Gsewd2d;
413   }
414   return okCurve;
415 }
416