summaryrefslogtreecommitdiff
path: root/src/AIS/AIS_OffsetDimension.cxx
blob: eb326ff831c565a7c338c75fa2914d674914cf96 (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// File:	AIS_OffsetDimension.cxx
// Created:	Tue Mar  4 10:01:41 1997
// Author:	Jean-Pierre COMBE
//		<jpr>


#include <Standard_NotImplemented.hxx>

#include <AIS_OffsetDimension.ixx>

#include <AIS.hxx>
#include <AIS_Drawer.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRep_Tool.hxx>
#include <DsgPrs_OffsetPresentation.hxx>
#include <ElCLib.hxx>
#include <ElSLib.hxx>
#include <GeomAbs_SurfaceType.hxx>
#include <Graphic3d_Structure.hxx>
#include <Precision.hxx>
#include <Prs3d_ArrowAspect.hxx>
#include <Prs3d_Drawer.hxx>
#include <Prs3d_LengthAspect.hxx>
#include <Prs3d_LineAspect.hxx>
#include <Select3D_SensitiveBox.hxx>
#include <Select3D_SensitiveSegment.hxx>
#include <SelectMgr_EntityOwner.hxx>
#include <StdPrs_WFDeflectionShape.hxx>
#include <TColStd_Array2OfReal.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <gce_MakeLin.hxx>
#include <gp_Ax1.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax3.hxx>
#include <gp_Lin.hxx>
#include <gp_Pln.hxx>
#include <gp_Pnt.hxx>
#include <gp_Trsf.hxx>
#include <gp_Vec.hxx>

//=======================================================================
//function : AIS_OffsetDimension
//purpose  : 
//=======================================================================
AIS_OffsetDimension::AIS_OffsetDimension(const TopoDS_Shape& FistShape, 
					 const TopoDS_Shape& SecondShape,
					 const Standard_Real aVal,
					 const TCollection_ExtendedString& aText)
:AIS_Relation(),
myFAttach(0.,0.,0.),
mySAttach(0.,0.,0.)
{
  myFShape = FistShape;
  mySShape = SecondShape;
  mySymbolPrs = DsgPrs_AS_BOTHAR;
  myVal = aVal;
  myText = aText;
  //myArrowSize = fabs (myVal/5.);
  myArrowSize = fabs (myVal/10.0);
  if (myArrowSize > 30.) myArrowSize = 30.;
  if (myArrowSize < 15.) myArrowSize = 15.;
  //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
}

//=======================================================================
//function : Compute
//purpose  : 
//=======================================================================
void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager3d)&,
				  const Handle(Prs3d_Presentation)& aprs,
				  const Standard_Integer)
{
  aprs->Clear();

  //cout << endl << "This is strange Offset Dimension!" << endl;

  gp_Trsf aInvertTrsf = myRelativePos;
  //myArrowSize = fabs (myVal/5.);
  myArrowSize = fabs (myVal/10.0);
  if (myArrowSize > 30.) myArrowSize = 30.;
  if (myArrowSize < 15.) myArrowSize = 15.;
  //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
  
  BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
  BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
  
  if (surf1.GetType() == GeomAbs_Cylinder || 
      surf1.GetType() == GeomAbs_Cone     || 
      surf1.GetType() == GeomAbs_Torus) {
    if (surf2.GetType() == GeomAbs_Cylinder ||
	surf2.GetType() == GeomAbs_Cone     ||
	surf2.GetType() == GeomAbs_Torus) {
      ComputeTwoAxesOffset(aprs, aInvertTrsf);
    } else {
      ComputeAxeFaceOffset(aprs, aInvertTrsf);
    }
  }
  else {
    //myDirAttach : oriente de myFShape vers mySShape
    gp_Pln aPln = surf1.Plane();
    gp_Pnt aPnt = aPln.Location();
    
    gp_Pln bPln = surf2.Plane();
    
    Standard_Real uPnt, vPnt;
    ElSLib::Parameters (bPln , aPnt , uPnt, vPnt);
    gp_Pnt bPnt = ElSLib::Value (uPnt, vPnt, bPln);
    if (aPnt.IsEqual(bPnt,Precision::Confusion())) {
      gp_Ax1 aAx1 = aPln.Axis();
      myDirAttach = aAx1.Direction();
    } else {
      gp_Vec aVec (aPnt,bPnt);
      myDirAttach.SetCoord(aVec.X(),aVec.Y(),aVec.Z());
    }
    ComputeTwoFacesOffset(aprs, aInvertTrsf);
  }  
}


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

//=======================================================================
//function : Compute
//purpose  : to avoid warning at compilation (SUN)
//=======================================================================
void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& /*aPresentationManager2d*/,
				  const Handle(Graphic2d_GraphicObject)& /*aGraphicObject*/,
				  const Standard_Integer /*anInteger*/)
{
// Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)&,const Handle(Graphic2d_GraphicObject)&,const Standard_Integer)");
// PrsMgr_PresentableObject::Compute( aPresentationManager2d ,aGraphicObject,anInteger) ;
}

void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector, 
				  const Handle(Geom_Transformation)& aTransformation,
				  const Handle(Prs3d_Presentation)& aPresentation)
{
// Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
}

//=======================================================================
//function : ComputeSelection
//purpose  : 
//=======================================================================
void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel, 
					   const Standard_Integer)
{
  //myArrowSize = fabs (myVal/5.);
  myArrowSize = fabs (myVal/10.0);
  if (myArrowSize > 30.) myArrowSize = 30.;
  if (myArrowSize < 15.) myArrowSize = 15.;
  //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
  gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
  gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
  gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
  gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
  gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;

  gp_Lin L1 (myTFAttach,myTDirAttach);
  gp_Lin L2 (myTSAttach,myTDirAttach2);
  gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
  gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
  gp_Lin L3;

  Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);

  if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
    L3 = gce_MakeLin(Proj1,Proj2);
  }
  else {    // cas ou la dimension est nulle
    if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
      gp_Vec v3 (Proj1,Tcurpos);
      gp_Dir d3 (v3);
      L3 = gce_MakeLin(Proj1,d3);
    } else {
      L3 = gce_MakeLin(Proj1,myTDirAttach);
    }

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

  Standard_Real parmin,parmax,parcur;
  parmin = ElCLib::Parameter(L3,Proj1);
  parmax = parmin;

  parcur = ElCLib::Parameter(L3,Proj2);
  parmin = Min(parmin,parcur);
  parmax = Max(parmax,parcur);

  parcur = ElCLib::Parameter(L3,Tcurpos);
  parmin = Min(parmin,parcur);
  parmax = Max(parmax,parcur);

  gp_Pnt PointMin = ElCLib::Value(parmin,L3);
  gp_Pnt PointMax = ElCLib::Value(parmax,L3);

  Handle(Select3D_SensitiveSegment) seg;
  if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
    seg = new Select3D_SensitiveSegment(own,
					PointMin,
					PointMax);
    aSel->Add(seg);
  }
  
  if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
    seg = new Select3D_SensitiveSegment(own,
					myTFAttach,
					Proj1);
    aSel->Add(seg);
  }
  if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
    seg = new Select3D_SensitiveSegment(own,
					myTSAttach,
					Proj2);
    aSel->Add(seg);
  }
}

//=======================================================================
//function : ComputeTwoAxesOffset
//purpose  : 
//=======================================================================
void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs, 
						const gp_Trsf& aTrsf)
{
  BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
  BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));

  gp_Ax1 Ax1Surf1, Ax1Surf2;

  if (surf1.GetType() == GeomAbs_Cylinder) {
    gp_Cylinder aCyl= surf1.Cylinder();
    Ax1Surf1 = aCyl.Axis();    
  } else if (surf1.GetType() == GeomAbs_Cone) {
    gp_Cone aCone= surf1.Cone();
    Ax1Surf1 = aCone.Axis();    
  } else if (surf1.GetType() == GeomAbs_Torus) {
    gp_Torus aTore= surf1.Torus();
    Ax1Surf1 = aTore.Axis();    
  }
  Standard_Real FirstUParam = surf1.FirstUParameter();
  Standard_Real FirstVParam = surf1.FirstVParameter();
  Standard_Real LastVParam  = surf1.LastVParameter();
  gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
  gp_Pnt P1Last  = surf1.Value(FirstUParam,LastVParam);


  if (surf2.GetType() == GeomAbs_Cylinder) {
    gp_Cylinder aCyl= surf2.Cylinder();
    Ax1Surf2 = aCyl.Axis();    
  } else if (surf2.GetType() == GeomAbs_Cone) {
    gp_Cone aCone= surf2.Cone();
    Ax1Surf2 = aCone.Axis();    
  } else if (surf2.GetType() == GeomAbs_Torus) {
    gp_Torus aTore= surf2.Torus();
    Ax1Surf2 = aTore.Axis();    
  }
  FirstUParam = surf2.FirstUParameter();
  FirstVParam = surf2.FirstVParameter();
  LastVParam  = surf2.LastVParameter();
  gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
  gp_Pnt P2Last  = surf2.Value(FirstUParam,LastVParam);

  
  
  myFAttach = Ax1Surf1.Location();
  mySAttach = Ax1Surf2.Location();
  myDirAttach = Ax1Surf1.Direction();
  myDirAttach2 = myDirAttach;
  gp_Pnt curpos;
  gp_Lin aProjLine  = gce_MakeLin(myFAttach,myDirAttach);

  if (myAutomaticPosition) {
    curpos.SetX ( (myFAttach.X() +  mySAttach.X()) /2. + 0.01);
    curpos.SetY ( (myFAttach.Y() +  mySAttach.Y()) /2. + 0.01);
    curpos.SetZ ( (myFAttach.Z() +  mySAttach.Z()) /2. + 0.01);
    // + 0.01 pour eviter un raise de ComputeSelection...

    myPosition = curpos;
  } 
  else {
    curpos = myPosition;
  }
  
  curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
  // on projette pour la presentation

  gp_Pnt P1FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
  gp_Pnt P1LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
  if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
    myFAttach = P1FirstProj;
  else
    myFAttach = P1LastProj;
  
  gp_Pnt P2FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
  gp_Pnt P2LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
  if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
    mySAttach = P2FirstProj;
  else
    mySAttach = P2LastProj;


  Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
  Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
  //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
  arr->SetLength(myArrowSize);
  arr = la->Arrow2Aspect();
  arr->SetLength(myArrowSize);

  gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
  gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
  gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
  gp_Dir myTDirAttach2 = myTDirAttach;
  gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
 
  if (myIsSetBndBox)
    Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );

  DsgPrs_OffsetPresentation::AddAxes(aprs,
				     myDrawer,
				     myText,
				     myTFAttach,
				     myTSAttach,
				     myTDirAttach,
				     myTDirAttach2,
				     Tcurpos);
  
  BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
  TopoDS_Shape myTFShape = transform1.Shape();
  BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
  TopoDS_Shape myTSShape = transform2.Shape();

  StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
  StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
}

//=======================================================================
//function : ComputeTwoFacesOffset
//purpose  : 
//=======================================================================
void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs, 
						const gp_Trsf& aTrsf)
{
  gp_Dir norm1 = myDirAttach;
  gp_Pnt curpos;
  gp_Ax2 myax2;
  if (myAutomaticPosition && ! myIsSetBndBox) {
    TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
    if (explo.More()) {
      TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
      myFAttach = BRep_Tool::Pnt(vertref);
      gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
      gp_Ax2 ax2(myFAttach,norm1);
      myDirAttach = ax2.XDirection();
      curpos = myFAttach.Translated(trans);
      if (myVal <= Precision::Confusion()) {
	gp_Vec vecnorm1 = norm1.XYZ()*.001;
	curpos.Translate(vecnorm1);
      }
      myPosition = curpos;
      myax2 = ax2;
    }
  }
  else {
    if (myAutomaticPosition && myIsSetBndBox)
      {
	Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
	myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
	myPosition.SetCoord( aXmax, aYmax, aZmax );
      }
     
    curpos = myPosition;
    myFAttach = AIS::Nearest(myFShape,curpos);
    if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
      gp_Ax2 ax2(myFAttach,norm1);
      myDirAttach = ax2.XDirection();
      myax2 = ax2;
    }
    else {
      gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
      gp_Ax2 ax2(myFAttach,norm1);
      if (orient.Angle(norm1) <= Precision::Angular()) {
	myDirAttach = ax2.XDirection();
      }
      else {
	gp_Dir adir = norm1 ^ orient;
	myDirAttach = adir ^ norm1;
      }
      myax2 = ax2;
    }
  }
  // en attendant mieux
  mySAttach = AIS::Nearest(mySShape,curpos);
  gp_Ax3 anax3 (myax2);
  gp_Pln apln (anax3);
  
  //gp_Pnt proj2;
  Standard_Real u2,v2, uatt, vatt;
  ElSLib::Parameters (apln , mySAttach, uatt,vatt);
  ElSLib::Parameters (apln , curpos   , u2,v2);
  
  if (uatt== u2 && vatt == v2) {
    myDirAttach2 = myDirAttach;
  } else {
    gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
    myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
  }
  
  Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
  Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
  //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
  arr->SetLength(myArrowSize);
  arr = la->Arrow2Aspect();
  arr->SetLength(myArrowSize);

  gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
  gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
  gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
  gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
  gp_Pnt Tcurpos = curpos.Transformed (aTrsf);

/*
  if (myIsSetBndBox)
    {
      BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
      Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
    }
*/
  DsgPrs_OffsetPresentation::Add(aprs,
				 myDrawer,
				 myText,
				 myTFAttach,
				 myTSAttach,
				 myTDirAttach,
				 myTDirAttach2,
				 Tcurpos);
  

  BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
  TopoDS_Shape myTFShape = transform1.Shape();
  BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
  TopoDS_Shape myTSShape = transform2.Shape();

  StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
  StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);

}

//=======================================================================
//function : ComputeAxeFaceOffset
//purpose  : 
//=======================================================================
void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs, 
					       const gp_Trsf& aTrsf)
{
  BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
  TopoDS_Shape myTFShape = transform1.Shape();
  BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
  TopoDS_Shape myTSShape = transform2.Shape();
  
  StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
  StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
}