1 // Copyright (C) 2007-2013 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.
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>
80 // plane associated with custom data
81 struct PlaneAndSegment
84 PlaneAndSegment(const gp_Pln& thePlane, const MeasureGUI_DimensionCreateTool::Segment& theSegment) : pln(thePlane), seg(theSegment) {}
85 operator gp_Pln () const { return pln; }
86 operator MeasureGUI_DimensionCreateTool::Segment () const { return seg; }
88 MeasureGUI_DimensionCreateTool::Segment seg;
91 typedef NCollection_Sequence<PlaneAndSegment> SeqOfPlnsAndSegments;
93 //=================================================================================
94 // function : Constructor
96 //=================================================================================
97 MeasureGUI_DimensionCreateTool::MeasureGUI_DimensionCreateTool()
99 Settings.DefaultFlyout = 0.0;
100 Settings.ActiveView = NULL;
103 //=================================================================================
104 // function : LengthOnEdge
106 //=================================================================================
107 Handle(AIS_LengthDimension) MeasureGUI_DimensionCreateTool::LengthOnEdge( const GEOM::GeomObjPtr& theMeasuredObj ) const
109 /* ---------------------------------------------------------------- *
110 * get the edge and parent shape *
111 * ---------------------------------------------------------------- */
113 TopoDS_Shape aMeasuredShape;
114 TopoDS_Shape aMainShape;
115 if ( !GEOMBase::GetShape( theMeasuredObj.operator ->(), aMeasuredShape ) )
120 if ( !GEOMBase::GetShape( GetMainShape( theMeasuredObj ).get(), aMainShape ) )
125 /* ------------------------------------------------- */
126 /* check the input geometry */
127 /* ------------------------------------------------- */
129 TopoDS_Edge anEdge = TopoDS::Edge( aMeasuredShape );
131 TopoDS_Vertex aVertex1;
132 TopoDS_Vertex aVertex2;
133 TopExp::Vertices( anEdge, aVertex1, aVertex2 );
135 gp_Pnt aPnt1 = BRep_Tool::Pnt( aVertex1 );
136 gp_Pnt aPnt2 = BRep_Tool::Pnt( aVertex2 );
137 if ( aPnt1.Distance( aPnt2 ) <= Precision::Confusion() )
142 /* ------------------------- *
143 * position the dimension
144 * ------------------------- */
147 BRepBndLib::AddClose( aMainShape, aBnd );
150 TopTools_IndexedDataMapOfShapeListOfShape aRelationMap;
151 TopExp::MapShapesAndAncestors( aMainShape, TopAbs_EDGE, TopAbs_FACE, aRelationMap );
152 const TopTools_ListOfShape& aRelatedFaces = aRelationMap.FindFromKey( anEdge );
154 gp_Vec aFaceN1( gp::Origin(), gp::Origin() );
155 gp_Vec aFaceN2( gp::Origin(), gp::Origin() );
156 gp_Vec aFaceS1( gp::Origin(), gp::Origin() );
157 gp_Vec aFaceS2( gp::Origin(), gp::Origin() );
159 gp_Pnt aMiddlePnt = gp_Pnt( ( aPnt1.XYZ() + aPnt2.XYZ() ) * 0.5 );
161 TopTools_ListIteratorOfListOfShape aFaceIt( aRelatedFaces );
163 // get face side directions
164 if ( aFaceIt.More() )
166 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
169 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
174 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
176 Standard_Real aU = 0.0, aV = 0.0;
177 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
180 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
182 aFaceN1 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
188 if ( aFaceIt.More() )
190 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
193 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
198 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
200 Standard_Real aU = 0.0, aV = 0.0;
201 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
204 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
206 aFaceN2 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
211 PositionLength( aBnd, aFaceN1, aFaceN2, aFaceS1, aFaceS2, aPnt1, aPnt2, aPln );
213 /* --------------------------------------------------------- *
214 * construct the dimension for the best selected position
215 * --------------------------------------------------------- */
217 Handle(AIS_LengthDimension) aDimension = new AIS_LengthDimension( anEdge, aPln );
219 aDimension->SetFlyout( Settings.DefaultFlyout );
221 if ( !aDimension->IsValid() )
229 //=================================================================================
230 // function : LengthByPoints
232 //=================================================================================
233 Handle(AIS_LengthDimension) MeasureGUI_DimensionCreateTool::LengthByPoints( const GEOM::GeomObjPtr& theMeasuredObj1,
234 const GEOM::GeomObjPtr& theMeasuredObj2 ) const
236 /* ---------------------------------------------------------------- *
237 * get the edge and parent shape *
238 * ---------------------------------------------------------------- */
240 TopoDS_Shape aMeasuredShape1;
241 TopoDS_Shape aMeasuredShape2;
242 TopoDS_Shape aMainShape;
244 if ( !GEOMBase::GetShape( theMeasuredObj1.operator ->(), aMeasuredShape1 ) )
249 if ( !GEOMBase::GetShape( theMeasuredObj2.operator ->(), aMeasuredShape2 ) )
254 if ( !GEOMBase::GetShape( GetMainShape( theMeasuredObj1 ).get(), aMainShape ) )
259 /* ------------------------------------------------- */
260 /* check the input geometry */
261 /* ------------------------------------------------- */
263 TopoDS_Vertex aVertex1 = TopoDS::Vertex( aMeasuredShape1 );
264 TopoDS_Vertex aVertex2 = TopoDS::Vertex( aMeasuredShape2 );
266 gp_Pnt aPnt1 = BRep_Tool::Pnt( aVertex1 );
267 gp_Pnt aPnt2 = BRep_Tool::Pnt( aVertex2 );
268 if ( aPnt1.Distance( aPnt2 ) <= Precision::Confusion() )
273 /* ------------------------- *
274 * position the dimension
275 * ------------------------- */
278 BRepBndLib::AddClose( aMainShape, aBnd );
280 // check whether the points share same edge
281 TopExp_Explorer anEdgeExp( aMainShape, TopAbs_EDGE, TopAbs_EDGE );
282 for ( ; anEdgeExp.More(); anEdgeExp.Next() )
284 TopoDS_Vertex anEdgeV1;
285 TopoDS_Vertex anEdgeV2;
286 TopExp::Vertices( TopoDS::Edge( anEdgeExp.Current() ), anEdgeV1, anEdgeV2 );
287 gp_Pnt anEdgePnt1 = BRep_Tool::Pnt( anEdgeV1 );
288 gp_Pnt anEdgePnt2 = BRep_Tool::Pnt( anEdgeV2 );
290 if ( aPnt1.Distance( anEdgePnt1 ) <= Precision::Confusion() )
292 if ( aPnt2.Distance( anEdgePnt2 ) <= Precision::Confusion() )
298 if ( aPnt2.Distance( anEdgePnt1 ) <= Precision::Confusion() )
300 if ( aPnt1.Distance( anEdgePnt2 ) <= Precision::Confusion() )
307 gp_Vec aFaceN1( gp::Origin(), gp::Origin() );
308 gp_Vec aFaceN2( gp::Origin(), gp::Origin() );
309 gp_Vec aFaceS1( gp::Origin(), gp::Origin() );
310 gp_Vec aFaceS2( gp::Origin(), gp::Origin() );
313 if ( anEdgeExp.More() )
315 TopoDS_Edge anEdge = TopoDS::Edge( anEdgeExp.Current() );
316 TopTools_IndexedDataMapOfShapeListOfShape aRelationMap;
317 TopExp::MapShapesAndAncestors( aMainShape, TopAbs_EDGE, TopAbs_FACE, aRelationMap );
318 const TopTools_ListOfShape& aRelatedFaces = aRelationMap.FindFromKey( anEdge );
320 gp_Pnt aMiddlePnt = gp_Pnt( ( aPnt1.XYZ() + aPnt2.XYZ() ) * 0.5 );
322 TopTools_ListIteratorOfListOfShape aFaceIt( aRelatedFaces );
324 // get face side directions
325 if ( aFaceIt.More() )
327 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
330 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
335 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
337 Standard_Real aU = 0.0, aV = 0.0;
338 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
341 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
343 aFaceN1 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
349 if ( aFaceIt.More() )
351 TopoDS_Face aFace = TopoDS::Face( aFaceIt.Value() );
354 if ( GetFaceSide( aFace, anEdge, aSideDir ) )
359 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( aFace );
361 Standard_Real aU = 0.0, aV = 0.0;
362 GeomLib_Tool::Parameters( aSurface, aMiddlePnt, Precision::Confusion(), aU, aV );
365 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aU, aV ), Precision::Confusion(), aNorm ) <= 1 )
367 aFaceN2 = aFace.Orientation() == TopAbs_REVERSED ? -aNorm : aNorm;
373 PositionLength( aBnd, aFaceN1, aFaceN2, aFaceS1, aFaceS2, aPnt1, aPnt2, aPln );
375 /* --------------------------------------------------------- *
376 * construct the dimension for the best selected position
377 * --------------------------------------------------------- */
379 Handle(AIS_LengthDimension) aDimension = new AIS_LengthDimension( aPnt1, aPnt2, aPln );
381 aDimension->SetFlyout( Settings.DefaultFlyout );
383 if ( !aDimension->IsValid() )
391 //=================================================================================
392 // function : LengthByParallelEdges
394 //=================================================================================
395 Handle(AIS_LengthDimension) MeasureGUI_DimensionCreateTool::LengthByParallelEdges( const GEOM::GeomObjPtr& theEdge1,
396 const GEOM::GeomObjPtr& theEdge2 ) const
398 TopoDS_Shape aFirstSh;
399 if ( !GEOMBase::GetShape( theEdge1.operator ->(), aFirstSh ) )
404 TopoDS_Shape aSecondSh;
405 if ( !GEOMBase::GetShape( theEdge2.operator ->(), aSecondSh ) )
410 TopoDS_Edge aFirstEdge = TopoDS::Edge( aFirstSh );
411 TopoDS_Edge aSecondEdge = TopoDS::Edge( aSecondSh );
413 // Build plane through three points
414 BRepAdaptor_Curve aCurve1( aFirstEdge );
415 BRepAdaptor_Curve aCurve2( aSecondEdge );
417 gp_Pnt aPnt1 = aCurve1.Value( 0.1 );
418 gp_Pnt aPnt2 = aCurve1.Value( 0.9 );
419 gp_Pnt aPnt3 = aCurve2.Value( 0.5 );
421 GC_MakePlane aMkPlane( aPnt1, aPnt2, aPnt3 );
422 Handle(Geom_Plane) aPlane = aMkPlane.Value();
424 // check whether it is possible to compute valid dimension
425 Handle(AIS_LengthDimension) aDimension = new AIS_LengthDimension ( aFirstEdge, aSecondEdge, aPlane->Pln() );
427 aDimension->SetFlyout( Settings.DefaultFlyout );
429 if ( !aDimension->IsValid() )
437 //=================================================================================
438 // function : Diameter
440 //=================================================================================
441 Handle(AIS_DiameterDimension) MeasureGUI_DimensionCreateTool::Diameter( const GEOM::GeomObjPtr& theMeasuredObj ) const
443 /* ------------------------------------------------ *
444 * get the shape and its parent (if exist) *
445 * ------------------------------------------------ */
447 TopoDS_Shape aMeasuredShape;
448 TopoDS_Shape aMainShape;
449 if ( !GEOMBase::GetShape( theMeasuredObj.operator ->(), aMeasuredShape ) )
454 if ( !GEOMBase::GetShape( GetMainShape( theMeasuredObj ).get(), aMainShape ) )
460 BRepBndLib::AddClose( aMainShape, aBnd );
462 /* ------------------------------------------------ *
463 * get the dimension construction arguments *
464 * ------------------------------------------------ */
466 Handle(Geom_Circle) aCircle;
468 Standard_Real aPmin = 0, aPmax = 2 * M_PI;
470 gp_Vec aFaceN( gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(0.0, 0.0, 0.0) );
472 switch ( aMeasuredShape.ShapeType() )
476 TopoDS_Face aMeasuredFace = TopoDS::Face(aMeasuredShape);
478 BRepAdaptor_Surface aSurf( aMeasuredFace );
480 Standard_Real aVmin = aSurf.FirstVParameter();
481 Standard_Real aVmax = aSurf.LastVParameter();
483 // get arguments of closed sphere
484 if ( aSurf.GetType() == GeomAbs_Sphere )
486 if ( !aSurf.IsUClosed() || !aSurf.IsVClosed() )
491 // take circle in XOY plane from sphere
492 gp_Sphere aSphere = aSurf.Sphere();
493 gp_Ax2 anAx2 = gp_Ax2( aSphere.Location(), gp::DZ() );
494 aCircle = new Geom_Circle( anAx2, aSphere.Radius() );
499 // get arguments of closed torus
500 if ( aSurf.GetType() == GeomAbs_Torus )
502 if ( !aSurf.IsUClosed() || !aSurf.IsVClosed() )
507 gp_Torus aTorus = aSurf.Torus();
508 gp_Ax2 anAx2 = aTorus.Position().Ax2();
509 aCircle = new Geom_Circle( anAx2, aTorus.MinorRadius() );
513 // get arguments of closed cone
514 if ( aSurf.GetType() == GeomAbs_Cone )
516 if ( !aSurf.IsUClosed() || !aSurf.IsVClosed() )
521 gp_Cone aCone = aSurf.Cone();
522 gp_Ax2 anAx2 = aCone.Position().Ax2();
523 aCircle = new Geom_Circle( anAx2, aCone.RefRadius() );
525 aFaceN = aCone.SemiAngle() > 0.0
526 ? anAx2.Axis().Direction()
527 : -anAx2.Axis().Direction();
531 // get arguments of closed/opened cylinder
532 if ( aSurf.GetType() == GeomAbs_Cylinder )
534 Handle(Geom_Surface) aBasisSurface = Handle(Geom_Surface)::DownCast(
535 aSurf.Surface().Surface()->Transformed( aSurf.Trsf() ) );
537 Handle(Geom_Curve) aCurve = aBasisSurface->VIso( (aVmax + aVmin) * 0.5 );
539 if ( aCurve->IsKind( STANDARD_TYPE( Geom_Circle ) ) )
541 aPmin = aSurf.FirstUParameter();
542 aPmax = aSurf.LastUParameter();
543 aCircle = Handle(Geom_Circle)::DownCast( aCurve );
545 else if ( aCurve->IsKind( STANDARD_TYPE( Geom_TrimmedCurve ) ) )
547 Handle(Geom_TrimmedCurve) aTrimmedCurve = Handle(Geom_TrimmedCurve)::DownCast( aCurve );
548 aPmin = aTrimmedCurve->FirstParameter();
549 aPmax = aTrimmedCurve->LastParameter();
551 aCircle = Handle(Geom_Circle)::DownCast( aTrimmedCurve );
557 // face containing edge?
558 TopExp_Explorer anExp( aMeasuredShape, TopAbs_EDGE );
564 TopoDS_Shape anExpEdge = anExp.Current();
565 if ( anExpEdge.IsNull() )
570 // only a single edge is expected
577 // do not break, go to edge checking
578 aMeasuredShape = anExpEdge;
583 TopoDS_Edge aMeasureEdge = TopoDS::Edge( aMeasuredShape );
585 BRepAdaptor_Curve aCurve(aMeasureEdge);
587 if ( aCurve.GetType() != GeomAbs_Circle )
592 aPmin = aCurve.FirstParameter();
593 aPmax = aCurve.LastParameter();
595 aCircle = new Geom_Circle( aCurve.Circle() );
597 // check if there is an parent face containing the edge
598 TopTools_IndexedDataMapOfShapeListOfShape aShapeMap;
599 TopExp::MapShapesAndAncestors( aMainShape, TopAbs_EDGE, TopAbs_FACE, aShapeMap );
600 const TopTools_ListOfShape& aFaces = aShapeMap.FindFromKey( aMeasureEdge );
602 TopTools_ListIteratorOfListOfShape aFaceIt( aFaces );
603 for ( ; aFaceIt.More(); aFaceIt.Next() )
605 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( TopoDS::Face( aFaceIt.Value() ) );
607 gp_Pnt aCircCenter = aCircle->Circ().Location();
608 Standard_Real aCircU = 0.0, aCircV = 0.0;
609 GeomLib_Tool::Parameters( aSurface, aCircCenter, Precision::Confusion(), aCircU, aCircV );
612 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aCircU, aCircV ), Precision::Confusion(), aNorm ) > 1 )
617 if ( aNorm.Angle( aCircle->Circ().Axis().Direction() ) > M_PI * 0.25 )
622 aFaceN = gp_Vec( aNorm );
628 if ( aCircle.IsNull() )
633 ElCLib::AdjustPeriodic( 0.0, M_PI * 2, Precision::PConfusion(), aPmin, aPmax );
635 /* ------------------------- *
636 * position the dimension
637 * ------------------------- */
643 // diameter for closed circle
644 if ( Abs( ( aPmax - aPmin ) - M_PI * 2 ) <= Precision::PConfusion() )
646 PositionDiameter( aBnd, aFaceN, aCircle->Circ(), aPnt1, aPnt2, aPln );
648 // diameter for half-closed circle
649 else if ( Abs( aPmax - aPmin ) > M_PI )
651 Standard_Real anAnchor = aPmin + ( ( aPmax - aPmin ) - M_PI ) * 0.5;
653 PositionDiameter( aBnd, aFaceN, aCircle->Circ(), anAnchor, aPln );
655 aPnt1 = ElCLib::Value( anAnchor, aCircle->Circ() );
656 aPnt2 = ElCLib::Value( anAnchor + M_PI, aCircle->Circ() );
658 // diameter for less than half-closed circle
661 Standard_Real anAnchor = aPmin + ( aPmax - aPmin ) * 0.5;
663 PositionDiameter( aBnd, aFaceN, aCircle->Circ(), anAnchor, aPln );
665 aPnt1 = ElCLib::Value( anAnchor, aCircle->Circ() );
666 aPnt2 = ElCLib::Value( anAnchor + M_PI, aCircle->Circ() );
669 /* --------------------------------------------------------- *
670 * construct the dimension for the best selected position
671 * --------------------------------------------------------- */
673 gp_Pnt aCircP = aCircle->Circ().Location();
674 gp_Dir aCircN = aCircle->Circ().Axis().Direction();
675 gp_Dir aCircX = gce_MakeDir( aPnt1, aPnt2 );
676 Standard_Real aCircR = aCircle->Circ().Radius();
678 // construct closed circle as base for the diameter dimension
679 gp_Circ aRuledCirc = gce_MakeCirc( gp_Ax2( aCircP, aCircN, aCircX ), aCircR );
681 Handle(AIS_DiameterDimension) aDimension = new AIS_DiameterDimension( aRuledCirc, aPln );
683 // if flyout is extended in tangent direction to circle, the default flyout value is used
684 // if flyout is extended in plane of circle, the zero flyout value is choosen initially
685 Standard_Real aFlyout = aCircN.IsParallel( aPln.Axis().Direction(), Precision::Angular() ) ? 0.0 : Settings.DefaultFlyout;
687 aDimension->SetFlyout(aFlyout);
689 if ( !aDimension->IsValid() )
697 //=================================================================================
698 // function : AngleByTwoEdges
700 //=================================================================================
701 Handle(AIS_AngleDimension) MeasureGUI_DimensionCreateTool::AngleByTwoEdges( const GEOM::GeomObjPtr& theEdge1,
702 const GEOM::GeomObjPtr& theEdge2 ) const
704 /* --------------------------------------------------- */
705 /* get construction and parent shapes */
706 /* --------------------------------------------------- */
708 TopoDS_Shape aShapeEdge1;
709 TopoDS_Shape aShapeMain1;
710 if ( !GEOMBase::GetShape( theEdge1.get(), aShapeEdge1 ) )
714 if ( !GEOMBase::GetShape( GetMainShape( theEdge1 ).get(), aShapeMain1 ) )
719 TopoDS_Shape aShapeEdge2;
720 TopoDS_Shape aShapeMain2;
721 if ( !GEOMBase::GetShape( theEdge2.get(), aShapeEdge2 ) )
725 if ( !GEOMBase::GetShape( GetMainShape( theEdge2 ).get(), aShapeMain2 ) )
730 /* ---------------------------------------------------- */
731 /* check construction edges */
732 /* ---------------------------------------------------- */
734 TopoDS_Edge aFirstEdge = TopoDS::Edge( aShapeEdge1 );
735 TopoDS_Edge aSecondEdge = TopoDS::Edge( aShapeEdge2 );
737 // check whether it is possible to compute dimension on the passed edges
738 Handle(AIS_AngleDimension) aDimension = new AIS_AngleDimension( aFirstEdge, aSecondEdge );
740 if ( !aDimension->IsValid() )
745 const gp_Pnt& aFirstPoint = aDimension->FirstPoint();
746 const gp_Pnt& aSecondPoint = aDimension->SecondPoint();
747 const gp_Pnt& aCenterPoint = aDimension->CenterPoint();
749 gp_Vec aVec1( aCenterPoint, aFirstPoint );
750 gp_Vec aVec2( aCenterPoint, aSecondPoint );
752 Standard_Real anAngle = aVec2.AngleWithRef( aVec1, aDimension->GetPlane().Axis().Direction() );
756 aDimension = new AIS_AngleDimension( aSecondPoint, aCenterPoint, aFirstPoint );
759 aDimension->SetFlyout( Settings.DefaultFlyout );
764 //=================================================================================
765 // function : AngleByThreePoints
767 //=================================================================================
768 Handle(AIS_AngleDimension) MeasureGUI_DimensionCreateTool::AngleByThreePoints( const GEOM::GeomObjPtr& thePoint1,
769 const GEOM::GeomObjPtr& thePoint2,
770 const GEOM::GeomObjPtr& thePoint3 ) const
772 TopoDS_Shape aFirstSh;
773 if ( !GEOMBase::GetShape( thePoint1.operator ->(), aFirstSh ) )
778 TopoDS_Shape aSecondSh;
779 if ( !GEOMBase::GetShape( thePoint2.operator ->(), aSecondSh ) )
784 TopoDS_Shape aThirdSh;
785 if ( !GEOMBase::GetShape( thePoint3.operator ->(), aThirdSh ) )
790 TopoDS_Vertex aFirstVertex = TopoDS::Vertex( aFirstSh );
791 TopoDS_Vertex aSecondVertex = TopoDS::Vertex( aSecondSh );
792 TopoDS_Vertex aThirdVertex = TopoDS::Vertex( aThirdSh );
794 gp_Pnt aPnt1 = BRep_Tool::Pnt( aFirstVertex );
795 gp_Pnt aPnt2 = BRep_Tool::Pnt( aSecondVertex );
796 gp_Pnt aPnt3 = BRep_Tool::Pnt( aThirdVertex );
798 // check whether it is possible to compute dimension on the passed points
799 Handle(AIS_AngleDimension) aDimension = new AIS_AngleDimension( aPnt1, aPnt2, aPnt3 );
801 if ( !aDimension->IsValid() )
809 //=================================================================================
810 // function : PositionLength
811 // purpose : The method provides preliminary positioning algorithm for
812 // for length dimensions measuring the length between two points.
814 // theBnd [in] - the bounding box of the main shape
815 // theFaceN1 [in] - the normal to a first face of edge length (if any)
816 // theFaceN2 [in] - the normal to a second face of edge length (if any)
817 // theFaceS1 [in] - the side vector from a first face of edge length (if any)
818 // theFaceS2 [in] - the side vector from a second face of edge length (if any)
819 // thePnt1 [in] - the first measured point
820 // thePnt2 [in] - the last measured point
821 // The method selects flyout plane to best match the current
822 // view projection. If edge length is constructed, then the flyout
823 // can go in line with sides of faces, normal to the faces, or
824 // aligned to XOY, YOZ, ZOX planes.
825 //=================================================================================
826 void MeasureGUI_DimensionCreateTool::PositionLength( const Bnd_Box& theBnd,
827 const gp_Vec& theFaceN1,
828 const gp_Vec& theFaceN2,
829 const gp_Vec& theFaceS1,
830 const gp_Vec& theFaceS2,
831 const gp_Pnt& thePnt1,
832 const gp_Pnt& thePnt2,
833 gp_Pln& thePln ) const
835 Standard_Boolean isFace1 = theFaceN1.Magnitude() > Precision::Confusion();
836 Standard_Boolean isFace2 = theFaceN2.Magnitude() > Precision::Confusion();
837 gp_Vec anAverageN( gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(0.0, 0.0, 0.0) );
839 // get average direction in case of two non-sharp angled faces
840 if ( isFace1 && isFace2 )
842 Standard_Boolean isSame = theFaceN1.IsParallel( theFaceN2, Precision::Angular() );
845 gp_Dir aReferenceDir = theFaceN1 ^ theFaceN2;
846 // compute angle between face sides [0 - 2PI]
847 Standard_Real aDirAngle = theFaceN1.AngleWithRef( theFaceN2, aReferenceDir );
850 aDirAngle = ( M_PI * 2.0 ) - aDirAngle;
853 // non-sharp angle, use averaged directio
854 if ( aDirAngle > M_PI * 0.5 )
856 anAverageN = theFaceN1 + theFaceN2;
859 if ( aDirAngle > M_PI )
861 isFace1 = Standard_False;
862 isFace2 = Standard_False;
867 Standard_Boolean isAverage = anAverageN.Magnitude() > Precision::Confusion();
869 SeqOfDirs aFlyoutDirs;
872 aFlyoutDirs.Append( theFaceN1 );
873 aFlyoutDirs.Append( theFaceS1 );
877 aFlyoutDirs.Append( theFaceN2 );
878 aFlyoutDirs.Append( theFaceS2 );
882 aFlyoutDirs.Append( anAverageN );
885 ChooseLengthFlyoutsFromBnd( aFlyoutDirs, thePnt1, thePnt2, theBnd );
887 if ( aFlyoutDirs.IsEmpty() )
892 gp_Dir aPointDir = gce_MakeDir( thePnt1, thePnt2 );
894 // make planes for dimension presentation according to flyout directions
895 SeqOfPlanes aSeqOfPlanes;
896 for ( Standard_Integer aFlyoutIt = 1; aFlyoutIt <= aFlyoutDirs.Length(); ++aFlyoutIt )
898 gp_Pln aPlane( thePnt1, aPointDir ^ aFlyoutDirs.Value( aFlyoutIt ) );
899 aSeqOfPlanes.Append( aPlane );
902 Handle(V3d_View) aView = Settings.ActiveView;
904 thePln = !aView.IsNull()
905 ? SelectPlaneForProjection( aSeqOfPlanes, aView )
906 : aSeqOfPlanes.First();
909 //=================================================================================
910 // function : PositionDiameter
911 // purpose : The method provides preliminary positioning algorithm for
912 // for diameter dimensions measuring the circle.
914 // theBnd [in] - the bounding box of the shape
915 // theFaceN [in] - the circle face normal (can be void)
916 // theCirc [in] - the measured circle
917 // thePnt1 [out] - first dimension point
918 // thePnt2 [out] - second dimension point
919 // thePln [out] - dimension flyout plane
920 // The method selects points on the circle for diameter dimension and
921 // flyout plane to best match the current view projection (if any)
922 // The points are aligned to XOY, YOZ, ZOX planes.
923 // The flyout takes into account bounding box of main shape of face normal
924 // vector. The flyouts tangetial to the circle plane are directed in
925 // accordance with the face normal (if not-null), otherwise the flyouts
926 // are turned to direct to the closest border of bounding box.
927 //=================================================================================
928 void MeasureGUI_DimensionCreateTool::PositionDiameter( const Bnd_Box& theBnd,
929 const gp_Vec& theFaceN,
930 const gp_Circ& theCirc,
933 gp_Pln& thePln ) const
935 // select list of measured segments aligned to projection planes
936 SeqOfDirs aProjectionDirs;
937 aProjectionDirs.Append( gp::DX() );
938 aProjectionDirs.Append( gp::DY() );
939 aProjectionDirs.Append( gp::DZ() );
941 SeqOfSegments aMeasureSegments = GetInPlaneSegments( theCirc, aProjectionDirs );
943 SeqOfPlnsAndSegments aSelectedPlanes;
945 // select in-circle-plane direction for flyout closest to border of bounding box
946 for ( Standard_Integer aSegmentIt = 1; aSegmentIt <= aMeasureSegments.Length(); ++aSegmentIt )
948 const Segment& aSegment = aMeasureSegments.Value(aSegmentIt);
950 Standard_Real anAnchor = ElCLib::Parameter( theCirc, aSegment.First );
952 gp_Pln aSelectedPlane;
954 PositionDiameter( theBnd, theFaceN, theCirc, anAnchor, aSelectedPlane );
956 aSelectedPlanes.Append( PlaneAndSegment( aSelectedPlane, aSegment ) );
959 Handle(V3d_View) aView = Settings.ActiveView;
961 PlaneAndSegment aChoosenParams = !aView.IsNull()
962 ? SelectPlaneForProjection( aSelectedPlanes, aView )
963 : aSelectedPlanes.First();
965 thePnt1 = ((Segment)aChoosenParams).First;
966 thePnt2 = ((Segment)aChoosenParams).Last;
967 thePln = ((gp_Pln)aChoosenParams);
970 //=================================================================================
971 // function : PositionDiameter
972 // purpose : The method provides preliminary positioning algorithm for
973 // for diameter dimensions measuring the circle. The diameter
974 // dimension is bound at anchor point on the circle.
976 // theBnd [in] the bounding box of the shape
977 // theFaceN [in] - the circle face normal (can be void)
978 // theCirc [in] - the measured circle
979 // theAnchorAt [in] - the anchoring parameter
980 // thePln [out] - dimension flyout plane
981 // The method selects flyout plane to best match the current
982 // view projection. The flyout plane can be parallel to circle,
984 //=================================================================================
985 void MeasureGUI_DimensionCreateTool::PositionDiameter( const Bnd_Box& theBnd,
986 const gp_Vec& theFaceN,
987 const gp_Circ& theCirc,
988 const Standard_Real& theAnchorAt,
989 gp_Pln& thePln ) const
991 gp_Dir aCircN = theCirc.Axis().Direction();
992 gp_Pnt aCircP = theCirc.Location();
994 // select tangent direction for flyout closest to border of bounding box
995 gp_Dir aSelectedTanDir;
996 if ( theFaceN.Magnitude() < Precision::Confusion() )
998 SeqOfDirs aTangentDirs;
999 aTangentDirs.Append( aCircN );
1000 aTangentDirs.Append( -aCircN );
1001 aSelectedTanDir = ChooseDirFromBnd( aTangentDirs, aCircP, theBnd );
1005 aSelectedTanDir = gp_Dir( theFaceN );
1008 gp_Pnt aPnt1 = ElCLib::Value( theAnchorAt, theCirc );
1009 gp_Pnt aPnt2 = ElCLib::Value( theAnchorAt + M_PI, theCirc );
1011 gp_Dir aSegmentDir = gce_MakeDir( aPnt1, aPnt2 );
1013 SeqOfDirs aSegmentDirs;
1014 aSegmentDirs.Append( aCircN ^ aSegmentDir );
1015 aSegmentDirs.Append( -aCircN ^ aSegmentDir );
1016 gp_Dir aSelectedSegDir = ChooseDirFromBnd( aSegmentDirs, aCircP, theBnd );
1018 gp_Pln aTangentFlyout( aCircP, aSegmentDir ^ aSelectedTanDir );
1019 gp_Pln aCoplanarFlyout( aCircP, aSegmentDir ^ aSelectedSegDir );
1021 SeqOfPlanes aSelectedPlanes;
1022 aSelectedPlanes.Append( aTangentFlyout );
1023 aSelectedPlanes.Append( aCoplanarFlyout );
1025 Handle(V3d_View) aView = Settings.ActiveView;
1027 thePln = !aView.IsNull()
1028 ? SelectPlaneForProjection( aSelectedPlanes, aView )
1029 : aSelectedPlanes.First();
1032 //=================================================================================
1033 // function : ChooseLengthFlyoutsFromBnd
1035 //=================================================================================
1036 void MeasureGUI_DimensionCreateTool::ChooseLengthFlyoutsFromBnd( SeqOfDirs& theDirs,
1037 const gp_Pnt& thePnt1,
1038 const gp_Pnt& thePnt2,
1039 const Bnd_Box& theBnd ) const
1041 // compose a list of axis-aligned planes for lying-in flyouts
1042 NCollection_Sequence<gp_Pln> anAAPlanes;
1044 // the axis-aligned planes for flyouts are built from
1045 // three points (P1, P2, and P1 translated in orthogonal
1046 // direction dx, dy, dz)
1047 gp_Dir anAxes[3] = { gp::DX(), gp::DY(), gp::DZ() };
1049 for ( int anIt = 0; anIt < 3; ++anIt )
1051 const gp_Dir& anAxisDir = anAxes[anIt];
1052 gp_Pnt aPnt3 = thePnt1.Translated( gp_Vec( anAxisDir ) );
1053 gce_MakePln aMakePlane( thePnt1, thePnt2, aPnt3 );
1054 if ( !aMakePlane.IsDone() )
1059 anAAPlanes.Append( aMakePlane.Value() );
1062 // find out what is the closest direction outside of the bounding box
1063 NCollection_Sequence<gp_Pln>::Iterator aPlaneIt( anAAPlanes );
1065 gp_Dir aPointDir = gce_MakeDir( thePnt1, thePnt2 );
1067 for ( ; aPlaneIt.More(); aPlaneIt.Next() )
1069 const gp_Pln& aPlane = aPlaneIt.Value();
1071 // transform bounding box to orthogonal coordiantes relative to
1072 // dimension points P1, P2 (x-axis) and plane direction (z-axis),
1073 // where y coordinates will correspond to flyout direction against
1074 // the dimension point line
1075 gp_Ax3 aFlyoutSpace( thePnt1, aPlane.Axis().Direction(), aPointDir );
1077 gp_Trsf aRelativeTransform;
1078 aRelativeTransform.SetTransformation( gp_Ax3(), aFlyoutSpace );
1079 Bnd_Box aRelativeBounds = theBnd.Transformed( aRelativeTransform );
1081 Standard_Real aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
1082 aRelativeBounds.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
1084 gp_Dir aPosFlyout = aPlane.Axis().Direction() ^ aPointDir;
1085 gp_Dir aNegFlyout = aPosFlyout.Reversed();
1087 // select positive or negative flyout
1088 theDirs.Append( Abs( aYmax ) < Abs( aYmin ) ? aPosFlyout : aNegFlyout );
1092 //=================================================================================
1093 // function : ChooseDirFromBnd
1094 // purpose : The method chooses the best direction from the passed list of
1095 // directions, which is closest to the bounding box border.
1097 // theCandidates [in] the list of candidate directions
1098 // thePos [in] the position from where the directions are traced
1099 // theBnd [in] the bounding box of main shape
1100 //=================================================================================
1101 gp_Dir MeasureGUI_DimensionCreateTool::ChooseDirFromBnd( const SeqOfDirs& theCandidates,
1102 const gp_Pnt& thePos,
1103 const Bnd_Box& theBnd ) const
1107 Standard_Real aBestDistance = RealLast();
1109 SeqOfDirs::Iterator anIt( theCandidates );
1110 for ( ; anIt.More(); anIt.Next() )
1112 const gp_Dir& aDir = anIt.Value();
1114 gp_Ax3 aFlyoutSpace( thePos, aDir );
1116 gp_Trsf aRelativeTransform;
1117 aRelativeTransform.SetTransformation( gp_Ax3(), aFlyoutSpace );
1118 Bnd_Box aRelativeBounds = theBnd.Transformed( aRelativeTransform );
1120 Standard_Real aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
1121 aRelativeBounds.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
1123 if ( aYmax < aBestDistance )
1126 aBestDistance = aYmax;
1133 //=================================================================================
1134 // function : SelectPlaneForProjection
1135 // purpose : Select best matching plane in current view projection
1136 //=================================================================================
1137 template <typename TPlane>
1138 TPlane MeasureGUI_DimensionCreateTool::SelectPlaneForProjection( const NCollection_Sequence<TPlane>& thePlanes,
1139 const Handle(V3d_View)& theView ) const
1141 Quantity_Parameter U[3];
1142 Quantity_Parameter N[3];
1143 theView->Up( U[0], U[1], U[2] );
1144 theView->Proj( N[0], N[1], N[2] );
1146 gp_Dir aViewN( (Standard_Real)N[0], (Standard_Real)N[1], (Standard_Real)N[2] );
1147 gp_Dir aViewU( (Standard_Real)U[0], (Standard_Real)U[1], (Standard_Real)U[2] );
1149 TPlane aBestPlane = thePlanes.First();
1151 Standard_Real aBestDotProduct = RealFirst();
1153 for ( Standard_Integer aPlnIt = 1; aPlnIt <= thePlanes.Length(); ++aPlnIt )
1155 const TPlane& aPlane = thePlanes.Value( aPlnIt );
1157 Standard_Real aDotProduct = Abs( ((gp_Pln)aPlane).Axis().Direction() * aViewN );
1159 // preferred plane is "view parallel"
1160 if ( aDotProduct <= aBestDotProduct )
1165 aBestPlane = aPlane;
1167 aBestDotProduct = aDotProduct;
1173 //=================================================================================
1174 // function : GetMainShape
1176 //=================================================================================
1177 GEOM::GeomObjPtr MeasureGUI_DimensionCreateTool::GetMainShape( const GEOM::GeomObjPtr& theShape ) const
1179 // iterate over top-level objects to search for main shape
1180 GEOM::GeomObjPtr aMainShapeIt = theShape;
1181 while ( !aMainShapeIt->IsMainShape() )
1183 aMainShapeIt = aMainShapeIt->GetMainShape();
1185 return aMainShapeIt;
1188 //=================================================================================
1189 // function : GetFaceSide
1191 //=================================================================================
1192 bool MeasureGUI_DimensionCreateTool::GetFaceSide( const TopoDS_Face& theFace, const TopoDS_Edge& theEdge, gp_Dir& theDir ) const
1194 // get correctly oriented edge from main shape
1195 TopoDS_Edge anEdgeFromFace;
1196 TopExp_Explorer anExplorer( theFace.Oriented( TopAbs_FORWARD ), TopAbs_EDGE );
1197 for ( ; anExplorer.More(); anExplorer.Next() )
1199 TopoDS_Edge aCurrentEdge = TopoDS::Edge( anExplorer.Current() );
1200 if ( theEdge.IsSame( aCurrentEdge ) )
1202 anEdgeFromFace = aCurrentEdge;
1207 if ( anEdgeFromFace.IsNull() )
1212 // check out the direction of face extensions from its boundaries at the edge location
1213 // made assumption here that for any linear bounding edge the
1214 // normals are same on the whole length of that edge
1215 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( theFace );
1216 if ( aSurface.IsNull() || !aSurface->IsKind( STANDARD_TYPE(Geom_ElementarySurface) ) )
1221 BRepAdaptor_Curve aSurfCurve( anEdgeFromFace, theFace );
1222 if ( !aSurfCurve.IsCurveOnSurface() )
1227 Standard_Real aHalfRange = ( aSurfCurve.FirstParameter() + aSurfCurve.LastParameter() ) / 2.0;
1229 gp_Pnt aPoint = aSurfCurve.Value( aHalfRange );
1231 Standard_Real aPointU = 0.0;
1232 Standard_Real aPointV = 0.0;
1233 GeomLib_Tool::Parameters( aSurface, aPoint, Precision::Confusion(), aPointU, aPointV );
1236 if ( GeomLib::NormEstim( aSurface, gp_Pnt2d( aPointU, aPointV ), Precision::Confusion(), aNorm ) > 1 )
1241 gp_Vec aTangent = aSurfCurve.DN( aHalfRange, 1 );
1242 if ( aTangent.Magnitude() < Precision::Confusion() )
1247 TopAbs_Orientation anEdgeOrientation = anEdgeFromFace.Orientation();
1248 if ( anEdgeOrientation == TopAbs_REVERSED )
1253 theDir = gp_Dir( aTangent ) ^ aNorm;
1257 //=================================================================================
1258 // function : GetInPlaneSegments
1259 // purpose : The method finds segments crossing the passed circle,
1260 // which lie in the passed planes.
1262 // theCirc [in] the circle to be crossed.
1263 // thePlanes [in] the projection planes crossing the circle.
1264 //=================================================================================
1265 MeasureGUI_DimensionCreateTool::SeqOfSegments
1266 MeasureGUI_DimensionCreateTool::GetInPlaneSegments( const gp_Circ& theCirc,
1267 const SeqOfDirs& thePlanes ) const
1269 SeqOfSegments aResult;
1271 gp_Pnt aCircP = theCirc.Location();
1272 gp_Dir aCircN = theCirc.Axis().Direction();
1273 Standard_Real aCircR = theCirc.Radius();
1275 SeqOfDirs::Iterator anIt( thePlanes );
1276 for ( ; anIt.More(); anIt.Next() )
1278 const gp_Dir& aDir = anIt.Value();
1280 if ( aDir.IsParallel( aCircN, Precision::Angular() ) )
1285 gp_Dir aIntDir = aDir ^ aCircN;
1287 gp_Pnt aPnt1 = gp_Pnt( aCircP.XYZ() - aIntDir.XYZ() * aCircR );
1288 gp_Pnt aPnt2 = gp_Pnt( aCircP.XYZ() + aIntDir.XYZ() * aCircR );
1290 aSegment.First = aPnt1;
1291 aSegment.Last = aPnt2;
1292 aResult.Append( aSegment );