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