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