c3edce7187244f120ec6ce693b3bcb3d1e24554e
[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 <Select3D_SensitiveBox.hxx>
39 #include <Select3D_SensitiveSegment.hxx>
40 #include <SelectMgr_EntityOwner.hxx>
41 #include <SelectMgr_Selection.hxx>
42 #include <Standard_NotImplemented.hxx>
43 #include <Standard_Type.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <TCollection_ExtendedString.hxx>
46 #include <TopAbs_Orientation.hxx>
47 #include <TopoDS.hxx>
48 #include <TopoDS_Edge.hxx>
49 #include <TopoDS_Shape.hxx>
50
51 IMPLEMENT_STANDARD_RTTIEXT(AIS_Chamf3dDimension,AIS_Relation)
52
53 //=======================================================================
54 //function : Constructor
55 //purpose  : 
56 //=======================================================================
57 AIS_Chamf3dDimension::AIS_Chamf3dDimension(const TopoDS_Shape& aFShape, 
58                                            const Standard_Real aVal, 
59                                            const TCollection_ExtendedString& aText)
60 :AIS_Relation()
61 {
62   myFShape = aFShape;
63   myVal = aVal;
64   myText = aText;
65   mySymbolPrs = DsgPrs_AS_LASTAR;
66   myAutomaticPosition = Standard_True;
67
68   myArrowSize = myVal / 100.;
69 }
70 //=======================================================================
71 //function : Constructor
72 //purpose  : 
73 //=======================================================================
74 AIS_Chamf3dDimension::AIS_Chamf3dDimension(const TopoDS_Shape& aFShape, 
75                                            const Standard_Real aVal, 
76                                            const TCollection_ExtendedString& aText,
77                                            const gp_Pnt& aPosition, 
78                                            const DsgPrs_ArrowSide aSymbolPrs ,
79                                            const Standard_Real anArrowSize)
80 :AIS_Relation()
81 {
82   myFShape = aFShape;
83   myVal = aVal;
84   myText = aText;
85   myPosition = aPosition;
86   mySymbolPrs = aSymbolPrs;
87   SetArrowSize( anArrowSize );
88   myAutomaticPosition = Standard_False;
89 }
90
91
92 //=======================================================================
93 //function : Compute
94 //purpose  : 
95 //=======================================================================
96
97 void AIS_Chamf3dDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& , 
98                                    const Handle(Prs3d_Presentation)& aPresentation, 
99                                    const Standard_Integer)
100 {
101   //----------------------------
102   // Calcul du centre de la face
103   //----------------------------
104   BRepAdaptor_Surface surfAlgo (TopoDS::Face(myFShape));
105   Standard_Real uFirst, uLast, vFirst, vLast;
106   uFirst = surfAlgo.FirstUParameter();
107   uLast = surfAlgo.LastUParameter();
108   vFirst = surfAlgo.FirstVParameter();
109   vLast = surfAlgo.LastVParameter();
110   Standard_Real uMoy = (uFirst + uLast)/2;
111   Standard_Real vMoy = (vFirst + vLast)/2;
112   gp_Pnt apos ;
113   gp_Vec d1u, d1v;
114   surfAlgo.D1(uMoy, vMoy, apos, d1u, d1v);
115   myPntAttach = apos;
116
117   myDir = d1u ^ d1v;
118 //  myDir = surfAlgo.Plane().Axis().Direction();
119
120
121  
122    
123   //--------------------------------------------
124   //Calcul du point de positionnement du texte
125   //--------------------------------------------
126   gp_Pnt curpos;
127   if (myAutomaticPosition) {
128     gp_Vec transVec(myDir);
129     transVec*=myVal;
130     curpos = myPntAttach.Translated(transVec);
131     
132     if (myIsSetBndBox)
133       curpos = AIS::TranslatePointToBound( curpos, myDir, myBndBox );
134     
135     myPosition = curpos;
136   }
137   else {
138     
139     Handle(Geom_Line) dimLin = new Geom_Line(myPntAttach, myDir);
140     Standard_Real parcurpos = ElCLib::Parameter(dimLin->Lin(),myPosition);
141     curpos = ElCLib::Value(parcurpos,dimLin->Lin());
142
143     if ( curpos.Distance(myPntAttach) < 5. ) {
144       gp_Vec transVec(myDir);
145       transVec*=5.;
146       curpos = myPntAttach.Translated(transVec);
147     }
148     myPosition = curpos;
149   }
150     
151   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
152   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
153     
154   //-------------------------------------------------
155   //Calcul de la boite englobante du component pour
156   //determiner la taille de la fleche
157   //-------------------------------------------------
158   if( !myArrowSizeIsDefined ) {
159     Standard_Real arrsize = myArrowSize;
160     if ( (myVal/4) < arrsize)
161       arrsize = myVal/4;
162     if (arrsize > 30.) 
163       arrsize = 30.;
164     else if (arrsize < 8.)
165       arrsize = 8.;
166     myArrowSize = arrsize;
167   }
168   arr->SetLength(myArrowSize);
169   
170   //Calcul de la presentation
171   DsgPrs_Chamf2dPresentation::Add(aPresentation,
172                                   myDrawer,
173                                   myPntAttach,
174                                   curpos,
175                                   myText,
176                                   mySymbolPrs);
177   
178 }
179
180 //=======================================================================
181 //function : ComputeSelection
182 //purpose  : 
183 //=======================================================================
184
185 void AIS_Chamf3dDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
186                                             const Standard_Integer)
187 {
188   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
189   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(own,myPntAttach,myPosition);
190   aSelection->Add(seg);
191
192   // Text
193   Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
194   Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
195                                                                    myPosition.X(),
196                                                                    myPosition.Y(),
197                                                                    myPosition.Z(),
198                                                                    myPosition.X() + size,
199                                                                    myPosition.Y() + size,
200                                                                    myPosition.Z() + size);    
201   aSelection->Add(box);
202 }
203