0027916: Visualization - access violation occurs within AIS_ColoredShape::Compute...
[occt.git] / src / AIS / AIS_MinRadiusDimension.cxx
1 // Created on: 1998-01-27
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 1998-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_EllipseRadiusDimension.hxx>
20 #include <AIS_MinRadiusDimension.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepAdaptor_Surface.hxx>
23 #include <DsgPrs_EllipseRadiusPresentation.hxx>
24 #include <ElCLib.hxx>
25 #include <ElSLib.hxx>
26 #include <Geom_CylindricalSurface.hxx>
27 #include <Geom_Ellipse.hxx>
28 #include <Geom_OffsetCurve.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Geom_Surface.hxx>
31 #include <Geom_SurfaceOfLinearExtrusion.hxx>
32 #include <Geom_SurfaceOfRevolution.hxx>
33 #include <Geom_Transformation.hxx>
34 #include <Geom_TrimmedCurve.hxx>
35 #include <gp_Ax1.hxx>
36 #include <gp_Dir.hxx>
37 #include <gp_Lin.hxx>
38 #include <gp_Pln.hxx>
39 #include <gp_Pnt.hxx>
40 #include <gp_Vec.hxx>
41 #include <Precision.hxx>
42 #include <Prs3d_ArrowAspect.hxx>
43 #include <Prs3d_DimensionAspect.hxx>
44 #include <Prs3d_Drawer.hxx>
45 #include <Prs3d_Presentation.hxx>
46 #include <Prs3d_Projector.hxx>
47 #include <Prs3d_Text.hxx>
48 #include <Prs3d_TextAspect.hxx>
49 #include <Select3D_SensitiveBox.hxx>
50 #include <Select3D_SensitiveCurve.hxx>
51 #include <Select3D_SensitiveSegment.hxx>
52 #include <SelectMgr_EntityOwner.hxx>
53 #include <SelectMgr_Selection.hxx>
54 #include <Standard_ConstructionError.hxx>
55 #include <Standard_NotImplemented.hxx>
56 #include <Standard_Type.hxx>
57 #include <TCollection_ExtendedString.hxx>
58 #include <TopoDS.hxx>
59 #include <TopoDS_Shape.hxx>
60
61 IMPLEMENT_STANDARD_RTTIEXT(AIS_MinRadiusDimension,AIS_EllipseRadiusDimension)
62
63 //=======================================================================
64 //function : AIS_MinRadiusDimension
65 //purpose  : 
66 //=======================================================================
67 AIS_MinRadiusDimension::AIS_MinRadiusDimension(const TopoDS_Shape& aShape, 
68                                                const Standard_Real aVal, 
69                                                const TCollection_ExtendedString& aText)
70 :AIS_EllipseRadiusDimension(aShape, aText)
71 {
72   myVal = aVal;
73   mySymbolPrs = DsgPrs_AS_LASTAR;
74   myAutomaticPosition = Standard_True;
75   myArrowSize = myVal / 100.;
76 }
77
78 //=======================================================================
79 //function : AIS_MinRadiusDimension
80 //purpose  : 
81 //=======================================================================
82
83 AIS_MinRadiusDimension::AIS_MinRadiusDimension(const TopoDS_Shape& aShape, 
84                                                const Standard_Real aVal, 
85                                                const TCollection_ExtendedString& aText,
86                                                const gp_Pnt& aPosition, 
87                                                const DsgPrs_ArrowSide aSymbolPrs,
88                                                const Standard_Real anArrowSize)
89 :AIS_EllipseRadiusDimension(aShape, aText)
90 {
91   myVal = aVal;
92   mySymbolPrs = aSymbolPrs;
93   myPosition = aPosition;
94   myAutomaticPosition = Standard_False;
95   SetArrowSize( anArrowSize );
96 }
97
98 //=======================================================================
99 //function : Compute
100 //purpose  : 
101 //=======================================================================
102
103 void AIS_MinRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
104                                      const Handle(Prs3d_Presentation)& aPresentation, 
105                                      const Standard_Integer /*aMode*/)
106 {
107 //  if( myAutomaticPosition )
108     //{ //ota : recompute ellipse always
109   ComputeGeometry();
110   myEllipse.SetMinorRadius(myVal);
111   gp_Vec v1(myEllipse.YAxis().Direction());
112   v1 *=myVal;
113   myApexP = myEllipse.Location().Translated(v1); 
114   myApexN = myEllipse.Location().Translated(-v1); 
115 //   }
116   if(myIsAnArc) ComputeArcOfEllipse(aPresentation);
117   else 
118     ComputeEllipse(aPresentation);
119 }
120
121 //=======================================================================
122 //function : Compute
123 //purpose  : to avoid warning
124 //=======================================================================
125
126 void  AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
127                                       const Handle(Prs3d_Presentation)& aPresentation)
128 {
129 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)");
130  PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
131 }
132
133 //=======================================================================
134 //function : Compute
135 //purpose  : 
136 //=======================================================================
137
138 void AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
139                                      const Handle(Geom_Transformation)& aTransformation,
140                                      const Handle(Prs3d_Presentation)& aPresentation)
141 {
142 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
143  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
144 }
145
146 //=======================================================================
147 //function : ComputeEllipse
148 //purpose  : 
149 //=======================================================================
150
151 void AIS_MinRadiusDimension::ComputeEllipse(const Handle(Prs3d_Presentation)& aPresentation)
152 {
153
154   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
155   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
156   
157   // size
158   if( !myArrowSizeIsDefined ) {
159     myArrowSize = Min(myArrowSize,myVal/5.);
160   }
161   arr->SetLength(myArrowSize);
162
163   Standard_Real U;//,V;
164   gp_Pnt curPos, Center;
165   Center = myEllipse.Location();
166   if( myAutomaticPosition )
167     {
168       myPosition = Center;
169       myEndOfArrow = myApexP;
170       myAutomaticPosition = Standard_True;   
171       
172       if ( myIsSetBndBox )
173         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
174                                                 myBndBox );        
175       curPos = myPosition;  
176     }
177   else //!AutomaticPosition
178     {
179       curPos = myPosition;
180       gp_Lin L1(myEllipse.YAxis());
181       U = ElCLib::Parameter ( L1, curPos );
182       curPos = ElCLib::Value (U, L1);
183       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
184         myEndOfArrow = myApexP ;
185       else
186         myEndOfArrow = myApexN ;
187       myPosition = curPos;
188     }
189  
190   // Presenatation  
191   DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, curPos,
192                                         myEndOfArrow, Center, Standard_False, mySymbolPrs);
193
194 }
195
196 //=======================================================================
197 //function : ComputeArcOfEllipse
198 //purpose  : 
199 //=======================================================================
200
201 void AIS_MinRadiusDimension::ComputeArcOfEllipse(const Handle(Prs3d_Presentation)& aPresentation)
202 {
203
204   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
205   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
206   
207   // size
208   if( !myArrowSizeIsDefined ) {
209     myArrowSize = Min(myArrowSize,myVal/5.);
210   }
211   arr->SetLength(myArrowSize);
212   
213   Standard_Real par;
214   gp_Pnt curPos, Center;
215   Center = myEllipse.Location();
216   Standard_Boolean IsInDomain = Standard_True;
217   if( myAutomaticPosition )
218     {
219       myEndOfArrow = AIS::NearestApex(myEllipse, myApexP, myApexN,
220                                       myFirstPar, myLastPar, IsInDomain);
221       myPosition = Center;
222       myAutomaticPosition = Standard_True;
223       if ( myIsSetBndBox )
224         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
225                                                 myBndBox );
226       curPos = myPosition;  
227
228     }
229   else //!AutomaticPosition
230     {
231       curPos = myPosition;
232       gp_Lin L1(myEllipse.YAxis());
233       par = ElCLib::Parameter ( L1, curPos );
234       curPos = ElCLib::Value (par, L1);
235       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
236         myEndOfArrow = myApexP ;
237       else
238         myEndOfArrow = myApexN ;
239       par = ElCLib::Parameter ( myEllipse, myEndOfArrow );
240       IsInDomain = AIS::InDomain(myFirstPar, myLastPar, par);
241       myPosition = curPos;
242     }
243
244   Standard_Real parStart =0.;
245   if( !IsInDomain )
246     {
247       if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
248          AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
249         parStart = myFirstPar;
250       else
251         parStart = myLastPar;
252
253     }
254
255   if(!myIsOffset)
256     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myEllipse,
257                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
258                                           Standard_True, mySymbolPrs);
259   else 
260     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myOffsetCurve,
261                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
262                                           Standard_True, mySymbolPrs);
263 }
264
265
266 //=======================================================================
267 //function : ComputeSelection
268 //purpose  : 
269 //=======================================================================
270
271 void AIS_MinRadiusDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
272                                               const Standard_Integer /*aMode*/)
273 {
274
275     gp_Pnt        center   = myEllipse.Location();
276     gp_Pnt AttachmentPoint = myPosition;
277     Standard_Real dist    = center.Distance(AttachmentPoint);
278     Standard_Real aRadius = myVal;
279     //Standard_Real inside  = Standard_False;
280     gp_Pnt pt1;
281     if (dist > aRadius) pt1 = AttachmentPoint; 
282     else 
283       pt1 = myEndOfArrow;
284     Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
285     Handle(Select3D_SensitiveSegment) 
286       seg = new Select3D_SensitiveSegment(own, center , pt1);
287     aSelection->Add(seg);
288
289     // Text
290     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
291     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
292                                                                     AttachmentPoint.X(),
293                                                                     AttachmentPoint.Y(),
294                                                                     AttachmentPoint.Z(),
295                                                                     AttachmentPoint.X()+size,
296                                                                     AttachmentPoint.Y()+size,
297                                                                     AttachmentPoint.Z()+size);
298     aSelection->Add(box);
299
300   // Arc of Ellipse
301     if(myIsAnArc)
302       {
303         
304         Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
305         if(!AIS::InDomain(myFirstPar, myLastPar, parEnd))
306           {
307             Standard_Real parStart, par;
308             if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
309                AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
310               par = myFirstPar;
311             else
312               par = myLastPar;
313             gp_Vec Vapex(center, ElCLib::Value( parEnd, myEllipse )) ;
314             gp_Vec Vpnt (center, ElCLib::Value( par, myEllipse )) ;
315             gp_Dir dir(Vpnt ^ Vapex);
316             if(myEllipse.Position().Direction().IsOpposite( dir, Precision::Angular())) {
317               parStart = parEnd;
318               parEnd   = par;
319             }
320             else 
321               parStart = par;
322             Handle(Geom_Curve)TrimCurve;
323             if(myIsOffset)
324               {
325                 Handle(Geom_Curve) aCurve = myOffsetCurve;
326                 TrimCurve = new Geom_TrimmedCurve( aCurve,  parStart, parEnd );
327               }
328             else
329               {
330                 Handle(Geom_Ellipse) Ellipse = new Geom_Ellipse( myEllipse );
331                 TrimCurve = new Geom_TrimmedCurve( Ellipse,  parStart, parEnd );
332               }
333             Handle( Select3D_SensitiveCurve ) SensArc;
334             SensArc = new Select3D_SensitiveCurve( own, TrimCurve );
335             aSelection->Add( SensArc );
336           }
337     }
338
339 }