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)
95 //cout << endl << "This is strange Offset Dimension!" << endl;
97 gp_Trsf aInvertTrsf = myRelativePos;
98 //myArrowSize = fabs (myVal/5.);
99 myArrowSize = fabs (myVal/10.0);
100 if (myArrowSize > 30.) myArrowSize = 30.;
101 if (myArrowSize < 15.) myArrowSize = 15.;
102 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
104 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
105 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
107 if (surf1.GetType() == GeomAbs_Cylinder ||
108 surf1.GetType() == GeomAbs_Cone ||
109 surf1.GetType() == GeomAbs_Torus) {
110 if (surf2.GetType() == GeomAbs_Cylinder ||
111 surf2.GetType() == GeomAbs_Cone ||
112 surf2.GetType() == GeomAbs_Torus) {
113 ComputeTwoAxesOffset(aprs, aInvertTrsf);
115 ComputeAxeFaceOffset(aprs, aInvertTrsf);
119 //myDirAttach : oriente de myFShape vers mySShape
120 gp_Pln aPln = surf1.Plane();
121 gp_Pnt aPnt = aPln.Location();
123 gp_Pln bPln = surf2.Plane();
125 Standard_Real uPnt, vPnt;
126 ElSLib::Parameters (bPln , aPnt , uPnt, vPnt);
127 gp_Pnt bPnt = ElSLib::Value (uPnt, vPnt, bPln);
128 if (aPnt.IsEqual(bPnt,Precision::Confusion())) {
129 gp_Ax1 aAx1 = aPln.Axis();
130 myDirAttach = aAx1.Direction();
132 gp_Vec aVec (aPnt,bPnt);
133 myDirAttach.SetCoord(aVec.X(),aVec.Y(),aVec.Z());
135 ComputeTwoFacesOffset(aprs, aInvertTrsf);
140 //=======================================================================
142 //purpose : to avoid warning at compilation (SUN)
143 //=======================================================================
144 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
145 const Handle(Prs3d_Presentation)& /*aPresentation*/)
147 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,const Handle(Prs3d_Presentation)& aPresentation)");
148 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
151 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
152 const Handle(Geom_Transformation)& aTransformation,
153 const Handle(Prs3d_Presentation)& aPresentation)
155 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
156 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
159 //=======================================================================
160 //function : ComputeSelection
162 //=======================================================================
163 void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel,
164 const Standard_Integer)
166 //myArrowSize = fabs (myVal/5.);
167 myArrowSize = fabs (myVal/10.0);
168 if (myArrowSize > 30.) myArrowSize = 30.;
169 if (myArrowSize < 15.) myArrowSize = 15.;
170 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
171 gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
172 gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
173 gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
174 gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
175 gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;
177 gp_Lin L1 (myTFAttach,myTDirAttach);
178 gp_Lin L2 (myTSAttach,myTDirAttach2);
179 gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
180 gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
183 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
185 if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
186 L3 = gce_MakeLin(Proj1,Proj2);
188 else { // cas ou la dimension est nulle
189 if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
190 gp_Vec v3 (Proj1,Tcurpos);
192 L3 = gce_MakeLin(Proj1,d3);
194 L3 = gce_MakeLin(Proj1,myTDirAttach);
198 Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
199 Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
209 Standard_Real parmin,parmax,parcur;
210 parmin = ElCLib::Parameter(L3,Proj1);
213 parcur = ElCLib::Parameter(L3,Proj2);
214 parmin = Min(parmin,parcur);
215 parmax = Max(parmax,parcur);
217 parcur = ElCLib::Parameter(L3,Tcurpos);
218 parmin = Min(parmin,parcur);
219 parmax = Max(parmax,parcur);
221 gp_Pnt PointMin = ElCLib::Value(parmin,L3);
222 gp_Pnt PointMax = ElCLib::Value(parmax,L3);
224 Handle(Select3D_SensitiveSegment) seg;
225 if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
226 seg = new Select3D_SensitiveSegment(own,
232 if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
233 seg = new Select3D_SensitiveSegment(own,
238 if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
239 seg = new Select3D_SensitiveSegment(own,
246 //=======================================================================
247 //function : ComputeTwoAxesOffset
249 //=======================================================================
250 void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs,
251 const gp_Trsf& aTrsf)
253 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
254 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
256 gp_Ax1 Ax1Surf1, Ax1Surf2;
258 if (surf1.GetType() == GeomAbs_Cylinder) {
259 gp_Cylinder aCyl= surf1.Cylinder();
260 Ax1Surf1 = aCyl.Axis();
261 } else if (surf1.GetType() == GeomAbs_Cone) {
262 gp_Cone aCone= surf1.Cone();
263 Ax1Surf1 = aCone.Axis();
264 } else if (surf1.GetType() == GeomAbs_Torus) {
265 gp_Torus aTore= surf1.Torus();
266 Ax1Surf1 = aTore.Axis();
268 Standard_Real FirstUParam = surf1.FirstUParameter();
269 Standard_Real FirstVParam = surf1.FirstVParameter();
270 Standard_Real LastVParam = surf1.LastVParameter();
271 gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
272 gp_Pnt P1Last = surf1.Value(FirstUParam,LastVParam);
275 if (surf2.GetType() == GeomAbs_Cylinder) {
276 gp_Cylinder aCyl= surf2.Cylinder();
277 Ax1Surf2 = aCyl.Axis();
278 } else if (surf2.GetType() == GeomAbs_Cone) {
279 gp_Cone aCone= surf2.Cone();
280 Ax1Surf2 = aCone.Axis();
281 } else if (surf2.GetType() == GeomAbs_Torus) {
282 gp_Torus aTore= surf2.Torus();
283 Ax1Surf2 = aTore.Axis();
285 FirstUParam = surf2.FirstUParameter();
286 FirstVParam = surf2.FirstVParameter();
287 LastVParam = surf2.LastVParameter();
288 gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
289 gp_Pnt P2Last = surf2.Value(FirstUParam,LastVParam);
293 myFAttach = Ax1Surf1.Location();
294 mySAttach = Ax1Surf2.Location();
295 myDirAttach = Ax1Surf1.Direction();
296 myDirAttach2 = myDirAttach;
298 gp_Lin aProjLine = gce_MakeLin(myFAttach,myDirAttach);
300 if (myAutomaticPosition) {
301 curpos.SetX ( (myFAttach.X() + mySAttach.X()) /2. + 0.01);
302 curpos.SetY ( (myFAttach.Y() + mySAttach.Y()) /2. + 0.01);
303 curpos.SetZ ( (myFAttach.Z() + mySAttach.Z()) /2. + 0.01);
304 // + 0.01 pour eviter un raise de ComputeSelection...
312 curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
313 // on projette pour la presentation
315 gp_Pnt P1FirstProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
316 gp_Pnt P1LastProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
317 if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
318 myFAttach = P1FirstProj;
320 myFAttach = P1LastProj;
322 gp_Pnt P2FirstProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
323 gp_Pnt P2LastProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
324 if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
325 mySAttach = P2FirstProj;
327 mySAttach = P2LastProj;
330 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
331 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
332 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
333 arr->SetLength(myArrowSize);
334 arr = la->ArrowAspect();
335 arr->SetLength(myArrowSize);
337 gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
338 gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
339 gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
340 gp_Dir myTDirAttach2 = myTDirAttach;
341 gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
344 Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
346 DsgPrs_OffsetPresentation::AddAxes(aprs,
355 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
356 TopoDS_Shape myTFShape = transform1.Shape();
357 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
358 TopoDS_Shape myTSShape = transform2.Shape();
360 StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
361 StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
364 //=======================================================================
365 //function : ComputeTwoFacesOffset
367 //=======================================================================
368 void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs,
369 const gp_Trsf& aTrsf)
371 gp_Dir norm1 = myDirAttach;
374 if (myAutomaticPosition && ! myIsSetBndBox) {
375 TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
377 TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
378 myFAttach = BRep_Tool::Pnt(vertref);
379 gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
380 gp_Ax2 ax2(myFAttach,norm1);
381 myDirAttach = ax2.XDirection();
382 curpos = myFAttach.Translated(trans);
383 if (myVal <= Precision::Confusion()) {
384 gp_Vec vecnorm1 = norm1.XYZ()*.001;
385 curpos.Translate(vecnorm1);
392 if (myAutomaticPosition && myIsSetBndBox)
394 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
395 myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
396 myPosition.SetCoord( aXmax, aYmax, aZmax );
400 myFAttach = AIS::Nearest(myFShape,curpos);
401 if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
402 gp_Ax2 ax2(myFAttach,norm1);
403 myDirAttach = ax2.XDirection();
407 gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
408 gp_Ax2 ax2(myFAttach,norm1);
409 if (orient.Angle(norm1) <= Precision::Angular()) {
410 myDirAttach = ax2.XDirection();
413 gp_Dir adir = norm1 ^ orient;
414 myDirAttach = adir ^ norm1;
419 // en attendant mieux
420 mySAttach = AIS::Nearest(mySShape,curpos);
421 gp_Ax3 anax3 (myax2);
425 Standard_Real u2,v2, uatt, vatt;
426 ElSLib::Parameters (apln , mySAttach, uatt,vatt);
427 ElSLib::Parameters (apln , curpos , u2,v2);
429 if (uatt== u2 && vatt == v2) {
430 myDirAttach2 = myDirAttach;
432 gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
433 myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
436 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
437 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
438 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
439 arr->SetLength(myArrowSize);
440 arr = la->ArrowAspect();
441 arr->SetLength(myArrowSize);
443 gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
444 gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
445 gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
446 gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
447 gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
452 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
453 Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
456 DsgPrs_OffsetPresentation::Add(aprs,
466 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
467 TopoDS_Shape myTFShape = transform1.Shape();
468 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
469 TopoDS_Shape myTSShape = transform2.Shape();
471 StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
472 StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
475 //=======================================================================
476 //function : ComputeAxeFaceOffset
478 //=======================================================================
479 void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs,
480 const gp_Trsf& aTrsf)
482 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
483 TopoDS_Shape myTFShape = transform1.Shape();
484 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
485 TopoDS_Shape myTSShape = transform2.Shape();
487 StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
488 StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);