0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / RWStepShape / RWStepShape_RWEdgeCurve.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 <Interface_Check.hxx>
16 #include <Interface_EntityIterator.hxx>
17 #include <Interface_ShareTool.hxx>
18 #include <Precision.hxx>
19 #include <RWStepShape_RWEdgeCurve.hxx>
20 #include <StepData_StepReaderData.hxx>
21 #include <StepData_StepWriter.hxx>
22 #include <StepGeom_CartesianPoint.hxx>
23 #include <StepGeom_Curve.hxx>
24 #include <StepShape_EdgeCurve.hxx>
25 #include <StepShape_EdgeLoop.hxx>
26 #include <StepShape_FaceBound.hxx>
27 #include <StepShape_OrientedEdge.hxx>
28 #include <StepShape_Vertex.hxx>
29 #include <StepShape_VertexPoint.hxx>
30
31 RWStepShape_RWEdgeCurve::RWStepShape_RWEdgeCurve () {}
32
33 void RWStepShape_RWEdgeCurve::ReadStep
34         (const Handle(StepData_StepReaderData)& data,
35          const Standard_Integer num,
36          Handle(Interface_Check)& ach,
37          const Handle(StepShape_EdgeCurve)& ent) const
38 {
39
40
41         // --- Number of Parameter Control ---
42
43         if (!data->CheckNbParams(num,5,ach,"edge_curve")) return;
44
45         // --- inherited field : name ---
46
47         Handle(TCollection_HAsciiString) aName;
48         data->ReadString (num,1,"name",ach,aName);
49
50         // --- inherited field : edgeStart ---
51
52         Handle(StepShape_Vertex) aEdgeStart;
53         data->ReadEntity(num, 2,"edge_start", ach, STANDARD_TYPE(StepShape_Vertex), aEdgeStart);
54
55         // --- inherited field : edgeEnd ---
56
57         Handle(StepShape_Vertex) aEdgeEnd;
58         data->ReadEntity(num, 3,"edge_end", ach, STANDARD_TYPE(StepShape_Vertex), aEdgeEnd);
59
60         // --- own field : edgeGeometry ---
61
62         Handle(StepGeom_Curve) aEdgeGeometry;
63         data->ReadEntity(num, 4,"edge_geometry", ach, STANDARD_TYPE(StepGeom_Curve), aEdgeGeometry);
64
65         // --- own field : sameSense ---
66
67         Standard_Boolean aSameSense;
68         data->ReadBoolean (num,5,"same_sense",ach,aSameSense);
69
70         //--- Initialisation of the read entity ---
71
72
73         ent->Init(aName, aEdgeStart, aEdgeEnd, aEdgeGeometry, aSameSense);
74 }
75
76
77 void RWStepShape_RWEdgeCurve::WriteStep
78         (StepData_StepWriter& SW,
79          const Handle(StepShape_EdgeCurve)& ent) const
80 {
81
82         // --- inherited field name ---
83
84         SW.Send(ent->Name());
85
86         // --- inherited field edgeStart ---
87
88         SW.Send(ent->EdgeStart());
89
90         // --- inherited field edgeEnd ---
91
92         SW.Send(ent->EdgeEnd());
93
94         // --- own field : edgeGeometry ---
95
96         SW.Send(ent->EdgeGeometry());
97
98         // --- own field : sameSense ---
99
100         SW.SendBoolean(ent->SameSense());
101 }
102
103
104 void RWStepShape_RWEdgeCurve::Share(const Handle(StepShape_EdgeCurve)& ent, Interface_EntityIterator& iter) const
105 {
106
107         iter.GetOneItem(ent->EdgeStart());
108
109
110         iter.GetOneItem(ent->EdgeEnd());
111
112
113         iter.GetOneItem(ent->EdgeGeometry());
114 }
115
116
117
118 void RWStepShape_RWEdgeCurve::Check
119   (const Handle(StepShape_EdgeCurve)& ent,
120    const Interface_ShareTool& aShto,
121    Handle(Interface_Check)& ach) const
122 {
123 //  std::cout << "------ calling CheckEdgeCurve ------" << std::endl;
124   
125   Handle(StepShape_OrientedEdge) theOE1, theOE2;
126   Handle(StepShape_FaceBound)    theFOB1, theFOB2;
127   //Handle(StepShape_FaceSurface)  theFS1, theFS2;
128
129   Standard_Boolean theOEOri1 = Standard_True;
130   Standard_Boolean theOEOri2 = Standard_True;
131   Standard_Boolean theFBOri1 = Standard_True;
132   Standard_Boolean theFBOri2 = Standard_True;
133   //Standard_Boolean theFSOri1 = Standard_True;
134   //Standard_Boolean theFSOri2 = Standard_True;
135   Standard_Boolean Cumulated1, Cumulated2;
136
137   // 1- First Vertex != LastVertex but First VertexPoint == Last VertexPoint
138   // Remark : time comsuming process but useful !
139   // If this append, we can drop one of the two vertices and replace it 
140   // everywhere it is referenced. Side effect : tolerance problem !!!
141
142   Handle(StepShape_VertexPoint) StartVertex = 
143     Handle(StepShape_VertexPoint)::DownCast(ent->EdgeStart());
144   Handle(StepShape_VertexPoint) EndVertex   = 
145     Handle(StepShape_VertexPoint)::DownCast(ent->EdgeEnd());
146
147   if (StartVertex != EndVertex) {
148
149     Handle(StepGeom_CartesianPoint) StartPoint = 
150       Handle(StepGeom_CartesianPoint)::DownCast(StartVertex->VertexGeometry());
151     Handle(StepGeom_CartesianPoint) EndPoint   = 
152       Handle(StepGeom_CartesianPoint)::DownCast(EndVertex->VertexGeometry());
153
154     // it can also be a degenerated pcurve
155
156     if (!StartPoint.IsNull() && !EndPoint.IsNull()) {
157       Standard_Real Dist = Sqrt
158         ((StartPoint->CoordinatesValue(1) - EndPoint->CoordinatesValue(1)) *
159          (StartPoint->CoordinatesValue(1) - EndPoint->CoordinatesValue(1)) +
160          (StartPoint->CoordinatesValue(2) - EndPoint->CoordinatesValue(2)) *
161          (StartPoint->CoordinatesValue(2) - EndPoint->CoordinatesValue(2)) +
162          (StartPoint->CoordinatesValue(3) - EndPoint->CoordinatesValue(3)) *
163          (StartPoint->CoordinatesValue(3) - EndPoint->CoordinatesValue(3)));
164       if (Dist < Precision::Confusion() ) {
165         ach->AddWarning("Two instances of Vertex have equal (within uncertainty) coordinates");
166       }
167     }
168   }
169   
170   // 2- Two-Manifold Topology
171
172   Standard_Boolean sharEC = aShto.IsShared(ent);
173   Standard_Integer nbRef;
174   if(!sharEC){
175     ach->AddFail("ERROR: EdgeCurve not referenced");
176   }
177   else {
178     Interface_EntityIterator myShRef = aShto.Sharings(ent);
179     myShRef.SelectType (STANDARD_TYPE(StepShape_OrientedEdge),Standard_True);
180     nbRef = myShRef.NbEntities();
181     if (nbRef ==2) {
182       theOE1 = Handle(StepShape_OrientedEdge)::DownCast(myShRef.Value());
183       theOEOri1 = theOE1->Orientation();
184       myShRef.Next();
185       theOE2 = Handle(StepShape_OrientedEdge)::DownCast(myShRef.Value());
186       theOEOri2 = theOE2->Orientation();
187       
188       // get the FaceBound orientation for theOE1
189       
190       Standard_Boolean sharOE1 = aShto.IsShared(theOE1);
191       if(!sharOE1){
192 #ifdef OCCT_DEBUG
193         std::cout << "OrientedEdge1 not shared" <<std::endl;
194 #endif
195       }
196       else {
197         myShRef = aShto.Sharings(theOE1);
198         myShRef.SelectType (STANDARD_TYPE(StepShape_EdgeLoop),Standard_True);
199         nbRef = myShRef.NbEntities();
200         if (nbRef == 1) {
201           myShRef.Start();
202           Handle(StepShape_EdgeLoop) theEL1 =
203             Handle(StepShape_EdgeLoop)::DownCast(myShRef.Value());
204           Standard_Boolean sharEL1 = aShto.IsShared(theEL1);
205           if(!sharEL1) {
206 #ifdef OCCT_DEBUG
207             std::cout << "EdgeLoop1 not shared" <<std::endl;
208 #endif
209           }
210           else {
211             myShRef = aShto.Sharings(theEL1);
212             myShRef.SelectType (STANDARD_TYPE(StepShape_FaceBound),Standard_True);
213 #ifdef OCCT_DEBUG
214             nbRef = 
215 #endif
216               myShRef.NbEntities();
217             myShRef.Start();
218             theFOB1 = Handle(StepShape_FaceBound)::DownCast(myShRef.Value());
219             if (!theFOB1.IsNull()) {
220               theFBOri1 = theFOB1->Orientation();
221             }
222             else {
223 #ifdef OCCT_DEBUG
224               std::cout << "EdgeLoop not referenced by FaceBound" << std::endl;
225 #endif
226             }
227           }
228         }
229         else {
230           if (nbRef == 0) {
231 #ifdef OCCT_DEBUG
232             std::cout << "OrientedEdge not referenced" << std::endl;
233 #endif
234           }
235           else {
236             if (aShto.NbTypedSharings(theOE1,
237                                       STANDARD_TYPE(StepShape_EdgeLoop)) > 1) {
238 #ifdef OCCT_DEBUG
239               std::cout << "OrientedEdge referenced more than once" << std::endl;
240 #endif
241             }
242           }
243         }
244       }
245
246       // get the FaceBound orientation for theOE2
247
248       Standard_Boolean sharOE2 = aShto.IsShared(theOE2);
249       if(!sharOE2){
250 #ifdef OCCT_DEBUG
251         std::cout << "OrientedEdge2 not shared" <<std::endl;
252 #endif
253       }
254       else {
255         myShRef = aShto.Sharings(theOE2);
256 #ifdef OCCT_DEBUG
257 //      Standard_Integer nbRef = 
258 #endif
259 // unused         myShRef.NbEntities(); 
260         myShRef.Start();
261         Handle(StepShape_EdgeLoop) theEL2 =
262           Handle(StepShape_EdgeLoop)::DownCast(myShRef.Value());
263         Standard_Boolean sharEL2 = aShto.IsShared(theEL2);
264         if(!sharEL2){
265 #ifdef OCCT_DEBUG
266           std::cout << "EdgeLoop2 not shared" <<std::endl;
267 #endif
268         }
269         else {
270           myShRef = aShto.Sharings(theEL2);
271           // unused Standard_Integer nbRef = myShRef.NbEntities();      
272           myShRef.Start();
273           theFOB2 = Handle(StepShape_FaceBound)::DownCast(myShRef.Value());
274           if (!theFOB2.IsNull()) {
275             theFBOri2 = theFOB2->Orientation();
276           }
277           else {
278 #ifdef OCCT_DEBUG
279             std::cout << "EdgeLoop not referenced by FaceBound" << std::endl;
280 #endif
281           }
282         }
283       }
284         
285         // "cumulate" the FaceBound and the OrientedEdge orientation
286         
287         Cumulated1 = theFBOri1 ^ theOEOri1;
288       Cumulated2 = theFBOri2 ^ theOEOri2;
289       
290       // the orientation of the OrientedEdges must be opposite
291       
292       if (Cumulated1 == Cumulated2) {
293         ach->AddFail("ERROR: non 2-manifold topology");
294       }
295     }
296   }
297 }
298