1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : MeasureGUI_DimensionCreateTool.cxx
25 // Author : Anton POLETAEV, Open CASCADE S.A.S.
27 #include "MeasureGUI_DimensionCreateTool.h"
33 #include <SalomeApp_Application.h>
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <OCCViewer_ViewPort3d.h>
42 #include <Adaptor3d_CurveOnSurface.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRepTools.hxx>
45 #include <BRepAdaptor_Curve.hxx>
46 #include <BRepAdaptor_Surface.hxx>
47 #include <BRepBndLib.hxx>
50 #include <gp_Circ.hxx>
51 #include <gp_Sphere.hxx>
52 #include <gp_Cone.hxx>
53 #include <gp_Torus.hxx>
54 #include <gce_MakeDir.hxx>
55 #include <gce_MakePln.hxx>
56 #include <gce_MakeCirc.hxx>
57 #include <GC_MakePlane.hxx>
58 #include <Geom_Circle.hxx>
59 #include <Geom_Plane.hxx>
60 #include <Geom_ElementarySurface.hxx>
61 #include <Geom_Surface.hxx>
62 #include <Geom_ConicalSurface.hxx>
63 #include <Geom_SphericalSurface.hxx>
64 #include <Geom_ToroidalSurface.hxx>
65 #include <Geom_TrimmedCurve.hxx>
66 #include <GeomLib.hxx>
67 #include <GeomLib_Tool.hxx>
68 #include <TopoDS_Shape.hxx>
69 #include <TopoDS_Vertex.hxx>
70 #include <TopoDS_Edge.hxx>
72 #include <TopExp_Explorer.hxx>
73 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
74 #include <TopTools_ListOfShape.hxx>
75 #include <TopTools_ListIteratorOfListOfShape.hxx>
76 #include <TColgp_SequenceOfDir.hxx>
77 #include <V3d_View.hxx>
79 // plane associated with custom data
80 struct PlaneAndSegment
83 PlaneAndSegment(const gp_Pln& thePlane, const MeasureGUI_DimensionCreateTool::Segment& theSegment) : pln(thePlane), seg(theSegment) {}
84 operator gp_Pln () const { return pln; }
85 operator MeasureGUI_DimensionCreateTool::Segment () const { return seg; }
87 MeasureGUI_DimensionCreateTool::Segment seg;
90 typedef NCollection_Sequence<PlaneAndSegment> SeqOfPlnsAndSegments;
92 //=================================================================================
93 // function : Constructor
95 //=================================================================================
96 MeasureGUI_DimensionCreateTool::MeasureGUI_DimensionCreateTool()
98 Settings.DefaultFlyout = 0.0;
99 Settings.ActiveView = NULL;
102 //=================================================================================
103 // function : LengthOnEdge
105 //=================================================================================
106 Handle(AIS_LengthDimension) MeasureGUI_DimensionCreateTool::LengthOnEdge( const GEOM::GeomObjPtr& theMeasuredObj ) const
108 /* ---------------------------------------------------------------- *
109 * get the edge and parent shape *
110 * ---------------------------------------------------------------- */
112 TopoDS_Shape aMeasuredShape;
113 TopoDS_Shape aMainShape;
114 if ( !GEOMBase::GetShape( theMeasuredObj.operator ->(), aMeasuredShape ) )
119 if ( !GEOMBase::GetShape( GetMainShape( theMeasuredObj ).get(), aMainShape ) )
124 /* ------------------------------------------------- */
125 /* check the input geometry */
126 /* ------------------------------------------------- */
128 TopoDS_Edge anEdge = TopoDS::Edge( aMeasuredShape );
130 TopoDS_Vertex aVertex1;
131 TopoDS_Vertex aVertex2;
132 TopExp::Vertices( anEdge, aVertex1, aVertex2 );
134 gp_Pnt aPnt1 = BRep_Tool::Pnt( aVertex1 );
135 gp_Pnt aPnt2 = BRep_Tool::Pnt( aVertex2 );
136 if ( aPnt1.Distance( aPnt2 ) <= Precision::Confusion() )
141 /* ------------------------- *
142 * position the dimension
143 * ------------------------- */
146 BRepBndLib::AddClose( aMainShape, aBnd );
149 TopTools_IndexedDataMapOfShapeListOfShape aRelationMap;
150 TopExp::MapShapesAndAncestors( aMainShape, TopAbs_EDGE, TopAbs_FACE, aRelationMap );
151 const TopTools_ListOfShape& aRelatedFaces = aRelationMap.FindFromKey( anEdge );
153 gp_Vec aFaceN1( gp::Origin(), gp::Origin() );
154 gp_Vec aFaceN2( gp::Origin(), gp::Origin() );
155 gp_Vec aFaceS1( gp::Origin(), gp::Origin() );
156 gp_Vec aFaceS2( gp::Origin(), gp::Origin() );
158 gp_Pnt aMiddlePnt = gp_Pnt( ( aPnt1.XYZ() + aPnt2.XYZ() ) * 0.5 );
160 TopTools_ListIteratorOfListOfShape aFaceIt( aRelatedFaces );
162 // get face side directions
163 if ( aFaceIt.More() )
165 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
168 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
173 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
175 Standard_Real aU = 0.0, aV = 0.0;
176 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
179 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
181 aFaceN1 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
187 if ( aFaceIt.More() )
189 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
192 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
197 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
199 Standard_Real aU = 0.0, aV = 0.0;
200 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
203 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
205 aFaceN2 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
210 PositionLength( aBnd, aFaceN1, aFaceN2, aFaceS1, aFaceS2, aPnt1, aPnt2, aPln );
212 /* --------------------------------------------------------- *
213 * construct the dimension for the best selected position
214 * --------------------------------------------------------- */
216 Handle(AIS_LengthDimension) aDimension = new AIS_LengthDimension( anEdge, aPln );
218 aDimension->SetFlyout( Settings.DefaultFlyout );
220 if ( !aDimension->IsValid() )
228 //=================================================================================
229 // function : LengthByPoints
231 //=================================================================================
232 Handle(AIS_LengthDimension) MeasureGUI_DimensionCreateTool::LengthByPoints( const GEOM::GeomObjPtr& theMeasuredObj1,
233 const GEOM::GeomObjPtr& theMeasuredObj2 ) const
235 /* ---------------------------------------------------------------- *
236 * get the edge and parent shape *
237 * ---------------------------------------------------------------- */
239 TopoDS_Shape aMeasuredShape1;
240 TopoDS_Shape aMeasuredShape2;
241 TopoDS_Shape aMainShape;
243 if ( !GEOMBase::GetShape( theMeasuredObj1.operator ->(), aMeasuredShape1 ) )
248 if ( !GEOMBase::GetShape( theMeasuredObj2.operator ->(), aMeasuredShape2 ) )
253 if ( !GEOMBase::GetShape( GetMainShape( theMeasuredObj1 ).get(), aMainShape ) )
258 /* ------------------------------------------------- */
259 /* check the input geometry */
260 /* ------------------------------------------------- */
262 TopoDS_Vertex aVertex1 = TopoDS::Vertex( aMeasuredShape1 );
263 TopoDS_Vertex aVertex2 = TopoDS::Vertex( aMeasuredShape2 );
265 gp_Pnt aPnt1 = BRep_Tool::Pnt( aVertex1 );
266 gp_Pnt aPnt2 = BRep_Tool::Pnt( aVertex2 );
267 if ( aPnt1.Distance( aPnt2 ) <= Precision::Confusion() )
272 /* ------------------------- *
273 * position the dimension
274 * ------------------------- */
277 BRepBndLib::AddClose( aMainShape, aBnd );
279 // check whether the points share same edge
280 TopExp_Explorer anEdgeExp( aMainShape, TopAbs_EDGE, TopAbs_EDGE );
281 for ( ; anEdgeExp.More(); anEdgeExp.Next() )
283 TopoDS_Vertex anEdgeV1;
284 TopoDS_Vertex anEdgeV2;
285 TopExp::Vertices( TopoDS::Edge( anEdgeExp.Current() ), anEdgeV1, anEdgeV2 );
286 gp_Pnt anEdgePnt1 = BRep_Tool::Pnt( anEdgeV1 );
287 gp_Pnt anEdgePnt2 = BRep_Tool::Pnt( anEdgeV2 );
289 if ( aPnt1.Distance( anEdgePnt1 ) <= Precision::Confusion() )
291 if ( aPnt2.Distance( anEdgePnt2 ) <= Precision::Confusion() )
297 if ( aPnt2.Distance( anEdgePnt1 ) <= Precision::Confusion() )
299 if ( aPnt1.Distance( anEdgePnt2 ) <= Precision::Confusion() )
306 gp_Vec aFaceN1( gp::Origin(), gp::Origin() );
307 gp_Vec aFaceN2( gp::Origin(), gp::Origin() );
308 gp_Vec aFaceS1( gp::Origin(), gp::Origin() );
309 gp_Vec aFaceS2( gp::Origin(), gp::Origin() );
312 if ( anEdgeExp.More() )
314 TopoDS_Edge anEdge = TopoDS::Edge( anEdgeExp.Current() );
315 TopTools_IndexedDataMapOfShapeListOfShape aRelationMap;
316 TopExp::MapShapesAndAncestors( aMainShape, TopAbs_EDGE, TopAbs_FACE, aRelationMap );
317 const TopTools_ListOfShape& aRelatedFaces = aRelationMap.FindFromKey( anEdge );
319 gp_Pnt aMiddlePnt = gp_Pnt( ( aPnt1.XYZ() + aPnt2.XYZ() ) * 0.5 );
321 TopTools_ListIteratorOfListOfShape aFaceIt( aRelatedFaces );
323 // get face side directions
324 if ( aFaceIt.More() )
326 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
329 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
334 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
336 Standard_Real aU = 0.0, aV = 0.0;
337 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
340 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
342 aFaceN1 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
348 if ( aFaceIt.More() )
350 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
353 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
358 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
360 Standard_Real aU = 0.0, aV = 0.0;
361 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
364 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
366 aFaceN2 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
372 PositionLength( aBnd, aFaceN1, aFaceN2, aFaceS1, aFaceS2, aPnt1, aPnt2, aPln );
374 /* --------------------------------------------------------- *
375 * construct the dimension for the best selected position
376 * --------------------------------------------------------- */
378 Handle(AIS_LengthDimension) aDimension = new AIS_LengthDimension( aPnt1, aPnt2, aPln );
380 aDimension->SetFlyout( Settings.DefaultFlyout );
382 if ( !aDimension->IsValid() )
390 //=================================================================================
391 // function : LengthByParallelEdges
393 //=================================================================================
394 Handle(AIS_LengthDimension) MeasureGUI_DimensionCreateTool::LengthByParallelEdges( const GEOM::GeomObjPtr& theEdge1,
395 const GEOM::GeomObjPtr& theEdge2 ) const
397 TopoDS_Shape aFirstSh;
398 if ( !GEOMBase::GetShape( theEdge1.operator ->(), aFirstSh ) )
403 TopoDS_Shape aSecondSh;
404 if ( !GEOMBase::GetShape( theEdge2.operator ->(), aSecondSh ) )
409 if( aFirstSh == aSecondSh )
412 TopoDS_Edge aFirstEdge = TopoDS::Edge( aFirstSh );
413 TopoDS_Edge aSecondEdge = TopoDS::Edge( aSecondSh );
415 // Build plane through three points
416 BRepAdaptor_Curve aCurve1( aFirstEdge );
417 BRepAdaptor_Curve aCurve2( aSecondEdge );
419 gp_Pnt aPnt1 = aCurve1.Value( 0.1 );
420 gp_Pnt aPnt2 = aCurve1.Value( 0.9 );
421 gp_Pnt aPnt3 = aCurve2.Value( 0.5 );
423 GC_MakePlane aMkPlane( aPnt1, aPnt2, aPnt3 );
424 Handle(Geom_Plane) aPlane = aMkPlane.Value();
426 // check whether it is possible to compute valid dimension
427 Handle(AIS_LengthDimension) aDimension = new AIS_LengthDimension ( aFirstEdge, aSecondEdge, aPlane->Pln() );
429 aDimension->SetFlyout( Settings.DefaultFlyout );
431 if ( !aDimension->IsValid() )
439 //=================================================================================
440 // function : Diameter
442 //=================================================================================
443 Handle(AIS_DiameterDimension) MeasureGUI_DimensionCreateTool::Diameter( const GEOM::GeomObjPtr& theMeasuredObj ) const
445 /* ------------------------------------------------ *
446 * get the shape and its parent (if exist) *
447 * ------------------------------------------------ */
449 TopoDS_Shape aMeasuredShape;
450 TopoDS_Shape aMainShape;
451 if ( !GEOMBase::GetShape( theMeasuredObj.operator ->(), aMeasuredShape ) )
456 if ( !GEOMBase::GetShape( GetMainShape( theMeasuredObj ).get(), aMainShape ) )
462 BRepBndLib::AddClose( aMainShape, aBnd );
464 /* ------------------------------------------------ *
465 * get the dimension construction arguments *
466 * ------------------------------------------------ */
468 Handle(Geom_Circle) aCircle;
470 Standard_Real aPmin = 0, aPmax = 2 * M_PI;
472 gp_Vec aFaceN( gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(0.0, 0.0, 0.0) );
474 switch ( aMeasuredShape.ShapeType() )
478 TopoDS_Face aMeasuredFace = TopoDS::Face(aMeasuredShape);
480 BRepAdaptor_Surface aSurf( aMeasuredFace );
482 Standard_Real aUmin = aSurf.FirstUParameter();
483 Standard_Real aUmax = aSurf.LastUParameter();
484 Standard_Real aVmin = aSurf.FirstVParameter();
485 Standard_Real aVmax = aSurf.LastVParameter();
487 // get arguments of sphere (the sphere should not be cutted at v-center)
488 if ( aSurf.GetType() == GeomAbs_Sphere )
490 if ( aVmax <= Precision::PConfusion() || aVmin >= Precision::PConfusion() )
495 Handle(Geom_Surface) aBasisSurface = Handle(Geom_Surface)::DownCast(
496 aSurf.Surface().Surface()->Transformed( aSurf.Trsf() ) );
498 Handle(Geom_Curve) aCurve = aBasisSurface->VIso( 0.0 );
500 if ( aCurve->IsKind( STANDARD_TYPE( Geom_Circle ) ) )
504 aCircle = Handle(Geom_Circle)::DownCast( aCurve );
506 else if ( aCurve->IsKind( STANDARD_TYPE( Geom_TrimmedCurve ) ) )
508 Handle(Geom_TrimmedCurve) aTrimmedCurve = Handle(Geom_TrimmedCurve)::DownCast( aCurve );
509 aPmin = aTrimmedCurve->FirstParameter();
510 aPmax = aTrimmedCurve->LastParameter();
512 aCircle = Handle(Geom_Circle)::DownCast( aTrimmedCurve );
517 // get arguments of cone
518 if ( aSurf.GetType() == GeomAbs_Cone )
523 gp_Cone aCone = aSurf.Cone();
524 gp_Ax2 anAx2 = aCone.Position().Ax2();
525 aCircle = new Geom_Circle( anAx2, aCone.RefRadius() );
527 aFaceN = aCone.SemiAngle() > 0.0
528 ? anAx2.Axis().Direction()
529 : -anAx2.Axis().Direction();
533 // get arguments of closed torus or cylinder
534 if ( aSurf.GetType() == GeomAbs_Torus || aSurf.GetType() == GeomAbs_Cylinder )
536 Handle(Geom_Surface) aBasisSurface = Handle(Geom_Surface)::DownCast(
537 aSurf.Surface().Surface()->Transformed( aSurf.Trsf() ) );
539 Handle(Geom_Curve) aCurve = aBasisSurface->VIso( (aVmax + aVmin) * 0.5 );
541 if ( aCurve->IsKind( STANDARD_TYPE( Geom_Circle ) ) )
545 aCircle = Handle(Geom_Circle)::DownCast( aCurve );
547 else if ( aCurve->IsKind( STANDARD_TYPE( Geom_TrimmedCurve ) ) )
549 Handle(Geom_TrimmedCurve) aTrimmedCurve = Handle(Geom_TrimmedCurve)::DownCast( aCurve );
550 aPmin = aTrimmedCurve->FirstParameter();
551 aPmax = aTrimmedCurve->LastParameter();
553 aCircle = Handle(Geom_Circle)::DownCast( aTrimmedCurve );
559 // face containing edge?
560 TopExp_Explorer anExp( aMeasuredShape, TopAbs_EDGE );
566 TopoDS_Shape anExpEdge = anExp.Current();
567 if ( anExpEdge.IsNull() )
572 // only a single edge is expected
579 // do not break, go to edge checking
580 aMeasuredShape = anExpEdge;
585 TopoDS_Edge aMeasureEdge = TopoDS::Edge( aMeasuredShape );
587 BRepAdaptor_Curve aCurve(aMeasureEdge);
589 if ( aCurve.GetType() != GeomAbs_Circle )
594 aPmin = aCurve.FirstParameter();
595 aPmax = aCurve.LastParameter();
597 aCircle = new Geom_Circle( aCurve.Circle() );
599 // check if there is an parent face containing the edge
600 TopTools_IndexedDataMapOfShapeListOfShape aShapeMap;
601 TopExp::MapShapesAndAncestors( aMainShape, TopAbs_EDGE, TopAbs_FACE, aShapeMap );
602 const TopTools_ListOfShape& aFaces = aShapeMap.FindFromKey( aMeasureEdge );
604 TopTools_ListIteratorOfListOfShape aFaceIt( aFaces );
605 for ( ; aFaceIt.More(); aFaceIt.Next() )
607 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
609 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( TopoDS::Face( aFace ) );
611 gp_Pnt aCircCenter = aCircle->Circ().Location();
612 Standard_Real aCircU = 0.0, aCircV = 0.0;
613 GeomLib_Tool::Parameters( aSurface, aCircCenter, Precision::Confusion(), aCircU, aCircV );
616 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aCircU, aCircV ), Precision::Confusion(), aNorm ) > 1 )
621 if ( aNorm.Angle( aCircle->Circ().Axis().Direction() ) > M_PI * 0.25 )
626 aFaceN = gp_Vec( aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm );
632 if ( aCircle.IsNull() )
637 ElCLib::AdjustPeriodic( 0.0, M_PI * 2, Precision::PConfusion(), aPmin, aPmax );
639 /* ------------------------- *
640 * position the dimension
641 * ------------------------- */
647 // diameter for closed circle
648 if ( Abs( ( aPmax - aPmin ) - M_PI * 2 ) <= Precision::PConfusion() )
650 PositionDiameter( aBnd, aFaceN, aCircle->Circ(), aPnt1, aPnt2, aPln );
652 // diameter for half-closed circle
653 else if ( Abs( aPmax - aPmin ) > M_PI )
655 Standard_Real anAnchor = aPmin + ( ( aPmax - aPmin ) - M_PI ) * 0.5;
657 PositionDiameter( aBnd, aFaceN, aCircle->Circ(), anAnchor, aPln );
659 aPnt1 = ElCLib::Value( anAnchor, aCircle->Circ() );
660 aPnt2 = ElCLib::Value( anAnchor + M_PI, aCircle->Circ() );
662 // diameter for less than half-closed circle
665 Standard_Real anAnchor = aPmin + ( aPmax - aPmin ) * 0.5;
667 PositionDiameter( aBnd, aFaceN, aCircle->Circ(), anAnchor, aPln );
669 aPnt1 = ElCLib::Value( anAnchor, aCircle->Circ() );
670 aPnt2 = ElCLib::Value( anAnchor + M_PI, aCircle->Circ() );
673 /* --------------------------------------------------------- *
674 * construct the dimension for the best selected position
675 * --------------------------------------------------------- */
677 gp_Pnt aCircP = aCircle->Circ().Location();
678 gp_Dir aCircN = aCircle->Circ().Axis().Direction();
679 gp_Dir aCircX = gce_MakeDir( aPnt1, aPnt2 );
680 Standard_Real aCircR = aCircle->Circ().Radius();
682 // construct closed circle as base for the diameter dimension
683 Standard_Boolean isReversed = ( ( aPln.Axis().Direction() ^ aCircX ) * aCircN ) < 0.0;
685 gp_Circ aRuledCirc = gce_MakeCirc( gp_Ax2( aCircP, isReversed ? -aCircN : aCircN, aCircX ), aCircR );
687 Handle(AIS_DiameterDimension) aDimension = new AIS_DiameterDimension( aRuledCirc, aPln );
689 // if flyout is extended in tangent direction to circle, the default flyout value is used
690 // if flyout is extended in plane of circle, the zero flyout value is chosen initially
691 Standard_Real aFlyout = aCircN.IsParallel( aPln.Axis().Direction(), Precision::Angular() ) ? 0.0 : Settings.DefaultFlyout;
693 aDimension->SetFlyout(aFlyout);
695 if ( !aDimension->IsValid() )
703 //=================================================================================
704 // function : AngleByTwoEdges
706 //=================================================================================
707 Handle(AIS_AngleDimension) MeasureGUI_DimensionCreateTool::AngleByTwoEdges( const GEOM::GeomObjPtr& theEdge1,
708 const GEOM::GeomObjPtr& theEdge2 ) const
710 /* --------------------------------------------------- */
711 /* get construction and parent shapes */
712 /* --------------------------------------------------- */
714 TopoDS_Shape aShapeEdge1;
715 TopoDS_Shape aShapeMain1;
716 if ( !GEOMBase::GetShape( theEdge1.get(), aShapeEdge1 ) )
720 if ( !GEOMBase::GetShape( GetMainShape( theEdge1 ).get(), aShapeMain1 ) )
725 TopoDS_Shape aShapeEdge2;
726 TopoDS_Shape aShapeMain2;
727 if ( !GEOMBase::GetShape( theEdge2.get(), aShapeEdge2 ) )
731 if ( !GEOMBase::GetShape( GetMainShape( theEdge2 ).get(), aShapeMain2 ) )
736 /* ---------------------------------------------------- */
737 /* check construction edges */
738 /* ---------------------------------------------------- */
740 TopoDS_Edge aFirstEdge = TopoDS::Edge( aShapeEdge1 );
741 TopoDS_Edge aSecondEdge = TopoDS::Edge( aShapeEdge2 );
743 // check whether it is possible to compute dimension on the passed edges
744 Handle(AIS_AngleDimension) aDimension = new AIS_AngleDimension( aFirstEdge, aSecondEdge );
746 if ( !aDimension->IsValid() )
751 const gp_Pnt& aFirstPoint = aDimension->FirstPoint();
752 const gp_Pnt& aSecondPoint = aDimension->SecondPoint();
753 const gp_Pnt& aCenterPoint = aDimension->CenterPoint();
755 gp_Vec aVec1( aCenterPoint, aFirstPoint );
756 gp_Vec aVec2( aCenterPoint, aSecondPoint );
758 Standard_Real anAngle = aVec2.AngleWithRef( aVec1, aDimension->GetPlane().Axis().Direction() );
762 aDimension = new AIS_AngleDimension( aSecondPoint, aCenterPoint, aFirstPoint );
765 aDimension->SetFlyout( Settings.DefaultFlyout );
770 //=================================================================================
771 // function : AngleByThreePoints
773 //=================================================================================
774 Handle(AIS_AngleDimension) MeasureGUI_DimensionCreateTool::AngleByThreePoints( const GEOM::GeomObjPtr& thePoint1,
775 const GEOM::GeomObjPtr& thePoint2,
776 const GEOM::GeomObjPtr& thePoint3 ) const
778 TopoDS_Shape aFirstSh;
779 if ( !GEOMBase::GetShape( thePoint1.operator ->(), aFirstSh ) )
784 TopoDS_Shape aSecondSh;
785 if ( !GEOMBase::GetShape( thePoint2.operator ->(), aSecondSh ) )
790 TopoDS_Shape aThirdSh;
791 if ( !GEOMBase::GetShape( thePoint3.operator ->(), aThirdSh ) )
796 TopoDS_Vertex aFirstVertex = TopoDS::Vertex( aFirstSh );
797 TopoDS_Vertex aSecondVertex = TopoDS::Vertex( aSecondSh );
798 TopoDS_Vertex aThirdVertex = TopoDS::Vertex( aThirdSh );
800 gp_Pnt aPnt1 = BRep_Tool::Pnt( aFirstVertex );
801 gp_Pnt aPnt2 = BRep_Tool::Pnt( aSecondVertex );
802 gp_Pnt aPnt3 = BRep_Tool::Pnt( aThirdVertex );
804 // check whether it is possible to compute dimension on the passed points
805 Handle(AIS_AngleDimension) aDimension = new AIS_AngleDimension( aPnt1, aPnt2, aPnt3 );
807 if ( !aDimension->IsValid() )
812 aDimension->SetFlyout( Settings.DefaultFlyout );
817 //=================================================================================
818 // function : PositionLength
819 // purpose : The method provides preliminary positioning algorithm for
820 // for length dimensions measuring the length between two points.
822 // theBnd [in] - the bounding box of the main shape
823 // theFaceN1 [in] - the normal to a first face of edge length (if any)
824 // theFaceN2 [in] - the normal to a second face of edge length (if any)
825 // theFaceS1 [in] - the side vector from a first face of edge length (if any)
826 // theFaceS2 [in] - the side vector from a second face of edge length (if any)
827 // thePnt1 [in] - the first measured point
828 // thePnt2 [in] - the last measured point
829 // The method selects flyout plane to best match the current
830 // view projection. If edge length is constructed, then the flyout
831 // can go in line with sides of faces, normal to the faces, or
832 // aligned to XOY, YOZ, ZOX planes.
833 //=================================================================================
834 void MeasureGUI_DimensionCreateTool::PositionLength( const Bnd_Box& theBnd,
835 const gp_Vec& theFaceN1,
836 const gp_Vec& theFaceN2,
837 const gp_Vec& theFaceS1,
838 const gp_Vec& theFaceS2,
839 const gp_Pnt& thePnt1,
840 const gp_Pnt& thePnt2,
841 gp_Pln& thePln ) const
843 Standard_Boolean isFace1 = theFaceN1.Magnitude() > Precision::Confusion();
844 Standard_Boolean isFace2 = theFaceN2.Magnitude() > Precision::Confusion();
845 gp_Vec anAverageN( gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(0.0, 0.0, 0.0) );
847 // get average direction in case of two non-sharp angled faces
848 if ( isFace1 && isFace2 )
850 Standard_Boolean isSame = theFaceN1.IsParallel( theFaceN2, Precision::Angular() );
853 gp_Dir aReferenceDir = theFaceN1 ^ theFaceN2;
854 // compute angle between face sides [0 - 2PI]
855 Standard_Real aDirAngle = theFaceN1.AngleWithRef( theFaceN2, aReferenceDir );
858 aDirAngle = ( M_PI * 2.0 ) - aDirAngle;
861 // non-sharp angle, use averaged directio
862 if ( aDirAngle > M_PI * 0.5 )
864 anAverageN = theFaceN1 + theFaceN2;
867 if ( aDirAngle > M_PI )
869 isFace1 = Standard_False;
870 isFace2 = Standard_False;
875 Standard_Boolean isAverage = anAverageN.Magnitude() > Precision::Confusion();
877 SeqOfDirs aFlyoutDirs;
880 aFlyoutDirs.Append( theFaceN1 );
881 aFlyoutDirs.Append( theFaceS1 );
885 aFlyoutDirs.Append( theFaceN2 );
886 aFlyoutDirs.Append( theFaceS2 );
890 aFlyoutDirs.Append( anAverageN );
893 ChooseLengthFlyoutsFromBnd( aFlyoutDirs, thePnt1, thePnt2, theBnd );
895 if ( aFlyoutDirs.IsEmpty() )
900 gp_Dir aPointDir = gce_MakeDir( thePnt1, thePnt2 );
902 // make planes for dimension presentation according to flyout directions
903 SeqOfPlanes aSeqOfPlanes;
904 for ( Standard_Integer aFlyoutIt = 1; aFlyoutIt <= aFlyoutDirs.Length(); ++aFlyoutIt )
906 gp_Pln aPlane( thePnt1, aPointDir ^ aFlyoutDirs.Value( aFlyoutIt ) );
907 aSeqOfPlanes.Append( aPlane );
910 Handle(V3d_View) aView = Settings.ActiveView;
912 thePln = !aView.IsNull()
913 ? SelectPlaneForProjection( aSeqOfPlanes, aView )
914 : aSeqOfPlanes.First();
917 //=================================================================================
918 // function : PositionDiameter
919 // purpose : The method provides preliminary positioning algorithm for
920 // for diameter dimensions measuring the circle.
922 // theBnd [in] - the bounding box of the shape
923 // theFaceN [in] - the circle face normal (can be void)
924 // theCirc [in] - the measured circle
925 // thePnt1 [out] - first dimension point
926 // thePnt2 [out] - second dimension point
927 // thePln [out] - dimension flyout plane
928 // The method selects points on the circle for diameter dimension and
929 // flyout plane to best match the current view projection (if any)
930 // The points are aligned to XOY, YOZ, ZOX planes.
931 // The flyout takes into account bounding box of main shape of face normal
932 // vector. The flyouts tangential to the circle plane are directed in
933 // accordance with the face normal (if not-null), otherwise the flyouts
934 // are turned to direct to the closest border of bounding box.
935 //=================================================================================
936 void MeasureGUI_DimensionCreateTool::PositionDiameter( const Bnd_Box& theBnd,
937 const gp_Vec& theFaceN,
938 const gp_Circ& theCirc,
941 gp_Pln& thePln ) const
943 // select list of measured segments aligned to projection planes
944 SeqOfDirs aProjectionDirs;
945 aProjectionDirs.Append( gp::DX() );
946 aProjectionDirs.Append( gp::DY() );
947 aProjectionDirs.Append( gp::DZ() );
949 SeqOfSegments aMeasureSegments = GetInPlaneSegments( theCirc, aProjectionDirs );
951 SeqOfPlnsAndSegments aSelectedPlanes;
953 // select in-circle-plane direction for flyout closest to border of bounding box
954 for ( Standard_Integer aSegmentIt = 1; aSegmentIt <= aMeasureSegments.Length(); ++aSegmentIt )
956 const Segment& aSegment = aMeasureSegments.Value(aSegmentIt);
958 Standard_Real anAnchor = ElCLib::Parameter( theCirc, aSegment.First );
960 gp_Pln aSelectedPlane;
962 PositionDiameter( theBnd, theFaceN, theCirc, anAnchor, aSelectedPlane );
964 aSelectedPlanes.Append( PlaneAndSegment( aSelectedPlane, aSegment ) );
967 Handle(V3d_View) aView = Settings.ActiveView;
969 PlaneAndSegment aChoosenParams = !aView.IsNull()
970 ? SelectPlaneForProjection( aSelectedPlanes, aView )
971 : aSelectedPlanes.First();
973 thePnt1 = ((Segment)aChoosenParams).First;
974 thePnt2 = ((Segment)aChoosenParams).Last;
975 thePln = ((gp_Pln)aChoosenParams);
978 //=================================================================================
979 // function : PositionDiameter
980 // purpose : The method provides preliminary positioning algorithm for
981 // for diameter dimensions measuring the circle. The diameter
982 // dimension is bound at anchor point on the circle.
984 // theBnd [in] the bounding box of the shape
985 // theFaceN [in] - the circle face normal (can be void)
986 // theCirc [in] - the measured circle
987 // theAnchorAt [in] - the anchoring parameter
988 // thePln [out] - dimension flyout plane
989 // The method selects flyout plane to best match the current
990 // view projection. The flyout plane can be parallel to circle,
992 //=================================================================================
993 void MeasureGUI_DimensionCreateTool::PositionDiameter( const Bnd_Box& theBnd,
994 const gp_Vec& theFaceN,
995 const gp_Circ& theCirc,
996 const Standard_Real& theAnchorAt,
997 gp_Pln& thePln ) const
999 gp_Dir aCircN = theCirc.Axis().Direction();
1000 gp_Pnt aCircP = theCirc.Location();
1002 // select tangent direction for flyout closest to border of bounding box
1003 gp_Dir aSelectedTanDir;
1004 if ( theFaceN.Magnitude() < Precision::Confusion() )
1006 SeqOfDirs aTangentDirs;
1007 aTangentDirs.Append( aCircN );
1008 aTangentDirs.Append( -aCircN );
1009 aSelectedTanDir = ChooseDirFromBnd( aTangentDirs, aCircP, theBnd );
1013 aSelectedTanDir = gp_Dir( theFaceN );
1016 gp_Pnt aPnt1 = ElCLib::Value( theAnchorAt, theCirc );
1017 gp_Pnt aPnt2 = ElCLib::Value( theAnchorAt + M_PI, theCirc );
1019 gp_Dir aSegmentDir = gce_MakeDir( aPnt1, aPnt2 );
1021 SeqOfDirs aSegmentDirs;
1022 aSegmentDirs.Append( aCircN ^ aSegmentDir );
1023 aSegmentDirs.Append( -aCircN ^ aSegmentDir );
1024 gp_Dir aSelectedSegDir = ChooseDirFromBnd( aSegmentDirs, aCircP, theBnd );
1026 gp_Pln aTangentFlyout( aCircP, aSegmentDir ^ aSelectedTanDir );
1027 gp_Pln aCoplanarFlyout( aCircP, aSegmentDir ^ aSelectedSegDir );
1029 SeqOfPlanes aSelectedPlanes;
1030 aSelectedPlanes.Append( aTangentFlyout );
1031 aSelectedPlanes.Append( aCoplanarFlyout );
1033 Handle(V3d_View) aView = Settings.ActiveView;
1035 thePln = !aView.IsNull()
1036 ? SelectPlaneForProjection( aSelectedPlanes, aView )
1037 : aSelectedPlanes.First();
1040 //=================================================================================
1041 // function : ChooseLengthFlyoutsFromBnd
1043 //=================================================================================
1044 void MeasureGUI_DimensionCreateTool::ChooseLengthFlyoutsFromBnd( SeqOfDirs& theDirs,
1045 const gp_Pnt& thePnt1,
1046 const gp_Pnt& thePnt2,
1047 const Bnd_Box& theBnd ) const
1049 // compose a list of axis-aligned planes for lying-in flyouts
1050 NCollection_Sequence<gp_Pln> anAAPlanes;
1052 // the axis-aligned planes for flyouts are built from
1053 // three points (P1, P2, and P1 translated in orthogonal
1054 // direction dx, dy, dz)
1055 gp_Dir anAxes[3] = { gp::DX(), gp::DY(), gp::DZ() };
1057 for ( int anIt = 0; anIt < 3; ++anIt )
1059 const gp_Dir& anAxisDir = anAxes[anIt];
1060 gp_Pnt aPnt3 = thePnt1.Translated( gp_Vec( anAxisDir ) );
1061 gce_MakePln aMakePlane( thePnt1, thePnt2, aPnt3 );
1062 if ( !aMakePlane.IsDone() )
1067 anAAPlanes.Append( aMakePlane.Value() );
1070 // find out what is the closest direction outside of the bounding box
1071 NCollection_Sequence<gp_Pln>::Iterator aPlaneIt( anAAPlanes );
1073 gp_Dir aPointDir = gce_MakeDir( thePnt1, thePnt2 );
1075 for ( ; aPlaneIt.More(); aPlaneIt.Next() )
1077 const gp_Pln& aPlane = aPlaneIt.Value();
1079 // transform bounding box to orthogonal coordinates relative to
1080 // dimension points P1, P2 (x-axis) and plane direction (z-axis),
1081 // where y coordinates will correspond to flyout direction against
1082 // the dimension point line
1083 gp_Ax3 aFlyoutSpace( thePnt1, aPlane.Axis().Direction(), aPointDir );
1085 gp_Trsf aRelativeTransform;
1086 aRelativeTransform.SetTransformation( gp_Ax3(), aFlyoutSpace );
1087 Bnd_Box aRelativeBounds = theBnd.Transformed( aRelativeTransform );
1089 Standard_Real aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
1090 aRelativeBounds.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
1092 gp_Dir aPosFlyout = aPlane.Axis().Direction() ^ aPointDir;
1093 gp_Dir aNegFlyout = aPosFlyout.Reversed();
1095 // select positive or negative flyout
1096 theDirs.Append( Abs( aYmax ) < Abs( aYmin ) ? aPosFlyout : aNegFlyout );
1100 //=================================================================================
1101 // function : ChooseDirFromBnd
1102 // purpose : The method chooses the best direction from the passed list of
1103 // directions, which is closest to the bounding box border.
1105 // theCandidates [in] the list of candidate directions
1106 // thePos [in] the position from where the directions are traced
1107 // theBnd [in] the bounding box of main shape
1108 //=================================================================================
1109 gp_Dir MeasureGUI_DimensionCreateTool::ChooseDirFromBnd( const SeqOfDirs& theCandidates,
1110 const gp_Pnt& thePos,
1111 const Bnd_Box& theBnd ) const
1115 Standard_Real aBestDistance = RealLast();
1117 SeqOfDirs::Iterator anIt( theCandidates );
1118 for ( ; anIt.More(); anIt.Next() )
1120 const gp_Dir& aDir = anIt.Value();
1122 gp_Ax3 aFlyoutSpace( thePos, aDir );
1124 gp_Trsf aRelativeTransform;
1125 aRelativeTransform.SetTransformation( gp_Ax3(), aFlyoutSpace );
1126 Bnd_Box aRelativeBounds = theBnd.Transformed( aRelativeTransform );
1128 Standard_Real aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
1129 aRelativeBounds.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
1131 if ( aYmax < aBestDistance )
1134 aBestDistance = aYmax;
1141 //=================================================================================
1142 // function : SelectPlaneForProjection
1143 // purpose : Select best matching plane in current view projection
1144 //=================================================================================
1145 template <typename TPlane>
1146 TPlane MeasureGUI_DimensionCreateTool::SelectPlaneForProjection( const NCollection_Sequence<TPlane>& thePlanes,
1147 const Handle(V3d_View)& theView ) const
1151 theView->Up( U[0], U[1], U[2] );
1152 theView->Proj( N[0], N[1], N[2] );
1154 gp_Dir aViewN( (Standard_Real)N[0], (Standard_Real)N[1], (Standard_Real)N[2] );
1155 gp_Dir aViewU( (Standard_Real)U[0], (Standard_Real)U[1], (Standard_Real)U[2] );
1157 TPlane aBestPlane = thePlanes.First();
1159 Standard_Real aBestDotProduct = RealFirst();
1161 for ( Standard_Integer aPlnIt = 1; aPlnIt <= thePlanes.Length(); ++aPlnIt )
1163 const TPlane& aPlane = thePlanes.Value( aPlnIt );
1165 Standard_Real aDotProduct = Abs( ((gp_Pln)aPlane).Axis().Direction() * aViewN );
1167 // preferred plane is "view parallel"
1168 if ( aDotProduct <= aBestDotProduct )
1173 aBestPlane = aPlane;
1175 aBestDotProduct = aDotProduct;
1181 //=================================================================================
1182 // function : GetMainShape
1184 //=================================================================================
1185 GEOM::GeomObjPtr MeasureGUI_DimensionCreateTool::GetMainShape( const GEOM::GeomObjPtr& theShape ) const
1187 // iterate over top-level objects to search for main shape
1188 GEOM::GeomObjPtr aMainShapeIt = theShape;
1189 while ( !aMainShapeIt->IsMainShape() )
1191 aMainShapeIt = aMainShapeIt->GetMainShape();
1193 return aMainShapeIt;
1196 //=================================================================================
1197 // function : GetFaceSide
1199 //=================================================================================
1200 bool MeasureGUI_DimensionCreateTool::GetFaceSide( const TopoDS_Face& theFace, const TopoDS_Edge& theEdge, gp_Dir& theDir ) const
1202 // get correctly oriented edge from main shape
1203 TopoDS_Edge anEdgeFromFace;
1204 TopExp_Explorer anExplorer( theFace.Oriented( TopAbs_FORWARD ), TopAbs_EDGE );
1205 for ( ; anExplorer.More(); anExplorer.Next() )
1207 TopoDS_Edge aCurrentEdge = TopoDS::Edge( anExplorer.Current() );
1208 if ( theEdge.IsSame( aCurrentEdge ) )
1210 anEdgeFromFace = aCurrentEdge;
1215 if ( anEdgeFromFace.IsNull() )
1220 // check out the direction of face extensions from its boundaries at the edge location
1221 // made assumption here that for any linear bounding edge the
1222 // normals are same on the whole length of that edge
1223 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( theFace );
1224 if ( aSurface.IsNull() || !aSurface->IsKind( STANDARD_TYPE(Geom_ElementarySurface) ) )
1229 BRepAdaptor_Curve aSurfCurve( anEdgeFromFace, theFace );
1230 if ( !aSurfCurve.IsCurveOnSurface() )
1235 Standard_Real aHalfRange = ( aSurfCurve.FirstParameter() + aSurfCurve.LastParameter() ) / 2.0;
1237 gp_Pnt aPoint = aSurfCurve.Value( aHalfRange );
1239 Standard_Real aPointU = 0.0;
1240 Standard_Real aPointV = 0.0;
1241 GeomLib_Tool::Parameters( aSurface, aPoint, Precision::Confusion(), aPointU, aPointV );
1244 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aPointU, aPointV ), Precision::Confusion(), aNorm ) > 1 )
1249 gp_Vec aTangent = aSurfCurve.DN( aHalfRange, 1 );
1250 if ( aTangent.Magnitude() < Precision::Confusion() )
1255 TopAbs_Orientation anEdgeOrientation = anEdgeFromFace.Orientation();
1256 if ( anEdgeOrientation == TopAbs_REVERSED )
1261 theDir = gp_Dir( aTangent ) ^ aNorm;
1265 //=================================================================================
1266 // function : GetInPlaneSegments
1267 // purpose : The method finds segments crossing the passed circle,
1268 // which lie in the passed planes.
1270 // theCirc [in] the circle to be crossed.
1271 // thePlanes [in] the projection planes crossing the circle.
1272 //=================================================================================
1273 MeasureGUI_DimensionCreateTool::SeqOfSegments
1274 MeasureGUI_DimensionCreateTool::GetInPlaneSegments( const gp_Circ& theCirc,
1275 const SeqOfDirs& thePlanes ) const
1277 SeqOfSegments aResult;
1279 gp_Pnt aCircP = theCirc.Location();
1280 gp_Dir aCircN = theCirc.Axis().Direction();
1281 Standard_Real aCircR = theCirc.Radius();
1283 SeqOfDirs::Iterator anIt( thePlanes );
1284 for ( ; anIt.More(); anIt.Next() )
1286 const gp_Dir& aDir = anIt.Value();
1288 if ( aDir.IsParallel( aCircN, Precision::Angular() ) )
1293 gp_Dir aIntDir = aDir ^ aCircN;
1295 gp_Pnt aPnt1 = gp_Pnt( aCircP.XYZ() - aIntDir.XYZ() * aCircR );
1296 gp_Pnt aPnt2 = gp_Pnt( aCircP.XYZ() + aIntDir.XYZ() * aCircR );
1298 aSegment.First = aPnt1;
1299 aSegment.Last = aPnt2;
1300 aResult.Append( aSegment );