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