Integration of OCCT 6.5.0 from SVN
[occt.git] / src / AIS / AIS_Relation.cxx
1 // File:        AIS_Relation.cxx
2 // Created:     Tue Dec  5 15:09:04 1996
3 // Author:      Odile Olivier
4 //              <ODL>
5
6 #define GER61351        //GG_171199     Enable to set an object RGB color
7 //                                                instead a restricted object NameOfColor.
8
9 #define BUC60915        //GG 05/06/01 Enable to compute the requested arrow size 
10 //                      if any in all dimensions.
11
12 #include <AIS.hxx>
13 #include <Graphic3d_Array1OfVertex.hxx>
14 #include <Graphic3d_Group.hxx>
15
16 #include <AIS_Relation.ixx>
17
18 #include <AIS_Drawer.hxx>
19 #include <AIS_GraphicTool.hxx>
20 #include <BRepBuilderAPI_MakeEdge.hxx>
21 #include <BRep_Tool.hxx>
22 #include <Geom_Circle.hxx>
23 #include <Geom_Line.hxx>
24 #include <Geom_CartesianPoint.hxx>
25
26 #include <Prs3d_LineAspect.hxx>
27 #include <Prs3d_AngleAspect.hxx>
28 #include <Prs3d_PointAspect.hxx>
29 #include <Prs3d_LengthAspect.hxx>
30 #include <Prs3d_TextAspect.hxx>
31
32 #include <StdPrs_WFDeflectionShape.hxx>
33 #include <StdPrs_Point.hxx>
34
35 #include <TopoDS.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <gp_Circ.hxx>
38 #include <gp_Lin.hxx>
39 #include <gp_Pnt.hxx>
40 #include <ElCLib.hxx>
41 #include <TopExp.hxx>
42
43
44 #include <Precision.hxx>
45 #include <BRepBuilderAPI_MakeVertex.hxx>
46
47 //=======================================================================
48 //function : AIS_Relation
49 //purpose  : 
50 //=======================================================================
51 AIS_Relation::AIS_Relation(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
52 :AIS_InteractiveObject(aTypeOfPresentation3d),
53  myVal(1.),
54  myPosition(0.,0.,0.),
55 #ifdef BUC60915
56  myArrowSize( myVal / 10. ),
57 #endif
58  myAutomaticPosition(Standard_True),
59  myExtShape(0),
60  myFirstOffset(0.),mySecondOffset(0.),
61  myIsSetBndBox( Standard_False )
62 #ifdef BUC60915
63  ,myArrowSizeIsDefined( Standard_False)
64 #endif
65 {
66 }
67
68
69
70 //=======================================================================
71 //function : ComputeProjEdgePresentation
72 //purpose  : 
73 //=======================================================================
74
75 void AIS_Relation::ComputeProjEdgePresentation(const Handle(Prs3d_Presentation)& aPrs, 
76                                                const TopoDS_Edge& anEdge,
77                                                const Handle(Geom_Curve)& ProjCurv, 
78                                                const gp_Pnt& FirstP, 
79                                                const gp_Pnt& LastP, 
80                                                const Quantity_NameOfColor aColor,
81                                                const Standard_Real width,
82                                                const Aspect_TypeOfLine aProjTOL,
83                                                const Aspect_TypeOfLine aCallTOL) const 
84 {
85   if (!myDrawer->HasWireAspect()){
86     myDrawer->SetWireAspect(new Prs3d_LineAspect(aColor,aProjTOL,2.));}
87   else {
88     const Handle(Prs3d_LineAspect)& li = myDrawer->WireAspect();
89     li->SetColor(aColor);
90     li->SetTypeOfLine(aProjTOL);
91     li->SetWidth(width);
92   }
93
94   Standard_Real pf, pl;
95   TopLoc_Location loc;
96   Handle(Geom_Curve) curve;
97   Standard_Boolean isInfinite;
98   curve = BRep_Tool::Curve(anEdge,loc,pf,pl);
99   isInfinite = (Precision::IsInfinite(pf) || Precision::IsInfinite(pl));
100
101   TopoDS_Edge E;
102
103   // Calcul de la presentation de l'edge
104   if (ProjCurv->IsInstance(STANDARD_TYPE(Geom_Line)) ) {
105     const Handle(Geom_Line) & gl = (Handle(Geom_Line)&) ProjCurv;
106     if ( !isInfinite) {
107       pf = ElCLib::Parameter(gl->Lin(),FirstP);
108       pl = ElCLib::Parameter(gl->Lin(),LastP);
109       BRepBuilderAPI_MakeEdge MakEd(gl->Lin(), pf, pl);
110       E = MakEd.Edge();
111     }
112     else {
113       BRepBuilderAPI_MakeEdge MakEd(gl->Lin());
114       E = MakEd.Edge();
115     }
116   }
117   else if (ProjCurv->IsInstance(STANDARD_TYPE(Geom_Circle)) ) {
118     const Handle(Geom_Circle) & gc = (Handle(Geom_Circle)&) ProjCurv;
119     pf = ElCLib::Parameter(gc->Circ(),FirstP);
120     pl = ElCLib::Parameter(gc->Circ(),LastP);
121     BRepBuilderAPI_MakeEdge MakEd(gc->Circ(),pf, pl);
122     E = MakEd.Edge();
123   }
124   StdPrs_WFDeflectionShape::Add(aPrs, E, myDrawer);
125
126   //Calcul de la presentation des lignes de raccord
127   myDrawer->WireAspect()->SetTypeOfLine(aCallTOL);
128   if (!isInfinite) {
129     gp_Pnt ppf, ppl;
130     ppf = BRep_Tool::Pnt( TopExp::FirstVertex(TopoDS::Edge(anEdge)));
131     ppl = BRep_Tool::Pnt( TopExp::LastVertex(TopoDS::Edge(anEdge)));
132     if (FirstP.Distance( ppf ) > gp::Resolution())
133       {
134         BRepBuilderAPI_MakeEdge MakEd1(FirstP, ppf);
135         StdPrs_WFDeflectionShape::Add(aPrs, MakEd1.Edge(), myDrawer);
136       }
137     else
138       {
139         BRepBuilderAPI_MakeVertex MakVert1( FirstP );
140         StdPrs_WFDeflectionShape::Add(aPrs, MakVert1.Vertex(), myDrawer);
141       }
142     if (LastP.Distance( ppl ) > gp::Resolution())
143       {
144         BRepBuilderAPI_MakeEdge MakEd2(LastP, ppl);
145         StdPrs_WFDeflectionShape::Add(aPrs, MakEd2.Edge(), myDrawer);
146       }
147     else
148       {
149         BRepBuilderAPI_MakeVertex MakVert2( LastP );
150         StdPrs_WFDeflectionShape::Add(aPrs, MakVert2.Vertex(), myDrawer);
151       }
152 /*
153     BRepBuilderAPI_MakeEdge MakEd1(FirstP, ppf);
154     StdPrs_WFDeflectionShape::Add(aPrs, MakEd1.Edge(), myDrawer);
155     BRepBuilderAPI_MakeEdge MakEd2(LastP, ppl);
156     StdPrs_WFDeflectionShape::Add(aPrs, MakEd2.Edge(), myDrawer);
157 */
158   }
159 }
160
161
162 //=======================================================================
163 //function : ComputeProjVertexPresentation
164 //purpose  : 
165 //=======================================================================
166
167 void AIS_Relation::ComputeProjVertexPresentation(const Handle(Prs3d_Presentation)& aPrs, 
168                                                  const TopoDS_Vertex& aVertex,
169                                                  const gp_Pnt& ProjPoint, 
170                                                  const Quantity_NameOfColor aColor,
171                                                  const Standard_Real width,
172                                                  const Aspect_TypeOfMarker aProjTOM,
173                                                  const Aspect_TypeOfLine aCallTOL) const 
174 {
175   if (!myDrawer->HasPointAspect()){
176     myDrawer->SetPointAspect(new Prs3d_PointAspect(aProjTOM, aColor,1));}
177   else {
178     const Handle(Prs3d_PointAspect)& pa = myDrawer->PointAspect();
179     pa->SetColor(aColor);
180     pa->SetTypeOfMarker(aProjTOM);
181   }
182   
183   // calcul du projete
184   StdPrs_Point::Add(aPrs, new Geom_CartesianPoint(ProjPoint), myDrawer);
185
186   if (!myDrawer->HasWireAspect()){
187     myDrawer->SetWireAspect(new Prs3d_LineAspect(aColor,aCallTOL,2.));}
188   else {
189     const Handle(Prs3d_LineAspect)& li = myDrawer->WireAspect();
190     li->SetColor(aColor);
191     li->SetTypeOfLine(aCallTOL);
192     li->SetWidth(width);
193   }
194   
195   // Si les points ne sont pas confondus...
196   if (!ProjPoint.IsEqual (BRep_Tool::Pnt(aVertex),Precision::Confusion())) {
197     // calcul des lignes de rappel
198     BRepBuilderAPI_MakeEdge MakEd(ProjPoint,BRep_Tool::Pnt(aVertex));
199     StdPrs_WFDeflectionShape::Add(aPrs, MakEd.Edge(), myDrawer);
200   }
201 }
202
203 //=======================================================================
204 //function : SetColor
205 //purpose  : 
206 //=======================================================================
207
208 void AIS_Relation::SetColor(const Quantity_NameOfColor aCol)
209 #ifdef GER61351
210 {
211   SetColor(Quantity_Color(aCol));
212 }
213
214 void AIS_Relation::SetColor(const Quantity_Color &aCol)
215 #endif
216 {
217   if(hasOwnColor && myOwnColor==aCol) return;
218
219   if (!myDrawer->HasTextAspect()) myDrawer->SetTextAspect(new Prs3d_TextAspect());
220   hasOwnColor=Standard_True;
221   myOwnColor=aCol;
222   myDrawer->TextAspect()->SetColor(aCol);
223
224   Standard_Real WW = HasWidth()? Width():AIS_GraphicTool::GetLineWidth(myDrawer->Link(),AIS_TOA_Line);
225   if (!myDrawer->HasLineAspect()) {
226     myDrawer->SetLineAspect(new Prs3d_LineAspect(aCol,Aspect_TOL_SOLID,WW));
227   }
228   if (!myDrawer->HasLengthAspect()) {
229      myDrawer->SetLengthAspect(new Prs3d_LengthAspect);
230   }
231   if (!myDrawer->HasAngleAspect()) {
232     myDrawer->SetAngleAspect(new Prs3d_AngleAspect);
233   }
234   myDrawer->LineAspect()->SetColor(aCol);  
235   const Handle(Prs3d_LengthAspect)& LENGTH = myDrawer->LengthAspect();
236   const Handle(Prs3d_AngleAspect)&  ANGLE  = myDrawer->AngleAspect();
237   const Handle(Prs3d_LineAspect)&   LINE   = myDrawer->LineAspect();
238   const Handle(Prs3d_TextAspect)&   TEXT   = myDrawer->TextAspect();
239
240   LENGTH->SetLineAspect(LINE);
241   LENGTH->SetTextAspect(TEXT);
242   ANGLE->SetLineAspect(LINE);
243   ANGLE->SetTextAspect(TEXT);  
244 }
245
246 //=======================================================================
247 //function : UnsetColor
248 //purpose  : 
249 //=======================================================================
250 void AIS_Relation::UnsetColor()
251 {
252   if (!hasOwnColor) return;
253   hasOwnColor = Standard_False;
254   const Handle(Prs3d_LineAspect)& LA = myDrawer->LineAspect();
255 #ifdef GER61351
256   Quantity_Color CC;
257   AIS_GraphicTool::GetLineColor(myDrawer->Link(),AIS_TOA_Line,CC);
258   LA->SetColor(CC);
259 #else
260   LA->SetColor(AIS_GraphicTool::GetLineColor(myDrawer->Link(),AIS_TOA_Line));
261 #endif
262   myDrawer->AngleAspect()->SetLineAspect(LA);
263   myDrawer->LengthAspect()->SetLineAspect(LA);
264   myDrawer->SetTextAspect(myDrawer->Link()->TextAspect());
265 }
266
267 //=======================================================================
268 //function : AcceptDisplayMode
269 //purpose  : 
270 //=======================================================================
271
272  Standard_Boolean  AIS_Relation::
273 AcceptDisplayMode(const Standard_Integer aMode) const
274 {return aMode == 0;}
275
276
277 //=======================================================================
278 //function : SetFirstShape
279 //purpose  : 
280 //=======================================================================
281
282 void AIS_Relation::SetFirstShape(const TopoDS_Shape& aFShape)
283 {
284   myFShape = aFShape;
285 }
286
287
288 //=======================================================================
289 //function : SetSecondShape
290 //purpose  : 
291 //=======================================================================
292
293 void AIS_Relation::SetSecondShape(const TopoDS_Shape& aSShape)
294 {
295   mySShape = aSShape;
296 }
297
298 //=======================================================================
299 //function : KindOfDimension
300 //purpose  : 
301 //=======================================================================
302 AIS_KindOfDimension AIS_Relation::KindOfDimension() const 
303 {return AIS_KOD_NONE;}
304
305 //=======================================================================
306 //function : IsMovable
307 //purpose  : 
308 //=======================================================================
309 Standard_Boolean AIS_Relation::IsMovable() const 
310 {return Standard_False;}
311
312