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 #include <Graphic3d_AspectLine3d.hxx>
34 const double tolerance = 1e-7;
36 const int CONSTRAINT_TEXT_HEIGHT = 28; /// the text height of the constraint
37 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20; /// the text selection tolerance
39 GeomAPI_AISObject::GeomAPI_AISObject()
40 : GeomAPI_Interface(new Handle(AIS_InteractiveObject)())
44 GeomAPI_AISObject::~GeomAPI_AISObject()
47 // This is necessary for correct deletion of Handle entity.
48 // Without this Handle does not decremented counter to 0
49 Handle(AIS_InteractiveObject) *anAIS = implPtr<Handle(AIS_InteractiveObject)>();
55 void GeomAPI_AISObject::createShape(std::shared_ptr<GeomAPI_Shape> theShape)
57 const TopoDS_Shape& aTDS =
58 (theShape && theShape->implPtr<TopoDS_Shape>()) ?
59 theShape->impl<TopoDS_Shape>() : TopoDS_Shape();
61 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
62 if (!anAIS.IsNull()) {
63 Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
65 // if the AIS object is displayed in the opened local context in some mode, additional
66 // AIS sub objects are created there. They should be rebuild for correct selecting.
67 // It is possible to correct it by closing local context before the shape set and opening
68 // after. Another workaround to thrown down the selection and reselecting the AIS.
69 // If there was a problem here, try the first solution with close/open local context.
72 aShapeAIS->Redisplay(Standard_True);
75 setImpl(new Handle(AIS_InteractiveObject)(new AIS_Shape(aTDS)));
78 void GeomAPI_AISObject::createDistance(std::shared_ptr<GeomAPI_Pnt> theStartPoint,
79 std::shared_ptr<GeomAPI_Pnt> theEndPoint,
80 std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
81 std::shared_ptr<GeomAPI_Pln> thePlane, double theDistance)
86 if (theStartPoint->distance(theEndPoint) < tolerance)
87 aDist = theStartPoint->distance(theFlyoutPoint);
89 std::shared_ptr<GeomAPI_Lin> aLine = std::shared_ptr<GeomAPI_Lin>(
90 new GeomAPI_Lin(theStartPoint, theEndPoint));
91 aDist = aLine->distance(theFlyoutPoint);
94 std::shared_ptr<GeomAPI_XYZ> aLineDir = theEndPoint->xyz()->decreased(theStartPoint->xyz());
95 std::shared_ptr<GeomAPI_XYZ> aFOutDir = theFlyoutPoint->xyz()->decreased(
96 theStartPoint->xyz());
97 std::shared_ptr<GeomAPI_XYZ> aNorm = thePlane->direction()->xyz();
98 if (aLineDir->cross(aFOutDir)->dot(aNorm) < 0)
103 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
104 if (anAIS.IsNull()) {
105 Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension(theStartPoint->impl<gp_Pnt>(),
106 theEndPoint->impl<gp_Pnt>(),
107 thePlane->impl<gp_Pln>());
108 aDimAIS->SetCustomValue(theDistance);
110 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
111 anAspect->MakeArrows3d(Standard_False);
112 anAspect->MakeText3d(Standard_False);
113 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
114 anAspect->MakeTextShaded(Standard_True);
115 anAspect->ArrowAspect()->SetLength(theDistance / 10.);
116 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
117 aDimAIS->SetDimensionAspect(anAspect);
118 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
119 aDimAIS->SetFlyout(aFlyout);
121 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
123 // update presentation
124 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
125 if (!aDimAIS.IsNull()) {
126 aDimAIS->SetMeasuredGeometry(theStartPoint->impl<gp_Pnt>(), theEndPoint->impl<gp_Pnt>(),
127 thePlane->impl<gp_Pln>());
128 aDimAIS->SetCustomValue(theDistance);
129 aDimAIS->SetFlyout(aFlyout);
131 aDimAIS->Redisplay(Standard_True);
136 void GeomAPI_AISObject::createRadius(std::shared_ptr<GeomAPI_Circ> theCircle,
137 std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
140 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
142 // TODO: a bug in AIS_RadiusDimension:
143 // The anchor point can't be myCirc.Location() - an exception is raised.
144 // But we need exactly this case...
145 // We want to show a radius dimension starting from the circle centre and
146 // ending at the user-defined point.
147 // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
148 std::shared_ptr<GeomAPI_Pnt> anAnchor = theCircle->project(theFlyoutPoint);
149 std::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
150 anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
151 std::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
152 const double aDelta = 1e-3;
153 anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
154 anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
155 anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
157 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
158 if (anAIS.IsNull()) {
159 Handle(AIS_RadiusDimension) aDimAIS = new AIS_RadiusDimension(theCircle->impl<gp_Circ>(),
160 anAnchor->impl<gp_Pnt>());
161 aDimAIS->SetCustomValue(theRadius);
163 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
164 anAspect->MakeArrows3d(Standard_False);
165 anAspect->MakeText3d(false);
166 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
167 anAspect->ArrowAspect()->SetLength(theRadius / 5.);
168 anAspect->MakeTextShaded(false);
169 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
170 aDimAIS->SetDimensionAspect(anAspect);
171 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
173 setImpl(new Handle(AIS_InteractiveObject)(aDimAIS));
175 // update presentation
176 Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
177 if (!aDimAIS.IsNull()) {
178 aDimAIS->SetMeasuredGeometry(theCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
179 aDimAIS->SetCustomValue(theRadius);
180 aDimAIS->Redisplay(Standard_True);
185 void GeomAPI_AISObject::createParallel(std::shared_ptr<GeomAPI_Shape> theLine1,
186 std::shared_ptr<GeomAPI_Shape> theLine2,
187 std::shared_ptr<GeomAPI_Pnt> theFlyoutPoint,
188 std::shared_ptr<GeomAPI_Pln> thePlane)
190 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
191 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
192 if (anAIS.IsNull()) {
193 Handle(AIS_ParallelRelation) aParallel = new AIS_ParallelRelation(
194 theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
196 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
198 setImpl(new Handle(AIS_InteractiveObject)(aParallel));
200 Handle(AIS_ParallelRelation) aParallel = Handle(AIS_ParallelRelation)::DownCast(anAIS);
201 if (!aParallel.IsNull()) {
202 aParallel->SetFirstShape(theLine1->impl<TopoDS_Shape>());
203 aParallel->SetSecondShape(theLine2->impl<TopoDS_Shape>());
204 aParallel->SetPlane(aPlane);
206 aParallel->SetPosition(theFlyoutPoint->impl<gp_Pnt>());
207 aParallel->Redisplay(Standard_True);
212 void GeomAPI_AISObject::createPerpendicular(std::shared_ptr<GeomAPI_Shape> theLine1,
213 std::shared_ptr<GeomAPI_Shape> theLine2,
214 std::shared_ptr<GeomAPI_Pln> thePlane)
216 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
217 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
218 if (anAIS.IsNull()) {
219 Handle(AIS_PerpendicularRelation) aPerpendicular = new AIS_PerpendicularRelation(
220 theLine1->impl<TopoDS_Shape>(), theLine2->impl<TopoDS_Shape>(), aPlane);
222 setImpl(new Handle(AIS_InteractiveObject)(aPerpendicular));
224 Handle(AIS_PerpendicularRelation) aPerpendicular = Handle(AIS_PerpendicularRelation)::DownCast(
226 if (!aPerpendicular.IsNull()) {
227 aPerpendicular->SetFirstShape(theLine1->impl<TopoDS_Shape>());
228 aPerpendicular->SetSecondShape(theLine2->impl<TopoDS_Shape>());
229 aPerpendicular->SetPlane(aPlane);
230 aPerpendicular->Redisplay(Standard_True);
236 void GeomAPI_AISObject::createFixed(std::shared_ptr<GeomAPI_Shape> theShape,
237 std::shared_ptr<GeomAPI_Pln> thePlane)
239 Handle(Geom_Plane) aPlane = new Geom_Plane(thePlane->impl<gp_Pln>());
240 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
241 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
242 Handle(AIS_FixRelation) aFixPrs;
243 if (anAIS.IsNull()) {
244 aFixPrs = new AIS_FixRelation(aShape, aPlane);
246 setImpl(new Handle(AIS_InteractiveObject)(aFixPrs));
248 aFixPrs = Handle(AIS_FixRelation)::DownCast(anAIS);
249 if (!aFixPrs.IsNull()) {
250 aFixPrs->SetFirstShape(aShape);
251 aFixPrs->SetPlane(aPlane);
252 aFixPrs->Redisplay(Standard_True);
255 if (!aFixPrs.IsNull()) {
257 BRepBndLib::Add(aShape, aBox);
258 double aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
259 aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
260 gp_Pnt aXYZ1(aXmin, aXmax, aYmin);
261 gp_Pnt aXYZ2(aXmax, aYmax, aZmax);
262 double aDist = aXYZ1.Distance(aXYZ2);
263 if (aDist > Precision::Confusion()) {
264 aFixPrs->SetArrowSize(aDist/8.);
269 void GeomAPI_AISObject::setColor(const int& theColor)
271 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
274 Quantity_Color aColor((Quantity_NameOfColor) theColor);
275 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
276 if (!aDimAIS.IsNull()) {
277 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
279 Handle(AIS_InteractiveContext) aContext = anAIS->GetContext();
280 aContext->SetColor(anAIS, aColor, false);
283 bool GeomAPI_AISObject::setWidth(const double& theWidth)
285 bool isChanged = false;
286 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
287 if (!anAIS.IsNull()) {
288 isChanged = anAIS->Width() != theWidth;
290 anAIS->SetWidth(theWidth);
295 bool GeomAPI_AISObject::setColor(int theR, int theG, int theB)
297 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
300 Quantity_Color aColor(theR / 255., theG / 255., theB / 255., Quantity_TOC_RGB);
301 Quantity_Color aCurrentColor;
302 anAIS->Color(aCurrentColor);
303 // do not set the same color to the presentation
304 if (aColor.IsEqual(aCurrentColor))
307 Handle(AIS_Dimension) aDimAIS = Handle(AIS_Dimension)::DownCast(anAIS);
308 if (!aDimAIS.IsNull()) {
309 aDimAIS->DimensionAspect()->SetCommonColor(aColor);
311 Handle(AIS_InteractiveContext) aContext = anAIS->GetContext();
312 aContext->SetColor(anAIS, aColor, false);
316 void GeomAPI_AISObject::getColor(int& theR, int& theG, int& theB)
318 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
322 Quantity_Color aColor = anAIS->Color();
323 theR = (int)(aColor.Red()*255.);
324 theG = (int)(aColor.Green()*255.);
325 theB = (int)(aColor.Blue()*255.);
328 bool GeomAPI_AISObject::empty() const
330 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
331 ->impl<Handle(AIS_InteractiveObject)>();
337 int GeomAPI_AISObject::getShapeType() const
339 Handle(AIS_InteractiveObject) anAIS = const_cast<GeomAPI_AISObject*>(this)
340 ->impl<Handle(AIS_InteractiveObject)>();
341 if (!anAIS.IsNull()) {
342 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(anAIS);
343 if (!aAISShape.IsNull()) {
344 const TopoDS_Shape aShape = aAISShape->Shape();
345 if (!aShape.IsNull())
346 return aShape.ShapeType();
352 void GeomAPI_AISObject::setPointMarker(int theType, double theScale)
354 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
355 if (!anAIS.IsNull()) {
356 Handle(Prs3d_Drawer) aDrawer = anAIS->Attributes();
357 if (aDrawer->HasOwnPointAspect()) {
358 Handle(Prs3d_PointAspect) aPA = aDrawer->PointAspect();
359 aPA->SetTypeOfMarker((Aspect_TypeOfMarker)theType);
360 aPA->SetScale(theScale);
362 Quantity_NameOfColor aCol = Quantity_NOC_YELLOW;
363 aDrawer->SetPointAspect(new Prs3d_PointAspect((Aspect_TypeOfMarker)theType, aCol, theScale));
368 bool GeomAPI_AISObject::setLineStyle(int theStyle)
370 bool isChanged = false;
371 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
372 if (!anAIS.IsNull()) {
373 Handle(Prs3d_Drawer) aDrawer = anAIS->Attributes();
374 Handle(Prs3d_LineAspect) aLineAspect;
376 Aspect_TypeOfLine aType = (Aspect_TypeOfLine)theStyle;
377 if (aDrawer->HasOwnLineAspect()) {
378 aLineAspect = aDrawer->LineAspect();
380 if (aDrawer->HasOwnWireAspect()) {
381 aLineAspect = aDrawer->WireAspect();
383 Quantity_Color aCurrentColor;
384 Aspect_TypeOfLine aCurrentType;
385 Standard_Real aCurrentWidth;
386 aLineAspect->Aspect()->Values(aCurrentColor, aCurrentType, aCurrentWidth);
387 isChanged = aType != aCurrentType;
389 aLineAspect->SetTypeOfLine(aType);
395 bool GeomAPI_AISObject::setTransparensy(double theVal)
397 bool isChanged = false;
398 Handle(AIS_InteractiveObject) anAIS = impl<Handle(AIS_InteractiveObject)>();
399 if (!anAIS.IsNull()) {
400 Handle(AIS_InteractiveContext) aContext = anAIS->GetContext();
401 if (!aContext.IsNull()) {
402 double aCurrentValue = anAIS->Transparency();
403 isChanged = aCurrentValue != theVal;
405 aContext->SetTransparency(anAIS, theVal, false);