1 // Created on: 1996-12-05
2 // Created by: Jean-Pierre COMBE/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.
17 #include <PrsDim_PerpendicularRelation.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepBuilderAPI_MakeFace.hxx>
23 #include <DsgPrs_PerpenPresentation.hxx>
25 #include <gce_MakeDir.hxx>
26 #include <Geom2d_Line.hxx>
27 #include <Geom_Ellipse.hxx>
28 #include <Geom_Line.hxx>
29 #include <Geom_Plane.hxx>
30 #include <GeomAPI.hxx>
32 #include <gp_Pnt2d.hxx>
34 #include <IntAna2d_AnaIntersection.hxx>
35 #include <IntAna2d_IntPoint.hxx>
36 #include <Precision.hxx>
37 #include <Prs3d_Presentation.hxx>
38 #include <Select3D_SensitiveSegment.hxx>
39 #include <SelectMgr_EntityOwner.hxx>
40 #include <SelectMgr_Selection.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Shape.hxx>
45 IMPLEMENT_STANDARD_RTTIEXT(PrsDim_PerpendicularRelation, PrsDim_Relation)
47 //=======================================================================
48 //function : Constructor
49 //purpose : TwoEdgesPerpendicular
50 //=======================================================================
51 PrsDim_PerpendicularRelation::PrsDim_PerpendicularRelation(const TopoDS_Shape& aFShape,
52 const TopoDS_Shape& aSShape,
53 const Handle(Geom_Plane)& aPlane)
61 //=======================================================================
62 //function : Constructor
63 //purpose : TwoFacesPerpendicular
64 //=======================================================================
65 PrsDim_PerpendicularRelation::PrsDim_PerpendicularRelation(const TopoDS_Shape& aFShape,
66 const TopoDS_Shape& aSShape)
73 //=======================================================================
76 //=======================================================================
77 void PrsDim_PerpendicularRelation::Compute (const Handle(PrsMgr_PresentationManager)& ,
78 const Handle(Prs3d_Presentation)& aPresentation,
79 const Standard_Integer )
81 if (myFShape.ShapeType() == mySShape.ShapeType()) {
82 switch (myFShape.ShapeType()) {
85 // cas perpendiculaire entre deux faces
86 ComputeTwoFacesPerpendicular(aPresentation);
91 // cas perpendiculaire entre deux edges
92 ComputeTwoEdgesPerpendicular(aPresentation);
99 // Cas pas traite - Edge/Face
102 //=======================================================================
103 //function : ComputeSelection
105 //=======================================================================
106 void PrsDim_PerpendicularRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
107 const Standard_Integer)
109 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
110 const gp_Pnt& pos = myPosition;
111 Handle(Select3D_SensitiveSegment) seg;
112 Standard_Boolean ok1(Standard_False),ok2(Standard_False);
114 if (!myFAttach.IsEqual(pos,Precision::Confusion())) {
115 seg = new Select3D_SensitiveSegment(own,
118 aSelection->Add(seg);
121 if (!mySAttach.IsEqual(myPosition,Precision::Confusion())) {
122 seg = new Select3D_SensitiveSegment(own,
125 aSelection->Add(seg);
130 gp_Vec vec1(gce_MakeDir(pos,myFAttach));
131 gp_Vec vec2(gce_MakeDir(pos,mySAttach));
132 Standard_Real dist1(pos.Distance(myFAttach));
133 Standard_Real dist2(pos.Distance(mySAttach));
139 gp_Pnt pAx11 = pos.Translated(vec1);
140 gp_Pnt pAx22 = pos.Translated(vec2);
141 gp_Pnt p_symb = pAx22.Translated(vec1);
142 seg = new Select3D_SensitiveSegment(own,pAx11,p_symb);
143 aSelection->Add(seg);
144 seg = new Select3D_SensitiveSegment(own,p_symb,pAx22);
145 aSelection->Add(seg);
149 //=======================================================================
150 //function : ComputeTwoFacesPerpendicular
152 //=======================================================================
153 void PrsDim_PerpendicularRelation::ComputeTwoFacesPerpendicular
154 (const Handle(Prs3d_Presentation)& /*aPresentation*/)
158 //=======================================================================
159 //function : ComputeTwoEdgesPerpendicular
161 //=======================================================================
162 void PrsDim_PerpendicularRelation::ComputeTwoEdgesPerpendicular(const Handle(Prs3d_Presentation)& aPresentation)
165 Handle(Geom_Curve) geom1,geom2;
166 gp_Pnt pint3d,p1,p2,pAx1,pAx2,ptat11,ptat12,ptat21,ptat22;
167 Standard_Boolean isInfinite1,isInfinite2;
168 Handle(Geom_Curve) extCurv;
169 if ( !PrsDim::ComputeGeometry(TopoDS::Edge(myFShape),TopoDS::Edge(mySShape),
172 ptat11,ptat12,ptat21,ptat22,
174 isInfinite1,isInfinite2,
177 Standard_Boolean interOut1(Standard_False),interOut2(Standard_False);
179 Handle(Geom_Line) geom_lin1;
180 Handle(Geom_Line) geom_lin2;
181 if ( geom1->IsInstance(STANDARD_TYPE(Geom_Ellipse)) )
183 Handle(Geom_Ellipse) geom_el (Handle(Geom_Ellipse)::DownCast (geom1));
184 // construct lines through focuses
185 gp_Ax1 elAx = geom_el->XAxis();
187 geom_lin1 = new Geom_Line(ll);
188 Standard_Real focex = geom_el->MajorRadius() - geom_el->Focal()/2.0;
189 gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
190 ptat11 = geom_el->Focus1().Translated(transvec);
191 ptat12 = geom_el->Focus2().Translated(-transvec);
192 interOut1 = Standard_True;
194 else if ( geom1->IsInstance(STANDARD_TYPE(Geom_Line)) )
196 geom_lin1 = Handle(Geom_Line)::DownCast (geom1);
200 if (geom2->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
202 Handle(Geom_Ellipse) geom_el (Handle(Geom_Ellipse)::DownCast (geom2));
203 // construct lines through focuses
204 gp_Ax1 elAx = geom_el->XAxis();
206 geom_lin2 = new Geom_Line(ll);
207 Standard_Real focex = geom_el->MajorRadius() - geom_el->Focal()/2.0;
208 gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
209 ptat21 = geom_el->Focus1().Translated(transvec);
210 ptat22 = geom_el->Focus2().Translated(-transvec);
211 interOut2 = Standard_True;
213 else if ( geom2->IsInstance(STANDARD_TYPE(Geom_Line)) )
215 geom_lin2 = Handle(Geom_Line)::DownCast (geom2);
220 BRepBuilderAPI_MakeFace makeface (myPlane->Pln());
221 BRepAdaptor_Surface adp (makeface.Face());
223 // 2d lines => projection of 3d on current plane
224 Handle(Geom2d_Curve) aGeom2dCurve = GeomAPI::To2d(geom_lin1,myPlane->Pln());
225 Handle(Geom2d_Line) lin1_2d = Handle(Geom2d_Line)::DownCast (aGeom2dCurve) ;
226 aGeom2dCurve = GeomAPI::To2d(geom_lin2,myPlane->Pln());
227 Handle(Geom2d_Line) lin2_2d = Handle(Geom2d_Line)::DownCast (aGeom2dCurve) ;
228 IntAna2d_AnaIntersection inter(lin1_2d->Lin2d(),lin2_2d->Lin2d());
229 if (!inter.IsDone()) return;
230 if (!inter.NbPoints()) return;
232 gp_Pnt2d pint(inter.Point(1).Value());
233 pint3d = adp.Value(pint.X(),pint.Y());
236 // recherche points attache
237 Standard_Real par1,par2,curpar,pmin,pmax;//,dist,sign;
238 Standard_Real length(0.);
240 if ( isInfinite1 && isInfinite2 )
242 Standard_Real curpar1 = ElCLib::Parameter(geom_lin1->Lin(),pint3d);
243 Standard_Real curpar2 = ElCLib::Parameter(geom_lin2->Lin(),pint3d);
246 myFAttach = ElCLib::Value(curpar1+par1,geom_lin1->Lin());
247 mySAttach = ElCLib::Value(curpar2+par2,geom_lin2->Lin());
251 Standard_Boolean lengthComputed (Standard_False);
254 curpar = ElCLib::Parameter(geom_lin1->Lin(),pint3d);
255 par1 = ElCLib::Parameter(geom_lin1->Lin(),ptat11);
256 par2 = ElCLib::Parameter(geom_lin1->Lin(),ptat12);
257 pmin = Min(par1,par2);
258 pmax = Max(par1,par2);
260 if ( myPosition.SquareDistance(ptat11) > myPosition.SquareDistance(ptat12) )
264 if ( (curpar < pmin) || (curpar > pmax) )
266 interOut1 = Standard_True;
268 if ( !isInfinite2 ) length = 2.*Min(ptat11.Distance(ptat12),ptat21.Distance(ptat22))/5.;
269 else length = 2.*ptat11.Distance(ptat12)/5.;
270 lengthComputed = Standard_True;
271 gp_Vec vec1 (gce_MakeDir(myPosition,p1));
272 vec1.Multiply(length);
273 pAx1 = myPosition.Translated(vec1);
278 curpar = ElCLib::Parameter(geom_lin2->Lin(),pint3d);
279 par1 = ElCLib::Parameter(geom_lin2->Lin(),ptat21);
280 par2 = ElCLib::Parameter(geom_lin2->Lin(),ptat22);
281 pmin = Min(par1,par2);
282 pmax = Max(par1,par2);
284 if ( myPosition.SquareDistance(ptat21) > myPosition.SquareDistance(ptat22) ) p2 = ptat21;
286 if ( (curpar < pmin) || (curpar > pmax) )
288 interOut2 = Standard_True;
290 gp_Vec vec2 (gce_MakeDir(myPosition,p2));
291 if ( !lengthComputed )
293 if ( !isInfinite1 ) length = 2.*Min(ptat11.Distance(ptat12),ptat21.Distance(ptat22))/5.;
294 else length = 2.*ptat21.Distance(ptat22)/5.;
296 vec2.Multiply(length);
297 pAx2 = myPosition.Translated(vec2);
303 gp_Vec vec1(geom_lin1->Lin().Direction());
304 vec1.Multiply(length);
305 myFAttach = myPosition.Translated(vec1);
310 gp_Vec vec2(geom_lin2->Lin().Direction());
311 vec2.Multiply(length);
312 mySAttach = myPosition.Translated(vec2);
315 DsgPrs_PerpenPresentation::Add(aPresentation,myDrawer,
319 interOut1,interOut2);
321 if ( (myExtShape != 0) && !extCurv.IsNull()) {
323 if ( myExtShape == 1 ) {
328 aPresentation->SetInfiniteState(isInfinite1);
329 ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(myFShape),geom_lin1,pf,pl);
336 aPresentation->SetInfiniteState(isInfinite2);
337 ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(mySShape),geom_lin2,pf,pl);