Integration of OCCT 6.5.0 from SVN
[occt.git] / src / SWDRAW / SWDRAW_ShapeTool.cxx
CommitLineData
7fd59977 1// 25.12.98 pdn renaming
2// 02.02.99 cky/rln PRO17746: transmitting 'sketch' command to XSDRAWEUC
3// 23.02.99 abv: method ShapeFix::FillFace() removed
4// 02.03.99 cky/rln: command edgeregul only accepts tolerance
5// 15.06.99 abv/pdn: command comptol added (from S4030)
6#include <SWDRAW_ShapeTool.ixx>
7
8#include <DBRep.hxx>
9#include <TopAbs_ShapeEnum.hxx>
10#include <TopoDS.hxx>
11#include <TopoDS_Shape.hxx>
12#include <TopoDS_Compound.hxx>
13#include <TopoDS_Shell.hxx>
14#include <TopoDS_Solid.hxx>
15#include <TopoDS_Face.hxx>
16#include <TopoDS_Wire.hxx>
17#include <TopoDS_Edge.hxx>
18#include <TopoDS_Vertex.hxx>
19
20#include <BRepBuilderAPI.hxx>
21
22// + edge, face
23#include <TopoDS_Iterator.hxx>
24#include <TopExp_Explorer.hxx>
25#include <TopExp.hxx>
26#include <BRep_Tool.hxx>
27#include <Geom_Curve.hxx>
28#include <Geom_Surface.hxx>
29#include <gp_Pnt.hxx>
30#include <gp_Pnt2d.hxx>
31#include <TopTools_IndexedMapOfShape.hxx>
32#include <BRepClass3d_SolidClassifier.hxx>
33
34#include <BRepTools_WireExplorer.hxx>
35#include <TopoDS_Iterator.hxx>
36
37#include <BRep_Builder.hxx>
38
39
40
41// + edgeregul/updtol
42#include <BRepLib.hxx>
43
44// + fillface
45#include <Geom_TrimmedCurve.hxx>
46#include <stdio.h>
47#include <Precision.hxx>
48#include <DrawTrSurf.hxx>
49#include <GeomLib.hxx>
50
51
52static Standard_Integer XSHAPE_edge
53 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
54{
55 if (argc < 2) { di<<"donner un nom de shape"<<"\n"; return 1 /* Error */; }
56 Standard_CString arg1 = argv[1];
57 TopoDS_Shape Shape = DBRep::Get(arg1);
58 if (Shape.IsNull()) { di<<arg1<<" inconnu"<<"\n"; return 1 /* Error */; }
59 Standard_Integer nbe = 0, nbf = 0; Standard_Real f3d,l3d;
60
61 for (TopExp_Explorer exp(Shape,TopAbs_EDGE); exp.More(); exp.Next()) {
62 TopoDS_Edge Edge = TopoDS::Edge (exp.Current()); nbe ++;
63 if (BRep_Tool::Degenerated(Edge)) continue;
64 Handle(Geom_Curve) curve3d = BRep_Tool::Curve (Edge,f3d,l3d);
65 if (curve3d.IsNull()) {
66 char nomsh[30];
67 nbf ++;
68 sprintf (nomsh,"faultedge_%d",nbf);
69 di<<"Edge sans Curve3d, n0 "<<nbe<<"\n";
70 DBRep::Set (nomsh,Edge);
71 }
72 }
73 return 0;
74}
75
76
77
78static Standard_Integer XSHAPE_explorewire
79 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
80{
81 char nomsh[30];
82 if (argc < 2) { di<<"donner un nom de wire"<<"\n"; return 1 /* Error */; }
83 Standard_CString arg1 = argv[1];
84 TopoDS_Shape Shape = DBRep::Get(arg1);
85 if (Shape.IsNull()) { di<<arg1<<" inconnu"<<"\n"; return 1 /* Error */; }
86 if (Shape.ShapeType() != TopAbs_WIRE) { di<<"Pas un WIRE"<<"\n"; return 1 /* Error */; }
87 TopoDS_Wire W = TopoDS::Wire (Shape);
88 TopoDS_Face F;
89 if (argc > 2) {
90 Standard_CString arg2 = argv[2];
91 TopoDS_Shape aLocalShape = DBRep::Get(arg2);
92 F = TopoDS::Face ( aLocalShape );
93 }
94
95 Standard_Integer i,num = 0, nbw, nbe = 0;
96 TopTools_IndexedMapOfShape map;
97 for (TopoDS_Iterator ext(W); ext.More(); ext.Next()) {
98 if (ext.Value().ShapeType() != TopAbs_EDGE) continue;
99 TopoDS_Edge E = TopoDS::Edge (ext.Value());
100 nbe ++; num = map.Add(E);
101 }
102 int* nbs = new int[nbe+1]; for (i = 0; i <= nbe; i ++) nbs[i] = 0;
103
104 di<<"TopoDS_Iterator(EDGE) donne "<<nbe<<" Edges dont "<<num<<" distinctes"<<"\n";
105 nbe = num;
106 nbw = 0;
107 for (TopExp_Explorer exe(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE); exe.More(); exe.Next()) nbw ++;
108 di<<"TopExp_Explorer(EDGE) donne "<<nbw<<" Edges"<<"\n";
109 nbw = 0;
110 BRepTools_WireExplorer bwe;
111 if (F.IsNull()) bwe.Init(W);
112 else bwe.Init (W,F);
113 for (; bwe.More(); bwe.Next()) {
114 TopoDS_Edge E = TopoDS::Edge (bwe.Current());
115 nbw ++;
116 num = map.FindIndex(E);
117 nbs[num] ++;
118 }
119 di<<"BRepTools_WireExplorer donne "<<nbw<<" Edges"<<"\n";
120 di<<"Par rapport a la map, edges sautees par WE en NOWE_num, passees > 1 fois en MULTWE_num"<<"\n";
121 if (nbs[0] > 0) di<<"NB : Edge n0 0 comptee "<<nbs[0]<<" fois"<<"\n";
122 for (i = 1; i <= nbe; i ++) {
123 if (nbs[i] < 1) {
124 di<<"Edge n0 "<<i<<" pas vue par WE"<<"\n";
125 sprintf (nomsh,"NOWE_%d",i);
126 DBRep::Set (nomsh,map.FindKey(i));
127 } else if (nbs[i] > 1) {
128 di<<"Edge n0 "<<i<<" vue par WE : "<<nbs[i]<<" fois"<<"\n";
129 sprintf (nomsh,"MULT_%d",i);
130 DBRep::Set (nomsh,map.FindKey(i));
131 }
132 }
133 delete [] nbs;
134 return 0;
135}
136
137
138
139static Standard_Integer XSHAPE_ssolid
140 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
141{
142 if (argc < 3) { di<<"Give new solid name + shell name"<<"\n"; return 1 /* Error */; }
143 Standard_CString arg1 = argv[1];
144 TopoDS_Shape Shape = DBRep::Get(arg1);
145 if (Shape.IsNull()) { di<<"Shape unknown : "<<arg1<<"\n"; return 1 /* Error */; }
146 TopAbs_ShapeEnum shen = Shape.ShapeType();
147 if (shen == TopAbs_SOLID) {
148 di<<" Already a Solide ! nothing done"<<"\n";
149 return 0;
150 }
151 if (shen != TopAbs_SHELL) {
152 di<<" Not a Shell"<<"\n"; return 1 /* Error */;
153 }
154 if (!Shape.Free ()) {
155 di<<"Shape non Free -> Freeing"<<"\n";
156 Shape.Free(Standard_True);
157 }
158 TopoDS_Shell sh = TopoDS::Shell (Shape);
159 TopoDS_Solid solid;
160 BRep_Builder B;
161 B.MakeSolid (solid);
162 B.Add (solid,sh);
163// Pas encore fini : il faut une bonne orientation
164 BRepClass3d_SolidClassifier bsc3d (solid);
165 bsc3d.PerformInfinitePoint(BRepBuilderAPI::Precision());
166 if (bsc3d.State() == TopAbs_IN) {
167// Ensuite, inverser C-A-D REPRENDRE LES SHELLS
168// (l inversion du solide n est pas bien prise en compte)
169 di<<"NB : Shell to be reversed"<<"\n";
170 TopoDS_Solid soli2;
171 B.MakeSolid (soli2); // on recommence
172 sh.Reverse();
173 B.Add (soli2,sh);
174 solid = soli2;
175 }
176 DBRep::Set(argv[2],solid);
177 return 0; // Done
178}
179
180
181static Standard_Integer XSHAPE_edgeregul
182 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
183{
184 //cky/rln 03.02.99 To eliminate dependence of SWDRAW on TKXSBase (to use only ShapeHealing)
185 Standard_Real tolang; // = Interface_Static::RVal("XSTEP.encoderegularity.angle");
186// if (argc < 3) di<<"Current value for regularity angle : "<<tolang<<"\n";
187 if (argc < 3) {
188 di<<"Donner nom de shape.\n + option : angle en radian, sinon la valeur courante est prise"<<"\n";
189 return 0;
190 }
191 Standard_CString arg1 = argv[1];
192 Standard_CString arg2 = argv[2];
193 if (argc > 2) tolang = atof (arg2);
194 if (tolang <= 0) {
195 di<<"Not a suitable value : "<<tolang<<"\n";
196 return 1 /* Error */;
197 }
198 TopoDS_Shape Shape = DBRep::Get(arg1);
199 if (Shape.IsNull()) { di<<"Shape unknown : "<<arg1<<"\n"; return 1 /* Error */; }
200
201 BRepLib::EncodeRegularity (Shape,tolang);
202 return 0; // Done
203}
204
205static Standard_Integer samerange (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
206{
207 if ( argc ==2 ) {
208 TopoDS_Shape Shape = DBRep::Get(argv[1]);
209 if (Shape.IsNull()) { di<<"Shape unknown: "<<argv[2]<<"\n"; return 1; }
210
211 for ( TopExp_Explorer exp(Shape,TopAbs_EDGE); exp.More(); exp.Next() ) {
212 TopoDS_Edge edge = TopoDS::Edge ( exp.Current() );
213 BRepLib::SameRange ( edge, Precision::PConfusion() );
214 }
215 }
216 else if ( argc == 7 ) {
217 Handle(Geom2d_Curve) C = DrawTrSurf::GetCurve2d(argv[2]);
218 if (C.IsNull()) { di<<"Curve unknown: "<<argv[2]<<"\n"; return 1; }
219
220 Standard_Real oldFirst = atof(argv[3]);
221 Standard_Real oldLast = atof(argv[4]);
222 Standard_Real current_first = atof(argv[5]);
223 Standard_Real current_last = atof(argv[6]);
224 Standard_Real Tol = Precision::PConfusion();
225 Handle(Geom2d_Curve) NewC2d;
226 GeomLib::SameRange(Tol, C, oldFirst,oldLast,
227 current_first, current_last, NewC2d);
228 DrawTrSurf::Set(argv[1],NewC2d);
229 }
230 else {
231 di << "Apply BRepLib::SameRange() to shape or GeomLib::SameRange() to pcurve:" << "\n";
232 di << "> samerange shape" << "\n";
233 di << "or" << "\n";
234 di << "> samerange newcurve curve2d first last newfirst newlast" << "\n";
235 }
236
237 return 0;
238}
239
240// ########################################
241// ## DECLARATIONS ##
242// ########################################
243
244static int initactor = 0;
245
246void SWDRAW_ShapeTool::InitCommands (Draw_Interpretor& theCommands)
247{
248 if (initactor) return; initactor = 1;
249
250 const char* g;
251 g = "DE: old";
252
253 theCommands.Add ("anaedges","nom shape",
254 __FILE__,XSHAPE_edge,g);
255 theCommands.Add ("expwire","nom wire [nom face]",
256 __FILE__,XSHAPE_explorewire,g);
257
258 theCommands.Add ("ssolid","nom shell + nouveau nom solid",
259 __FILE__,XSHAPE_ssolid,g);
260
261 theCommands.Add ("edgeregul","shape val",
262 __FILE__,XSHAPE_edgeregul,g);
263
264 theCommands.Add ("samerange","{ shape | result curve2d first last newfirst newlast }",
265 __FILE__,samerange,g);
266}