Salome HOME
Issue #106 (reopened) fix: Crash on distance between coincident points
[modules/shaper.git] / src / GeomAPI / GeomAPI_AISObject.cpp
1 // File:        GeomAPI_AISObject.cpp
2 // Created:     25 Jun 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAPI_AISObject.h>
6
7 #include <GeomAPI_Circ.h>
8 #include <GeomAPI_Dir.h>
9 #include <GeomAPI_Lin.h>
10 #include <GeomAPI_Pln.h>
11 #include <GeomAPI_Pnt.h>
12 #include <GeomAPI_Shape.h>
13 #include <GeomAPI_XYZ.h>
14
15 #include <Geom_Plane.hxx>
16 #include <TopoDS_Shape.hxx>
17 #include <Quantity_NameOfColor.hxx>
18
19 #include <AIS_InteractiveObject.hxx>
20 #include <AIS_LengthDimension.hxx>
21 #include <AIS_ParallelRelation.hxx>
22 #include <AIS_PerpendicularRelation.hxx>
23 #include <AIS_RadiusDimension.hxx>
24 #include <AIS_Shape.hxx>
25
26 const double tolerance = 1e-7;
27
28 const int CONSTRAINT_TEXT_HEIGHT = 28;  /// the text height of the constraint
29 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20;  /// the text selection tolerance
30
31 // Initialization of color constants
32 int Colors::COLOR_BROWN = Quantity_NOC_BROWN;
33 int Colors::COLOR_RED = Quantity_NOC_RED;
34 int Colors::COLOR_GREEN = Quantity_NOC_GREEN;
35 int Colors::COLOR_BLUE = Quantity_NOC_BLUE1;
36
37 GeomAPI_AISObject::GeomAPI_AISObject()
38     : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
39 {
40 }
41
42 void GeomAPI_AISObject::createShape(boost::shared_ptr<GeomAPI_Shape> theShape)
43 {
44   const TopoDS_Shape& aTDS =
45       (theShape && theShape->implPtr<TopoDS_Shape>()) ?
46           theShape->impl<TopoDS_Shape>() : TopoDS_Shape();
47
48   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
49   if (!anAIS.IsNull()) {
50     Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
51     if (aShapeAIS) {
52       // if the AIS object is displayed in the opened local context in some mode, additional
53       // AIS sub objects are created there. They should be rebuild for correct selecting.
54       // It is possible to correct it by closing local context before the shape set and opening
55       // after. Another workaround to thrown down the selection and reselecting the AIS.
56       // If there was a problem here, try the first solution with close/open local context.
57
58       aShapeAIS->Set(aTDS);
59       aShapeAIS->Redisplay(Standard_True);
60     }
61   } else
62     setImpl(new Handle(AIS_InteractiveObject)(new AIS_Shape(aTDS)));
63 }
64
65 void GeomAPI_AISObject::createDistance(boost::shared_ptr<GeomAPI_Pnt> theStartPoint,
66                                        boost::shared_ptr<GeomAPI_Pnt> theEndPoint,
67                                        boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
68                                        boost::shared_ptr<GeomAPI_Pln> thePlane, double theDistance)
69 {
70   double aFlyout = 0;
71   if (theFlyoutPoint) {
72     double aDist = 0.0;
73     if (theStartPoint->distance(theEndPoint) < tolerance)
74       aDist = theStartPoint->distance(theFlyoutPoint);
75     else {
76       boost::shared_ptr<GeomAPI_Lin> aLine = boost::shared_ptr<GeomAPI_Lin>(
77           new GeomAPI_Lin(theStartPoint, theEndPoint));
78       aDist = aLine->distance(theFlyoutPoint);
79     }
80
81     boost::shared_ptr<GeomAPI_XYZ> aLineDir = theEndPoint->xyz()->decreased(theStartPoint->xyz());
82     boost::shared_ptr<GeomAPI_XYZ> aFOutDir = theFlyoutPoint->xyz()->decreased(
83         theStartPoint->xyz());
84     boost::shared_ptr<GeomAPI_XYZ> aNorm = thePlane->direction()->xyz();
85     if (aLineDir->cross(aFOutDir)->dot(aNorm) < 0)
86       aDist = -aDist;
87     aFlyout = aDist;
88   }
89
90   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
91   if (anAIS.IsNull()) {
92     Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension(theStartPoint->impl<gp_Pnt>(),
93                                                                   theEndPoint->impl<gp_Pnt>(),
94                                                                   thePlane->impl<gp_Pln>());
95     aDimAIS->SetCustomValue(theDistance);
96
97     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
98     anAspect->MakeArrows3d(Standard_False);
99     anAspect->MakeText3d(false);
100     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
101     anAspect->MakeTextShaded(false);
102     anAspect->ArrowAspect()->SetLength(theDistance / 10.);
103     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
104     aDimAIS->SetDimensionAspect(anAspect);
105     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
106     aDimAIS->SetFlyout(aFlyout);
107
108     setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
109   } else {
110     // update presentation
111     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
112     if (!aDimAIS.IsNull()) {
113       aDimAIS->SetMeasuredGeometry(theStartPoint->impl<gp_Pnt>(), theEndPoint->impl<gp_Pnt>(),
114                                    thePlane->impl<gp_Pln>());
115       aDimAIS->SetCustomValue(theDistance);
116       aDimAIS->SetFlyout(aFlyout);
117
118       aDimAIS->Redisplay(Standard_True);
119     }
120   }
121 }
122
123 void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
124                                      boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
125                                      double theRadius)
126 {
127   boost::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
128
129   // TODO: a bug in AIS_RadiusDimension:
130   // The anchor point can't be myCirc.Location() - an exception is raised.
131   // But we need exactly this case...
132   // We want to show a radius dimension starting from the circle centre and 
133   // ending at the user-defined point.
134   // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
135   boost::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
136   boost::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
137   anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
138   boost::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
139   const double aDelta = 1e-3;
140   anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
141   anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
142   anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
143
144   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
145   if (anAIS.IsNull()) {
146     Handle(AIS_RadiusDimension) aDimAIS = new AIS_RadiusDimension(theCircle->impl<gp_Circ>(),
147                                                                   anAnchor->impl<gp_Pnt>());
148     aDimAIS->SetCustomValue(theRadius);
149
150     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
151     anAspect->MakeArrows3d(Standard_False);
152     anAspect->MakeText3d(false);
153     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
154     anAspect->ArrowAspect()->SetLength(theRadius / 5.);
155     anAspect->MakeTextShaded(false);
156     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
157     aDimAIS->SetDimensionAspect(anAspect);
158     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
159
160     setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
161   } else {
162     // update presentation
163     Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
164     if (!aDimAIS.IsNull()) {
165       aDimAIS->SetMeasuredGeometry(theCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
166       aDimAIS->SetCustomValue(theRadius);
167       aDimAIS->Redisplay(Standard_True);
168     }
169   }
170 }
171
172 void GeomAPI_AISObject::createParallel(boost::shared_ptr<GeomAPI_Shape> theLine1,
173                                        boost::shared_ptr<GeomAPI_Shape> theLine2,
174                                        boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
175                                        boost::shared_ptr<GeomAPI_Pln> thePlane)
176 {
177   Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
178   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
179   if (anAIS.IsNull()) {
180     Handle(AIS_ParallelRelation) aParallel = new AIS_ParallelRelation(
181         theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
182     if (theFlyoutPoint)
183       aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
184
185     setImpl(new Handle(AIS_InteractiveObject)(aParallel));
186   } else {
187     Handle(AIS_ParallelRelation) aParallel = Handle(AIS_ParallelRelation)::DownCast(anAIS);
188     if (!aParallel.IsNull()) {
189       aParallel->SetFirstShape(theLine1->impl<TopoDS_Shape>());
190       aParallel->SetSecondShape(theLine2->impl<TopoDS_Shape>());
191       aParallel->SetPlane(aPlane);
192       if (theFlyoutPoint)
193         aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
194       aParallel->Redisplay(Standard_True);
195     }
196   }
197 }
198
199 void GeomAPI_AISObject::createPerpendicular(boost::shared_ptr<GeomAPI_Shape> theLine1,
200                                             boost::shared_ptr<GeomAPI_Shape> theLine2,
201                                             boost::shared_ptr<GeomAPI_Pln> thePlane)
202 {
203   Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
204   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
205   if (anAIS.IsNull()) {
206     Handle(AIS_PerpendicularRelation) aPerpendicular = new AIS_PerpendicularRelation(
207         theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
208
209     setImpl(new Handle(AIS_InteractiveObject)(aPerpendicular));
210   } else {
211     Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(
212         anAIS);
213     if (!aPerpendicular.IsNull()) {
214       aPerpendicular->SetFirstShape(theLine1->impl<TopoDS_Shape>());
215       aPerpendicular->SetSecondShape(theLine2->impl<TopoDS_Shape>());
216       aPerpendicular->SetPlane(aPlane);
217       aPerpendicular->Redisplay(Standard_True);
218     }
219   }
220 }
221
222 void GeomAPI_AISObject::setColor(const int& theColor)
223 {
224   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
225   if (anAIS.IsNull())
226     return;
227   Quantity_Color aColor((Quantity_NameOfColor) theColor);
228   anAIS->SetColor(aColor);
229   Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
230   if (!aDimAIS.IsNull()) {
231     aDimAIS->DimensionAspect()->SetCommonColor(aColor);
232   }
233 }
234
235 void GeomAPI_AISObject::setWidth(const double& theWidth)
236 {
237   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
238   if (anAIS.IsNull())
239     return;
240   anAIS->SetWidth(theWidth);
241 }
242
243 void GeomAPI_AISObject::setColor(int theR, int theG, int theB)
244 {
245   Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
246   if (anAIS.IsNull())
247     return;
248   Quantity_Color aColor(theR / 255., theG / 255., theB / 255., Quantity_TOC_RGB);
249   anAIS->SetColor(aColor);
250   Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
251   if (!aDimAIS.IsNull()) {
252     aDimAIS->DimensionAspect()->SetCommonColor(aColor);
253   }
254 }
255
256 bool GeomAPI_AISObject::empty() const
257 {
258   Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
259       ->impl<Handle(AIS_InteractiveObject)>();
260   if (anAIS.IsNull())
261     return true;
262   return false;
263 }
264