0024428: Implementation of LGPL license
[occt.git] / src / DrawDim / DrawDim_Angle.cxx
1 // Created on: 1996-05-28
2 // Created by: Denis PASCAL
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
9 // under the terms of the GNU Lesser General Public 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 <DrawDim_Angle.ixx>
18 #include <DrawDim.hxx>
19
20 #include <BRepAdaptor_Surface.hxx>
21 #include <BRep_Tool.hxx>
22 #include <ElCLib.hxx>
23 #include <ElSLib.hxx>
24 #include <gp_Pnt.hxx>
25 #include <gp_Pln.hxx>
26 #include <gp_Lin.hxx>
27 #include <gp_Dir.hxx>
28 #include <gp_Ax1.hxx>
29 #include <TopoDS.hxx>
30 #include <IntAna_QuadQuadGeo.hxx>
31 #include <Precision.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopoDS_Vertex.hxx> 
34 #include <IntAna_QuadQuadGeo.hxx>
35
36 //=======================================================================
37 //function : DrawDim_Angle
38 //purpose  : 
39 //=======================================================================
40
41 DrawDim_Angle::DrawDim_Angle(const TopoDS_Face& plane1, const TopoDS_Face& plane2)
42 {
43   myPlane1 = plane1;
44   myPlane2 = plane2;
45 }
46
47 //=======================================================================
48 //function : Plane1
49 //purpose  : 
50 //=======================================================================
51
52 const TopoDS_Face& DrawDim_Angle::Plane1() const 
53 {
54   return myPlane1;
55 }
56
57 //=======================================================================
58 //function : Plane1
59 //purpose  : 
60 //=======================================================================
61
62 void DrawDim_Angle::Plane1(const TopoDS_Face& plane)
63 {
64   myPlane1 = plane;
65 }
66
67 //=======================================================================
68 //function : Plane2
69 //purpose  : 
70 //=======================================================================
71
72 const TopoDS_Face& DrawDim_Angle::Plane2() const 
73 {  
74   return myPlane2;
75 }
76
77 //=======================================================================
78 //function : Plane2
79 //purpose  : 
80 //=======================================================================
81
82 void DrawDim_Angle::Plane2(const TopoDS_Face& plane)
83 {
84   myPlane2 = plane;
85 }
86
87
88 //=======================================================================
89 //function : DrawOn
90 //purpose  : 
91 //=======================================================================
92
93 void DrawDim_Angle::DrawOn(Draw_Display& ) const 
94 {
95
96   // input
97   TopoDS_Shape myFShape = myPlane1;
98   TopoDS_Shape mySShape = myPlane2;  
99   Standard_Real myVal = GetValue();
100   gp_Ax1 myAxis;
101
102
103   // output
104 #ifdef DEB
105   gp_Dir myDirAttach;
106 #endif
107   gp_Pnt myFAttach;
108   gp_Pnt mySAttach;
109   gp_Pnt myPosition(0.,0.,0.);
110   gp_Pnt myCenter;
111   gp_Dir myFDir;
112   gp_Dir mySDir;
113   Standard_Boolean myAutomaticPosition = Standard_True;
114   
115
116
117   // calculation of myAxis
118   gp_Pln pln1, pln2;
119   if (!DrawDim::Pln(myPlane1,pln1)) return;
120   if (!DrawDim::Pln(myPlane2,pln2)) return; 
121   IntAna_QuadQuadGeo ip (pln1,pln2,Precision::Confusion(), Precision::Angular());
122   if (!ip.IsDone()) return;
123 #ifdef DEB
124   gp_Lin linter =
125 #endif
126                   ip.Line(1);
127   
128   //Handle(Geom_Surface) curve1 = BRep_Tool::Surface(myPlane1);
129   //Handle(Geom_PlaneLine) line1 = Handle(Geom_Line)::DownCast(curve1);    
130   
131
132
133 //=======================================================================
134 //function : ComputeTwoFacesAngle
135 //purpose  : 
136 //=======================================================================
137
138 // void AIS_AngleDimension::ComputeTwoFacesAngle(const Handle(Prs3d_Presentation)& aPresentation)
139 // {
140   // Return planes
141  
142   gp_Pnt curpos;
143   gp_Ax1 AxePos = myAxis;      
144   gp_Dir theAxisDir = AxePos.Direction();
145   gp_Lin theaxis= gp_Lin (myAxis);
146       
147   if (myAutomaticPosition) {
148     TopExp_Explorer explo1(myFShape,TopAbs_VERTEX);
149     Standard_Real curdist = 0;
150     while  (explo1.More()) {
151       TopoDS_Vertex vertref = TopoDS::Vertex(explo1.Current());
152       gp_Pnt curpt = BRep_Tool::Pnt(vertref);
153       if (theaxis.Distance(curpt) > curdist) {
154         curdist = theaxis.Distance(curpt);
155         myFAttach = BRep_Tool::Pnt(vertref);
156       }
157       explo1.Next();
158     }
159     curpos = myFAttach.Rotated(AxePos,myVal/2.);
160     myCenter = ElCLib::Value(ElCLib::Parameter(theaxis,curpos),theaxis);
161     Standard_Real thedista = myCenter.Distance(myFAttach);
162     if (thedista > Precision::Confusion()) {
163       curpos.Scale(myCenter,1.05);
164     }
165     myPosition = curpos;
166     myAutomaticPosition = Standard_True;      
167   }
168   else {
169     curpos = myPosition;
170     //myFAttach  = the point of myFShape closest to curpos (except for the case when this is a point on the axis)
171     Standard_Real dist = RealLast(); 
172     TopExp_Explorer explo1(myFShape,TopAbs_VERTEX);
173     gp_Pnt  AxePosition = AxePos.Location();
174     gp_Vec  AxeVector (theAxisDir);
175     gp_XYZ  AxeXYZ  = AxeVector.XYZ();
176     while (explo1.More()) {
177       gp_Pnt curpt = BRep_Tool::Pnt(TopoDS::Vertex(explo1.Current()));
178       gp_Vec curvec (AxePosition, curpt);
179       gp_XYZ curXYZ = curvec.XYZ();
180       gp_XYZ Norm (curXYZ.Crossed(AxeXYZ));
181
182       if (Norm.Modulus() > gp::Resolution()) {
183         Standard_Real curdist = curpos.Distance (curpt);
184         if (curdist < dist) {
185           myFAttach = curpt;
186           dist = curdist;
187         }
188       }
189       explo1.Next();
190     }
191     myCenter = ElCLib::Value(ElCLib::Parameter(theaxis,myFAttach),theaxis);
192   }
193
194   mySAttach = myFAttach.Rotated(AxePos,myVal);
195       
196   gp_Vec FVec (myCenter, myFAttach);
197   myFDir.SetXYZ (FVec.XYZ());
198   gp_Vec SVec (myCenter, mySAttach);
199   mySDir.SetXYZ (SVec.XYZ());
200
201   if (!myAutomaticPosition) {
202     //Projection of the position on the plane defined by myFDir mySDir and normal theAxisDir
203     gp_Pln aPln (myCenter, theAxisDir);
204     Standard_Real U,V;
205     ElSLib::Parameters (aPln, curpos, U, V);
206     curpos = ElSLib::Value (U, V, aPln);
207   }
208
209   // DISPLAY
210   // Add (myVal, myText,myCenter,myFAttach,mySAttach,myFDir,mySDir,theAxisDir,curpos)
211
212           
213 }
214
215
216
217
218
219
220
221
222