0029367: Visualization - simplify interface of V3d_View and V3d_Viewer
[occt.git] / src / AIS / AIS_Chamf3dDimension.cxx
1 // Created on: 1996-12-05
2 // Created by: Odile Olivier
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS.hxx>
19 #include <AIS_Chamf3dDimension.hxx>
20 #include <Bnd_Box.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepBndLib.hxx>
23 #include <BRepTools_WireExplorer.hxx>
24 #include <DsgPrs_Chamf2dPresentation.hxx>
25 #include <ElCLib.hxx>
26 #include <Geom_Line.hxx>
27 #include <Geom_Transformation.hxx>
28 #include <gp_Dir.hxx>
29 #include <gp_Pln.hxx>
30 #include <gp_Pnt.hxx>
31 #include <gp_Vec.hxx>
32 #include <Precision.hxx>
33 #include <ProjLib.hxx>
34 #include <Prs3d_ArrowAspect.hxx>
35 #include <Prs3d_DimensionAspect.hxx>
36 #include <Prs3d_Drawer.hxx>
37 #include <Prs3d_Presentation.hxx>
38 #include <Prs3d_Projector.hxx>
39 #include <Select3D_SensitiveBox.hxx>
40 #include <Select3D_SensitiveSegment.hxx>
41 #include <SelectMgr_EntityOwner.hxx>
42 #include <SelectMgr_Selection.hxx>
43 #include <Standard_NotImplemented.hxx>
44 #include <Standard_Type.hxx>
45 #include <TCollection_AsciiString.hxx>
46 #include <TCollection_ExtendedString.hxx>
47 #include <TopAbs_Orientation.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Shape.hxx>
51
52 IMPLEMENT_STANDARD_RTTIEXT(AIS_Chamf3dDimension,AIS_Relation)
53
54 //=======================================================================
55 //function : Constructor
56 //purpose  : 
57 //=======================================================================
58 AIS_Chamf3dDimension::AIS_Chamf3dDimension(const TopoDS_Shape& aFShape, 
59                                            const Standard_Real aVal, 
60                                            const TCollection_ExtendedString& aText)
61 :AIS_Relation()
62 {
63   myFShape = aFShape;
64   myVal = aVal;
65   myText = aText;
66   mySymbolPrs = DsgPrs_AS_LASTAR;
67   myAutomaticPosition = Standard_True;
68
69   myArrowSize = myVal / 100.;
70 }
71 //=======================================================================
72 //function : Constructor
73 //purpose  : 
74 //=======================================================================
75 AIS_Chamf3dDimension::AIS_Chamf3dDimension(const TopoDS_Shape& aFShape, 
76                                            const Standard_Real aVal, 
77                                            const TCollection_ExtendedString& aText,
78                                            const gp_Pnt& aPosition, 
79                                            const DsgPrs_ArrowSide aSymbolPrs ,
80                                            const Standard_Real anArrowSize)
81 :AIS_Relation()
82 {
83   myFShape = aFShape;
84   myVal = aVal;
85   myText = aText;
86   myPosition = aPosition;
87   mySymbolPrs = aSymbolPrs;
88   SetArrowSize( anArrowSize );
89   myAutomaticPosition = Standard_False;
90 }
91
92
93 //=======================================================================
94 //function : Compute
95 //purpose  : 
96 //=======================================================================
97
98 void AIS_Chamf3dDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& , 
99                                    const Handle(Prs3d_Presentation)& aPresentation, 
100                                    const Standard_Integer)
101 {
102   //----------------------------
103   // Calcul du centre de la face
104   //----------------------------
105   BRepAdaptor_Surface surfAlgo (TopoDS::Face(myFShape));
106   Standard_Real uFirst, uLast, vFirst, vLast;
107   uFirst = surfAlgo.FirstUParameter();
108   uLast = surfAlgo.LastUParameter();
109   vFirst = surfAlgo.FirstVParameter();
110   vLast = surfAlgo.LastVParameter();
111   Standard_Real uMoy = (uFirst + uLast)/2;
112   Standard_Real vMoy = (vFirst + vLast)/2;
113   gp_Pnt apos ;
114   gp_Vec d1u, d1v;
115   surfAlgo.D1(uMoy, vMoy, apos, d1u, d1v);
116   myPntAttach = apos;
117
118   myDir = d1u ^ d1v;
119 //  myDir = surfAlgo.Plane().Axis().Direction();
120
121
122  
123    
124   //--------------------------------------------
125   //Calcul du point de positionnement du texte
126   //--------------------------------------------
127   gp_Pnt curpos;
128   if (myAutomaticPosition) {
129     gp_Vec transVec(myDir);
130     transVec*=myVal;
131     curpos = myPntAttach.Translated(transVec);
132     
133     if (myIsSetBndBox)
134       curpos = AIS::TranslatePointToBound( curpos, myDir, myBndBox );
135     
136     myPosition = curpos;
137   }
138   else {
139     
140     Handle(Geom_Line) dimLin = new Geom_Line(myPntAttach, myDir);
141     Standard_Real parcurpos = ElCLib::Parameter(dimLin->Lin(),myPosition);
142     curpos = ElCLib::Value(parcurpos,dimLin->Lin());
143
144     if ( curpos.Distance(myPntAttach) < 5. ) {
145       gp_Vec transVec(myDir);
146       transVec*=5.;
147       curpos = myPntAttach.Translated(transVec);
148     }
149     myPosition = curpos;
150   }
151     
152   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
153   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
154     
155   //-------------------------------------------------
156   //Calcul de la boite englobante du component pour
157   //determiner la taille de la fleche
158   //-------------------------------------------------
159   if( !myArrowSizeIsDefined ) {
160     Standard_Real arrsize = myArrowSize;
161     if ( (myVal/4) < arrsize)
162       arrsize = myVal/4;
163     if (arrsize > 30.) 
164       arrsize = 30.;
165     else if (arrsize < 8.)
166       arrsize = 8.;
167     myArrowSize = arrsize;
168   }
169   arr->SetLength(myArrowSize);
170   
171   //Calcul de la presentation
172   DsgPrs_Chamf2dPresentation::Add(aPresentation,
173                                   myDrawer,
174                                   myPntAttach,
175                                   curpos,
176                                   myText,
177                                   mySymbolPrs);
178   
179 }
180
181 //=======================================================================
182 //function : Compute
183 //purpose  : to avoid warning
184 //=======================================================================
185
186 void AIS_Chamf3dDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
187                                    const Handle(Prs3d_Presentation)& aPresentation)
188 {
189 // throw Standard_NotImplemented("AIS_Chamf3dDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
190  PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
191 }
192
193 void AIS_Chamf3dDimension::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Geom_Transformation)& aTransformation, const Handle(Prs3d_Presentation)& aPresentation)
194 {
195 // throw Standard_NotImplemented("AIS_Chamf3dDimension::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
196   PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
197 }
198
199 //=======================================================================
200 //function : ComputeSelection
201 //purpose  : 
202 //=======================================================================
203
204 void AIS_Chamf3dDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
205                                             const Standard_Integer)
206 {
207   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
208   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(own,myPntAttach,myPosition);
209   aSelection->Add(seg);
210
211   // Text
212   Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
213   Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
214                                                                    myPosition.X(),
215                                                                    myPosition.Y(),
216                                                                    myPosition.Z(),
217                                                                    myPosition.X() + size,
218                                                                    myPosition.Y() + size,
219                                                                    myPosition.Z() + size);    
220   aSelection->Add(box);
221 }
222