summaryrefslogtreecommitdiff
path: root/src/AIS/AIS_RadiusDimension.cxx
blob: 544c0c9b3693c4157e95d03fbd8c6c66fed92b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// File:	AIS_RadiusDimension.cxx
// Created:	Tue Dec  5 15:09:04 1996
// Author:	Jean-Pierre COMBE/Odile Olivier/Serguei Zaritchny
// modified     <SZY> 20-feb-98
//              <VBU> myFShape could be a wire.

#define BUC60915        //GG 05/06/01 Enable to compute the requested arrow size
//                      if any in all dimensions.

#include <Standard_NotImplemented.hxx>

#include <AIS_RadiusDimension.ixx>

#include <AIS_DimensionOwner.hxx>
#include <DsgPrs_RadiusPresentation.hxx>
#include <DsgPrs.hxx>

#include <Geom_Surface.hxx>
#include <Geom_Plane.hxx>
#include <Geom_Circle.hxx>
#include <Geom_Curve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_ToroidalSurface.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom_SurfaceOfLinearExtrusion.hxx>

#include <gp_Circ.hxx>
#include <gp_Pnt.hxx>
#include <gp_Trsf.hxx>
#include <ElCLib.hxx>
#include <GC_MakeCircle.hxx>
#include <gce_MakeLin.hxx>
#include <gce_MakeCirc.hxx>

#include <SelectMgr_EntityOwner.hxx>
#include <Select3D_SensitiveSegment.hxx>

#include <Precision.hxx>

#include <TopExp_Explorer.hxx>
#include <TopAbs_ShapeEnum.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Edge.hxx>
#include <TopLoc_Location.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <Adaptor3d_HCurve.hxx>

#include <Prs3d_AngleAspect.hxx>
#include <Prs3d_ArrowAspect.hxx>
#include <Prs3d_LengthAspect.hxx>
#include <Prs3d_Drawer.hxx>

#include <TCollection_AsciiString.hxx>
#include <TCollection_ExtendedString.hxx>


#include <AIS.hxx>
#include <AIS_Drawer.hxx>
#include <gce_MakeDir.hxx>
#include <Select3D_SensitiveBox.hxx>



//======================================================================
//function : CircleFromPlanarFace
//purpose  : if possible gets circle from planar face
//=======================================================================
static Standard_Boolean CircleFromPlanarFace (const TopoDS_Shape& aFace,
					      Handle(Geom_Curve)& aCurve, 
					      gp_Pnt & ptfirst,
					      gp_Pnt & ptend)
{
  TopExp_Explorer ExploEd (aFace, TopAbs_EDGE);
  for ( ; ExploEd.More(); ExploEd.Next())
    {
      TopoDS_Edge curedge =  TopoDS::Edge (ExploEd.Current());
      if (AIS::ComputeGeometry (curedge, aCurve, ptfirst, ptend))
	if (aCurve->IsInstance (STANDARD_TYPE(Geom_Circle)) && 
	    !Handle(Geom_Circle)::DownCast(aCurve).IsNull())
	  return Standard_True;
    }
  return Standard_False;
}


//=======================================================================
//function : Constructor
//purpose  : 
//=======================================================================
AIS_RadiusDimension::AIS_RadiusDimension(const TopoDS_Shape& aShape, 
					 const Standard_Real aVal, 
					 const TCollection_ExtendedString& aText)
:mydrawFromCenter(Standard_True)
{
  myFShape = aShape;
  myVal = aVal;
  myText = aText;
  mySymbolPrs = DsgPrs_AS_FIRSTPT_LASTAR;
  myAutomaticPosition = Standard_True;

  myArrowSize = myVal / 100.;
  InitFirstShape();
}

//=======================================================================
//function : Constructor
//purpose  :(avec position et texte)
//=======================================================================
AIS_RadiusDimension::AIS_RadiusDimension( const TopoDS_Shape& aShape, 
					  const Standard_Real aVal, 
					  const TCollection_ExtendedString& aText, 
					  const gp_Pnt& aPosition,
					  const DsgPrs_ArrowSide aSymbolPrs, 
					  const Standard_Real anArrowSize )
:mydrawFromCenter(Standard_True)
{
  myFShape = aShape;
  myVal = aVal;
  myText = aText;
  myPosition = aPosition;
  mySymbolPrs = aSymbolPrs;
#ifdef BUC60915
  SetArrowSize( anArrowSize );
#else
  myArrowSize = anArrowSize;
#endif
  myAutomaticPosition = Standard_False;
  InitFirstShape();
}

//=======================================================================
//function : InitFirstShape
//purpose  : 
//=======================================================================
void AIS_RadiusDimension::InitFirstShape()
{
  if (myFShape.ShapeType() == TopAbs_FACE)
    {
      BRepAdaptor_Surface surfAlgo( TopoDS::Face( myFShape ) );
      Standard_Real uFirst, uLast, vFirst, vLast;
      uFirst = surfAlgo.FirstUParameter();
      uLast = surfAlgo.LastUParameter();
      vFirst = surfAlgo.FirstVParameter();
      vLast = surfAlgo.LastVParameter();
      Standard_Real uMoy = (uFirst + uLast)/2;
      Standard_Real vMoy = (vFirst + vLast)/2;
      Handle( Geom_Surface ) surf = surfAlgo.Surface().Surface();
      surf = Handle( Geom_Surface )::DownCast( surf->Transformed( surfAlgo.Trsf() ) );
      Handle( Geom_Curve ) aCurve;

      if (surf->DynamicType() == STANDARD_TYPE(Geom_ToroidalSurface))
	{
	  aCurve = surf->UIso( uMoy );
	  uFirst = vFirst;
	  uLast = vLast;
	}
      else if (surf->DynamicType() == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) 
	{
	  Handle( Adaptor3d_HCurve ) BasisCurve = surfAlgo.BasisCurve();
	  if ( BasisCurve->GetType() == GeomAbs_Circle )
	    aCurve = surf->VIso( vMoy );
	  else return;
	}
      else if (surf->DynamicType() == STANDARD_TYPE(Geom_Plane)) 
	{
	  gp_Pnt FirstPnt, LastPnt;
	  if (CircleFromPlanarFace (TopoDS::Face(myFShape), aCurve, FirstPnt, LastPnt))
//	  if (CircleFromPlanarFace (myFShape, aCurve, FirstPnt, LastPnt))
	    {
	      uFirst = ElCLib::Parameter ((Handle(Geom_Circle)::DownCast( aCurve ))->Circ(), FirstPnt);
	      uLast  = ElCLib::Parameter ((Handle(Geom_Circle)::DownCast( aCurve ))->Circ(), LastPnt);
	    }
	  else return;
	}
      else
	aCurve = surf->VIso( vMoy );
      
      if (aCurve->DynamicType() == STANDARD_TYPE(Geom_Circle))
	myCircle = (Handle( Geom_Circle )::DownCast( aCurve ))->Circ();
      else if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
	Handle(Geom_TrimmedCurve) tCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve); 
	aCurve = tCurve->BasisCurve();
	uFirst = tCurve->FirstParameter();
	uLast  = tCurve->LastParameter();
	if (aCurve->DynamicType() == STANDARD_TYPE(Geom_Circle))
	  myCircle = Handle(Geom_Circle)::DownCast(aCurve)->Circ();//gp_Circ
	else return;
      }
      else if (aCurve->DynamicType() == STANDARD_TYPE(Geom_BSplineCurve)) {
	gp_Pnt P1,P2,P3;
	aCurve->D0(uFirst,P1);
	aCurve->D0((uFirst+uLast)/2,P2);
	aCurve->D0(uLast,P3);
	gce_MakeCirc MakeCirc = gce_MakeCirc(P1,P2,P3);
	myCircle = MakeCirc.Value();
	uFirst = ElCLib::Parameter(myCircle,P1);
	uLast = ElCLib::Parameter(myCircle,P2);
      }

      myFirstPar = uFirst;
      myLastPar  = uLast;
    } // TopAbs_FACE

  else // it is edge or a wire
    {
      TopoDS_Edge anEdge;
      if (myFShape.ShapeType() == TopAbs_WIRE)  {
	TopExp_Explorer exp (myFShape,TopAbs_EDGE);
	if (exp.More())  anEdge = TopoDS::Edge (exp.Current());
      }
      else {
	if ( myFShape.ShapeType() == TopAbs_EDGE) { 
	  anEdge = TopoDS::Edge (myFShape);
	  BRepAdaptor_Curve AdaptedCurve (anEdge);
	  if (!AdaptedCurve.GetType() == GeomAbs_Circle) return;
	  
	  myCircle = AdaptedCurve.Circle();
	  myFirstPar = AdaptedCurve.FirstParameter();
	  myLastPar = AdaptedCurve.LastParameter();
	}
#ifdef DEB
	else {
	  cout << "AIS_RadiusDimension::InitFirstShape ==> myFShape.ShapeType() == " << myFShape.ShapeType()  << endl;
	}
#endif
      }
    } 

  myCenter = myCircle.Location();
  myCircle.SetRadius(myVal);
  while (myFirstPar > 2*PI) myFirstPar -= 2*PI;
  while (myFirstPar < 0.0 ) myFirstPar += 2*PI;
  while (myLastPar  > 2*PI) myLastPar  -= 2*PI;
  while (myLastPar  < 0.0 ) myLastPar  += 2*PI;
  myPlane = new Geom_Plane(gp_Pln(gp_Ax3(myCircle.Position())));
  myFirstLine = gce_MakeLin( myCenter, ElCLib::Value( myFirstPar, myCircle ) );
  myLastLine  = gce_MakeLin( myCenter, ElCLib::Value( myLastPar, myCircle ) );
}

//=======================================================================
//function : SetFirstShape
//purpose  : 
//=======================================================================
void AIS_RadiusDimension::SetFirstShape( const TopoDS_Shape& aFShape )
{
  myFShape = aFShape;
  InitFirstShape();
}

//=======================================================================
//function : Compute
//purpose  : 
//=======================================================================
void AIS_RadiusDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
				  const Handle(Prs3d_Presentation)& aPresentation, 
				  const Standard_Integer /*aMode*/)
{
  aPresentation->Clear();

  ComputeRadius( aPresentation );
}

//=======================================================================
//function : Compute
//purpose  :  to avoid warning
//=======================================================================
void AIS_RadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector, 
				  const Handle(Prs3d_Presentation)& aPresentation)
{
// Standard_NotImplemented::Raise("AIS_RadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,const Handle(Prs3d_Presentation)& aPresentation)");
 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
}

//=======================================================================
//function : Compute
//purpose  :  to avoid warning
//=======================================================================
void AIS_RadiusDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& aPresentationManager, 
				  const Handle(Graphic2d_GraphicObject)& aPresentation, 
				  const Standard_Integer aMode)
{
// Standard_NotImplemented::Raise("AIS_RadiusDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& aPresentationManager,const Handle(Graphic2d_GraphicObject)& aPresentation,const Standard_Integer aMode)");
 PrsMgr_PresentableObject::Compute( aPresentationManager ,aPresentation,aMode) ;
}

//=======================================================================
//function : Compute
//purpose  : 
//=======================================================================

void AIS_RadiusDimension::Compute(const Handle_Prs3d_Projector& aProjector,
                                  const Handle_Geom_Transformation& aTransformation,
                                  const Handle_Prs3d_Presentation& aPresentation)
{
// Standard_NotImplemented::Raise("AIS_RadiusDimension::Compute(const Handle_Prs3d_Projector&, const Handle_Geom_Transformation&, const Handle_Prs3d_Presentation&)");
 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
}

//=======================================================================
//function : ComputeSelection
//purpose  : 
//=======================================================================
void AIS_RadiusDimension::ComputeSelection( const Handle(SelectMgr_Selection)& aSelection, 
					    const Standard_Integer )
{
  gp_Pnt LineOrigin, LineEnd;
  DsgPrs::ComputeRadiusLine( myCenter, myEndOfArrow, myPosition, mydrawFromCenter,
			    LineOrigin,LineEnd); 
  Handle(AIS_DimensionOwner) own = new AIS_DimensionOwner(this,7);
  own->SetShape(myFShape);
  Handle( Select3D_SensitiveSegment ) seg = new Select3D_SensitiveSegment( own, LineOrigin, LineEnd );
  aSelection->Add( seg );

  // Text
  Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
  Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
                                                                   myPosition.X(),
                                                                   myPosition.Y(),
                                                                   myPosition.Z(),
                                                                   myPosition.X() + size,
                                                                   myPosition.Y() + size,
                                                                   myPosition.Z() + size );
  aSelection->Add(box);
}

//=======================================================================
//function : ComputeRadius
//purpose  : 
//=======================================================================

void AIS_RadiusDimension::ComputeRadius( const Handle( Prs3d_Presentation )& aPresentation )
{
   if (myAutomaticPosition)
     {
       InitFirstShape();
       myEndOfArrow = ElCLib::Value( (myFirstPar + myLastPar)/2, myCircle );
       if(mydrawFromCenter) {
	 myPosition = myCenter;
       }
       else 
	 {
	   gp_Vec v1(myCenter, myEndOfArrow);
	   myPosition = myCenter.Translated(v1 * 1.2);
	 }
       myAutomaticPosition = Standard_True;
       
       if (myIsSetBndBox)
	 myPosition = AIS::TranslatePointToBound(myPosition, gce_MakeDir( myCenter, myEndOfArrow ),
						 myBndBox );       
     }
   else
     {
       //!Automaticposition
       myPosition = AIS::ProjectPointOnPlane( myPosition, myPlane->Pln() );
       Standard_Real PosPar = ElCLib::Parameter( myCircle, myPosition );
       if (!AIS::InDomain(myFirstPar, myLastPar, PosPar))
	 { // not in domain
	   Standard_Real otherpar = PosPar + PI;
	   if (otherpar > 2*PI) otherpar -= 2*PI;
	   if (AIS::InDomain(myFirstPar, myLastPar, otherpar)){
	     PosPar = otherpar;// parameter on circle
	     myEndOfArrow = ElCLib::Value( PosPar, myCircle );
	   }
	   else {
	     Standard_Real Teta1 = Abs( PosPar - myFirstPar ), Teta2 = Abs( PosPar - myLastPar );
	     if (Teta1 > PI)
	       Teta1 = 2.0*PI - Teta1; 
	     if (Teta2 > PI)
	       Teta2 = 2.0*PI - Teta2; 
	     if (Teta1 < Teta2) 
	       {
		 if(myFirstLine.Contains(myPosition,Precision::Confusion()))
		   PosPar = myFirstPar;
		 else
		   PosPar = myLastPar;
	       }
	     else
	       {
		 if(myLastLine.Contains(myPosition,Precision::Confusion()))
		   PosPar = myLastPar; 
		 else 
		   PosPar = myFirstPar;
	       }
	     myEndOfArrow = ElCLib::Value( PosPar, myCircle );
	     gp_Lin RadiusLine = gce_MakeLin( myCenter, myEndOfArrow );
	     // project on radius line
	     myPosition = ElCLib::Value( ElCLib::Parameter( RadiusLine, myPosition ), RadiusLine );
	   }
	 }
       else
	 myEndOfArrow = ElCLib::Value( PosPar, myCircle );
     }
   
   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
   // size
#ifdef BUC60915
   if( !myArrowSizeIsDefined ) {
     myArrowSize = Min(myArrowSize,myVal/5.);
   }
   arr->SetLength(myArrowSize);
#else
   if (myVal/5. > myArrowSize) 
     arr->SetLength(myArrowSize);
   else 
     arr->SetLength(myVal/5.);
#endif
   
   DsgPrs_RadiusPresentation::Add( aPresentation, myDrawer, myText, myPosition,
				  myCenter, myEndOfArrow, mySymbolPrs, mydrawFromCenter );
 }