0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / DrawDim / DrawDim.cxx
CommitLineData
b311480e 1// Created on: 1996-01-10
2// Created by: Denis PASCAL
3// Copyright (c) 1996-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <BRep_Tool.hxx>
19#include <Draw.hxx>
20#include <Draw_Appli.hxx>
7fd59977 21#include <Draw_Text3D.hxx>
42cf5bc1 22#include <DrawDim.hxx>
23#include <ElCLib.hxx>
24#include <Geom_Circle.hxx>
7fd59977 25#include <Geom_Curve.hxx>
42cf5bc1 26#include <Geom_Line.hxx>
27#include <Geom_Plane.hxx>
28#include <gp_Circ.hxx>
29#include <gp_Lin.hxx>
30#include <gp_Pln.hxx>
31#include <gp_Pnt.hxx>
32#include <TCollection_AsciiString.hxx>
33#include <TopAbs.hxx>
7fd59977 34#include <TopExp.hxx>
35#include <TopExp_Explorer.hxx>
7fd59977 36#include <TopoDS.hxx>
42cf5bc1 37#include <TopoDS_Edge.hxx>
38#include <TopoDS_Face.hxx>
39#include <TopoDS_Shape.hxx>
7fd59977 40#include <TopoDS_Vertex.hxx>
42cf5bc1 41
57c28b61 42#ifdef _WIN32
7fd59977 43Standard_IMPORT Draw_Viewer dout;
44#endif
45
46//=======================================================================
47//function : AllCommands
48//purpose :
49//=======================================================================
50
51void DrawDim::AllCommands (Draw_Interpretor& theCommands)
52{
53 PlanarDimensionCommands (theCommands);
54}
55
56
57//=======================================================================
58//function : DrawShapeName
59//purpose :
60//=======================================================================
61
62void DrawDim::DrawShapeName (const TopoDS_Shape& ashape,
63 const Standard_CString aname)
64{
65 gp_Pnt position;
66 TCollection_AsciiString t (" ");
67 switch (ashape.ShapeType()) {
68 case TopAbs_EDGE :
69 {
70 Standard_Real f,l,parameter;
71 Handle(Geom_Curve) curve = BRep_Tool::Curve(TopoDS::Edge(ashape),f,l);
72 if (curve->IsKind(STANDARD_TYPE(Geom_Line))) {
73 parameter = (f+l)/2.;
74 position = ElCLib::Value(parameter,Handle(Geom_Line)::DownCast(curve)->Lin());
75 }
76 else if (curve->IsKind(STANDARD_TYPE(Geom_Circle))) {
77 parameter = (f+l)/2.;
c6541a0c 78 if (f > l) parameter = parameter + M_PI;
7fd59977 79 position = ElCLib::Value(parameter,Handle(Geom_Circle)::DownCast(curve)->Circ());
80 }
81 }
82 break;
83 case TopAbs_VERTEX :
84 {
85 position = BRep_Tool::Pnt(TopoDS::Vertex(ashape));
86 }
87 break;
7fd59977 88 default:
89 break;
7fd59977 90 }
91 t+=aname; //Name();
92 Handle(Draw_Text3D) text = new Draw_Text3D (position,t.ToCString(),Draw_blanc);
93 dout << text;
94}
95
96
97//=======================================================================
98//function : Pln
99//purpose :
100//=======================================================================
101
102Standard_Boolean DrawDim::Pln (const TopoDS_Face& f, gp_Pln& p)
103{
104 Handle(Geom_Plane) P = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(f));
105 if (!P.IsNull()) {
106 p = P->Pln();
107 return Standard_True;
108 }
109 return Standard_False;
110}
111
112
113
114//=======================================================================
115//function : Lin
116//purpose :
117//=======================================================================
118
119Standard_Boolean DrawDim::Lin (const TopoDS_Edge& e,
120 gp_Lin& l,
121 Standard_Boolean& infinite,
122 Standard_Real& first,
123 Standard_Real& last)
124{
125 Standard_Real f1,l1;
126 Handle(Geom_Line) L = Handle(Geom_Line)::DownCast(BRep_Tool::Curve(e,f1,l1));
127 if (!L.IsNull()) {
128 TopoDS_Vertex vf, vl;
129 TopExp::Vertices(TopoDS::Edge(e),vf,vl);
130 if (vf.IsNull() && vl.IsNull()) {
131 infinite = Standard_True;
132 l = L->Lin();
133 return Standard_True;
134 }
135 else if (vf.IsNull() || vl.IsNull()) {
9775fa61 136 throw Standard_DomainError("DrawDim::Lin : semi infinite edge");
7fd59977 137 }
138 else {
139 l = L->Lin();
140 infinite = Standard_True;
141 first = f1;
142 last = l1;
143 return Standard_True;
144 }
145 }
146 return Standard_False;
147}
148
149
150//=======================================================================
151//function : Lin
152//purpose :
153//=======================================================================
154
155Standard_Boolean DrawDim::Circ (const TopoDS_Edge& e,
156 gp_Circ& c,
157 Standard_Real& first,
158 Standard_Real& last)
159{
160 Standard_Real f1,l1;
161 Handle(Geom_Circle) C = Handle(Geom_Circle)::DownCast(BRep_Tool::Curve(e,f1,l1));
162 if (!C.IsNull()) {
163 c = C->Circ();
164 first = f1;
165 last = l1;
166 return Standard_True;
167 }
168 return Standard_False;
169}
170
171
172
173
174//=======================================================================
175//function : Nearest
176//purpose :
177//=======================================================================
178
179gp_Pnt DrawDim::Nearest(const TopoDS_Shape& ashape, const gp_Pnt& apoint)
180{
181 Standard_Real dist = RealLast();
182 Standard_Real curdist;
183 gp_Pnt result;
184 gp_Pnt curpnt;
185 TopExp_Explorer explo(ashape,TopAbs_VERTEX);
186 while (explo.More()) {
187 curpnt = BRep_Tool::Pnt(TopoDS::Vertex(explo.Current()));
188 curdist = apoint.Distance(curpnt);
189 if (curdist < dist) {
190 result = curpnt;
191 dist = curdist;
192 }
193 explo.Next();
194 }
195 return result;
196}