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>
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>
26 const double tolerance = 1e-7;
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
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;
37 GeomAPI_AISObject::GeomAPI_AISObject()
38 : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
42 void GeomAPI_AISObject::createShape(boost::shared_ptr<GeomAPI_Shape> theShape)
44 const TopoDS_Shape& aTDS =
45 (theShape && theShape->implPtr<TopoDS_Shape>()) ?
46 theShape->impl<TopoDS_Shape>() : TopoDS_Shape();
48 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
49 if (!anAIS.IsNull()) {
50 Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
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.
59 aShapeAIS->Redisplay(Standard_True);
62 setImpl(new Handle(AIS_InteractiveObject)(new AIS_Shape(aTDS)));
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)
73 if (theStartPoint->distance(theEndPoint) < tolerance)
74 aDist = theStartPoint->distance(theFlyoutPoint);
76 boost::shared_ptr<GeomAPI_Lin> aLine = boost::shared_ptr<GeomAPI_Lin>(
77 new GeomAPI_Lin(theStartPoint, theEndPoint));
78 aDist = aLine->distance(theFlyoutPoint);
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)
90 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
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);
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);
108 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
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);
118 aDimAIS->Redisplay(Standard_True);
123 void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
124 boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
127 boost::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
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());
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);
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);
160 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
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);
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)
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);
183 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
185 setImpl(new Handle(AIS_InteractiveObject)(aParallel));
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);
193 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
194 aParallel->Redisplay(Standard_True);
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)
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);
209 setImpl(new Handle(AIS_InteractiveObject)(aPerpendicular));
211 Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(
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);
222 void GeomAPI_AISObject::setColor(const int& theColor)
224 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
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);
235 void GeomAPI_AISObject::setWidth(const double& theWidth)
237 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
240 anAIS->SetWidth(theWidth);
243 void GeomAPI_AISObject::setColor(int theR, int theG, int theB)
245 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
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);
256 bool GeomAPI_AISObject::empty() const
258 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
259 ->impl<Handle(AIS_InteractiveObject)>();