1 // Created on: 1996-12-05
2 // Created by: Flore Lantheaume/Odile Olivier
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
19 #include <AIS_FixRelation.hxx>
20 #include <AIS_Shape.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepAdaptor_Curve.hxx>
23 #include <DsgPrs_FixPresentation.hxx>
26 #include <Geom_Circle.hxx>
27 #include <Geom_Curve.hxx>
28 #include <Geom_Line.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Geom_Transformation.hxx>
32 #include <gp_Circ.hxx>
38 #include <Precision.hxx>
39 #include <Prs3d_Presentation.hxx>
40 #include <Prs3d_Projector.hxx>
41 #include <Select3D_SensitiveSegment.hxx>
42 #include <SelectMgr_EntityOwner.hxx>
43 #include <SelectMgr_Selection.hxx>
44 #include <Standard_DomainError.hxx>
45 #include <Standard_NotImplemented.hxx>
46 #include <Standard_Type.hxx>
47 #include <TColStd_ListIteratorOfListOfTransient.hxx>
48 #include <TopAbs_ShapeEnum.hxx>
50 #include <TopLoc_Location.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopoDS_Vertex.hxx>
55 #include <TopoDS_Wire.hxx>
56 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
57 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 IMPLEMENT_STANDARD_RTTIEXT(AIS_FixRelation,AIS_Relation)
62 static Standard_Boolean InDomain(const Standard_Real fpar,
63 const Standard_Real lpar,
64 const Standard_Real para)
67 return ((para >= fpar) && (para <= lpar));
69 if (para >= (fpar+2*M_PI)) return Standard_True;
70 if (para <= lpar) return Standard_True;
71 return Standard_False;
74 //=======================================================================
75 //function : Constructor
76 //purpose : vertex Fix Relation
77 //=======================================================================
79 AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape,
80 const Handle(Geom_Plane)& aPlane,
81 const TopoDS_Wire& aWire)
87 myAutomaticPosition = Standard_True;
91 //=======================================================================
92 //function : Constructor
93 //purpose : vertex Fix Relation
94 //=======================================================================
96 AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape,
97 const Handle(Geom_Plane)& aPlane,
98 const TopoDS_Wire& aWire,
99 const gp_Pnt& aPosition,
100 const Standard_Real anArrowSize)
106 myPosition = aPosition;
107 SetArrowSize( anArrowSize );
108 myAutomaticPosition = Standard_False;
112 //=======================================================================
113 //function : Constructor
114 //purpose : edge (line or circle) Fix Relation
115 //=======================================================================
117 AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape,
118 const Handle(Geom_Plane)& aPlane)
122 myAutomaticPosition = Standard_True;
126 //=======================================================================
127 //function : Constructor
128 //purpose : edge (line or circle) Fix Relation
129 //=======================================================================
131 AIS_FixRelation::AIS_FixRelation(
132 const TopoDS_Shape& aShape,
133 const Handle(Geom_Plane)& aPlane,
134 const gp_Pnt& aPosition,
135 const Standard_Real anArrowSize)
139 myPosition = aPosition;
140 SetArrowSize( anArrowSize );
141 myAutomaticPosition = Standard_False;
144 //=======================================================================
147 //=======================================================================
149 TopoDS_Wire AIS_FixRelation::Wire()
154 //=======================================================================
157 //=======================================================================
159 void AIS_FixRelation::SetWire(const TopoDS_Wire& aWire)
165 //=======================================================================
168 //=======================================================================
170 void AIS_FixRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&,
171 const Handle(Prs3d_Presentation)& aPresentation,
172 const Standard_Integer)
174 aPresentation->Clear();
176 // Calculate position of the symbol and
177 // point of attach of the segment on the shape
179 if (myFShape.ShapeType() == TopAbs_VERTEX)
180 ComputeVertex(TopoDS::Vertex(myFShape), curpos);
181 else if (myFShape.ShapeType() == TopAbs_EDGE)
182 ComputeEdge(TopoDS::Edge(myFShape), curpos);
184 const gp_Dir& nor = myPlane->Axis().Direction();
187 // calculate presentation
188 // definition of the symbol size
189 if( !myArrowSizeIsDefined )
192 //creation of the presentation
193 DsgPrs_FixPresentation::Add(aPresentation,
201 //=======================================================================
203 //purpose : to avoid warning
204 //=======================================================================
206 void AIS_FixRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
207 const Handle(Prs3d_Presentation)& aPresentation)
209 // Standard_NotImplemented::Raise("AIS_FixRelation::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
210 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
213 //=======================================================================
216 //=======================================================================
218 void AIS_FixRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
219 const Handle(Geom_Transformation)& aTransformation,
220 const Handle(Prs3d_Presentation)& aPresentation)
222 // Standard_NotImplemented::Raise("AIS_FixRelation::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
223 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
226 //=======================================================================
227 //function : ComputeSelection
229 //=======================================================================
231 void AIS_FixRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
232 const Standard_Integer)
234 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
236 // creation of segment sensible for the linked segment
237 // of the shape fixed to symbol 'Fix'
238 Handle(Select3D_SensitiveSegment) seg;
239 seg = new Select3D_SensitiveSegment(own,
242 aSelection->Add(seg);
244 // Creation of the sensible zone of symbol 'Fix'
245 gp_Dir norm = myPlane->Axis().Direction();
247 gp_Vec dirac(myPntAttach,myPosition);
249 gp_Vec norac = dirac.Crossed(gp_Vec(norm));
250 gp_Ax1 ax(myPosition, norm);
251 norac.Rotate(ax, M_PI/8);
253 norac*=(myArrowSize/2);
254 gp_Pnt P1 = myPosition.Translated(norac);
255 gp_Pnt P2 = myPosition.Translated(-norac);
256 seg = new Select3D_SensitiveSegment(own,
259 aSelection->Add(seg);
262 P1 = myPosition.Translated(norac);
263 P2 = myPosition.Translated(-norac);
264 dirac*=(myArrowSize/2);
266 gp_Pnt PL = PF.Translated(dirac);
268 seg = new Select3D_SensitiveSegment(own,
271 aSelection->Add(seg);
275 PL = PF.Translated(dirac);
277 seg = new Select3D_SensitiveSegment(own,
280 aSelection->Add(seg);
282 PF.SetXYZ( (P1.XYZ() + P2.XYZ()) /2 );
283 PL = PF.Translated(dirac);
285 seg = new Select3D_SensitiveSegment(own,
290 //=======================================================================
291 //function : ComputeVertex
292 //purpose : computes myPntAttach and the position of the presentation
293 // when you fix a vertex
294 //=======================================================================
296 void AIS_FixRelation::ComputeVertex(const TopoDS_Vertex& /*FixVertex*/,
299 myPntAttach = BRep_Tool::Pnt(TopoDS::Vertex(myFShape));
301 if (myAutomaticPosition) {
302 gp_Pln pln(myPlane->Pln());
303 gp_Dir dir(pln.XAxis().Direction());
304 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
305 curpos = myPntAttach.Translated(transvec);;
307 myAutomaticPosition = Standard_True;
311 //=======================================================================
312 //function : ComputePosition
314 //=======================================================================
316 gp_Pnt AIS_FixRelation::ComputePosition(const Handle(Geom_Curve)& curv1,
317 const Handle(Geom_Curve)& curv2,
318 const gp_Pnt& firstp1,
319 const gp_Pnt& lastp1,
320 const gp_Pnt& firstp2,
321 const gp_Pnt& lastp2) const
323 //---------------------------------------------------------
324 // calculate the point of attach
325 //---------------------------------------------------------
328 if (curv1->IsInstance(STANDARD_TYPE(Geom_Circle)) || curv2->IsInstance(STANDARD_TYPE(Geom_Circle))) {
329 Handle(Geom_Circle) gcirc = Handle(Geom_Circle)::DownCast(curv1);
330 if (gcirc.IsNull()) gcirc = Handle(Geom_Circle)::DownCast(curv2);
331 gp_Dir dir( gcirc->Location().XYZ() + myPntAttach.XYZ() );
332 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
333 curpos = myPntAttach.Translated(transvec);
337 gp_Vec vec1(firstp1,lastp1);
338 gp_Vec vec2(firstp2,lastp2);
340 if (!vec1.IsParallel(vec2, Precision::Angular()) ) {
342 Standard_Real conf =Precision::Confusion();
343 if (lastp1.IsEqual(firstp2,conf) || firstp1.IsEqual(lastp2,conf)) dir.SetXYZ(vec1.XYZ() - vec2.XYZ());
344 else dir.SetXYZ(vec1.XYZ() + vec2.XYZ());
345 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
346 curpos = myPntAttach.Translated(transvec);
349 gp_Vec crossvec = vec1.Crossed(vec2);
350 vec1.Cross(crossvec);
352 curpos = myPntAttach.Translated(gp_Vec(dir)*myArrowSize);
359 //=======================================================================
360 //function : ComputePosition
361 //purpose : Computes the position of the "fix dimension" when the
362 // fixed object is a vertex which is set at the intersection
364 // The "dimension" is in the "middle" of the two edges.
365 //=======================================================================
367 gp_Pnt AIS_FixRelation::ComputePosition(const Handle(Geom_Curve)& curv,
368 const gp_Pnt& firstp,
369 const gp_Pnt& lastp) const
371 //---------------------------------------------------------
372 // calculate the point of attach
373 //---------------------------------------------------------
376 if (curv->IsKind(STANDARD_TYPE(Geom_Circle))) {
378 Handle(Geom_Circle) gcirc = Handle(Geom_Circle)::DownCast(curv);
379 gp_Dir dir( gcirc->Location().XYZ() + myPntAttach.XYZ() );
380 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
381 curpos = myPntAttach.Translated(transvec);
383 } //if (curv->IsKind(STANDARD_TYPE(Geom_Circle))
386 // gp_Pln pln(Component()->WorkingPlane()->Plane()->GetValue()->Pln());
387 gp_Pln pln(myPlane->Pln());
388 gp_Dir NormPln = pln.Axis().Direction();
389 gp_Vec vec(firstp,lastp);
390 vec.Cross( gp_Vec(NormPln));
392 gp_Vec transvec = vec*myArrowSize;
393 curpos = myPntAttach.Translated(transvec);
394 gp_Ax1 RotAx( myPntAttach, NormPln);
395 curpos.Rotate(RotAx, M_PI/10);
401 //=======================================================================
402 //function : ComputeEdge
403 //purpose : computes myPntAttach and the position of the presentation
404 // when you fix an edge
405 //=======================================================================
407 void AIS_FixRelation::ComputeEdge(const TopoDS_Edge& FixEdge, gp_Pnt& curpos)
409 Handle(Geom_Curve) curEdge;
411 if (!AIS::ComputeGeometry(FixEdge,curEdge,ptbeg,ptend)) return;
413 //---------------------------------------------------------
414 // calcul du point de positionnement du symbole 'fix'
415 //---------------------------------------------------------
416 //--> In case of a straight line
417 if (curEdge->IsKind(STANDARD_TYPE(Geom_Line))){
418 gp_Lin glin = Handle(Geom_Line)::DownCast(curEdge)->Lin();
419 Standard_Real pfirst(ElCLib::Parameter(glin,ptbeg));
420 Standard_Real plast(ElCLib::Parameter(glin,ptend));
421 ComputeLinePosition(glin, curpos, pfirst, plast);
424 //--> In case of a circle
425 else if (curEdge->IsKind(STANDARD_TYPE(Geom_Circle))) {
426 gp_Circ gcirc = Handle(Geom_Circle)::DownCast(curEdge)->Circ();
427 Standard_Real pfirst, plast;
428 BRepAdaptor_Curve cu(FixEdge);
429 pfirst = cu.FirstParameter();
430 plast = cu.LastParameter();
431 ComputeCirclePosition(gcirc, curpos, pfirst, plast);
439 //=======================================================================
440 //function : ComputeLinePosition
441 //purpose : compute the values of myPntAttach and the position <pos> of
442 // the symbol when the fixed edge has a geometric support equal
444 //=======================================================================
446 void AIS_FixRelation::ComputeLinePosition(const gp_Lin& glin,
448 Standard_Real& pfirst,
449 Standard_Real& plast)
451 if (myAutomaticPosition) {
452 // point of attach is chosen as middle of the segment
453 myPntAttach = ElCLib::Value((pfirst+ plast)/2, glin);
455 gp_Dir norm = myPlane ->Axis().Direction();
457 norm.Cross(glin.Position().Direction());
458 pos = myPntAttach.Translated(gp_Vec(norm)*myArrowSize);
459 myAutomaticPosition = Standard_True;
460 } // if (myAutomaticPosition)
464 Standard_Real linparam = ElCLib::Parameter(glin, pos);
466 // case if the projection of position is located between 2 vertices
468 if ( (linparam >= pfirst) && (linparam <= plast) )
469 myPntAttach = ElCLib::Value(linparam,glin);
471 // case if the projection of Position is outside of the limits
472 // of the edge : the point closest to the projection is chosen
473 // as the attach point
475 Standard_Real pOnLin;
476 if (linparam > plast)
480 myPntAttach = ElCLib::Value(pOnLin,glin);
481 gp_Dir norm = myPlane->Axis().Direction();
483 norm.Cross(glin.Position().Direction());
484 gp_Lin lsup(myPntAttach, norm);
485 Standard_Real parpos = ElCLib::Parameter(lsup,myPosition);
486 pos = ElCLib::Value(parpos,lsup);
493 //=======================================================================
494 //function : ComputeCirclePosition
495 //purpose : compute the values of myPntAttach and the position <pos> of
496 // the symbol when the fixed edge has a geometric support equal
498 //=======================================================================
500 void AIS_FixRelation::ComputeCirclePosition(
501 const gp_Circ& gcirc,
503 Standard_Real& pfirst,
504 Standard_Real& plast)
506 // readjust parametres on the circle
507 if (plast > 2*M_PI ) {
508 Standard_Real nbtours = Floor(plast / (2*M_PI));
509 plast -= nbtours*2*M_PI;
510 pfirst -= nbtours*2*M_PI;
513 if (myAutomaticPosition) {
514 // the point attach is the "middle" of the segment (relatively
515 // to the parametres of start and end vertices of the edge
517 Standard_Real circparam = (pfirst + plast)/2.;
519 if ( !InDomain(pfirst,plast,circparam)) {
520 Standard_Real otherpar = circparam + M_PI;
521 if (otherpar > 2*M_PI) otherpar -= 2*M_PI;
522 circparam = otherpar;
525 myPntAttach = ElCLib::Value(circparam, gcirc );
527 gp_Vec dir( gcirc.Location().XYZ(), myPntAttach.XYZ() );
529 gp_Vec transvec = dir*myArrowSize;
530 pos = myPntAttach.Translated(transvec);
532 myAutomaticPosition = Standard_True;
533 } // if (myAutomaticPosition)
536 // case if the projection of myPosition is outside of 2
537 // vertices of the edge. In this case the parameter is readjusted
538 // in the valid part of the circle
541 Standard_Real circparam = ElCLib::Parameter(gcirc, pos);
543 if ( !InDomain(pfirst,plast,circparam)) {
544 Standard_Real otherpar = circparam + M_PI;
545 if (otherpar > 2*M_PI) otherpar -= 2*M_PI;
546 circparam = otherpar;
549 myPntAttach = ElCLib::Value(circparam,gcirc);
553 //=======================================================================
554 //function : ConnectedEdges
556 //=======================================================================
557 Standard_Boolean AIS_FixRelation::ConnectedEdges(const TopoDS_Wire& WIRE,
558 const TopoDS_Vertex& V,
562 TopTools_IndexedDataMapOfShapeListOfShape vertexMap;
563 TopExp::MapShapesAndAncestors (WIRE,TopAbs_VERTEX,TopAbs_EDGE,vertexMap);
565 Standard_Boolean found(Standard_False);
566 TopoDS_Vertex theVertex;
567 for (Standard_Integer i=1; i<=vertexMap.Extent() && !found; i++) {
568 if (vertexMap.FindKey(i).IsSame(V)) {
569 theVertex = TopoDS::Vertex(vertexMap.FindKey(i));
570 found = Standard_True;
576 return Standard_False;
579 TopTools_ListIteratorOfListOfShape iterator(vertexMap.FindFromKey(theVertex));
580 if (iterator.More()) {
581 E1 = TopoDS::Edge(iterator.Value());
582 BRepAdaptor_Curve curv(E1);
587 return Standard_False;
590 if (iterator.More()) {
591 E2 = TopoDS::Edge(iterator.Value());
592 BRepAdaptor_Curve curv(E2);
597 return Standard_False;
600 if (iterator.More()) {
603 return Standard_False;
605 return Standard_True;