1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_AISObject.cpp
4 // Created: 25 Jun 2014
5 // Author: Artem ZHIDKOV
7 #include <GeomAPI_AISObject.h>
9 #include <GeomAPI_Circ.h>
10 #include <GeomAPI_Dir.h>
11 #include <GeomAPI_Lin.h>
12 #include <GeomAPI_Pln.h>
13 #include <GeomAPI_Pnt.h>
14 #include <GeomAPI_Shape.h>
15 #include <GeomAPI_XYZ.h>
17 #include <Geom_Plane.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <Quantity_NameOfColor.hxx>
20 #include <BRepBndLib.hxx>
22 #include <AIS_InteractiveObject.hxx>
23 #include <AIS_InteractiveContext.hxx>
24 #include <AIS_LengthDimension.hxx>
25 #include <AIS_ParallelRelation.hxx>
26 #include <AIS_PerpendicularRelation.hxx>
27 #include <AIS_RadiusDimension.hxx>
28 #include <AIS_Shape.hxx>
29 #include <AIS_FixRelation.hxx>
30 #include <Prs3d_PointAspect.hxx>
32 const double tolerance = 1e-7;
34 const int CONSTRAINT_TEXT_HEIGHT = 28; /// the text height of the constraint
35 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20; /// the text selection tolerance
37 GeomAPI_AISObject::GeomAPI_AISObject()
38 : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
42 void GeomAPI_AISObject::createShape(std::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(std::shared_ptr<GeomAPI_Pnt> theStartPoint,
66 std::shared_ptr<GeomAPI_Pnt> theEndPoint,
67 std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
68 std::shared_ptr<GeomAPI_Pln> thePlane, double theDistance)
73 if (theStartPoint->distance(theEndPoint) < tolerance)
74 aDist = theStartPoint->distance(theFlyoutPoint);
76 std::shared_ptr<GeomAPI_Lin> aLine = std::shared_ptr<GeomAPI_Lin>(
77 new GeomAPI_Lin(theStartPoint, theEndPoint));
78 aDist = aLine->distance(theFlyoutPoint);
81 std::shared_ptr<GeomAPI_XYZ> aLineDir = theEndPoint->xyz()->decreased(theStartPoint->xyz());
82 std::shared_ptr<GeomAPI_XYZ> aFOutDir = theFlyoutPoint->xyz()->decreased(
83 theStartPoint->xyz());
84 std::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(std::shared_ptr<GeomAPI_Circ> theCircle,
124 std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
127 std::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 std::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
136 std::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
137 anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
138 std::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(std::shared_ptr<GeomAPI_Shape> theLine1,
173 std::shared_ptr<GeomAPI_Shape> theLine2,
174 std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
175 std::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(std::shared_ptr<GeomAPI_Shape> theLine1,
200 std::shared_ptr<GeomAPI_Shape> theLine2,
201 std::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);
223 void GeomAPI_AISObject::createFixed(std::shared_ptr<GeomAPI_Shape> theShape,
224 std::shared_ptr<GeomAPI_Pln> thePlane)
226 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
227 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
228 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
229 Handle(AIS_FixRelation) aFixPrs;
230 if (anAIS.IsNull()) {
231 aFixPrs = new AIS_FixRelation(aShape, aPlane);
233 setImpl(new Handle(AIS_InteractiveObject)(aFixPrs));
235 aFixPrs = Handle(AIS_FixRelation)::DownCast(anAIS);
236 if (!aFixPrs.IsNull()) {
237 aFixPrs->SetFirstShape(aShape);
238 aFixPrs->SetPlane(aPlane);
239 aFixPrs->Redisplay(Standard_True);
242 if (!aFixPrs.IsNull()) {
244 BRepBndLib::Add(aShape, aBox);
245 double aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
246 aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
247 gp_Pnt aXYZ1(aXmin, aXmax, aYmin);
248 gp_Pnt aXYZ2(aXmax, aYmax, aZmax);
249 double aDist = aXYZ1.Distance(aXYZ2);
250 if (aDist > Precision::Confusion()) {
251 aFixPrs->SetArrowSize(aDist/8.);
258 void GeomAPI_AISObject::setColor(const int& theColor)
260 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
263 Quantity_Color aColor((Quantity_NameOfColor) theColor);
264 anAIS->SetColor(aColor);
265 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
266 if (!aDimAIS.IsNull()) {
267 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
271 void GeomAPI_AISObject::setWidth(const double& theWidth)
273 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
276 anAIS->SetWidth(theWidth);
279 void GeomAPI_AISObject::setColor(int theR, int theG, int theB)
281 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
284 Quantity_Color aColor(theR / 255., theG / 255., theB / 255., Quantity_TOC_RGB);
285 anAIS->SetColor(aColor);
286 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
287 if (!aDimAIS.IsNull()) {
288 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
292 bool GeomAPI_AISObject::empty() const
294 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
295 ->impl<Handle(AIS_InteractiveObject)>();
301 int GeomAPI_AISObject::getShapeType() const
303 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
304 ->impl<Handle(AIS_InteractiveObject)>();
305 if (!anAIS.IsNull()) {
306 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(anAIS);
307 if (!aAISShape.IsNull()) {
308 return aAISShape->Shape().ShapeType();
314 void GeomAPI_AISObject::setPointMarker(int theType, double theScale)
316 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
317 if (!anAIS.IsNull()) {
318 Handle(AIS_Drawer) aDrawer = anAIS->Attributes();
319 if (aDrawer->HasPointAspect()) {
320 Handle(Prs3d_PointAspect) aPA = aDrawer->PointAspect();
321 aPA->SetTypeOfMarker((Aspect_TypeOfMarker)theType);
322 aPA->SetScale(theScale);
324 Quantity_NameOfColor aCol = Quantity_NOC_YELLOW;
325 aDrawer->SetPointAspect(new Prs3d_PointAspect((Aspect_TypeOfMarker)theType, aCol, theScale));
331 void GeomAPI_AISObject::setLineStyle(int theStyle)
333 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
334 if (!anAIS.IsNull()) {
335 Handle(AIS_Drawer) aDrawer = anAIS->Attributes();
336 if (aDrawer->HasLineAspect())
337 aDrawer->LineAspect()->SetTypeOfLine((Aspect_TypeOfLine)theStyle);
338 if (aDrawer->HasWireAspect())
339 aDrawer->WireAspect()->SetTypeOfLine((Aspect_TypeOfLine)theStyle);
344 void GeomAPI_AISObject::setTransparensy(double theVal)
346 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
347 if (!anAIS.IsNull()) {
348 Handle(AIS_InteractiveContext) aContext = anAIS->GetContext();
349 if (!aContext.IsNull())
350 aContext->SetTransparency(anAIS, theVal, false);