1 // Created on: 1997-03-04
2 // Created by: Jean-Pierre COMBE
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
23 #include <Standard_NotImplemented.hxx>
25 #include <AIS_OffsetDimension.ixx>
28 #include <AIS_Drawer.hxx>
29 #include <BRepBuilderAPI_Transform.hxx>
30 #include <BRepAdaptor_Surface.hxx>
31 #include <BRep_Tool.hxx>
32 #include <DsgPrs_OffsetPresentation.hxx>
35 #include <GeomAbs_SurfaceType.hxx>
36 #include <Graphic3d_Structure.hxx>
37 #include <Precision.hxx>
38 #include <Prs3d_ArrowAspect.hxx>
39 #include <Prs3d_Drawer.hxx>
40 #include <Prs3d_LengthAspect.hxx>
41 #include <Prs3d_LineAspect.hxx>
42 #include <Select3D_SensitiveBox.hxx>
43 #include <Select3D_SensitiveSegment.hxx>
44 #include <SelectMgr_EntityOwner.hxx>
45 #include <StdPrs_WFDeflectionShape.hxx>
46 #include <TColStd_Array2OfReal.hxx>
47 #include <TCollection_ExtendedString.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <gce_MakeLin.hxx>
58 #include <gp_Trsf.hxx>
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 //=======================================================================
153 //purpose : to avoid warning at compilation (SUN)
154 //=======================================================================
155 void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& /*aPresentationManager2d*/,
156 const Handle(Graphic2d_GraphicObject)& /*aGraphicObject*/,
157 const Standard_Integer /*anInteger*/)
159 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)&,const Handle(Graphic2d_GraphicObject)&,const Standard_Integer)");
160 // PrsMgr_PresentableObject::Compute( aPresentationManager2d ,aGraphicObject,anInteger) ;
163 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
164 const Handle(Geom_Transformation)& aTransformation,
165 const Handle(Prs3d_Presentation)& aPresentation)
167 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
168 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
171 //=======================================================================
172 //function : ComputeSelection
174 //=======================================================================
175 void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel,
176 const Standard_Integer)
178 //myArrowSize = fabs (myVal/5.);
179 myArrowSize = fabs (myVal/10.0);
180 if (myArrowSize > 30.) myArrowSize = 30.;
181 if (myArrowSize < 15.) myArrowSize = 15.;
182 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
183 gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
184 gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
185 gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
186 gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
187 gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;
189 gp_Lin L1 (myTFAttach,myTDirAttach);
190 gp_Lin L2 (myTSAttach,myTDirAttach2);
191 gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
192 gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
195 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
197 if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
198 L3 = gce_MakeLin(Proj1,Proj2);
200 else { // cas ou la dimension est nulle
201 if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
202 gp_Vec v3 (Proj1,Tcurpos);
204 L3 = gce_MakeLin(Proj1,d3);
206 L3 = gce_MakeLin(Proj1,myTDirAttach);
210 Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
211 Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
221 Standard_Real parmin,parmax,parcur;
222 parmin = ElCLib::Parameter(L3,Proj1);
225 parcur = ElCLib::Parameter(L3,Proj2);
226 parmin = Min(parmin,parcur);
227 parmax = Max(parmax,parcur);
229 parcur = ElCLib::Parameter(L3,Tcurpos);
230 parmin = Min(parmin,parcur);
231 parmax = Max(parmax,parcur);
233 gp_Pnt PointMin = ElCLib::Value(parmin,L3);
234 gp_Pnt PointMax = ElCLib::Value(parmax,L3);
236 Handle(Select3D_SensitiveSegment) seg;
237 if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
238 seg = new Select3D_SensitiveSegment(own,
244 if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
245 seg = new Select3D_SensitiveSegment(own,
250 if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
251 seg = new Select3D_SensitiveSegment(own,
258 //=======================================================================
259 //function : ComputeTwoAxesOffset
261 //=======================================================================
262 void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs,
263 const gp_Trsf& aTrsf)
265 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
266 BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
268 gp_Ax1 Ax1Surf1, Ax1Surf2;
270 if (surf1.GetType() == GeomAbs_Cylinder) {
271 gp_Cylinder aCyl= surf1.Cylinder();
272 Ax1Surf1 = aCyl.Axis();
273 } else if (surf1.GetType() == GeomAbs_Cone) {
274 gp_Cone aCone= surf1.Cone();
275 Ax1Surf1 = aCone.Axis();
276 } else if (surf1.GetType() == GeomAbs_Torus) {
277 gp_Torus aTore= surf1.Torus();
278 Ax1Surf1 = aTore.Axis();
280 Standard_Real FirstUParam = surf1.FirstUParameter();
281 Standard_Real FirstVParam = surf1.FirstVParameter();
282 Standard_Real LastVParam = surf1.LastVParameter();
283 gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
284 gp_Pnt P1Last = surf1.Value(FirstUParam,LastVParam);
287 if (surf2.GetType() == GeomAbs_Cylinder) {
288 gp_Cylinder aCyl= surf2.Cylinder();
289 Ax1Surf2 = aCyl.Axis();
290 } else if (surf2.GetType() == GeomAbs_Cone) {
291 gp_Cone aCone= surf2.Cone();
292 Ax1Surf2 = aCone.Axis();
293 } else if (surf2.GetType() == GeomAbs_Torus) {
294 gp_Torus aTore= surf2.Torus();
295 Ax1Surf2 = aTore.Axis();
297 FirstUParam = surf2.FirstUParameter();
298 FirstVParam = surf2.FirstVParameter();
299 LastVParam = surf2.LastVParameter();
300 gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
301 gp_Pnt P2Last = surf2.Value(FirstUParam,LastVParam);
305 myFAttach = Ax1Surf1.Location();
306 mySAttach = Ax1Surf2.Location();
307 myDirAttach = Ax1Surf1.Direction();
308 myDirAttach2 = myDirAttach;
310 gp_Lin aProjLine = gce_MakeLin(myFAttach,myDirAttach);
312 if (myAutomaticPosition) {
313 curpos.SetX ( (myFAttach.X() + mySAttach.X()) /2. + 0.01);
314 curpos.SetY ( (myFAttach.Y() + mySAttach.Y()) /2. + 0.01);
315 curpos.SetZ ( (myFAttach.Z() + mySAttach.Z()) /2. + 0.01);
316 // + 0.01 pour eviter un raise de ComputeSelection...
324 curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
325 // on projette pour la presentation
327 gp_Pnt P1FirstProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
328 gp_Pnt P1LastProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
329 if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
330 myFAttach = P1FirstProj;
332 myFAttach = P1LastProj;
334 gp_Pnt P2FirstProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
335 gp_Pnt P2LastProj = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
336 if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
337 mySAttach = P2FirstProj;
339 mySAttach = P2LastProj;
342 Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
343 Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
344 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
345 arr->SetLength(myArrowSize);
346 arr = la->Arrow2Aspect();
347 arr->SetLength(myArrowSize);
349 gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
350 gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
351 gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
352 gp_Dir myTDirAttach2 = myTDirAttach;
353 gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
356 Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
358 DsgPrs_OffsetPresentation::AddAxes(aprs,
367 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
368 TopoDS_Shape myTFShape = transform1.Shape();
369 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
370 TopoDS_Shape myTSShape = transform2.Shape();
372 StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
373 StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
376 //=======================================================================
377 //function : ComputeTwoFacesOffset
379 //=======================================================================
380 void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs,
381 const gp_Trsf& aTrsf)
383 gp_Dir norm1 = myDirAttach;
386 if (myAutomaticPosition && ! myIsSetBndBox) {
387 TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
389 TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
390 myFAttach = BRep_Tool::Pnt(vertref);
391 gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
392 gp_Ax2 ax2(myFAttach,norm1);
393 myDirAttach = ax2.XDirection();
394 curpos = myFAttach.Translated(trans);
395 if (myVal <= Precision::Confusion()) {
396 gp_Vec vecnorm1 = norm1.XYZ()*.001;
397 curpos.Translate(vecnorm1);
404 if (myAutomaticPosition && myIsSetBndBox)
406 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
407 myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
408 myPosition.SetCoord( aXmax, aYmax, aZmax );
412 myFAttach = AIS::Nearest(myFShape,curpos);
413 if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
414 gp_Ax2 ax2(myFAttach,norm1);
415 myDirAttach = ax2.XDirection();
419 gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
420 gp_Ax2 ax2(myFAttach,norm1);
421 if (orient.Angle(norm1) <= Precision::Angular()) {
422 myDirAttach = ax2.XDirection();
425 gp_Dir adir = norm1 ^ orient;
426 myDirAttach = adir ^ norm1;
431 // en attendant mieux
432 mySAttach = AIS::Nearest(mySShape,curpos);
433 gp_Ax3 anax3 (myax2);
437 Standard_Real u2,v2, uatt, vatt;
438 ElSLib::Parameters (apln , mySAttach, uatt,vatt);
439 ElSLib::Parameters (apln , curpos , u2,v2);
441 if (uatt== u2 && vatt == v2) {
442 myDirAttach2 = myDirAttach;
444 gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
445 myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
448 Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
449 Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
450 //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " << myArrowSize << " myArrowSize"<<endl;
451 arr->SetLength(myArrowSize);
452 arr = la->Arrow2Aspect();
453 arr->SetLength(myArrowSize);
455 gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
456 gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
457 gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
458 gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
459 gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
464 BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
465 Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
468 DsgPrs_OffsetPresentation::Add(aprs,
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_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
484 StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
488 //=======================================================================
489 //function : ComputeAxeFaceOffset
491 //=======================================================================
492 void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs,
493 const gp_Trsf& aTrsf)
495 BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
496 TopoDS_Shape myTFShape = transform1.Shape();
497 BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
498 TopoDS_Shape myTSShape = transform2.Shape();
500 StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
501 StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);