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