0031456: Visualization - move out Dimensions and Relations from package AIS to PrsDims
[occt.git] / src / PrsDim / PrsDim_EqualRadiusRelation.cxx
CommitLineData
b311480e 1// Created on: 1998-01-20
2// Created by: Julia GERASIMOVA
3// Copyright (c) 1998-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
787ff240 17#include <PrsDim_EqualRadiusRelation.hxx>
7fd59977 18
787ff240 19#include <PrsDim.hxx>
7fd59977 20#include <BRepAdaptor_Curve.hxx>
42cf5bc1 21#include <DsgPrs_EqualRadiusPresentation.hxx>
7fd59977 22#include <ElCLib.hxx>
42cf5bc1 23#include <Geom_Circle.hxx>
24#include <Geom_Plane.hxx>
7fd59977 25#include <GeomAPI_ProjectPointOnSurf.hxx>
42cf5bc1 26#include <gp_Circ.hxx>
27#include <Precision.hxx>
7fd59977 28#include <Prs3d_ArrowAspect.hxx>
42cf5bc1 29#include <Prs3d_DimensionAspect.hxx>
6262338c 30#include <Prs3d_Drawer.hxx>
42cf5bc1 31#include <Prs3d_Presentation.hxx>
42cf5bc1 32#include <Select3D_SensitiveBox.hxx>
33#include <Select3D_SensitiveSegment.hxx>
34#include <SelectMgr_EntityOwner.hxx>
35#include <SelectMgr_Selection.hxx>
36#include <Standard_NotImplemented.hxx>
37#include <Standard_Type.hxx>
38#include <TopoDS.hxx>
39#include <TopoDS_Edge.hxx>
7fd59977 40
787ff240 41IMPLEMENT_STANDARD_RTTIEXT(PrsDim_EqualRadiusRelation, PrsDim_Relation)
92efcf78 42
7fd59977 43//=======================================================================
787ff240 44//function : PrsDim_EqualRadiusRelation
7fd59977 45//purpose :
46//=======================================================================
787ff240 47PrsDim_EqualRadiusRelation::PrsDim_EqualRadiusRelation( const TopoDS_Edge& aFirstEdge,
7fd59977 48 const TopoDS_Edge& aSecondEdge,
49 const Handle( Geom_Plane )& aPlane )
787ff240 50: PrsDim_Relation()
7fd59977 51{
52 myFShape = aFirstEdge;
53 mySShape = aSecondEdge;
54 myPlane = aPlane;
55}
56
57//=======================================================================
58//function : Compute
59//purpose :
60//=======================================================================
61
787ff240 62void PrsDim_EqualRadiusRelation::Compute( const Handle( PrsMgr_PresentationManager3d )&,
7fd59977 63 const Handle( Prs3d_Presentation )& aPresentation,
64 const Standard_Integer )
65{
7fd59977 66 BRepAdaptor_Curve FirstCurve( TopoDS::Edge( myFShape ) ), SecondCurve( TopoDS::Edge( mySShape ) );
67
68 Standard_Real FirstPar1 = FirstCurve.FirstParameter(), LastPar1 = FirstCurve.LastParameter(),
69 FirstPar2 = SecondCurve.FirstParameter(), LastPar2 = SecondCurve.LastParameter();
70
71 Handle( Geom_Curve ) FirstProjCurve = FirstCurve.Curve().Curve(),
72 SecondProjCurve = SecondCurve.Curve().Curve();
73 gp_Pnt FirstPoint1, LastPoint1, FirstPoint2, LastPoint2;
74 Standard_Boolean isFirstOnPlane, isSecondOnPlane;
75
787ff240 76 PrsDim::ComputeGeomCurve (FirstProjCurve, FirstPar1, LastPar1, FirstPoint1, LastPoint1, myPlane, isFirstOnPlane);
77 PrsDim::ComputeGeomCurve (SecondProjCurve, FirstPar2, LastPar2, FirstPoint2, LastPoint2, myPlane, isSecondOnPlane);
7fd59977 78
79 if (!isFirstOnPlane)
80 ComputeProjEdgePresentation( aPresentation, TopoDS::Edge( myFShape ), FirstProjCurve, FirstPoint1, LastPoint1 );
81 if (! isSecondOnPlane)
82 ComputeProjEdgePresentation( aPresentation, TopoDS::Edge( mySShape ), SecondProjCurve, FirstPoint2, LastPoint2 );
83
84 gp_Circ FirstCirc = (Handle( Geom_Circle )::DownCast( FirstProjCurve ))->Circ();
85 gp_Circ SecondCirc = (Handle( Geom_Circle )::DownCast( SecondProjCurve ))->Circ();
86
87 myFirstCenter = FirstCirc.Location();
88 mySecondCenter = SecondCirc.Location();
89
90 //ota -- begin --
91 if (myAutomaticPosition)
92 {
93 myFirstPoint = ElCLib::Value( (FirstPar1 + LastPar1)*0.5, FirstCirc );
94 mySecondPoint = ElCLib::Value( (FirstPar2 + LastPar2)*0.5, SecondCirc );
95 }
96 else {
97 Standard_Real aPar = ElCLib::Parameter(FirstCirc, myFirstPoint);
c6541a0c
D
98 if (IntegerPart(0.5*LastPar1/M_PI) != 0 && aPar < FirstPar1 )
99 aPar +=2*M_PI*IntegerPart(0.5*LastPar1/M_PI);
7fd59977 100 Standard_Real aRadius = FirstCirc.Radius();
101
102 if (Abs(myFirstPoint.Distance(myFirstCenter) - aRadius) >= Precision::Confusion())
103 myFirstPoint = ElCLib::Value(aPar, FirstCirc);
104 if ( FirstPoint1.Distance(LastPoint1) > Precision::Confusion()){
105 //check where is myFirstPoint
106 if (aPar > LastPar1 || aPar < FirstPar1)
107 {
108 //myFirstPoint is out of Arc of FirstCircle
109 if (FirstPoint1.Distance(myFirstPoint)< LastPoint1.Distance(myFirstPoint))
110 myFirstPoint = FirstPoint1;
111 else
112 myFirstPoint = LastPoint1;
113 }
114 }
115
116
117 aPar = ElCLib::Parameter(SecondCirc, mySecondPoint);
c6541a0c
D
118 if (IntegerPart(0.5*LastPar2/M_PI) != 0 && aPar < FirstPar2 )
119 aPar +=2*M_PI*IntegerPart(0.5*LastPar2/M_PI);
7fd59977 120
121 aRadius = SecondCirc.Radius();
122 if (Abs(mySecondPoint.Distance(mySecondCenter) - aRadius) >= Precision::Confusion())
123 mySecondPoint = ElCLib::Value(aPar, SecondCirc);
124 if (FirstPoint2.Distance(LastPoint2) > Precision::Confusion()){
125 if (aPar > LastPar2 || aPar < FirstPar2)
126 { //mySecondPoint is out of Arc of mySecondCircle
127 if (FirstPoint2.Distance(mySecondPoint)< LastPoint2.Distance(mySecondPoint))
128 mySecondPoint = FirstPoint2;
129 else
130 mySecondPoint = LastPoint2;
131 }
132 }
133 }
7fd59977 134 if( !myArrowSizeIsDefined )
7fd59977 135 myArrowSize = (Min(myFirstCenter.Distance(myFirstPoint),
136 mySecondCenter.Distance(mySecondPoint)))*0.05;
137
a6eb515f 138 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
139 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
7fd59977 140 arr->SetLength(myArrowSize);
141
142 //ota -- end --
143
144 DsgPrs_EqualRadiusPresentation::Add(aPresentation, myDrawer,
145 myFirstCenter, mySecondCenter, myFirstPoint, mySecondPoint, myPlane );
146}
147
148//=======================================================================
7fd59977 149//function : ComputeSelection
150//purpose :
151//=======================================================================
152
787ff240 153void PrsDim_EqualRadiusRelation::ComputeSelection( const Handle( SelectMgr_Selection )& aSelection,
7fd59977 154 const Standard_Integer )
155{
156 Handle( SelectMgr_EntityOwner ) own = new SelectMgr_EntityOwner( this, 7 );
157 Handle( Select3D_SensitiveSegment ) seg;
158
159 seg = new Select3D_SensitiveSegment( own, myFirstCenter, myFirstPoint );
160 aSelection->Add( seg );
161
162 if(!myAutomaticPosition)
163 ComputeRadiusPosition();
164
165 seg = new Select3D_SensitiveSegment( own, mySecondCenter, mySecondPoint );
166 aSelection->Add( seg );
167
168 seg = new Select3D_SensitiveSegment( own, myFirstCenter, mySecondCenter );
169 aSelection->Add( seg );
170
171
172 // Two small lines
173 gp_Pnt Middle( (myFirstCenter.XYZ() + mySecondCenter.XYZ())*0.5 );
174
175 Standard_Real SmallDist = .001;
176 //Should be changed as the domain of small lines could be changed.
177 Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox(own,
178 Middle.X() - SmallDist,
179 Middle.Y() - SmallDist,
180 Middle.Z() - SmallDist,
181 Middle.X() + SmallDist,
182 Middle.Y() + SmallDist,
183 Middle.Z() + SmallDist );
184 aSelection->Add(box);
185}
186
187//=================================================================
188//function : ComputeRadiusPosition
189//purpose :
190//=================================================================
787ff240 191void PrsDim_EqualRadiusRelation::ComputeRadiusPosition()
7fd59977 192{
193 if (myAutomaticPosition ||
194 myFirstCenter.Distance(myPosition) < Precision::Confusion() ||
195 mySecondCenter.Distance(myPosition) < Precision::Confusion())
196 return;
197
198 gp_Pnt aPosition;
199
200 //project myPosition to the plane of constraint
201 GeomAPI_ProjectPointOnSurf aProj(myPosition, myPlane);
202 aPosition = aProj.NearestPoint();
203
204 Standard_Real aDist1 = myFirstPoint.Distance(aPosition);
205 Standard_Real aDist2 = mySecondPoint.Distance(aPosition);
206
207 if(aDist1<aDist2)
208 {
209 Standard_Real Rad1 = myFirstPoint.Distance(myFirstCenter);
210 const gp_Dir aNewDir1(aPosition.XYZ() - myFirstCenter.XYZ());
211 const gp_Vec aTVec (aNewDir1.XYZ()*Rad1);
212 myFirstPoint = myFirstCenter.Translated(aTVec);
213 }
214 else {
215 Standard_Real Rad2 = mySecondPoint.Distance(mySecondCenter);
216 const gp_Dir aNewDir2(aPosition.XYZ() - mySecondCenter.XYZ());
217 gp_Vec aTVec (aNewDir2.XYZ()*Rad2);
218 mySecondPoint = mySecondCenter.Translated(aTVec);
219 }
220
221}
222