0022627: Change OCCT memory management defaults
[occt.git] / src / AIS / AIS_OffsetDimension.cxx
CommitLineData
7fd59977 1// File: AIS_OffsetDimension.cxx
2// Created: Tue Mar 4 10:01:41 1997
3// Author: Jean-Pierre COMBE
4// <jpr>
5
6
7#include <Standard_NotImplemented.hxx>
8
9#include <AIS_OffsetDimension.ixx>
10
11#include <AIS.hxx>
12#include <AIS_Drawer.hxx>
13#include <BRepBuilderAPI_Transform.hxx>
14#include <BRepAdaptor_Surface.hxx>
15#include <BRep_Tool.hxx>
16#include <DsgPrs_OffsetPresentation.hxx>
17#include <ElCLib.hxx>
18#include <ElSLib.hxx>
19#include <GeomAbs_SurfaceType.hxx>
20#include <Graphic3d_Structure.hxx>
21#include <Precision.hxx>
22#include <Prs3d_ArrowAspect.hxx>
23#include <Prs3d_Drawer.hxx>
24#include <Prs3d_LengthAspect.hxx>
25#include <Prs3d_LineAspect.hxx>
26#include <Select3D_SensitiveBox.hxx>
27#include <Select3D_SensitiveSegment.hxx>
28#include <SelectMgr_EntityOwner.hxx>
29#include <StdPrs_WFDeflectionShape.hxx>
30#include <TColStd_Array2OfReal.hxx>
31#include <TCollection_ExtendedString.hxx>
32#include <TopExp_Explorer.hxx>
33#include <TopoDS.hxx>
34#include <TopoDS_Vertex.hxx>
35#include <gce_MakeLin.hxx>
36#include <gp_Ax1.hxx>
37#include <gp_Ax2.hxx>
38#include <gp_Ax3.hxx>
39#include <gp_Lin.hxx>
40#include <gp_Pln.hxx>
41#include <gp_Pnt.hxx>
42#include <gp_Trsf.hxx>
43#include <gp_Vec.hxx>
44
45//=======================================================================
46//function : AIS_OffsetDimension
47//purpose :
48//=======================================================================
49AIS_OffsetDimension::AIS_OffsetDimension(const TopoDS_Shape& FistShape,
50 const TopoDS_Shape& SecondShape,
51 const Standard_Real aVal,
52 const TCollection_ExtendedString& aText)
53:AIS_Relation(),
54myFAttach(0.,0.,0.),
55mySAttach(0.,0.,0.)
56{
57 myFShape = FistShape;
58 mySShape = SecondShape;
59 mySymbolPrs = DsgPrs_AS_BOTHAR;
60 myVal = aVal;
61 myText = aText;
62 //myArrowSize = fabs (myVal/5.);
63 myArrowSize = fabs (myVal/10.0);
64 if (myArrowSize > 30.) myArrowSize = 30.;
65 if (myArrowSize < 15.) myArrowSize = 15.;
66 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
67}
68
69//=======================================================================
70//function : Compute
71//purpose :
72//=======================================================================
73void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager3d)&,
74 const Handle(Prs3d_Presentation)& aprs,
75 const Standard_Integer)
76{
77 aprs->Clear();
78
79 //cout << endl << "This is strange Offset Dimension!" << endl;
80
81 gp_Trsf aInvertTrsf = myRelativePos;
82 //myArrowSize = fabs (myVal/5.);
83 myArrowSize = fabs (myVal/10.0);
84 if (myArrowSize > 30.) myArrowSize = 30.;
85 if (myArrowSize < 15.) myArrowSize = 15.;
86 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
87
88 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
89 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
90
91 if (surf1.GetType() == GeomAbs_Cylinder ||
92 surf1.GetType() == GeomAbs_Cone ||
93 surf1.GetType() == GeomAbs_Torus) {
94 if (surf2.GetType() == GeomAbs_Cylinder ||
95 surf2.GetType() == GeomAbs_Cone ||
96 surf2.GetType() == GeomAbs_Torus) {
97 ComputeTwoAxesOffset(aprs, aInvertTrsf);
98 } else {
99 ComputeAxeFaceOffset(aprs, aInvertTrsf);
100 }
101 }
102 else {
103 //myDirAttach : oriente de myFShape vers mySShape
104 gp_Pln aPln = surf1.Plane();
105 gp_Pnt aPnt = aPln.Location();
106
107 gp_Pln bPln = surf2.Plane();
108
109 Standard_Real uPnt, vPnt;
110 ElSLib::Parameters (bPln , aPnt , uPnt, vPnt);
111 gp_Pnt bPnt = ElSLib::Value (uPnt, vPnt, bPln);
112 if (aPnt.IsEqual(bPnt,Precision::Confusion())) {
113 gp_Ax1 aAx1 = aPln.Axis();
114 myDirAttach = aAx1.Direction();
115 } else {
116 gp_Vec aVec (aPnt,bPnt);
117 myDirAttach.SetCoord(aVec.X(),aVec.Y(),aVec.Z());
118 }
119 ComputeTwoFacesOffset(aprs, aInvertTrsf);
120 }
121}
122
123
124//=======================================================================
125//function : Compute
126//purpose : to avoid warning at compilation (SUN)
127//=======================================================================
128void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
129 const Handle(Prs3d_Presentation)& /*aPresentation*/)
130{
131// Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,const Handle(Prs3d_Presentation)& aPresentation)");
132// PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
133}
134
135//=======================================================================
136//function : Compute
137//purpose : to avoid warning at compilation (SUN)
138//=======================================================================
139void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& /*aPresentationManager2d*/,
140 const Handle(Graphic2d_GraphicObject)& /*aGraphicObject*/,
141 const Standard_Integer /*anInteger*/)
142{
143// Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)&,const Handle(Graphic2d_GraphicObject)&,const Standard_Integer)");
144// PrsMgr_PresentableObject::Compute( aPresentationManager2d ,aGraphicObject,anInteger) ;
145}
146
147void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
148 const Handle(Geom_Transformation)& aTransformation,
149 const Handle(Prs3d_Presentation)& aPresentation)
150{
151// Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
152 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
153}
154
155//=======================================================================
156//function : ComputeSelection
157//purpose :
158//=======================================================================
159void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel,
160 const Standard_Integer)
161{
162 //myArrowSize = fabs (myVal/5.);
163 myArrowSize = fabs (myVal/10.0);
164 if (myArrowSize > 30.) myArrowSize = 30.;
165 if (myArrowSize < 15.) myArrowSize = 15.;
166 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
167 gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
168 gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
169 gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
170 gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
171 gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;
172
173 gp_Lin L1 (myTFAttach,myTDirAttach);
174 gp_Lin L2 (myTSAttach,myTDirAttach2);
175 gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
176 gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
177 gp_Lin L3;
178
179 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
180
181 if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
182 L3 = gce_MakeLin(Proj1,Proj2);
183 }
184 else { // cas ou la dimension est nulle
185 if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
186 gp_Vec v3 (Proj1,Tcurpos);
187 gp_Dir d3 (v3);
188 L3 = gce_MakeLin(Proj1,d3);
189 } else {
190 L3 = gce_MakeLin(Proj1,myTDirAttach);
191 }
192
193 // Text
194 Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
195 Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
196 Tcurpos.X(),
197 Tcurpos.Y(),
198 Tcurpos.Z(),
199 Tcurpos.X() + size,
200 Tcurpos.Y() + size,
201 Tcurpos.Z() + size);
202 aSel->Add(box);
203 }
204
205 Standard_Real parmin,parmax,parcur;
206 parmin = ElCLib::Parameter(L3,Proj1);
207 parmax = parmin;
208
209 parcur = ElCLib::Parameter(L3,Proj2);
210 parmin = Min(parmin,parcur);
211 parmax = Max(parmax,parcur);
212
213 parcur = ElCLib::Parameter(L3,Tcurpos);
214 parmin = Min(parmin,parcur);
215 parmax = Max(parmax,parcur);
216
217 gp_Pnt PointMin = ElCLib::Value(parmin,L3);
218 gp_Pnt PointMax = ElCLib::Value(parmax,L3);
219
220 Handle(Select3D_SensitiveSegment) seg;
221 if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
222 seg = new Select3D_SensitiveSegment(own,
223 PointMin,
224 PointMax);
225 aSel->Add(seg);
226 }
227
228 if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
229 seg = new Select3D_SensitiveSegment(own,
230 myTFAttach,
231 Proj1);
232 aSel->Add(seg);
233 }
234 if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
235 seg = new Select3D_SensitiveSegment(own,
236 myTSAttach,
237 Proj2);
238 aSel->Add(seg);
239 }
240}
241
242//=======================================================================
243//function : ComputeTwoAxesOffset
244//purpose :
245//=======================================================================
246void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs,
247 const gp_Trsf& aTrsf)
248{
249 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
250 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
251
252 gp_Ax1 Ax1Surf1, Ax1Surf2;
253
254 if (surf1.GetType() == GeomAbs_Cylinder) {
255 gp_Cylinder aCyl= surf1.Cylinder();
256 Ax1Surf1 = aCyl.Axis();
257 } else if (surf1.GetType() == GeomAbs_Cone) {
258 gp_Cone aCone= surf1.Cone();
259 Ax1Surf1 = aCone.Axis();
260 } else if (surf1.GetType() == GeomAbs_Torus) {
261 gp_Torus aTore= surf1.Torus();
262 Ax1Surf1 = aTore.Axis();
263 }
264 Standard_Real FirstUParam = surf1.FirstUParameter();
265 Standard_Real FirstVParam = surf1.FirstVParameter();
266 Standard_Real LastVParam = surf1.LastVParameter();
267 gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
268 gp_Pnt P1Last = surf1.Value(FirstUParam,LastVParam);
269
270
271 if (surf2.GetType() == GeomAbs_Cylinder) {
272 gp_Cylinder aCyl= surf2.Cylinder();
273 Ax1Surf2 = aCyl.Axis();
274 } else if (surf2.GetType() == GeomAbs_Cone) {
275 gp_Cone aCone= surf2.Cone();
276 Ax1Surf2 = aCone.Axis();
277 } else if (surf2.GetType() == GeomAbs_Torus) {
278 gp_Torus aTore= surf2.Torus();
279 Ax1Surf2 = aTore.Axis();
280 }
281 FirstUParam = surf2.FirstUParameter();
282 FirstVParam = surf2.FirstVParameter();
283 LastVParam = surf2.LastVParameter();
284 gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
285 gp_Pnt P2Last = surf2.Value(FirstUParam,LastVParam);
286
287
288
289 myFAttach = Ax1Surf1.Location();
290 mySAttach = Ax1Surf2.Location();
291 myDirAttach = Ax1Surf1.Direction();
292 myDirAttach2 = myDirAttach;
293 gp_Pnt curpos;
294 gp_Lin aProjLine = gce_MakeLin(myFAttach,myDirAttach);
295
296 if (myAutomaticPosition) {
297 curpos.SetX ( (myFAttach.X() + mySAttach.X()) /2. + 0.01);
298 curpos.SetY ( (myFAttach.Y() + mySAttach.Y()) /2. + 0.01);
299 curpos.SetZ ( (myFAttach.Z() + mySAttach.Z()) /2. + 0.01);
300 // + 0.01 pour eviter un raise de ComputeSelection...
301
302 myPosition = curpos;
303 }
304 else {
305 curpos = myPosition;
306 }
307
308 curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
309 // on projette pour la presentation
310
311 gp_Pnt P1FirstProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
312 gp_Pnt P1LastProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
313 if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
314 myFAttach = P1FirstProj;
315 else
316 myFAttach = P1LastProj;
317
318 gp_Pnt P2FirstProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
319 gp_Pnt P2LastProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
320 if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
321 mySAttach = P2FirstProj;
322 else
323 mySAttach = P2LastProj;
324
325
326 Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
327 Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
328 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
329 arr->SetLength(myArrowSize);
330 arr = la->Arrow2Aspect();
331 arr->SetLength(myArrowSize);
332
333 gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
334 gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
335 gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
336 gp_Dir myTDirAttach2 = myTDirAttach;
337 gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
338
339 if (myIsSetBndBox)
340 Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
341
342 DsgPrs_OffsetPresentation::AddAxes(aprs,
343 myDrawer,
344 myText,
345 myTFAttach,
346 myTSAttach,
347 myTDirAttach,
348 myTDirAttach2,
349 Tcurpos);
350
351 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
352 TopoDS_Shape myTFShape = transform1.Shape();
353 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
354 TopoDS_Shape myTSShape = transform2.Shape();
355
356 StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
357 StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
358}
359
360//=======================================================================
361//function : ComputeTwoFacesOffset
362//purpose :
363//=======================================================================
364void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs,
365 const gp_Trsf& aTrsf)
366{
367 gp_Dir norm1 = myDirAttach;
368 gp_Pnt curpos;
369 gp_Ax2 myax2;
370 if (myAutomaticPosition && ! myIsSetBndBox) {
371 TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
372 if (explo.More()) {
373 TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
374 myFAttach = BRep_Tool::Pnt(vertref);
375 gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
376 gp_Ax2 ax2(myFAttach,norm1);
377 myDirAttach = ax2.XDirection();
378 curpos = myFAttach.Translated(trans);
379 if (myVal <= Precision::Confusion()) {
380 gp_Vec vecnorm1 = norm1.XYZ()*.001;
381 curpos.Translate(vecnorm1);
382 }
383 myPosition = curpos;
384 myax2 = ax2;
385 }
386 }
387 else {
388 if (myAutomaticPosition && myIsSetBndBox)
389 {
390 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
391 myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
392 myPosition.SetCoord( aXmax, aYmax, aZmax );
393 }
394
395 curpos = myPosition;
396 myFAttach = AIS::Nearest(myFShape,curpos);
397 if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
398 gp_Ax2 ax2(myFAttach,norm1);
399 myDirAttach = ax2.XDirection();
400 myax2 = ax2;
401 }
402 else {
403 gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
404 gp_Ax2 ax2(myFAttach,norm1);
405 if (orient.Angle(norm1) <= Precision::Angular()) {
406 myDirAttach = ax2.XDirection();
407 }
408 else {
409 gp_Dir adir = norm1 ^ orient;
410 myDirAttach = adir ^ norm1;
411 }
412 myax2 = ax2;
413 }
414 }
415 // en attendant mieux
416 mySAttach = AIS::Nearest(mySShape,curpos);
417 gp_Ax3 anax3 (myax2);
418 gp_Pln apln (anax3);
419
420 //gp_Pnt proj2;
421 Standard_Real u2,v2, uatt, vatt;
422 ElSLib::Parameters (apln , mySAttach, uatt,vatt);
423 ElSLib::Parameters (apln , curpos , u2,v2);
424
425 if (uatt== u2 && vatt == v2) {
426 myDirAttach2 = myDirAttach;
427 } else {
428 gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
429 myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
430 }
431
432 Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
433 Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
434 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
435 arr->SetLength(myArrowSize);
436 arr = la->Arrow2Aspect();
437 arr->SetLength(myArrowSize);
438
439 gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
440 gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
441 gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
442 gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
443 gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
444
445/*
446 if (myIsSetBndBox)
447 {
448 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
449 Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
450 }
451*/
452 DsgPrs_OffsetPresentation::Add(aprs,
453 myDrawer,
454 myText,
455 myTFAttach,
456 myTSAttach,
457 myTDirAttach,
458 myTDirAttach2,
459 Tcurpos);
460
461
462 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
463 TopoDS_Shape myTFShape = transform1.Shape();
464 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
465 TopoDS_Shape myTSShape = transform2.Shape();
466
467 StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
468 StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
469
470}
471
472//=======================================================================
473//function : ComputeAxeFaceOffset
474//purpose :
475//=======================================================================
476void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs,
477 const gp_Trsf& aTrsf)
478{
479 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
480 TopoDS_Shape myTFShape = transform1.Shape();
481 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
482 TopoDS_Shape myTSShape = transform2.Shape();
483
484 StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
485 StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
486}
487