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