Integration of OCCT 6.5.0 from SVN
[occt.git] / src / VrmlConverter / VrmlConverter_WFDeflectionShape.cxx
1 #include <VrmlConverter_WFDeflectionShape.ixx>
2 #include <Bnd_Box.hxx>
3 #include <gp_Pnt.hxx>
4 #include <Prs3d_ShapeTool.hxx>
5 #include <BRepAdaptor_HSurface.hxx>
6 #include <BRepAdaptor_Curve.hxx>
7 #include <BRepBndLib.hxx>
8 #include <VrmlConverter_IsoAspect.hxx>
9 #include <VrmlConverter_LineAspect.hxx>
10 #include <VrmlConverter_WFDeflectionRestrictedFace.hxx>
11 #include <VrmlConverter_DeflectionCurve.hxx>
12 #include <TColgp_HArray1OfVec.hxx> 
13 #include <BRep_Tool.hxx> 
14 #include <VrmlConverter_PointAspect.hxx> 
15 #include <Vrml_Separator.hxx>
16 #include <Vrml_Coordinate3.hxx>
17 #include <Vrml_Material.hxx>
18 #include <Vrml_PointSet.hxx>
19 #include <Precision.hxx>
20
21
22 //=========================================================================
23 // function: Add
24 // purpose
25 //=========================================================================
26 void VrmlConverter_WFDeflectionShape::Add( Standard_OStream&                   anOStream,
27                                           const TopoDS_Shape&                  aShape,
28                                           const Handle (VrmlConverter_Drawer)& aDrawer)
29 {
30
31     Prs3d_ShapeTool Tool(aShape);
32
33     Standard_Real theRequestedDeflection;
34     if(aDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE)   // TOD_RELATIVE, TOD_ABSOLUTE
35       {
36         Bnd_Box box;
37         BRepBndLib::AddClose(aShape, box);
38
39         Standard_Real  Xmin, Xmax, Ymin, Ymax, Zmin, Zmax, diagonal;
40         box.Get( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
41         if (!(box.IsOpenXmin() || box.IsOpenXmax() ||
42               box.IsOpenYmin() || box.IsOpenYmax() ||
43               box.IsOpenZmin() || box.IsOpenZmax()))
44             {
45
46             diagonal = Sqrt ((Xmax - Xmin)*( Xmax - Xmin) + ( Ymax - Ymin)*( Ymax - Ymin) + ( Zmax - Zmin)*( Zmax - Zmin));
47             diagonal = Max(diagonal, Precision::Confusion());
48             theRequestedDeflection = aDrawer->DeviationCoefficient() * diagonal;      
49           }
50         else
51           {
52             diagonal =1000000.;
53             theRequestedDeflection = aDrawer->DeviationCoefficient() * diagonal;  
54           }
55
56       }
57     else 
58       {
59         theRequestedDeflection = aDrawer->MaximalChordialDeviation(); 
60       }
61 //==
62     if (aDrawer->UIsoAspect()->Number() != 0 ||
63         aDrawer->VIsoAspect()->Number() != 0 ) {
64
65       BRepAdaptor_Surface S;
66       Standard_Boolean isoU, isoV;
67       for(Tool.InitFace();Tool.MoreFace();Tool.NextFace()){
68         isoU = (aDrawer->UIsoAspect()->Number() != 0);
69         isoV = (aDrawer->VIsoAspect()->Number() != 0);
70         if (Tool.HasSurface()) {
71           if (Tool.IsPlanarFace()) {
72             isoU = (isoU && aDrawer->IsoOnPlane());
73             isoV = (isoV && aDrawer->IsoOnPlane()); 
74           }
75           if (isoU || isoV) {
76             S.Initialize(Tool.GetFace());
77             Handle(BRepAdaptor_HSurface) HS = new BRepAdaptor_HSurface(S);
78             VrmlConverter_WFDeflectionRestrictedFace::Add(anOStream, HS,
79                                                           isoU, isoV,
80                                                           theRequestedDeflection,
81                                                           aDrawer->UIsoAspect()->Number(),
82                                                           aDrawer->VIsoAspect()->Number(),
83                                                           aDrawer);
84           }
85         }
86       }
87     }
88
89   else {
90
91     if (aDrawer->UIsoAspect()->Number() != 0) {
92
93       BRepAdaptor_Surface S;
94       for(Tool.InitFace();Tool.MoreFace();Tool.NextFace()){
95         Standard_Boolean isoU = Standard_True;
96         if (Tool.HasSurface()) {
97           if (Tool.IsPlanarFace()) isoU = aDrawer->IsoOnPlane();
98           if (isoU) {
99             S.Initialize(Tool.GetFace());
100             Handle(BRepAdaptor_HSurface) HS = new BRepAdaptor_HSurface(S);
101             VrmlConverter_WFDeflectionRestrictedFace::Add(anOStream, HS,
102                                                           isoU, Standard_False,
103                                                           theRequestedDeflection,
104                                                           aDrawer->UIsoAspect()->Number(),
105                                                           0,
106                                                           aDrawer);
107           }
108         }
109       }
110     }
111
112     if (aDrawer->VIsoAspect()->Number() != 0) {
113
114       BRepAdaptor_Surface S;
115       for(Tool.InitFace();Tool.MoreFace();Tool.NextFace()){
116         Standard_Boolean isoV = Standard_True;
117         if (Tool.HasSurface()) {
118           if (Tool.IsPlanarFace()) isoV = aDrawer->IsoOnPlane();
119           if (isoV) {
120             S.Initialize(Tool.GetFace());
121             Handle(BRepAdaptor_HSurface) HS = new BRepAdaptor_HSurface(S);
122             VrmlConverter_WFDeflectionRestrictedFace::Add(anOStream, HS,
123                                                           Standard_False, isoV,
124                                                           theRequestedDeflection,
125                                                           0,
126                                                           aDrawer->VIsoAspect()->Number(),
127                                                           aDrawer);
128           }
129         }
130       }
131     }
132   }
133
134 //====
135     Standard_Integer qnt=0;
136     for(Tool.InitCurve();Tool.MoreCurve();Tool.NextCurve())
137       {
138         qnt++;
139       }
140
141 //   cout << "Quantity of Curves  = " << qnt << endl;
142
143 // Wire (without any neighbour)
144
145     if (aDrawer->WireDraw()) {
146       if (qnt != 0)
147         {
148           Handle(VrmlConverter_LineAspect) latmp = new VrmlConverter_LineAspect; 
149           latmp->SetMaterial(aDrawer->LineAspect()->Material());
150           latmp->SetHasMaterial(aDrawer->LineAspect()->HasMaterial());
151           
152           aDrawer->SetLineAspect(aDrawer->WireAspect());
153           
154           for(Tool.InitCurve();Tool.MoreCurve();Tool.NextCurve()){
155             if (Tool.Neighbours() == 0) {
156               if (Tool.HasCurve()) {
157                 BRepAdaptor_Curve C(Tool.GetCurve());
158                 VrmlConverter_DeflectionCurve::Add(anOStream, C, theRequestedDeflection, aDrawer);
159               }
160             }
161           }
162           aDrawer->SetLineAspect(latmp);
163         }
164     }
165 //end of wire
166
167 // Free boundaries;
168     if (aDrawer->FreeBoundaryDraw()) {
169       if (qnt != 0)
170         {
171           Handle(VrmlConverter_LineAspect) latmp = new VrmlConverter_LineAspect; 
172           latmp->SetMaterial(aDrawer->LineAspect()->Material());
173           latmp->SetHasMaterial(aDrawer->LineAspect()->HasMaterial());
174           
175           aDrawer->SetLineAspect(aDrawer->FreeBoundaryAspect());
176           
177           for(Tool.InitCurve();Tool.MoreCurve();Tool.NextCurve()){
178             if (Tool.Neighbours() == 1) {
179               if (Tool.HasCurve()) {
180                 BRepAdaptor_Curve C(Tool.GetCurve());
181                 VrmlConverter_DeflectionCurve::Add(anOStream, C, theRequestedDeflection, aDrawer);
182               }
183             }
184           }
185           aDrawer->SetLineAspect(latmp);
186         }
187     }
188 // end of Free boundaries
189
190 // Unfree boundaries;
191   if (aDrawer->UnFreeBoundaryDraw()) { 
192     if (qnt != 0)
193       {
194         Handle(VrmlConverter_LineAspect) latmp = new VrmlConverter_LineAspect; 
195         latmp->SetMaterial(aDrawer->LineAspect()->Material());
196         latmp->SetHasMaterial(aDrawer->LineAspect()->HasMaterial());
197
198         aDrawer->SetLineAspect(aDrawer->UnFreeBoundaryAspect());
199
200         for(Tool.InitCurve();Tool.MoreCurve();Tool.NextCurve()){
201           if (Tool.Neighbours() >= 2) {
202             if (Tool.HasCurve()) {
203               BRepAdaptor_Curve C(Tool.GetCurve());
204               VrmlConverter_DeflectionCurve::Add(anOStream, C, theRequestedDeflection, aDrawer);
205             }
206           }
207         }
208         aDrawer->SetLineAspect(latmp);
209       }
210   }
211 // end of Unfree boundaries
212
213 // Points
214  
215     qnt=0;
216     for(Tool.InitVertex();Tool.MoreVertex();Tool.NextVertex())
217       {
218         qnt++;
219       }
220
221 //   cout << "Quantity of Vertexes  = " << qnt << endl;
222
223     if (qnt != 0)
224       {
225         Handle(TColgp_HArray1OfVec) HAV = new TColgp_HArray1OfVec(1,qnt);
226         gp_Vec V;
227         gp_Pnt P;
228         Standard_Integer i=0;
229
230         for(Tool.InitVertex();Tool.MoreVertex();Tool.NextVertex())
231           {
232             i++;
233             P = BRep_Tool::Pnt(Tool.GetVertex());
234             V.SetX(P.X()); V.SetY(P.Y()); V.SetZ(P.Z());
235             HAV->SetValue (i,V);
236           }
237
238         Handle(VrmlConverter_PointAspect) PA = new VrmlConverter_PointAspect;
239         PA = aDrawer->PointAspect();
240
241         // Separator P {
242         Vrml_Separator SEP;
243         SEP.Print(anOStream);
244
245         // Material
246         if (PA->HasMaterial()){
247
248           Handle(Vrml_Material) MP;
249           MP = PA->Material();
250           
251           MP->Print(anOStream);
252         }
253         // Coordinate3
254         Handle(Vrml_Coordinate3)  C3 = new Vrml_Coordinate3(HAV);
255         C3->Print(anOStream);
256         
257         // PointSet
258         Vrml_PointSet PS;
259         PS.Print(anOStream);
260
261         // Separator P }
262         SEP.Print(anOStream);
263       }
264
265 }