0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / RWStepGeom / RWStepGeom_RWBSplineSurfaceWithKnots.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 <RWStepGeom_RWBSplineSurfaceWithKnots.hxx>
19 #include <StepData_Logical.hxx>
20 #include <StepData_StepReaderData.hxx>
21 #include <StepData_StepWriter.hxx>
22 #include <StepGeom_BSplineSurfaceForm.hxx>
23 #include <StepGeom_BSplineSurfaceWithKnots.hxx>
24 #include <StepGeom_CartesianPoint.hxx>
25 #include <StepGeom_HArray2OfCartesianPoint.hxx>
26 #include <StepGeom_KnotType.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TColStd_HArray1OfInteger.hxx>
29 #include <TColStd_HArray1OfReal.hxx>
30
31 // --- Enum : KnotType ---
32 static TCollection_AsciiString ktUniformKnots(".UNIFORM_KNOTS.");
33 static TCollection_AsciiString ktQuasiUniformKnots(".QUASI_UNIFORM_KNOTS.");
34 static TCollection_AsciiString ktPiecewiseBezierKnots(".PIECEWISE_BEZIER_KNOTS.");
35 static TCollection_AsciiString ktUnspecified(".UNSPECIFIED.");
36
37
38         // --- Enum : BSplineSurfaceForm ---
39 static TCollection_AsciiString bssfSurfOfLinearExtrusion(".SURF_OF_LINEAR_EXTRUSION.");
40 static TCollection_AsciiString bssfPlaneSurf(".PLANE_SURF.");
41 static TCollection_AsciiString bssfGeneralisedCone(".GENERALISED_CONE.");
42 static TCollection_AsciiString bssfToroidalSurf(".TOROIDAL_SURF.");
43 static TCollection_AsciiString bssfConicalSurf(".CONICAL_SURF.");
44 static TCollection_AsciiString bssfSphericalSurf(".SPHERICAL_SURF.");
45 static TCollection_AsciiString bssfUnspecified(".UNSPECIFIED.");
46 static TCollection_AsciiString bssfRuledSurf(".RULED_SURF.");
47 static TCollection_AsciiString bssfSurfOfRevolution(".SURF_OF_REVOLUTION.");
48 static TCollection_AsciiString bssfCylindricalSurf(".CYLINDRICAL_SURF.");
49 static TCollection_AsciiString bssfQuadricSurf(".QUADRIC_SURF.");
50
51 RWStepGeom_RWBSplineSurfaceWithKnots::RWStepGeom_RWBSplineSurfaceWithKnots () {}
52
53 void RWStepGeom_RWBSplineSurfaceWithKnots::ReadStep
54         (const Handle(StepData_StepReaderData)& data,
55          const Standard_Integer num,
56          Handle(Interface_Check)& ach,
57          const Handle(StepGeom_BSplineSurfaceWithKnots)& ent) const
58 {
59
60
61         // --- Number of Parameter Control ---
62
63         if (!data->CheckNbParams(num,13,ach,"b_spline_surface_with_knots")) return;
64
65         // --- inherited field : name ---
66
67         Handle(TCollection_HAsciiString) aName;
68         //szv#4:S4163:12Mar99 `Standard_Boolean stat1 =` not needed
69         data->ReadString (num,1,"name",ach,aName);
70
71         // --- inherited field : uDegree ---
72
73         Standard_Integer aUDegree;
74         //szv#4:S4163:12Mar99 `Standard_Boolean stat2 =` not needed
75         data->ReadInteger (num,2,"u_degree",ach,aUDegree);
76
77         // --- inherited field : vDegree ---
78
79         Standard_Integer aVDegree;
80         //szv#4:S4163:12Mar99 `Standard_Boolean stat3 =` not needed
81         data->ReadInteger (num,3,"v_degree",ach,aVDegree);
82
83         // --- inherited field : controlPointsList ---
84
85         Handle(StepGeom_HArray2OfCartesianPoint) aControlPointsList;
86         Handle(StepGeom_CartesianPoint) anent4;
87         Standard_Integer nsub4;
88         if (data->ReadSubList (num,4,"control_points_list",ach,nsub4)) {
89           Standard_Integer nbi4 = data->NbParams(nsub4);
90           Standard_Integer nbj4 = data->NbParams(data->ParamNumber(nsub4,1));
91           aControlPointsList = new StepGeom_HArray2OfCartesianPoint (1, nbi4, 1, nbj4);
92           for (Standard_Integer i4 = 1; i4 <= nbi4; i4 ++) {
93             Standard_Integer nsi4;
94             if (data->ReadSubList (nsub4,i4,"sub-part(control_points_list)",ach,nsi4)) {
95               for (Standard_Integer j4 =1; j4 <= nbj4; j4 ++) {
96                 //szv#4:S4163:12Mar99 `Standard_Boolean stat4 =` not needed
97                 if (data->ReadEntity (nsi4, j4,"cartesian_point", ach,
98                                       STANDARD_TYPE(StepGeom_CartesianPoint), anent4))
99                   aControlPointsList->SetValue(i4, j4, anent4);
100               }
101             }
102           }
103         }
104
105         // --- inherited field : surfaceForm ---
106
107         StepGeom_BSplineSurfaceForm aSurfaceForm = StepGeom_bssfPlaneSurf;
108         if (data->ParamType(num,5) == Interface_ParamEnum) {
109           Standard_CString text = data->ParamCValue(num,5);
110           if      (bssfSurfOfLinearExtrusion.IsEqual(text)) aSurfaceForm = StepGeom_bssfSurfOfLinearExtrusion;
111           else if (bssfPlaneSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfPlaneSurf;
112           else if (bssfGeneralisedCone.IsEqual(text)) aSurfaceForm = StepGeom_bssfGeneralisedCone;
113           else if (bssfToroidalSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfToroidalSurf;
114           else if (bssfConicalSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfConicalSurf;
115           else if (bssfSphericalSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfSphericalSurf;
116           else if (bssfUnspecified.IsEqual(text)) aSurfaceForm = StepGeom_bssfUnspecified;
117           else if (bssfRuledSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfRuledSurf;
118           else if (bssfSurfOfRevolution.IsEqual(text)) aSurfaceForm = StepGeom_bssfSurfOfRevolution;
119           else if (bssfCylindricalSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfCylindricalSurf;
120           else if (bssfQuadricSurf.IsEqual(text)) aSurfaceForm = StepGeom_bssfQuadricSurf;
121           else ach->AddFail("Enumeration b_spline_surface_form has not an allowed value");
122         }
123         else ach->AddFail("Parameter #5 (surface_form) is not an enumeration");
124
125         // --- inherited field : uClosed ---
126
127         StepData_Logical aUClosed;
128         //szv#4:S4163:12Mar99 `Standard_Boolean stat6 =` not needed
129         data->ReadLogical (num,6,"u_closed",ach,aUClosed);
130
131         // --- inherited field : vClosed ---
132
133         StepData_Logical aVClosed;
134         //szv#4:S4163:12Mar99 `Standard_Boolean stat7 =` not needed
135         data->ReadLogical (num,7,"v_closed",ach,aVClosed);
136
137         // --- inherited field : selfIntersect ---
138
139         StepData_Logical aSelfIntersect;
140         //szv#4:S4163:12Mar99 `Standard_Boolean stat8 =` not needed
141         data->ReadLogical (num,8,"self_intersect",ach,aSelfIntersect);
142
143         // --- own field : uMultiplicities ---
144
145         Handle(TColStd_HArray1OfInteger) aUMultiplicities;
146         Standard_Integer aUMultiplicitiesItem;
147         Standard_Integer nsub9;
148         if (data->ReadSubList (num,9,"u_multiplicities",ach,nsub9)) {
149           Standard_Integer nb9 = data->NbParams(nsub9);
150           aUMultiplicities = new TColStd_HArray1OfInteger (1, nb9);
151           for (Standard_Integer i9 = 1; i9 <= nb9; i9 ++) {
152             //szv#4:S4163:12Mar99 `Standard_Boolean stat9 =` not needed
153             if (data->ReadInteger (nsub9,i9,"u_multiplicities",ach,aUMultiplicitiesItem))
154               aUMultiplicities->SetValue(i9,aUMultiplicitiesItem);
155           }
156         }
157
158         // --- own field : vMultiplicities ---
159
160         Handle(TColStd_HArray1OfInteger) aVMultiplicities;
161         Standard_Integer aVMultiplicitiesItem;
162         Standard_Integer nsub10;
163         if (data->ReadSubList (num,10,"v_multiplicities",ach,nsub10)) {
164           Standard_Integer nb10 = data->NbParams(nsub10);
165           aVMultiplicities = new TColStd_HArray1OfInteger (1, nb10);
166           for (Standard_Integer i10 = 1; i10 <= nb10; i10 ++) {
167             //szv#4:S4163:12Mar99 `Standard_Boolean stat10 =` not needed
168             if (data->ReadInteger (nsub10,i10,"v_multiplicities",ach,aVMultiplicitiesItem))
169               aVMultiplicities->SetValue(i10,aVMultiplicitiesItem);
170           }
171         }
172
173         // --- own field : uKnots ---
174
175         Handle(TColStd_HArray1OfReal) aUKnots;
176         Standard_Real aUKnotsItem;
177         Standard_Integer nsub11;
178         if (data->ReadSubList (num,11,"u_knots",ach,nsub11)) {
179           Standard_Integer nb11 = data->NbParams(nsub11);
180           aUKnots = new TColStd_HArray1OfReal (1, nb11);
181           for (Standard_Integer i11 = 1; i11 <= nb11; i11 ++) {
182             //szv#4:S4163:12Mar99 `Standard_Boolean stat11 =` not needed
183             if (data->ReadReal (nsub11,i11,"u_knots",ach,aUKnotsItem))
184               aUKnots->SetValue(i11,aUKnotsItem);
185           }
186         }
187
188         // --- own field : vKnots ---
189
190         Handle(TColStd_HArray1OfReal) aVKnots;
191         Standard_Real aVKnotsItem;
192         Standard_Integer nsub12;
193         if (data->ReadSubList (num,12,"v_knots",ach,nsub12)) {
194           Standard_Integer nb12 = data->NbParams(nsub12);
195           aVKnots = new TColStd_HArray1OfReal (1, nb12);
196           for (Standard_Integer i12 = 1; i12 <= nb12; i12 ++) {
197             //szv#4:S4163:12Mar99 `Standard_Boolean stat12 =` not needed
198             if (data->ReadReal (nsub12,i12,"v_knots",ach,aVKnotsItem))
199               aVKnots->SetValue(i12,aVKnotsItem);
200           }
201         }
202
203         // --- own field : knotSpec ---
204
205         StepGeom_KnotType aKnotSpec = StepGeom_ktUniformKnots;
206         if (data->ParamType(num,13) == Interface_ParamEnum) {
207           Standard_CString text = data->ParamCValue(num,13);
208           if      (ktUniformKnots.IsEqual(text)) aKnotSpec = StepGeom_ktUniformKnots;
209           else if (ktQuasiUniformKnots.IsEqual(text)) aKnotSpec = StepGeom_ktQuasiUniformKnots;
210           else if (ktPiecewiseBezierKnots.IsEqual(text)) aKnotSpec = StepGeom_ktPiecewiseBezierKnots;
211           else if (ktUnspecified.IsEqual(text)) aKnotSpec = StepGeom_ktUnspecified;
212           else ach->AddFail("Enumeration knot_type has not an allowed value");
213         }
214         else ach->AddFail("Parameter #13 (knot_spec) is not an enumeration");
215
216         //--- Initialisation of the read entity ---
217
218
219         ent->Init(aName, aUDegree, aVDegree, aControlPointsList, aSurfaceForm, aUClosed, aVClosed, aSelfIntersect, aUMultiplicities, aVMultiplicities, aUKnots, aVKnots, aKnotSpec);
220 }
221
222
223 void RWStepGeom_RWBSplineSurfaceWithKnots::WriteStep
224         (StepData_StepWriter& SW,
225          const Handle(StepGeom_BSplineSurfaceWithKnots)& ent) const
226 {
227
228         // --- inherited field name ---
229
230         SW.Send(ent->Name());
231
232         // --- inherited field uDegree ---
233
234         SW.Send(ent->UDegree());
235
236         // --- inherited field vDegree ---
237
238         SW.Send(ent->VDegree());
239
240         // --- inherited field controlPointsList ---
241
242         SW.OpenSub();
243         for (Standard_Integer i4 = 1;  i4 <= ent->NbControlPointsListI(); i4 ++) {
244           SW.NewLine(Standard_False);
245           SW.OpenSub();
246           for (Standard_Integer j4 = 1;  j4 <= ent->NbControlPointsListJ(); j4 ++) {
247             SW.Send(ent->ControlPointsListValue(i4,j4));
248             SW.JoinLast(Standard_False);
249           }
250           SW.CloseSub();
251         }
252         SW.CloseSub();
253
254         // --- inherited field surfaceForm ---
255
256         switch(ent->SurfaceForm()) {
257           case StepGeom_bssfSurfOfLinearExtrusion : SW.SendEnum (bssfSurfOfLinearExtrusion); break;
258           case StepGeom_bssfPlaneSurf : SW.SendEnum (bssfPlaneSurf); break;
259           case StepGeom_bssfGeneralisedCone : SW.SendEnum (bssfGeneralisedCone); break;
260           case StepGeom_bssfToroidalSurf : SW.SendEnum (bssfToroidalSurf); break;
261           case StepGeom_bssfConicalSurf : SW.SendEnum (bssfConicalSurf); break;
262           case StepGeom_bssfSphericalSurf : SW.SendEnum (bssfSphericalSurf); break;
263           case StepGeom_bssfUnspecified : SW.SendEnum (bssfUnspecified); break;
264           case StepGeom_bssfRuledSurf : SW.SendEnum (bssfRuledSurf); break;
265           case StepGeom_bssfSurfOfRevolution : SW.SendEnum (bssfSurfOfRevolution); break;
266           case StepGeom_bssfCylindricalSurf : SW.SendEnum (bssfCylindricalSurf); break;
267           case StepGeom_bssfQuadricSurf : SW.SendEnum (bssfQuadricSurf); break;
268         }
269
270         // --- inherited field uClosed ---
271
272         SW.SendLogical(ent->UClosed());
273
274         // --- inherited field vClosed ---
275
276         SW.SendLogical(ent->VClosed());
277
278         // --- inherited field selfIntersect ---
279
280         SW.SendLogical(ent->SelfIntersect());
281
282         // --- own field : uMultiplicities ---
283
284         SW.OpenSub();
285         for (Standard_Integer i9 = 1;  i9 <= ent->NbUMultiplicities();  i9 ++) {
286           SW.Send(ent->UMultiplicitiesValue(i9));
287         }
288         SW.CloseSub();
289
290         // --- own field : vMultiplicities ---
291
292         SW.OpenSub();
293         for (Standard_Integer i10 = 1;  i10 <= ent->NbVMultiplicities();  i10 ++) {
294           SW.Send(ent->VMultiplicitiesValue(i10));
295         }
296         SW.CloseSub();
297
298         // --- own field : uKnots ---
299
300         SW.OpenSub();
301         for (Standard_Integer i11 = 1;  i11 <= ent->NbUKnots();  i11 ++) {
302           SW.Send(ent->UKnotsValue(i11));
303         }
304         SW.CloseSub();
305
306         // --- own field : vKnots ---
307
308         SW.OpenSub();
309         for (Standard_Integer i12 = 1;  i12 <= ent->NbVKnots();  i12 ++) {
310           SW.Send(ent->VKnotsValue(i12));
311         }
312         SW.CloseSub();
313
314         // --- own field : knotSpec ---
315
316         switch(ent->KnotSpec()) {
317           case StepGeom_ktUniformKnots : SW.SendEnum (ktUniformKnots); break;
318           case StepGeom_ktQuasiUniformKnots : SW.SendEnum (ktQuasiUniformKnots); break;
319           case StepGeom_ktPiecewiseBezierKnots : SW.SendEnum (ktPiecewiseBezierKnots); break;
320           case StepGeom_ktUnspecified : SW.SendEnum (ktUnspecified); break;
321         }
322 }
323
324
325 void RWStepGeom_RWBSplineSurfaceWithKnots::Share(const Handle(StepGeom_BSplineSurfaceWithKnots)& ent, Interface_EntityIterator& iter) const
326 {
327
328         Standard_Integer nbiElem1 = ent->NbControlPointsListI();
329         Standard_Integer nbjElem1 = ent->NbControlPointsListJ();
330         for (Standard_Integer is1=1; is1<=nbiElem1; is1 ++) {
331           for (Standard_Integer js1=1; js1<=nbjElem1; js1 ++) {
332             iter.GetOneItem(ent->ControlPointsListValue(is1,js1));
333           }
334         }
335
336 }
337
338
339
340 void RWStepGeom_RWBSplineSurfaceWithKnots::Check
341   (const Handle(StepGeom_BSplineSurfaceWithKnots)& ent,
342    const Interface_ShareTool& ,
343    Handle(Interface_Check)& ach) const
344 {
345   Standard_Integer nbCPLU  = ent->NbControlPointsListI();
346   Standard_Integer nbCPLV  = ent->NbControlPointsListJ();
347   Standard_Integer dgBSSU  = ent->UDegree();
348   Standard_Integer dgBSSV  = ent->VDegree();
349   Standard_Integer nbMulU  = ent->NbUMultiplicities();
350   Standard_Integer nbMulV  = ent->NbVMultiplicities();
351   Standard_Integer nbKnoU  = ent->NbUKnots();
352   Standard_Integer nbKnoV  = ent->NbVKnots();
353   Standard_Integer sumMulU = 0;
354   Standard_Integer sumMulV = 0;
355   Standard_Integer i;
356 //  std::cout << "BSplineSurfaceWithKnots: nbMulU=" << nbMulU << " nbKnoU= " << 
357 //    nbKnoU << " nbCPLU= " << nbCPLU << " degreeU= " << dgBSSU << std::endl;
358 //  std::cout << "                         nbMulV=" << nbMulV << " nbKnoV= " << 
359 //    nbKnoV << " nbCPLV= " << nbCPLV << " degreeV= " << dgBSSV << std::endl;
360   if(nbMulU != nbKnoU) {
361     ach->AddFail("ERROR: No.of KnotMultiplicities not equal No.of Knots in U");
362   }
363   if(nbMulV != nbKnoV) {
364     ach->AddFail("ERROR: No.of KnotMultiplicities not equal No.of Knots in V");
365   }
366
367   // check in U direction
368
369   for(i=1; i<=nbMulU-1; i++) {
370     sumMulU = sumMulU + ent->UMultiplicitiesValue(i);
371   }
372   Standard_Integer sumNonPU = nbCPLU + dgBSSU + 1;
373   Standard_Integer mult1U = ent->UMultiplicitiesValue(1);
374   Standard_Integer multNU = ent->UMultiplicitiesValue(nbMulU);
375 //  std::cout << "BSplineSurfaceWithKnots: mult1U=" << mult1U << " multNU= " <<
376 //    multNU << " sumMulU= " << sumMulU << std::endl;
377   if((sumMulU + multNU) == sumNonPU) {
378   }
379   else if((sumMulU == nbCPLU) && (mult1U == multNU)) {
380   }
381   else {
382     ach->AddFail("ERROR: wrong number of Knot Multiplicities in U");
383   }
384   for(i=2; i<=nbKnoU; i++) {
385     Standard_Real distKn  = ent->UKnotsValue(i-1) - ent->UKnotsValue(i);
386     if(Abs(distKn) <= RealEpsilon())
387       ach->AddWarning("WARNING: Surface contains identical KnotsValues in U");
388     else if(distKn > RealEpsilon())
389       ach->AddFail("ERROR: Surface contains descending KnotsValues in U");
390   }
391
392   // check in V direction
393
394   for(i=1; i<=nbMulV-1; i++) {
395     sumMulV = sumMulV + ent->VMultiplicitiesValue(i);
396   }
397   Standard_Integer sumNonPV = nbCPLV + dgBSSV + 1;
398   Standard_Integer mult1V = ent->VMultiplicitiesValue(1);
399   Standard_Integer multNV = ent->VMultiplicitiesValue(nbMulV);
400 //  std::cout << "                       : mult1V=" << mult1V << " multNV= " <<
401 //    multNV << " sumMulV= " << sumMulV << std::endl;
402   if((sumMulV + multNV) == sumNonPV) {
403   }
404   else if((sumMulV == nbCPLV) && (mult1V == multNV)) {
405   }
406   else {
407     ach->AddFail("ERROR: wrong number of Knot Multiplicities in V");
408   }
409   for(i=2; i<=nbKnoV; i++) {
410     Standard_Real distKn  = ent->VKnotsValue(i-1) - ent->VKnotsValue(i);
411     if(Abs(distKn) <= RealEpsilon())
412       ach->AddWarning("WARNING: Surface contains identical KnotsValues in V");
413     else if(distKn > RealEpsilon())
414       ach->AddFail("ERROR: Surface contains descending KnotsValues in V");
415   }
416 }