0024428: Implementation of LGPL license
[occt.git] / src / AIS / AIS_EqualDistanceRelation.cxx
1 // Created on: 1998-01-24
2 // Created by: Julia GERASIMOVA
3 // Copyright (c) 1998-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 <Standard_NotImplemented.hxx>
18
19 #include <AIS_EqualDistanceRelation.ixx>
20
21 #include <DsgPrs_EqualDistancePresentation.hxx>
22 #include <AIS_LengthDimension.hxx>
23 #include <gp_Lin.hxx>
24 #include <DsgPrs_EqualDistancePresentation.hxx>
25 #include <SelectMgr_EntityOwner.hxx>
26 #include <Select3D_SensitiveSegment.hxx>
27 #include <Select3D_SensitiveBox.hxx>
28 #include <Select3D_SensitiveCircle.hxx>
29 #include <Precision.hxx>
30 #include <BRepAdaptor_Curve.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <BRep_Tool.hxx>
36 #include <GeomAPI_ProjectPointOnCurve.hxx>
37 #include <GeomAPI_ProjectPointOnSurf.hxx>
38 #include <Prs3d_DimensionAspect.hxx>
39 #include <Prs3d_ArrowAspect.hxx>
40 #include <Geom_Line.hxx>
41 #include <Geom_Circle.hxx>
42 #include <AIS_Drawer.hxx>
43 #include <ElCLib.hxx>
44 //=======================================================================
45 //function : AIS_EqualDistanceRelation
46 //purpose  : 
47 //=======================================================================
48
49 AIS_EqualDistanceRelation::AIS_EqualDistanceRelation( const TopoDS_Shape& aShape1,
50                                                       const TopoDS_Shape& aShape2,
51                                                       const TopoDS_Shape& aShape3,
52                                                       const TopoDS_Shape& aShape4,
53                                                       const Handle( Geom_Plane )& aPlane )
54      :AIS_Relation()
55 {
56   myFShape = aShape1;
57   mySShape = aShape2;
58   myShape3 = aShape3;
59   myShape4 = aShape4;
60   myPlane  = aPlane;
61
62   //Temporary
63   myArrowSize = 3.0; //set the concrete value
64   mySymbolPrs = DsgPrs_AS_BOTHAR;
65 }
66
67 //=======================================================================
68 //function : Compute
69 //purpose  : 
70 //=======================================================================
71
72 void AIS_EqualDistanceRelation::Compute( const Handle( PrsMgr_PresentationManager3d )&,
73                                          const Handle( Prs3d_Presentation )& aPresentation,
74                                          const Standard_Integer ) 
75 {
76
77   aPresentation->Clear();
78
79   gp_Pnt Position12 =  myPosition, Position34 = myPosition;
80
81   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
82   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
83   arr->SetLength(myArrowSize);
84 // -- ota -- begin
85   if (!myAutomaticPosition ){
86     gp_Pnt aMiddle12 ((myPoint1.XYZ() + myPoint2.XYZ())*0.5);
87     gp_Pnt aMiddle34 ((myPoint3.XYZ() + myPoint4.XYZ())*0.5);
88
89     if (myPosition.Distance(aMiddle12) > myPosition.Distance(aMiddle34))
90       Position12.SetXYZ((myPoint1.XYZ() + myPoint2.XYZ())*0.5);
91     else        
92       Position34.SetXYZ((myPoint3.XYZ() + myPoint4.XYZ())*0.5);
93
94   }
95   
96   if (myFShape.ShapeType() == TopAbs_EDGE && mySShape.ShapeType() == TopAbs_EDGE)
97     AIS_EqualDistanceRelation::ComputeTwoEdgesLength(aPresentation,
98                                                      myDrawer,
99                                                      myArrowSize,
100                                                      TopoDS::Edge(myFShape),
101                                                      TopoDS::Edge(mySShape),
102                                                      myPlane,
103                                                      myAutomaticPosition,
104                                                      myIsSetBndBox,
105                                                      myBndBox,
106                                                      Position12,
107                                                      myAttachPoint1,
108                                                      myAttachPoint2,
109                                                      myPoint1,
110                                                      myPoint2,
111                                                      mySymbolPrs );
112          
113   
114   else if (myFShape.ShapeType() == TopAbs_VERTEX && mySShape.ShapeType() == TopAbs_VERTEX)
115     AIS_EqualDistanceRelation::ComputeTwoVerticesLength( aPresentation,
116                                                         myDrawer,
117                                                         myArrowSize,
118                                                         TopoDS::Vertex(myFShape),
119                                                         TopoDS::Vertex(mySShape),
120                                                         myPlane,
121                                                         myAutomaticPosition,
122                                                         myIsSetBndBox,
123                                                         myBndBox,
124                                                         AIS_TOD_Unknown,
125                                                         Position12,
126                                                         myAttachPoint1,
127                                                         myAttachPoint2,
128                                                         myPoint1,
129                                                         myPoint2,
130                                                         mySymbolPrs );
131   else 
132     AIS_EqualDistanceRelation::ComputeOneEdgeOneVertexLength( aPresentation,
133                                                              myDrawer,
134                                                              myArrowSize,
135                                                              myFShape,
136                                                              mySShape,
137                                                              myPlane,
138                                                              myAutomaticPosition,
139                                                              myIsSetBndBox,
140                                                              myBndBox,
141                                                              Position12,
142                                                              myAttachPoint1,
143                                                              myAttachPoint2,
144                                                              myPoint1,
145                                                              myPoint2,
146                                                              mySymbolPrs );
147
148   if (myShape3.ShapeType() == TopAbs_EDGE && myShape4.ShapeType() == TopAbs_EDGE)
149     AIS_EqualDistanceRelation::ComputeTwoEdgesLength(aPresentation,
150                                                      myDrawer,
151                                                      myArrowSize,
152                                                      TopoDS::Edge(myShape3),
153                                                      TopoDS::Edge(myShape4),
154                                                      myPlane,
155                                                      myAutomaticPosition,
156                                                      myIsSetBndBox,
157                                                      myBndBox,
158                                                      Position34,
159                                                      myAttachPoint3,
160                                                      myAttachPoint4,
161                                                      myPoint3,
162                                                      myPoint4,
163                                                      mySymbolPrs );
164   
165   else if (myShape3.ShapeType() == TopAbs_VERTEX && myShape4.ShapeType() == TopAbs_VERTEX)
166     AIS_EqualDistanceRelation::ComputeTwoVerticesLength( aPresentation,
167                                                         myDrawer,
168                                                         myArrowSize,
169                                                         TopoDS::Vertex(myShape3),
170                                                         TopoDS::Vertex(myShape4),
171                                                         myPlane,
172                                                         myAutomaticPosition,
173                                                         myIsSetBndBox,
174                                                         myBndBox,
175                                                         AIS_TOD_Unknown,
176                                                         Position34,
177                                                         myAttachPoint3,
178                                                         myAttachPoint4,
179                                                         myPoint3,
180                                                         myPoint4,
181                                                         mySymbolPrs );
182    
183   else
184     AIS_EqualDistanceRelation::ComputeOneEdgeOneVertexLength( aPresentation,
185                                                              myDrawer,
186                                                              myArrowSize,
187                                                              myShape3,
188                                                              myShape4,
189                                                              myPlane,
190                                                              myAutomaticPosition,
191                                                              myIsSetBndBox,
192                                                              myBndBox,
193                                                              Position34,
194                                                              myAttachPoint3,
195                                                              myAttachPoint4,
196                                                              myPoint3,
197                                                              myPoint4,
198                                                              mySymbolPrs );
199   
200   DsgPrs_EqualDistancePresentation::Add( aPresentation, myDrawer, 
201                                         myPoint1, myPoint2, myPoint3, myPoint4, myPlane );
202 }
203
204
205 //=======================================================================
206 //function : Compute
207 //purpose  : to avoid warning at compilation (SUN)
208 //=======================================================================
209
210 void AIS_EqualDistanceRelation::Compute( const Handle( Prs3d_Projector )& /*aProjector*/,
211                                          const Handle( Prs3d_Presentation )& /*aPresentation*/) 
212 {
213 // Standard_NotImplemented::Raise("AIS_EqualDistanceRelation::Compute( const Handle( Prs3d_Projector )&,
214  // const Handle( Prs3d_Presentation )& )");
215 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ; 
216 }
217
218 //=======================================================================
219 //function : Compute
220 //purpose  : to avoid warning at compilation (SUN)
221 //=======================================================================
222 void AIS_EqualDistanceRelation::Compute(const Handle(Prs3d_Projector)& aProjector, 
223                                   const Handle(Geom_Transformation)& aTransformation,
224                                   const Handle(Prs3d_Presentation)& aPresentation)
225 {
226 // Standard_NotImplemented::Raise("AIS_EqualDistanceRelation::Compute(const Handle(Prs3d_Projector)&,
227 //  const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
228  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ; 
229 }
230
231 //=======================================================================
232 //function : ComputeSelection
233 //purpose  : 
234 //=======================================================================
235
236 void AIS_EqualDistanceRelation::ComputeSelection( const Handle( SelectMgr_Selection )& aSelection,
237                                                   const Standard_Integer ) 
238 {
239   Handle( SelectMgr_EntityOwner ) own = new SelectMgr_EntityOwner( this, 7 );
240   Handle( Select3D_SensitiveSegment ) seg;
241
242   seg = new Select3D_SensitiveSegment( own, myPoint1, myPoint2 );
243   aSelection->Add( seg );
244
245   seg = new Select3D_SensitiveSegment( own, myPoint3, myPoint4 );
246   aSelection->Add( seg );
247
248   // Line between two middles
249   gp_Pnt Middle12( (myPoint1.XYZ() + myPoint2.XYZ()) * 0.5 ), 
250          Middle34( (myPoint3.XYZ() + myPoint4.XYZ()) *0.5 );
251   seg = new Select3D_SensitiveSegment( own, Middle12, Middle34 );
252   aSelection->Add( seg );
253
254   gp_Pnt Middle((Middle12.XYZ() +  Middle34.XYZ())*0.5);
255   Standard_Real SmallDist = .001;
256   Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
257                                                                   Middle.X() - SmallDist,
258                                                                   Middle.Y() - SmallDist,
259                                                                   Middle.Z() - SmallDist,
260                                                                   Middle.X() + SmallDist,
261                                                                   Middle.Y() + SmallDist,
262                                                                   Middle.Z() + SmallDist );
263   aSelection->Add(box);
264
265   if (myFShape.ShapeType() == TopAbs_EDGE){
266     BRepAdaptor_Curve aCurve(TopoDS::Edge(myFShape));
267     if (aCurve.GetType() == GeomAbs_Line){
268         //add sensetive element - line
269       seg = new Select3D_SensitiveSegment( own, myAttachPoint1, myPoint1);
270       aSelection->Add( seg );
271     }
272     else if (aCurve.GetType() == GeomAbs_Circle){
273       Handle(Geom_Circle) aCircle = Handle(Geom_Circle)::DownCast(aCurve.Curve().Curve());
274       Standard_Real FirstPar = ElCLib::Parameter(aCircle->Circ(), myAttachPoint1),
275                     LastPar  = ElCLib::Parameter(aCircle->Circ(), myPoint1);
276       if (LastPar < FirstPar ) LastPar+=M_PI*2;
277       //add sensetive arc
278       Handle(Select3D_SensitiveCircle) circ = 
279         new Select3D_SensitiveCircle( own, aCircle,  FirstPar, LastPar);
280       aSelection->Add( circ );
281     }
282   }
283   else {
284       seg = new Select3D_SensitiveSegment( own, myAttachPoint1, myPoint1);
285       aSelection->Add( seg );
286     }
287   
288   if (mySShape.ShapeType() == TopAbs_EDGE){
289     BRepAdaptor_Curve aCurve(TopoDS::Edge(mySShape));
290     if (aCurve.GetType() == GeomAbs_Line) {
291       //add sensetive element - line
292       seg = new Select3D_SensitiveSegment( own, myAttachPoint2, myPoint2);
293       aSelection->Add( seg );
294     }
295     else if (aCurve.GetType() == GeomAbs_Circle){
296       Handle(Geom_Circle) aCircle = Handle(Geom_Circle)::DownCast(aCurve.Curve().Curve());
297       Standard_Real FirstPar = ElCLib::Parameter(aCircle->Circ(), myAttachPoint2),
298       LastPar  = ElCLib::Parameter(aCircle->Circ(), myPoint2);
299       if (LastPar < FirstPar ) LastPar+=M_PI*2;
300       //add sensetive arc
301       Handle(Select3D_SensitiveCircle) circ = 
302         new Select3D_SensitiveCircle( own,aCircle,  FirstPar, LastPar);
303       aSelection->Add( circ );
304     }
305   }
306   else {
307     seg = new Select3D_SensitiveSegment( own, myAttachPoint2, myPoint2);
308     aSelection->Add( seg );
309   }
310     
311   if (myShape3.ShapeType() == TopAbs_EDGE){
312     BRepAdaptor_Curve aCurve(TopoDS::Edge(myShape3));
313     if (aCurve.GetType() == GeomAbs_Line) {
314       //add sensetive element - line
315       seg = new Select3D_SensitiveSegment( own, myAttachPoint3, myPoint3);
316       aSelection->Add( seg );
317     }
318     else if (aCurve.GetType() == GeomAbs_Circle){
319       Handle(Geom_Circle) aCircle = Handle(Geom_Circle)::DownCast(aCurve.Curve().Curve());
320       Standard_Real FirstPar = ElCLib::Parameter(aCircle->Circ(), myAttachPoint3),
321       LastPar  = ElCLib::Parameter(aCircle->Circ(), myPoint3);
322       if (LastPar < FirstPar ) LastPar+=M_PI*2;
323       Handle(Select3D_SensitiveCircle) circ = 
324         new Select3D_SensitiveCircle( own, aCircle,  FirstPar, LastPar);
325       aSelection->Add( circ );
326     }
327     else {
328       seg = new Select3D_SensitiveSegment( own, myAttachPoint3, myPoint3);
329       aSelection->Add( seg );
330     }
331   }
332   else {
333     seg = new Select3D_SensitiveSegment( own, myAttachPoint3, myPoint3);
334     aSelection->Add( seg );
335   }
336
337   if (myShape4.ShapeType() == TopAbs_EDGE){
338     BRepAdaptor_Curve aCurve(TopoDS::Edge(myShape4));
339     if (aCurve.GetType() == GeomAbs_Line) {
340       //add sensetive element - line
341       seg = new Select3D_SensitiveSegment( own, myAttachPoint4, myPoint4);
342       aSelection->Add( seg );
343     }
344     else if (aCurve.GetType() == GeomAbs_Circle){
345       Handle(Geom_Circle) aCircle = Handle(Geom_Circle)::DownCast(aCurve.Curve().Curve());
346       Standard_Real FirstPar = ElCLib::Parameter(aCircle->Circ(), myAttachPoint4),
347       LastPar  = ElCLib::Parameter(aCircle->Circ(), myPoint4);
348       if (LastPar < FirstPar ) LastPar+=M_PI*2;
349       //add sensetive arc
350       Handle(Select3D_SensitiveCircle) circ = 
351         new Select3D_SensitiveCircle( own,aCircle,  FirstPar, LastPar);
352       aSelection->Add( circ );
353     }
354   }
355   else {
356     seg = new Select3D_SensitiveSegment( own, myAttachPoint4, myPoint4);
357     aSelection->Add( seg );
358   }
359 }
360
361 //=======================================================================
362 //function : ComputeTwoEdgesLength
363 //purpose  : 
364 //=======================================================================
365 void AIS_EqualDistanceRelation::ComputeTwoEdgesLength( const Handle( Prs3d_Presentation )& aPresentation,
366                                                       const Handle( AIS_Drawer )& aDrawer,
367                                                       const Standard_Real ArrowSize,
368                                                       const TopoDS_Edge & FirstEdge,
369                                                       const TopoDS_Edge & SecondEdge,
370                                                       const Handle( Geom_Plane )& Plane,
371                                                       const Standard_Boolean AutomaticPos,
372                                                       const Standard_Boolean IsSetBndBox,
373                                                       const Bnd_Box & BndBox,
374                                                       gp_Pnt& Position,
375                                                       gp_Pnt& FirstAttach,
376                                                       gp_Pnt& SecondAttach,
377                                                       gp_Pnt& FirstExtreme,
378                                                       gp_Pnt& SecondExtreme, 
379                                                       DsgPrs_ArrowSide & SymbolPrs )
380
381   gp_Dir DirAttach;
382   BRepAdaptor_Curve cu1( FirstEdge );
383   BRepAdaptor_Curve cu2( SecondEdge );
384   
385   // 3d lines
386   Handle(Geom_Curve) geom1,geom2;
387   gp_Pnt ptat11,ptat12,ptat21,ptat22;
388   
389   Standard_Boolean isInfinite1(Standard_False),isInfinite2(Standard_False);
390   Handle(Geom_Curve) extCurv;
391   Standard_Real arrsize = ArrowSize;// size
392   Standard_Real Val=0.;
393   Standard_Boolean isInPlane1, isInPlane2;
394
395   if(!AIS::ComputeGeometry(FirstEdge,geom1, ptat11, ptat12,extCurv,isInfinite1,isInPlane1, Plane ))
396     return;
397   if(!AIS::ComputeGeometry(SecondEdge, geom2, ptat21, ptat22, extCurv, isInfinite2,isInPlane2, Plane))
398     return;
399   
400   aPresentation->SetInfiniteState(isInfinite1 || isInfinite2);
401   
402   if (cu1.GetType() == GeomAbs_Line && cu2.GetType() == GeomAbs_Line) 
403     {
404       const Handle(Geom_Line)& geom_lin1 = (Handle(Geom_Line)&) geom1;
405       const Handle(Geom_Line)& geom_lin2 = (Handle(Geom_Line)&) geom2;
406       const gp_Lin& l1 = geom_lin1->Lin();
407       const gp_Lin& l2 = geom_lin2->Lin();
408       
409       //Get Val value
410       Val = l1.Distance( l2 );
411       
412       DirAttach = l1.Direction();
413       
414       if (AutomaticPos) {
415         // compute position of offset point
416         gp_Pnt curpos;
417         Standard_Real par1=0., par2=0.;
418         if(!(isInfinite1 || isInfinite2))
419           {
420             par1 = ElCLib::Parameter(l1,ptat11);
421             par2 = ElCLib::Parameter(l1,ptat21);
422             if (par1 <par2){//project ptat11 on l2
423               gp_Pnt p2 = ElCLib::Value(ElCLib::Parameter(l2,ptat11),l2);
424               curpos.SetXYZ((ptat11.XYZ()+p2.XYZ())*0.5);
425             }
426             else {//project ptat21 on l1
427               gp_Pnt p2 = ElCLib::Value(par2, l1);
428               curpos.SetXYZ((ptat21.XYZ()+p2.XYZ())*0.5);
429             }
430           }
431         else if (!isInfinite1){
432           par2 = ElCLib::Parameter(l1,ptat21);
433           gp_Pnt p2 = ElCLib::Value(par2,l1);
434           curpos.SetXYZ((ptat21.XYZ()+p2.XYZ())/2.);
435         }
436         else if (!isInfinite2) {
437           gp_Pnt p2 = ElCLib::Value(ElCLib::Parameter(l2,ptat11),l2);
438           curpos.SetXYZ((ptat11.XYZ()+p2.XYZ())*0.5);
439         }
440         else   
441           curpos.SetXYZ((l1.Location().XYZ()+l2.Location().XYZ())*0.5);
442  
443     // compute  offset
444     gp_Vec offset(DirAttach);
445     offset = offset*ArrowSize*(-10.);
446     curpos.Translate(offset);
447     Position = curpos;
448   }
449   else {    // project point on the plane
450     Position = AIS::ProjectPointOnPlane( Position, Plane->Pln() );
451   }
452
453   // find attach points
454   if (!isInfinite1) {
455     if (Position.Distance(ptat11) > Position.Distance(ptat12)) FirstAttach = ptat12;
456     else FirstAttach = ptat11;
457   }
458   else {
459     FirstAttach = ElCLib::Value(ElCLib::Parameter(l1,Position),l1);
460   }
461   
462   if (!isInfinite2) {
463     if (Position.Distance(ptat21) > Position.Distance(ptat22)) SecondAttach = ptat22;
464     else SecondAttach = ptat21;
465   }
466   else {
467     SecondAttach = ElCLib::Value(ElCLib::Parameter(l2,Position),l2);
468   }
469
470   Standard_Real confusion(Precision::Confusion());
471   if (arrsize < confusion) arrsize = Val*0.1;
472   if (Abs(Val) <= confusion) {arrsize = 0.;}
473
474   Handle(Prs3d_DimensionAspect) la = aDrawer->DimensionAspect();
475   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();  
476   arr->SetLength(arrsize);
477   arr = la->ArrowAspect();
478   arr->SetLength(arrsize);
479
480   if (AutomaticPos && IsSetBndBox)
481     Position = AIS::TranslatePointToBound( Position, DirAttach, BndBox );
482  
483    DsgPrs_EqualDistancePresentation::AddInterval(aPresentation,
484                                                 aDrawer,
485                                                 FirstAttach,
486                                                 SecondAttach,
487                                                 DirAttach,
488                                                 Position,
489                                                 SymbolPrs,
490                                                 FirstExtreme,
491                                                 SecondExtreme);
492                                                 
493       
494 }
495   if (cu1.GetType() == GeomAbs_Circle && cu2.GetType() == GeomAbs_Circle){
496     //Get first and last points of circles
497     const Handle(Geom_Circle)& aCir1 = Handle(Geom_Circle)::DownCast(geom1);
498     const Handle(Geom_Circle)& aCir2 = Handle(Geom_Circle)::DownCast(geom2);
499     gp_Circ aCirc1 = aCir1->Circ();
500     gp_Circ aCirc2 = aCir2->Circ();
501
502     //To avoid circles with different orientaion
503     Standard_Real aTol = Precision::Confusion();
504     if(aCirc2.Axis().IsOpposite(aCirc1.Axis(), aTol) ||
505        aCirc2.XAxis().IsOpposite(aCirc1.XAxis(), aTol) || 
506        aCirc2.YAxis().IsOpposite(aCirc1.YAxis(), aTol) )
507       {
508         aCirc2.SetPosition(aCirc1.Position());
509         aCirc2.SetAxis(aCirc1.Axis());
510       }
511     
512     if (AutomaticPos){ 
513       Standard_Real par1 = 0, par2 = 0;
514       gp_Pln aPln =  Plane->Pln();
515       //Project ptat12 and ptat22 on constraint plane
516       gp_Pnt PrPnt12 = AIS::ProjectPointOnPlane(ptat12, aPln);
517       gp_Pnt PrPnt22 = AIS::ProjectPointOnPlane(ptat22, aPln);
518       //Project circles center on constraint plane
519       gp_Pnt PrCenter = AIS::ProjectPointOnPlane(aCirc1.Location(), aPln);
520
521       gp_Dir XDir = aPln.XAxis().Direction();
522       gp_Dir YDir = aPln.YAxis().Direction();
523       
524       
525       if (PrPnt12.Distance(PrCenter) >Precision::Confusion())
526         {
527           gp_Dir aDir1(PrPnt12.XYZ() - PrCenter.XYZ());
528           Standard_Real anAngle = aDir1.Angle(XDir); //Get the angle in range [0, M_PI]
529           if (aDir1.Dot(YDir) < 0)
530             anAngle = 2 * M_PI - anAngle;
531           par1 = anAngle;
532         }
533       
534       if (PrPnt22.Distance(PrCenter) >Precision::Confusion())
535         {
536           gp_Dir aDir2(PrPnt22.XYZ() - PrCenter.XYZ());
537           Standard_Real anAngle = aDir2.Angle(XDir); //Get the angle in range [0, M_PI]
538           if (aDir2.Dot(YDir) < 0)
539             anAngle = 2 * M_PI - anAngle;
540           par2 = anAngle;
541         }
542       
543       
544       if(par1 > par2 ){
545         FirstExtreme = ptat12;
546         Standard_Real aPar1 = ElCLib::Parameter(aCirc2, ptat12);
547         SecondExtreme = ElCLib::Value(aPar1, aCirc2);
548       }
549       else {
550         Standard_Real aPar2 =  ElCLib::Parameter(aCirc1, ptat22);
551         FirstExtreme = ElCLib::Value(aPar2, aCirc1);
552         SecondExtreme = ptat22;
553       }
554     }
555     else {
556       Standard_Real pospar = ElCLib::Parameter(aCirc1, Position);
557       FirstExtreme  =  ElCLib::Value(pospar, aCirc1);
558       pospar = ElCLib::Parameter(aCirc2, Position);
559       SecondExtreme =  ElCLib::Value(pospar, aCirc2);
560     }
561
562     DsgPrs_EqualDistancePresentation::AddIntervalBetweenTwoArcs(aPresentation,
563                                                                 aDrawer,
564                                                                 aCirc1,
565                                                                 aCirc2,
566                                                                 ptat12,
567                                                                 FirstExtreme, 
568                                                                 ptat22,
569                                                                 SecondExtreme,
570                                                                 SymbolPrs);
571     
572     FirstAttach = ptat12; SecondAttach = ptat22; //assign attach points
573     Position.SetXYZ( (FirstAttach.XYZ() + SecondAttach.XYZ())*0.5);
574   }
575
576   if (arrsize < Precision::Confusion()) arrsize = Val*0.1;
577   if (Abs(Val) <=  Precision::Confusion()) {arrsize = 0.;}
578
579 //  gp_Pnt pf, pl;
580   if (!isInPlane1) {
581     AIS::ComputeProjEdgePresentation( aPresentation, aDrawer, FirstEdge, geom1, ptat11, ptat12);
582   }
583   if(!isInPlane2) {
584     AIS::ComputeProjEdgePresentation( aPresentation, aDrawer, SecondEdge, geom2, ptat21, ptat22);
585   }
586 }
587
588 //=======================================================================
589 //function : ComputeTwoVerticesLength
590 //purpose  : 
591 //=======================================================================
592
593 void AIS_EqualDistanceRelation::ComputeTwoVerticesLength( const Handle( Prs3d_Presentation )& aPresentation,
594                                                          const Handle( AIS_Drawer )& aDrawer,
595                                                          const Standard_Real ArrowSize,
596                                                          const TopoDS_Vertex& FirstVertex,
597                                                          const TopoDS_Vertex& SecondVertex,
598                                                          const Handle( Geom_Plane )& Plane,
599                                                          const Standard_Boolean AutomaticPos,
600                                                          const Standard_Boolean IsSetBndBox,
601                                                          const Bnd_Box& BndBox,
602                                                          const AIS_TypeOfDist TypeDist,
603                                                          gp_Pnt& Position,
604                                                          gp_Pnt& FirstAttach,
605                                                          gp_Pnt& SecondAttach,
606                                                          gp_Pnt& FirstExtreme,
607                                                          gp_Pnt& SecondExtreme, 
608                                                          DsgPrs_ArrowSide& SymbolPrs )
609 {
610   Standard_Boolean isOnPlane1, isOnPlane2;
611   gp_Dir DirAttach;
612   AIS::ComputeGeometry( FirstVertex, FirstAttach, Plane, isOnPlane1);
613   AIS::ComputeGeometry( SecondVertex, SecondAttach, Plane, isOnPlane2);
614
615   //Computation of Val
616   Standard_Real Val = FirstAttach.Distance( SecondAttach );
617   Standard_Real confusion(Precision::Confusion());
618   Standard_Boolean samePoint(FirstAttach.IsEqual(SecondAttach,confusion));
619
620   if (TypeDist == AIS_TOD_Vertical) DirAttach =  Plane->Pln().XAxis().Direction();
621   else if (TypeDist == AIS_TOD_Horizontal) DirAttach =  Plane->Pln().YAxis().Direction();
622   else {
623     if (!samePoint) {
624       DirAttach.SetXYZ(SecondAttach.XYZ() - FirstAttach.XYZ());
625       DirAttach.Rotate(Plane->Pln().Axis(),M_PI/2.);
626     }
627   }
628   
629   // size
630   Standard_Real arrsize = ArrowSize;
631   if (Abs(Val) <= confusion) arrsize =0.;
632
633   if (AutomaticPos) {
634    if (!samePoint) {
635      gp_Pnt curpos((FirstAttach.XYZ()+SecondAttach.XYZ())*0.5);
636      // make offset of curpos
637      gp_Vec offset(DirAttach);
638      offset = offset*ArrowSize*(-10.);
639      curpos.Translate(offset);
640      Position = curpos;
641    }
642    else {  
643      const gp_Dir& aDir = Plane->Pln().Axis().Direction();
644      gp_Vec aVec (aDir.XYZ()*10*ArrowSize);
645      //Position = gp_Pnt(FirstAttach.XYZ()+gp_XYZ(1.,1.,1.)); // not correct
646      Position = FirstAttach.Translated(aVec);
647      Position = AIS::ProjectPointOnPlane( Position, Plane->Pln() );//not needed really
648      DirAttach.SetXYZ(Position.XYZ() - FirstAttach.XYZ());
649    }
650   }
651   else {   
652     Position = AIS::ProjectPointOnPlane( Position, Plane->Pln() );
653   }
654
655  
656   Handle(Prs3d_DimensionAspect) la = aDrawer->DimensionAspect();
657   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();  
658   arr->SetLength(ArrowSize);
659   arr = la->ArrowAspect();
660   arr->SetLength(ArrowSize);
661
662   if (AutomaticPos && IsSetBndBox)
663     Position = AIS::TranslatePointToBound( Position, DirAttach, BndBox );
664
665   DsgPrs_EqualDistancePresentation::AddInterval(aPresentation,
666                                                 aDrawer,
667                                                 FirstAttach,
668                                                 SecondAttach,
669                                                 DirAttach,
670                                                 Position,
671                                                 SymbolPrs,
672                                                 FirstExtreme, //returned
673                                                 SecondExtreme); //returned
674
675   // Compute projection
676   if ( !isOnPlane1)
677     AIS::ComputeProjVertexPresentation(aPresentation, aDrawer, FirstVertex, FirstAttach);
678   if ( !isOnPlane2)
679     AIS::ComputeProjVertexPresentation(aPresentation, aDrawer, SecondVertex, SecondAttach);
680
681
682
683
684 //=======================================================================
685 //function : ComputeOneEdgeOneVertexLength
686 //purpose  : 
687 //=======================================================================
688
689 void AIS_EqualDistanceRelation::ComputeOneEdgeOneVertexLength( const Handle( Prs3d_Presentation )& aPresentation,
690                                                               const Handle( AIS_Drawer )& aDrawer,
691                                                               const Standard_Real ArrowSize,
692                                                               const TopoDS_Shape & FirstShape,
693                                                               const TopoDS_Shape & SecondShape,
694                                                               const Handle( Geom_Plane )& Plane,
695                                                               const Standard_Boolean AutomaticPos,
696                                                               const Standard_Boolean IsSetBndBox,
697                                                               const Bnd_Box & BndBox,
698                                                               gp_Pnt & Position,
699                                                               gp_Pnt & FirstAttach,
700                                                               gp_Pnt & SecondAttach,
701                                                               gp_Pnt& FirstExtreme,
702                                                               gp_Pnt& SecondExtreme, 
703                                                               DsgPrs_ArrowSide & SymbolPrs )
704 {
705   TopoDS_Vertex thevertex;
706   TopoDS_Edge theedge;
707   
708   gp_Pnt ptonedge1,ptonedge2;
709   Handle(Geom_Curve) aCurve;
710   Handle(Geom_Curve) extCurv;
711   Standard_Boolean isInfinite;
712   Standard_Real Val;
713   Standard_Boolean isOnPlanEdge, isOnPlanVertex;
714   Standard_Integer edgenum ;
715
716   if (FirstShape.ShapeType() == TopAbs_VERTEX) {  
717     thevertex = TopoDS::Vertex(FirstShape);
718     theedge   = TopoDS::Edge(SecondShape);
719     edgenum   = 2; //edge is the second shape
720   }
721   else {
722     thevertex = TopoDS::Vertex(SecondShape);
723     theedge   = TopoDS::Edge(FirstShape);
724     edgenum   = 1;//edge is the first shape
725   }
726   if (!AIS::ComputeGeometry(theedge,aCurve,ptonedge1,ptonedge2,extCurv,isInfinite,isOnPlanEdge,Plane))
727     return;
728   aPresentation->SetInfiniteState(isInfinite);
729   AIS::ComputeGeometry(thevertex, FirstAttach, Plane, isOnPlanVertex);
730
731   if ( aCurve->IsInstance(STANDARD_TYPE(Geom_Line)) ) 
732     {
733   const Handle(Geom_Line)& geom_lin = (Handle(Geom_Line)&) aCurve;
734   const gp_Lin& l = geom_lin->Lin();
735
736   // computation of Val
737   Val = l.Distance( FirstAttach );
738
739   gp_Dir DirAttach = l.Direction();
740   // size
741   Standard_Real arrsize = ArrowSize;
742   if (Abs(Val) <= Precision::Confusion()) {arrsize = 0.;}
743
744   if (AutomaticPos) {
745     gp_Pnt p = ElCLib::Value(ElCLib::Parameter(l,FirstAttach),l);
746     gp_Pnt curpos((FirstAttach.XYZ()+p.XYZ())*0.5);
747     // make offset 
748     gp_Vec offset(DirAttach);
749     offset = offset*ArrowSize*(-10.);
750     curpos.Translate(offset);
751     Position = curpos;
752   }
753   else { // project point on the plane
754     Position = AIS::ProjectPointOnPlane( Position, Plane->Pln() );
755   }
756   
757   if (!isInfinite) {
758     if (Position.Distance(ptonedge1) > Position.Distance(ptonedge2)) SecondAttach = ptonedge2;
759     else SecondAttach = ptonedge1;
760   }
761   else {
762     SecondAttach = ElCLib::Value(ElCLib::Parameter(l,Position),l);
763   }
764   
765   Handle(Prs3d_DimensionAspect) la = aDrawer->DimensionAspect();
766   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();  
767   arr->SetLength(arrsize);
768   arr = la->ArrowAspect();
769   arr->SetLength(arrsize);
770
771   if (AutomaticPos && IsSetBndBox)
772     Position = AIS::TranslatePointToBound( Position, DirAttach, BndBox );
773   DsgPrs_EqualDistancePresentation::AddInterval(aPresentation,
774                                                 aDrawer,
775                                                 FirstAttach,
776                                                 SecondAttach,
777                                                 DirAttach,
778                                                 Position,
779                                                 SymbolPrs,
780                                                 FirstExtreme,
781                                                 SecondExtreme);
782   
783 }
784   if (aCurve->IsInstance(STANDARD_TYPE(Geom_Circle))){
785     gp_Circ aCirc1 = (Handle(Geom_Circle)::DownCast(aCurve))->Circ();
786     gp_Circ aCirc2(aCirc1); aCirc2.SetRadius(0); //create the second formal circle
787     if(AutomaticPos)
788       {
789         SecondAttach = ptonedge2; //a vertex
790         Position.SetXYZ((SecondAttach.XYZ() + aCirc1.Location().XYZ())*0.5);
791       }
792     else {
793       Standard_Real aPar =  ElCLib::Parameter(aCirc1, Position);
794       SecondAttach =  ElCLib::Value(aPar, aCirc1);
795     }
796
797     Handle(Geom_Circle) aCurve2 = new Geom_Circle(aCirc2);
798     DsgPrs_EqualDistancePresentation::AddIntervalBetweenTwoArcs(aPresentation,
799                                                                 aDrawer,
800                                                                 aCirc1, //circle or arc
801                                                                 aCirc2, //really vertex
802                                                                 ptonedge2, //last point of aCirc1
803                                                                 SecondAttach,
804                                                                 FirstAttach, //vertex really
805                                                                 FirstAttach, 
806                                                                 SymbolPrs);
807     
808     //Assign arc points 
809     if (edgenum == 1){
810       FirstExtreme = SecondAttach; SecondExtreme = FirstAttach;
811       SecondAttach = FirstAttach; FirstAttach = ptonedge2; 
812     } else { //vertex is the first shape, circle is sthe last.
813       FirstExtreme = FirstAttach; SecondExtreme = SecondAttach;
814       SecondAttach = ptonedge2;
815     }
816   }
817  
818   // computation of Val
819   Val = FirstAttach.Distance(SecondAttach);
820   
821   //Display the pieces of attached to the curve if it is not 
822   // in the WP
823   if (!isOnPlanEdge) { // add presentation of projection of the edge in WP
824       AIS::ComputeProjEdgePresentation(aPresentation,aDrawer,theedge,aCurve,ptonedge1,ptonedge2);
825       }
826   if (!isOnPlanVertex) { // add presentation of projection of the vertex in WP 
827     AIS::ComputeProjVertexPresentation(aPresentation,aDrawer,thevertex,FirstAttach);
828     }
829   
830 }
831 // -- ota -- end