1 // File: GeomAPI_AISObject.cpp
2 // Created: 25 Jun 2014
3 // Author: Artem ZHIDKOV
5 #include <GeomAPI_AISObject.h>
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>
15 #include <Geom_Plane.hxx>
16 #include <TopoDS_Shape.hxx>
17 #include <Quantity_NameOfColor.hxx>
18 #include <BRepBndLib.hxx>
20 #include <AIS_InteractiveObject.hxx>
21 #include <AIS_LengthDimension.hxx>
22 #include <AIS_ParallelRelation.hxx>
23 #include <AIS_PerpendicularRelation.hxx>
24 #include <AIS_RadiusDimension.hxx>
25 #include <AIS_Shape.hxx>
26 #include <AIS_FixRelation.hxx>
28 const double tolerance = 1e-7;
30 const int CONSTRAINT_TEXT_HEIGHT = 28; /// the text height of the constraint
31 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20; /// the text selection tolerance
33 // Initialization of color constants
34 int Colors::COLOR_BROWN = Quantity_NOC_BROWN;
35 int Colors::COLOR_RED = Quantity_NOC_RED;
36 int Colors::COLOR_GREEN = Quantity_NOC_GREEN;
37 int Colors::COLOR_BLUE = Quantity_NOC_BLUE1;
39 GeomAPI_AISObject::GeomAPI_AISObject()
40 : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
44 void GeomAPI_AISObject::createShape(boost::shared_ptr<GeomAPI_Shape> theShape)
46 const TopoDS_Shape& aTDS =
47 (theShape && theShape->implPtr<TopoDS_Shape>()) ?
48 theShape->impl<TopoDS_Shape>() : TopoDS_Shape();
50 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
51 if (!anAIS.IsNull()) {
52 Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
54 // if the AIS object is displayed in the opened local context in some mode, additional
55 // AIS sub objects are created there. They should be rebuild for correct selecting.
56 // It is possible to correct it by closing local context before the shape set and opening
57 // after. Another workaround to thrown down the selection and reselecting the AIS.
58 // If there was a problem here, try the first solution with close/open local context.
61 aShapeAIS->Redisplay(Standard_True);
64 setImpl(new Handle(AIS_InteractiveObject)(new AIS_Shape(aTDS)));
67 void GeomAPI_AISObject::createDistance(boost::shared_ptr<GeomAPI_Pnt> theStartPoint,
68 boost::shared_ptr<GeomAPI_Pnt> theEndPoint,
69 boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
70 boost::shared_ptr<GeomAPI_Pln> thePlane, double theDistance)
75 if (theStartPoint->distance(theEndPoint) < tolerance)
76 aDist = theStartPoint->distance(theFlyoutPoint);
78 boost::shared_ptr<GeomAPI_Lin> aLine = boost::shared_ptr<GeomAPI_Lin>(
79 new GeomAPI_Lin(theStartPoint, theEndPoint));
80 aDist = aLine->distance(theFlyoutPoint);
83 boost::shared_ptr<GeomAPI_XYZ> aLineDir = theEndPoint->xyz()->decreased(theStartPoint->xyz());
84 boost::shared_ptr<GeomAPI_XYZ> aFOutDir = theFlyoutPoint->xyz()->decreased(
85 theStartPoint->xyz());
86 boost::shared_ptr<GeomAPI_XYZ> aNorm = thePlane->direction()->xyz();
87 if (aLineDir->cross(aFOutDir)->dot(aNorm) < 0)
92 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
94 Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension(theStartPoint->impl<gp_Pnt>(),
95 theEndPoint->impl<gp_Pnt>(),
96 thePlane->impl<gp_Pln>());
97 aDimAIS->SetCustomValue(theDistance);
99 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
100 anAspect->MakeArrows3d(Standard_False);
101 anAspect->MakeText3d(false);
102 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
103 anAspect->MakeTextShaded(false);
104 anAspect->ArrowAspect()->SetLength(theDistance / 10.);
105 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
106 aDimAIS->SetDimensionAspect(anAspect);
107 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
108 aDimAIS->SetFlyout(aFlyout);
110 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
112 // update presentation
113 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
114 if (!aDimAIS.IsNull()) {
115 aDimAIS->SetMeasuredGeometry(theStartPoint->impl<gp_Pnt>(), theEndPoint->impl<gp_Pnt>(),
116 thePlane->impl<gp_Pln>());
117 aDimAIS->SetCustomValue(theDistance);
118 aDimAIS->SetFlyout(aFlyout);
120 aDimAIS->Redisplay(Standard_True);
125 void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
126 boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
129 boost::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
131 // TODO: a bug in AIS_RadiusDimension:
132 // The anchor point can't be myCirc.Location() - an exception is raised.
133 // But we need exactly this case...
134 // We want to show a radius dimension starting from the circle centre and
135 // ending at the user-defined point.
136 // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
137 boost::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
138 boost::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
139 anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
140 boost::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
141 const double aDelta = 1e-3;
142 anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
143 anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
144 anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
146 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
147 if (anAIS.IsNull()) {
148 Handle(AIS_RadiusDimension) aDimAIS = new AIS_RadiusDimension(theCircle->impl<gp_Circ>(),
149 anAnchor->impl<gp_Pnt>());
150 aDimAIS->SetCustomValue(theRadius);
152 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
153 anAspect->MakeArrows3d(Standard_False);
154 anAspect->MakeText3d(false);
155 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
156 anAspect->ArrowAspect()->SetLength(theRadius / 5.);
157 anAspect->MakeTextShaded(false);
158 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
159 aDimAIS->SetDimensionAspect(anAspect);
160 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
162 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
164 // update presentation
165 Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
166 if (!aDimAIS.IsNull()) {
167 aDimAIS->SetMeasuredGeometry(theCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
168 aDimAIS->SetCustomValue(theRadius);
169 aDimAIS->Redisplay(Standard_True);
174 void GeomAPI_AISObject::createParallel(boost::shared_ptr<GeomAPI_Shape> theLine1,
175 boost::shared_ptr<GeomAPI_Shape> theLine2,
176 boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
177 boost::shared_ptr<GeomAPI_Pln> thePlane)
179 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
180 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
181 if (anAIS.IsNull()) {
182 Handle(AIS_ParallelRelation) aParallel = new AIS_ParallelRelation(
183 theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
185 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
187 setImpl(new Handle(AIS_InteractiveObject)(aParallel));
189 Handle(AIS_ParallelRelation) aParallel = Handle(AIS_ParallelRelation)::DownCast(anAIS);
190 if (!aParallel.IsNull()) {
191 aParallel->SetFirstShape(theLine1->impl<TopoDS_Shape>());
192 aParallel->SetSecondShape(theLine2->impl<TopoDS_Shape>());
193 aParallel->SetPlane(aPlane);
195 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
196 aParallel->Redisplay(Standard_True);
201 void GeomAPI_AISObject::createPerpendicular(boost::shared_ptr<GeomAPI_Shape> theLine1,
202 boost::shared_ptr<GeomAPI_Shape> theLine2,
203 boost::shared_ptr<GeomAPI_Pln> thePlane)
205 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
206 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
207 if (anAIS.IsNull()) {
208 Handle(AIS_PerpendicularRelation) aPerpendicular = new AIS_PerpendicularRelation(
209 theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
211 setImpl(new Handle(AIS_InteractiveObject)(aPerpendicular));
213 Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(
215 if (!aPerpendicular.IsNull()) {
216 aPerpendicular->SetFirstShape(theLine1->impl<TopoDS_Shape>());
217 aPerpendicular->SetSecondShape(theLine2->impl<TopoDS_Shape>());
218 aPerpendicular->SetPlane(aPlane);
219 aPerpendicular->Redisplay(Standard_True);
225 void GeomAPI_AISObject::createFixed(boost::shared_ptr<GeomAPI_Shape> theShape,
226 boost::shared_ptr<GeomAPI_Pln> thePlane)
228 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
229 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
230 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
231 Handle(AIS_FixRelation) aFixPrs;
232 if (anAIS.IsNull()) {
233 aFixPrs = new AIS_FixRelation(aShape, aPlane);
235 setImpl(new Handle(AIS_InteractiveObject)(aFixPrs));
237 aFixPrs = Handle(AIS_FixRelation)::DownCast(anAIS);
238 if (!aFixPrs.IsNull()) {
239 aFixPrs->SetFirstShape(aShape);
240 aFixPrs->SetPlane(aPlane);
241 aFixPrs->Redisplay(Standard_True);
244 if (!aFixPrs.IsNull()) {
246 BRepBndLib::Add(aShape, aBox);
247 double aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
248 aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
249 gp_Pnt aXYZ1(aXmin, aXmax, aYmin);
250 gp_Pnt aXYZ2(aXmax, aYmax, aZmax);
251 double aDist = aXYZ1.Distance(aXYZ2);
252 if (aDist > Precision::Confusion()) {
253 aFixPrs->SetArrowSize(aDist/8.);
260 void GeomAPI_AISObject::setColor(const int& theColor)
262 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
265 Quantity_Color aColor((Quantity_NameOfColor) theColor);
266 anAIS->SetColor(aColor);
267 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
268 if (!aDimAIS.IsNull()) {
269 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
273 void GeomAPI_AISObject::setWidth(const double& theWidth)
275 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
278 anAIS->SetWidth(theWidth);
281 void GeomAPI_AISObject::setColor(int theR, int theG, int theB)
283 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
286 Quantity_Color aColor(theR / 255., theG / 255., theB / 255., Quantity_TOC_RGB);
287 anAIS->SetColor(aColor);
288 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
289 if (!aDimAIS.IsNull()) {
290 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
294 bool GeomAPI_AISObject::empty() const
296 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
297 ->impl<Handle(AIS_InteractiveObject)>();