1 // Created on: 1997-03-04
2 // Created by: Jean-Pierre COMBE
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
19 #include <AIS_OffsetDimension.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepBuilderAPI_Transform.hxx>
23 #include <DsgPrs_OffsetPresentation.hxx>
26 #include <gce_MakeLin.hxx>
27 #include <Geom_Transformation.hxx>
28 #include <GeomAbs_SurfaceType.hxx>
35 #include <gp_Trsf.hxx>
37 #include <Graphic3d_Structure.hxx>
38 #include <Precision.hxx>
39 #include <Prs3d_ArrowAspect.hxx>
40 #include <Prs3d_DimensionAspect.hxx>
41 #include <Prs3d_Drawer.hxx>
42 #include <Prs3d_LineAspect.hxx>
43 #include <Prs3d_Presentation.hxx>
44 #include <Prs3d_Projector.hxx>
45 #include <Select3D_SensitiveBox.hxx>
46 #include <Select3D_SensitiveSegment.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48 #include <SelectMgr_Selection.hxx>
49 #include <Standard_NotImplemented.hxx>
50 #include <Standard_Type.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <TCollection_ExtendedString.hxx>
53 #include <TColStd_Array2OfReal.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Vertex.hxx>
59 IMPLEMENT_STANDARD_RTTIEXT(AIS_OffsetDimension,AIS_Relation)
61 //=======================================================================
62 //function : AIS_OffsetDimension
64 //=======================================================================
65 AIS_OffsetDimension::AIS_OffsetDimension(const TopoDS_Shape& FistShape,
66 const TopoDS_Shape& SecondShape,
67 const Standard_Real aVal,
68 const TCollection_ExtendedString& aText)
74 mySShape = SecondShape;
75 mySymbolPrs = DsgPrs_AS_BOTHAR;
78 //myArrowSize = fabs (myVal/5.);
79 myArrowSize = fabs (myVal/10.0);
80 if (myArrowSize > 30.) myArrowSize = 30.;
81 if (myArrowSize < 15.) myArrowSize = 15.;
82 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
85 //=======================================================================
88 //=======================================================================
89 void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager3d)&,
90 const Handle(Prs3d_Presentation)& aprs,
91 const Standard_Integer)
93 gp_Trsf aInvertTrsf = myRelativePos;
94 //myArrowSize = fabs (myVal/5.);
95 myArrowSize = fabs (myVal/10.0);
96 if (myArrowSize > 30.) myArrowSize = 30.;
97 if (myArrowSize < 15.) myArrowSize = 15.;
98 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
100 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
101 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
103 if (surf1.GetType() == GeomAbs_Cylinder ||
104 surf1.GetType() == GeomAbs_Cone ||
105 surf1.GetType() == GeomAbs_Torus) {
106 if (surf2.GetType() == GeomAbs_Cylinder ||
107 surf2.GetType() == GeomAbs_Cone ||
108 surf2.GetType() == GeomAbs_Torus) {
109 ComputeTwoAxesOffset(aprs, aInvertTrsf);
111 ComputeAxeFaceOffset(aprs, aInvertTrsf);
115 //myDirAttach : oriente de myFShape vers mySShape
116 gp_Pln aPln = surf1.Plane();
117 gp_Pnt aPnt = aPln.Location();
119 gp_Pln bPln = surf2.Plane();
121 Standard_Real uPnt, vPnt;
122 ElSLib::Parameters (bPln , aPnt , uPnt, vPnt);
123 gp_Pnt bPnt = ElSLib::Value (uPnt, vPnt, bPln);
124 if (aPnt.IsEqual(bPnt,Precision::Confusion())) {
125 gp_Ax1 aAx1 = aPln.Axis();
126 myDirAttach = aAx1.Direction();
128 gp_Vec aVec (aPnt,bPnt);
129 myDirAttach.SetCoord(aVec.X(),aVec.Y(),aVec.Z());
131 ComputeTwoFacesOffset(aprs, aInvertTrsf);
136 //=======================================================================
138 //purpose : to avoid warning at compilation (SUN)
139 //=======================================================================
140 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
141 const Handle(Prs3d_Presentation)& /*aPresentation*/)
143 // throw Standard_NotImplemented("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,const Handle(Prs3d_Presentation)& aPresentation)");
144 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
147 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
148 const Handle(Geom_Transformation)& aTransformation,
149 const Handle(Prs3d_Presentation)& aPresentation)
151 // throw Standard_NotImplemented("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
152 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
155 //=======================================================================
156 //function : ComputeSelection
158 //=======================================================================
159 void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel,
160 const Standard_Integer)
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);;
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);
179 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
181 if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
182 L3 = gce_MakeLin(Proj1,Proj2);
184 else { // cas ou la dimension est nulle
185 if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
186 gp_Vec v3 (Proj1,Tcurpos);
188 L3 = gce_MakeLin(Proj1,d3);
190 L3 = gce_MakeLin(Proj1,myTDirAttach);
194 Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
195 Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
205 Standard_Real parmin,parmax,parcur;
206 parmin = ElCLib::Parameter(L3,Proj1);
209 parcur = ElCLib::Parameter(L3,Proj2);
210 parmin = Min(parmin,parcur);
211 parmax = Max(parmax,parcur);
213 parcur = ElCLib::Parameter(L3,Tcurpos);
214 parmin = Min(parmin,parcur);
215 parmax = Max(parmax,parcur);
217 gp_Pnt PointMin = ElCLib::Value(parmin,L3);
218 gp_Pnt PointMax = ElCLib::Value(parmax,L3);
220 Handle(Select3D_SensitiveSegment) seg;
221 if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
222 seg = new Select3D_SensitiveSegment(own,
228 if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
229 seg = new Select3D_SensitiveSegment(own,
234 if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
235 seg = new Select3D_SensitiveSegment(own,
242 //=======================================================================
243 //function : ComputeTwoAxesOffset
245 //=======================================================================
246 void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs,
247 const gp_Trsf& aTrsf)
249 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
250 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
252 gp_Ax1 Ax1Surf1, Ax1Surf2;
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();
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);
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();
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);
289 myFAttach = Ax1Surf1.Location();
290 mySAttach = Ax1Surf2.Location();
291 myDirAttach = Ax1Surf1.Direction();
292 myDirAttach2 = myDirAttach;
294 gp_Lin aProjLine = gce_MakeLin(myFAttach,myDirAttach);
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...
308 curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
309 // on projette pour la presentation
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;
316 myFAttach = P1LastProj;
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;
323 mySAttach = P2LastProj;
326 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
327 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
328 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
329 arr->SetLength(myArrowSize);
330 arr = la->ArrowAspect();
331 arr->SetLength(myArrowSize);
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);
340 Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
342 DsgPrs_OffsetPresentation::AddAxes(aprs,
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();
356 StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
357 StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
360 //=======================================================================
361 //function : ComputeTwoFacesOffset
363 //=======================================================================
364 void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs,
365 const gp_Trsf& aTrsf)
367 gp_Dir norm1 = myDirAttach;
370 if (myAutomaticPosition && ! myIsSetBndBox) {
371 TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
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);
388 if (myAutomaticPosition && myIsSetBndBox)
390 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
391 myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
392 myPosition.SetCoord( aXmax, aYmax, aZmax );
396 myFAttach = AIS::Nearest(myFShape,curpos);
397 if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
398 gp_Ax2 ax2(myFAttach,norm1);
399 myDirAttach = ax2.XDirection();
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();
409 gp_Dir adir = norm1 ^ orient;
410 myDirAttach = adir ^ norm1;
415 // en attendant mieux
416 mySAttach = AIS::Nearest(mySShape,curpos);
417 gp_Ax3 anax3 (myax2);
421 Standard_Real u2,v2, uatt, vatt;
422 ElSLib::Parameters (apln , mySAttach, uatt,vatt);
423 ElSLib::Parameters (apln , curpos , u2,v2);
425 if (uatt== u2 && vatt == v2) {
426 myDirAttach2 = myDirAttach;
428 gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
429 myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
432 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
433 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
434 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
435 arr->SetLength(myArrowSize);
436 arr = la->ArrowAspect();
437 arr->SetLength(myArrowSize);
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);
448 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
449 Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
452 DsgPrs_OffsetPresentation::Add(aprs,
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();
467 StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
468 StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
471 //=======================================================================
472 //function : ComputeAxeFaceOffset
474 //=======================================================================
475 void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs,
476 const gp_Trsf& aTrsf)
478 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
479 TopoDS_Shape myTFShape = transform1.Shape();
480 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
481 TopoDS_Shape myTSShape = transform2.Shape();
483 StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
484 StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);