0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / TestTopOpeDraw / TestTopOpeDraw_DrawableSHA.cxx
1 // Created on: 1994-10-24
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <TestTopOpeDraw_DrawableSHA.ixx>
18 #include <TestTopOpeDraw_DrawableSUR.hxx>
19 #include <TestTopOpeDraw_DrawableC3D.hxx>
20 #include <TestTopOpeDraw_DrawableP3D.hxx>
21
22 #include <TopoDS.hxx>
23 #include <TopExp_Explorer.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS_Face.hxx>
26 #include <BRep_Tool.hxx>
27 #include <BRepTools.hxx>
28 #include <BRepAdaptor_Surface.hxx>
29 #include <BRepAdaptor_Curve.hxx>
30 #include <Geom_Surface.hxx>
31 #include <Geom_TrimmedCurve.hxx>
32 #include <Geom_RectangularTrimmedSurface.hxx>
33 #include <Precision.hxx>
34 #include <gp_Vec.hxx>
35 #include <gp_Pnt.hxx>
36 #include <Draw_Color.hxx>
37 #include <Draw_Segment3D.hxx>
38 #include <gp.hxx>
39 #include <Geom_Curve.hxx>
40
41 Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation);
42
43 //=======================================================================
44 //function : TestTopOpeDraw_DrawableSHA
45 //purpose  : 
46 //=======================================================================
47
48 TestTopOpeDraw_DrawableSHA::TestTopOpeDraw_DrawableSHA
49 (const TopoDS_Shape& aShape,
50  const Draw_Color& FreeCol,
51  const Draw_Color& ConnCol,
52  const Draw_Color& EdgeCol,
53  const Draw_Color& IsosCol,
54  const Standard_Real size,
55  const Standard_Integer nbisos,
56  const Standard_Integer discret,
57  const Standard_CString Text,
58  const Draw_Color& TextColor,
59  const Standard_Boolean DisplayGeometry) :
60  DBRep_DrawableShape(aShape,FreeCol,ConnCol,EdgeCol,IsosCol,size,nbisos,discret),
61  myTol(0.0), myPar(-1.0)
62 {
63   myDisplayGeometry = DisplayGeometry;
64   myText = new Draw_Text3D(Pnt(),Text,TextColor);
65   myTextColor = TextColor;
66 }
67
68
69 //=======================================================================
70 //function : Pnt
71 //purpose  : 
72 //=======================================================================
73
74 gp_Pnt TestTopOpeDraw_DrawableSHA::Pnt() const
75 {
76   gp_Pnt P(0,0,0);
77   Standard_Real u1,u2,v1,v2;
78   TopExp_Explorer ex;
79
80   TopoDS_Shape S = Shape();
81   TopAbs_ShapeEnum T = S.ShapeType();
82   Standard_Real facpar = 0.;
83
84   while ( T == TopAbs_COMPOUND ) {
85     TopoDS_Iterator ti(S);
86     if ( ti.More() ) {
87       S = ti.Value(); T = S.ShapeType();
88     }
89     else {
90       break;
91     }
92   }
93   // si S final = compound --> P = 0 0 0 
94
95   switch (T) {
96     
97   case TopAbs_VERTEX : 
98     P = BRep_Tool::Pnt(TopoDS::Vertex(S)); 
99     break;
100
101   case TopAbs_WIRE : 
102     T = TopAbs_EDGE; 
103     ex.Init(S,T); S = ex.Current();
104     facpar = 0.33;
105     // no break, in order to execute EDGE case
106
107   case TopAbs_EDGE : 
108     {
109     BRepAdaptor_Curve CU(TopoDS::Edge(S));
110     u1 = CU.FirstParameter(); u2 = CU.LastParameter();
111     if (facpar == 0.) facpar = 0.20;
112     if (myPar != -1.0) facpar = myPar;
113     Standard_Real p = u1 + (u2-u1)*facpar;
114     P = CU.Value(p);
115     }
116     break;
117
118   case TopAbs_SHELL : case TopAbs_SOLID : case TopAbs_COMPSOLID :
119     T = TopAbs_FACE; 
120     ex.Init(S,T); S = ex.Current();
121     facpar = 0.33;
122     // no break, in order to execute FACE case
123
124   case TopAbs_FACE : 
125     {
126     BRepAdaptor_Surface SU(TopoDS::Face(S));
127     BRepTools::UVBounds(TopoDS::Face(S),u1,u2,v1,v2);
128     if (facpar == 0.) facpar = 0.20;
129     Standard_Real u = u1 + (u2-u1)*facpar;
130     Standard_Real v = v1 + (v2-v1)*facpar;
131     P = SU.Value(u,v);
132     }
133     break;
134   default:
135     break;
136   }
137
138   return P;
139 }
140
141 //=======================================================================
142 //function : SetDisplayGeometry
143 //purpose  : 
144 //=======================================================================
145
146 void TestTopOpeDraw_DrawableSHA::SetDisplayGeometry(const Standard_Boolean b)
147 {
148   myDisplayGeometry = b;
149 }
150
151 //=======================================================================
152 //function : SetTol
153 //purpose  : 
154 //=======================================================================
155
156 void TestTopOpeDraw_DrawableSHA::SetTol(const Standard_Real t)
157 {
158   myTol = t;
159   if(myTol != 0.0) 
160     myDM3d = new Draw_Marker3D(Pnt(), Draw_CircleZoom , Draw_rouge, myTol);
161 }
162
163
164 //=======================================================================
165 //function : SetPar
166 //purpose  : 
167 //=======================================================================
168
169 void TestTopOpeDraw_DrawableSHA::SetPar(const Standard_Real t)
170 {
171   myPar = t;
172   myText->SetPnt(Pnt());
173 }
174
175
176 //=======================================================================
177 //function : DrawOn
178 //purpose  : 
179 //=======================================================================
180
181 void TestTopOpeDraw_DrawableSHA::DrawOn(Draw_Display& dis) const
182 {
183   if(myDM3d.IsNull()) 
184     DBRep_DrawableShape::DrawOn(dis);
185   else 
186     myDM3d->DrawOn(dis);
187   myText->DrawOn(dis);
188   if (myDisplayGeometry) DisplayGeometry(dis);
189 }
190
191 //=======================================================================
192 //function : DisplayGeometry
193 //purpose  : 
194 //=======================================================================
195
196 void TestTopOpeDraw_DrawableSHA::DisplayGeometry(Draw_Display& dis) const 
197 {
198   const TopoDS_Shape& aShape = Shape();
199   const TopAbs_ShapeEnum t = aShape.ShapeType();
200
201   if ( t == TopAbs_FACE ) {
202     const TopoDS_Face& F = TopoDS::Face(aShape);
203     TopLoc_Location loc;
204     Handle(Geom_Surface) GS = BRep_Tool::Surface(F,loc);
205     if ( GS.IsNull() ) return;
206     Standard_Real u1,u2,v1,v2; BRepTools::UVBounds(F,u1,u2,v1,v2);
207     GS = Handle(Geom_Surface)::DownCast(GS->Transformed(loc.Transformation()));
208     Handle(Geom_RectangularTrimmedSurface) GRTS;
209     GRTS = new Geom_RectangularTrimmedSurface(GS,u1,u2,v1,v2);
210     Handle(TestTopOpeDraw_DrawableSUR) D;
211     D = new TestTopOpeDraw_DrawableSUR(GRTS,Draw_bleu);
212     Draw_Color norcol = DBRep_ColorOrientation(aShape.Orientation());
213     D->NormalColor(norcol);
214     D->DrawNormale(dis);
215   }
216
217   else if ( t == TopAbs_EDGE ) {
218     const TopoDS_Edge& E = TopoDS::Edge(aShape);
219     TopLoc_Location loc; Standard_Real f,l;
220     Handle(Geom_Curve) GC = BRep_Tool::Curve(E,loc,f,l);
221     if ( GC.IsNull() ) return;    
222     GC = Handle(Geom_Curve)::DownCast(GC->Transformed(loc.Transformation()));
223     Handle(Geom_TrimmedCurve) GTC = new Geom_TrimmedCurve(GC,f,l);
224     Handle(TestTopOpeDraw_DrawableC3D) D;
225     D = new TestTopOpeDraw_DrawableC3D(GTC,myTextColor);
226     D->DrawOn(dis);
227   }
228
229   else if ( t == TopAbs_VERTEX ) {
230     const TopoDS_Vertex& V = TopoDS::Vertex(aShape);
231     gp_Pnt P = BRep_Tool::Pnt(V);
232     Handle(TestTopOpeDraw_DrawableP3D) D;
233     D = new TestTopOpeDraw_DrawableP3D(P,myTextColor);
234     D->DrawOn(dis);
235   }
236
237 }