1 // Created on: 1995-10-06
2 // Created by: Jing Cheng MEI
3 // Copyright (c) 1995-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.
18 #include <Bnd_Box.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepBndLib.hxx>
21 #include <BRepClass_Edge.hxx>
22 #include <BRepClass_FaceClassifier.hxx>
23 #include <BRepTools_WireExplorer.hxx>
24 #include <DsgPrs_ShapeDirPresentation.hxx>
25 #include <Geom2d_Line.hxx>
26 #include <Geom_Curve.hxx>
27 #include <Geom_Surface.hxx>
28 #include <GeomLProp_CLProps.hxx>
29 #include <GeomLProp_SLProps.hxx>
32 #include <Graphic3d_ArrayOfSegments.hxx>
33 #include <Graphic3d_Group.hxx>
34 #include <Prs3d_Arrow.hxx>
35 #include <Prs3d_LineAspect.hxx>
36 #include <Prs3d_Presentation.hxx>
37 #include <TColgp_Array1OfPnt2d.hxx>
38 #include <TopAbs_Orientation.hxx>
39 #include <TopAbs_ShapeEnum.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopLoc_Location.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopTools_ListOfShape.hxx>
47 //=======================================================================
48 //function : FindPointOnFace
49 //purpose : internal use
50 //=======================================================================
51 static Standard_Boolean FindPointOnFace(const TopoDS_Face& face, gp_Pnt2d& pt2d)
53 // discredisation of the external contour and computing the center of gravity
55 TopExp_Explorer wireExp;
56 wireExp.Init(face, TopAbs_WIRE);
57 if (!wireExp.More()) {
58 return Standard_False;
61 Standard_Integer npoints, nptt = 21;
62 TColgp_Array1OfPnt2d points(1, nptt);
63 Standard_Real area=0., xcent=0., ycent=0.;
64 TopExp_Explorer edgeExp;
66 for (edgeExp.Init(wireExp.Current(), TopAbs_EDGE); edgeExp.More(); edgeExp.Next()) {
67 // discretize the 2d curve
68 Standard_Real first, last;
69 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(edgeExp.Current()), face, first, last);
70 if (TopoDS::Edge(edgeExp.Current()).Orientation() == TopAbs_REVERSED) {
71 Standard_Real change = first;
75 if (c2d->DynamicType() == STANDARD_TYPE(Geom2d_Line)) {
77 c2d->D0(first, points(1));
78 c2d->D0(last, points(2));
81 Standard_Real deltaT, t;
83 deltaT = (last - first) / (nptt-1);
84 for (Standard_Integer i=1; i<=nptt; i++) {
92 t = first + (i-1) * deltaT;
94 c2d->D0(t, points(i));
98 // compute the contribution to the center of gravity
100 Standard_Real h, c, d;
101 for (Standard_Integer i=1; i<=npoints-1; i++) {
103 h = 0.5*(points(i).Y() + points(i+1).Y());
104 c = points(i+1).X() - points(i).X();
105 d = points(i+1).X() + points(i).X();
112 if (Abs(area) < gp::Resolution()) {
113 pt2d.SetCoord(points(1).X(), points(1).Y());
114 return Standard_False;
117 pt2d.SetCoord(xcent / area, ycent / area);
119 // verify that (upar vpar) is a point on the face
121 BRepClass_FaceClassifier fClass(face, pt2d, gp::Resolution());
123 if ((fClass.State() == TopAbs_OUT) || (fClass.State() == TopAbs_UNKNOWN)) {
124 // try to find a point on face
127 return Standard_True;
131 //=======================================================================
132 //function : ComputeDir
133 //purpose : internal use
134 //=======================================================================
136 static Standard_Boolean ComputeDir(const TopoDS_Shape& shape, gp_Pnt& pt, gp_Dir& dir, const Standard_Integer mode)
139 if (shape.ShapeType() == TopAbs_EDGE) {
140 Standard_Real first, last;
141 Handle(Geom_Curve) curv0 = BRep_Tool::Curve(TopoDS::Edge(shape), loc, first, last);
142 Handle(Geom_Curve) curve = Handle(Geom_Curve)::DownCast(curv0->Copy());
143 curve->Transform(loc.Transformation());
144 GeomLProp_CLProps lProps(curve, 1, gp::Resolution());
145 lProps.SetParameter((mode == 0)? last : first);
146 if (!lProps.IsTangentDefined())
147 return Standard_False;
151 else if (shape.ShapeType() == TopAbs_FACE) {
153 Handle(Geom_Surface) surface = BRep_Tool::Surface(TopoDS::Face(shape));
154 if (BRep_Tool::NaturalRestriction(TopoDS::Face(shape))) {
155 Standard_Real u1, u2, v1, v2;
156 surface->Bounds(u1, u2, v1, v2);
157 pt2d.SetCoord((u1+u2)*0.5, (v1+v2)*0.5);
160 if (!FindPointOnFace(TopoDS::Face(shape), pt2d))
161 return Standard_False;
164 GeomLProp_SLProps lProps(surface, pt2d.X(), pt2d.Y(), 1, gp::Resolution());
165 if (!lProps.IsNormalDefined())
166 return Standard_False;
169 dir = lProps.Normal();
171 if (((shape.Orientation() == TopAbs_FORWARD) && (mode == 1)) ||
172 ((shape.Orientation() == TopAbs_REVERSED) && (mode == 0))) {
175 return Standard_True;
179 //=======================================================================
182 //=======================================================================
184 void DsgPrs_ShapeDirPresentation::Add(const Handle(Prs3d_Presentation)& prs,
185 const Handle(Prs3d_Drawer)& drawer,
186 const TopoDS_Shape& shape,
187 const Standard_Integer mode)
190 if ((mode != 0) && (mode != 1))
197 if (shape.ShapeType() == TopAbs_EDGE) {
198 ComputeDir(shape, pt, dir, mode);
199 BRepBndLib::Add(shape, box);
201 else if (shape.ShapeType() == TopAbs_FACE) {
202 ComputeDir(shape, pt, dir, mode);
203 BRepBndLib::Add(shape, box);
205 else if (shape.ShapeType() == TopAbs_WIRE) {
206 TopTools_ListOfShape aList;
207 Standard_Integer nb = 0;
208 BRepTools_WireExplorer anExp;
209 for (anExp.Init(TopoDS::Wire(shape)); anExp.More(); anExp.Next()) {
210 const TopoDS_Edge& edge = anExp.Current();
213 BRepBndLib::Add(edge, box);
218 const TopoDS_Edge& edge = TopoDS::Edge(aList.Last());
219 ComputeDir(edge, pt, dir, mode);
222 const TopoDS_Edge& edge = TopoDS::Edge(aList.First());
223 ComputeDir(edge, pt, dir, mode);
227 TopExp_Explorer faceExp;
229 TopTools_ListOfShape aList;
230 Standard_Integer nb = 0;
231 for (faceExp.Init(shape, TopAbs_FACE); faceExp.More(); faceExp.Next()) {
233 const TopoDS_Face& face = TopoDS::Face(faceExp.Current());
235 BRepBndLib::Add(face, box);
238 const TopoDS_Face& face = TopoDS::Face(aList.Last());
239 ComputeDir(face, pt, dir, mode);
243 box.Get(c[0],c[1],c[2],c[3],c[4],c[5]);
245 gp_Pnt ptmin(c[0], c[1], c[2]), ptmax(c[3], c[4], c[5]);
246 Standard_Real leng = ptmin.Distance(ptmax)/3.;
247 // mei 19/09/96 extrusion infinie -> taille fixe
248 if (leng >= 20000.) leng = 50;
250 gp_Pnt pt2(pt.XYZ()+leng*dir.XYZ());
252 Prs3d_Root::CurrentGroup(prs)->SetPrimitivesAspect(drawer->LineAspect()->Aspect());
254 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
255 aPrims->AddVertex(pt);
256 aPrims->AddVertex(pt2);
257 Prs3d_Root::CurrentGroup(prs)->AddPrimitiveArray(aPrims);
259 Prs3d_Arrow::Draw (Prs3d_Root::CurrentGroup (prs), pt2, dir, M_PI/180.*10., leng*0.3);