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