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 int CONSTRAINT_TEXT_HEIGHT = 28; /// the text height of the constraint
27 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20; /// the text selection tolerance
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;
35 GeomAPI_AISObject::GeomAPI_AISObject()
36 : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
40 void GeomAPI_AISObject::createShape(boost::shared_ptr<GeomAPI_Shape> theShape)
42 const TopoDS_Shape& aTDS =
43 (theShape && theShape->implPtr<TopoDS_Shape>()) ?
44 theShape->impl<TopoDS_Shape>() : TopoDS_Shape();
46 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
47 if (!anAIS.IsNull()) {
48 Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
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.
57 aShapeAIS->Redisplay(Standard_True);
60 setImpl(new Handle(AIS_InteractiveObject)(new AIS_Shape(aTDS)));
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)
70 boost::shared_ptr<GeomAPI_Lin> aLine = boost::shared_ptr<GeomAPI_Lin>(
71 new GeomAPI_Lin(theStartPoint, theEndPoint));
72 double aDist = aLine->distance(theFlyoutPoint);
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)
83 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
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);
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 anAspect->ArrowAspect()->SetLength(theDistance / 10.);
96 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
97 aDimAIS->SetDimensionAspect(anAspect);
98 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
99 aDimAIS->SetFlyout(aFlyout);
101 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
103 // update presentation
104 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
105 if (!aDimAIS.IsNull()) {
106 aDimAIS->SetMeasuredGeometry(theStartPoint->impl<gp_Pnt>(), theEndPoint->impl<gp_Pnt>(),
107 thePlane->impl<gp_Pln>());
108 aDimAIS->SetCustomValue(theDistance);
109 aDimAIS->SetFlyout(aFlyout);
111 aDimAIS->Redisplay(Standard_True);
116 void GeomAPI_AISObject::createRadius(boost::shared_ptr<GeomAPI_Circ> theCircle,
117 boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
120 boost::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
122 // TODO: a bug in AIS_RadiusDimension:
123 // The anchor point can't be myCirc.Location() - an exception is raised.
124 // But we need exactly this case...
125 // We want to show a radius dimension starting from the circle centre and
126 // ending at the user-defined point.
127 // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
128 boost::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
129 boost::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
130 anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
131 boost::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
132 const double aDelta = 1e-3;
133 anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
134 anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
135 anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
137 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
138 if (anAIS.IsNull()) {
139 Handle(AIS_RadiusDimension) aDimAIS = new AIS_RadiusDimension(theCircle->impl<gp_Circ>(),
140 anAnchor->impl<gp_Pnt>());
141 aDimAIS->SetCustomValue(theRadius);
143 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
144 anAspect->MakeArrows3d(Standard_False);
145 anAspect->MakeText3d(false);
146 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
147 anAspect->MakeTextShaded(false);
148 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
149 aDimAIS->SetDimensionAspect(anAspect);
150 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
152 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
154 // update presentation
155 Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
156 if (!aDimAIS.IsNull()) {
157 aDimAIS->SetMeasuredGeometry(theCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
158 aDimAIS->SetCustomValue(theRadius);
159 aDimAIS->Redisplay(Standard_True);
164 void GeomAPI_AISObject::createParallel(boost::shared_ptr<GeomAPI_Shape> theLine1,
165 boost::shared_ptr<GeomAPI_Shape> theLine2,
166 boost::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
167 boost::shared_ptr<GeomAPI_Pln> thePlane)
169 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
170 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
171 if (anAIS.IsNull()) {
172 Handle(AIS_ParallelRelation) aParallel = new AIS_ParallelRelation(
173 theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
175 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
177 setImpl(new Handle(AIS_InteractiveObject)(aParallel));
179 Handle(AIS_ParallelRelation) aParallel = Handle(AIS_ParallelRelation)::DownCast(anAIS);
180 if (!aParallel.IsNull()) {
181 aParallel->SetFirstShape(theLine1->impl<TopoDS_Shape>());
182 aParallel->SetSecondShape(theLine2->impl<TopoDS_Shape>());
183 aParallel->SetPlane(aPlane);
185 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
186 aParallel->Redisplay(Standard_True);
191 void GeomAPI_AISObject::createPerpendicular(boost::shared_ptr<GeomAPI_Shape> theLine1,
192 boost::shared_ptr<GeomAPI_Shape> theLine2,
193 boost::shared_ptr<GeomAPI_Pln> thePlane)
195 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
196 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
197 if (anAIS.IsNull()) {
198 Handle(AIS_PerpendicularRelation) aPerpendicular = new AIS_PerpendicularRelation(
199 theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
201 setImpl(new Handle(AIS_InteractiveObject)(aPerpendicular));
203 Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(
205 if (!aPerpendicular.IsNull()) {
206 aPerpendicular->SetFirstShape(theLine1->impl<TopoDS_Shape>());
207 aPerpendicular->SetSecondShape(theLine2->impl<TopoDS_Shape>());
208 aPerpendicular->SetPlane(aPlane);
209 aPerpendicular->Redisplay(Standard_True);
214 void GeomAPI_AISObject::setColor(const int& theColor)
216 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
219 Quantity_Color aColor((Quantity_NameOfColor) theColor);
220 anAIS->SetColor(aColor);
221 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
222 if (!aDimAIS.IsNull()) {
223 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
227 void GeomAPI_AISObject::setWidth(const double& theWidth)
229 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
232 anAIS->SetWidth(theWidth);
235 void GeomAPI_AISObject::setColor(int theR, int theG, int theB)
237 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
240 Quantity_Color aColor(theR / 255., theG / 255., theB / 255., Quantity_TOC_RGB);
241 anAIS->SetColor(aColor);
242 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
243 if (!aDimAIS.IsNull()) {
244 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
248 bool GeomAPI_AISObject::empty() const
250 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
251 ->impl<Handle(AIS_InteractiveObject)>();