1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 // 25.12.98 pdn renaming
19 // 02.02.99 cky/rln PRO17746: transmitting 'sketch' command to XSDRAWEUC
20 // 23.02.99 abv: method ShapeFix::FillFace() removed
21 // 02.03.99 cky/rln: command edgeregul only accepts tolerance
22 // 15.06.99 abv/pdn: command comptol added (from S4030)
23 #include <SWDRAW_ShapeTool.ixx>
27 #include <TopAbs_ShapeEnum.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_Shell.hxx>
32 #include <TopoDS_Solid.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Vertex.hxx>
38 #include <BRepBuilderAPI.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopExp_Explorer.hxx>
44 #include <BRep_Tool.hxx>
45 #include <Geom_Curve.hxx>
46 #include <Geom_Surface.hxx>
48 #include <gp_Pnt2d.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <BRepClass3d_SolidClassifier.hxx>
52 #include <BRepTools_WireExplorer.hxx>
53 #include <TopoDS_Iterator.hxx>
55 #include <BRep_Builder.hxx>
60 #include <BRepLib.hxx>
63 #include <Geom_TrimmedCurve.hxx>
65 #include <Precision.hxx>
66 #include <DrawTrSurf.hxx>
67 #include <GeomLib.hxx>
70 static Standard_Integer XSHAPE_edge
71 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
73 if (argc < 2) { di<<"donner un nom de shape"<<"\n"; return 1 /* Error */; }
74 Standard_CString arg1 = argv[1];
75 TopoDS_Shape Shape = DBRep::Get(arg1);
76 if (Shape.IsNull()) { di<<arg1<<" inconnu"<<"\n"; return 1 /* Error */; }
77 Standard_Integer nbe = 0, nbf = 0; Standard_Real f3d,l3d;
79 for (TopExp_Explorer exp(Shape,TopAbs_EDGE); exp.More(); exp.Next()) {
80 TopoDS_Edge Edge = TopoDS::Edge (exp.Current()); nbe ++;
81 if (BRep_Tool::Degenerated(Edge)) continue;
82 Handle(Geom_Curve) curve3d = BRep_Tool::Curve (Edge,f3d,l3d);
83 if (curve3d.IsNull()) {
86 Sprintf (nomsh,"faultedge_%d",nbf);
87 di<<"Edge sans Curve3d, n0 "<<nbe<<"\n";
88 DBRep::Set (nomsh,Edge);
96 static Standard_Integer XSHAPE_explorewire
97 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
100 if (argc < 2) { di<<"donner un nom de wire"<<"\n"; return 1 /* Error */; }
101 Standard_CString arg1 = argv[1];
102 TopoDS_Shape Shape = DBRep::Get(arg1);
103 if (Shape.IsNull()) { di<<arg1<<" inconnu"<<"\n"; return 1 /* Error */; }
104 if (Shape.ShapeType() != TopAbs_WIRE) { di<<"Pas un WIRE"<<"\n"; return 1 /* Error */; }
105 TopoDS_Wire W = TopoDS::Wire (Shape);
108 Standard_CString arg2 = argv[2];
109 TopoDS_Shape aLocalShape = DBRep::Get(arg2);
110 F = TopoDS::Face ( aLocalShape );
113 Standard_Integer i,num = 0, nbw, nbe = 0;
114 TopTools_IndexedMapOfShape map;
115 for (TopoDS_Iterator ext(W); ext.More(); ext.Next()) {
116 if (ext.Value().ShapeType() != TopAbs_EDGE) continue;
117 TopoDS_Edge E = TopoDS::Edge (ext.Value());
118 nbe ++; num = map.Add(E);
120 int* nbs = new int[nbe+1]; for (i = 0; i <= nbe; i ++) nbs[i] = 0;
122 di<<"TopoDS_Iterator(EDGE) donne "<<nbe<<" Edges dont "<<num<<" distinctes"<<"\n";
125 for (TopExp_Explorer exe(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE); exe.More(); exe.Next()) nbw ++;
126 di<<"TopExp_Explorer(EDGE) donne "<<nbw<<" Edges"<<"\n";
128 BRepTools_WireExplorer bwe;
129 if (F.IsNull()) bwe.Init(W);
131 for (; bwe.More(); bwe.Next()) {
132 TopoDS_Edge E = TopoDS::Edge (bwe.Current());
134 num = map.FindIndex(E);
137 di<<"BRepTools_WireExplorer donne "<<nbw<<" Edges"<<"\n";
138 di<<"Par rapport a la map, edges sautees par WE en NOWE_num, passees > 1 fois en MULTWE_num"<<"\n";
139 if (nbs[0] > 0) di<<"NB : Edge n0 0 comptee "<<nbs[0]<<" fois"<<"\n";
140 for (i = 1; i <= nbe; i ++) {
142 di<<"Edge n0 "<<i<<" pas vue par WE"<<"\n";
143 Sprintf (nomsh,"NOWE_%d",i);
144 DBRep::Set (nomsh,map.FindKey(i));
145 } else if (nbs[i] > 1) {
146 di<<"Edge n0 "<<i<<" vue par WE : "<<nbs[i]<<" fois"<<"\n";
147 Sprintf (nomsh,"MULT_%d",i);
148 DBRep::Set (nomsh,map.FindKey(i));
157 static Standard_Integer XSHAPE_ssolid
158 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
160 if (argc < 3) { di<<"Give new solid name + shell name"<<"\n"; return 1 /* Error */; }
161 Standard_CString arg1 = argv[1];
162 TopoDS_Shape Shape = DBRep::Get(arg1);
163 if (Shape.IsNull()) { di<<"Shape unknown : "<<arg1<<"\n"; return 1 /* Error */; }
164 TopAbs_ShapeEnum shen = Shape.ShapeType();
165 if (shen == TopAbs_SOLID) {
166 di<<" Already a Solide ! nothing done"<<"\n";
169 if (shen != TopAbs_SHELL) {
170 di<<" Not a Shell"<<"\n"; return 1 /* Error */;
172 if (!Shape.Free ()) {
173 di<<"Shape non Free -> Freeing"<<"\n";
174 Shape.Free(Standard_True);
176 TopoDS_Shell sh = TopoDS::Shell (Shape);
181 // Pas encore fini : il faut une bonne orientation
182 BRepClass3d_SolidClassifier bsc3d (solid);
183 bsc3d.PerformInfinitePoint(BRepBuilderAPI::Precision());
184 if (bsc3d.State() == TopAbs_IN) {
185 // Ensuite, inverser C-A-D REPRENDRE LES SHELLS
186 // (l inversion du solide n est pas bien prise en compte)
187 di<<"NB : Shell to be reversed"<<"\n";
189 B.MakeSolid (soli2); // on recommence
194 DBRep::Set(argv[2],solid);
199 static Standard_Integer XSHAPE_edgeregul
200 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
202 //cky/rln 03.02.99 To eliminate dependence of SWDRAW on TKXSBase (to use only ShapeHealing)
203 Standard_Real tolang = Precision::Angular(); // = Interface_Static::RVal("XSTEP.encoderegularity.angle");
204 // if (argc < 3) di<<"Current value for regularity angle : "<<tolang<<"\n";
206 di<<"Donner nom de shape.\n + option : angle en radian, sinon la valeur courante est prise"<<"\n";
209 Standard_CString arg1 = argv[1];
210 Standard_CString arg2 = argv[2];
211 if (argc > 2) tolang = Draw::Atof (arg2);
213 di<<"Not a suitable value : "<<tolang<<"\n";
214 return 1 /* Error */;
216 TopoDS_Shape Shape = DBRep::Get(arg1);
217 if (Shape.IsNull()) { di<<"Shape unknown : "<<arg1<<"\n"; return 1 /* Error */; }
219 BRepLib::EncodeRegularity (Shape,tolang);
223 static Standard_Integer samerange (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
226 TopoDS_Shape Shape = DBRep::Get(argv[1]);
227 if (Shape.IsNull()) { di<<"Shape unknown: "<<argv[2]<<"\n"; return 1; }
229 for ( TopExp_Explorer exp(Shape,TopAbs_EDGE); exp.More(); exp.Next() ) {
230 TopoDS_Edge edge = TopoDS::Edge ( exp.Current() );
231 BRepLib::SameRange ( edge, Precision::PConfusion() );
234 else if ( argc == 7 ) {
235 Handle(Geom2d_Curve) C = DrawTrSurf::GetCurve2d(argv[2]);
236 if (C.IsNull()) { di<<"Curve unknown: "<<argv[2]<<"\n"; return 1; }
238 Standard_Real oldFirst = Draw::Atof(argv[3]);
239 Standard_Real oldLast = Draw::Atof(argv[4]);
240 Standard_Real current_first = Draw::Atof(argv[5]);
241 Standard_Real current_last = Draw::Atof(argv[6]);
242 Standard_Real Tol = Precision::PConfusion();
243 Handle(Geom2d_Curve) NewC2d;
244 GeomLib::SameRange(Tol, C, oldFirst,oldLast,
245 current_first, current_last, NewC2d);
246 DrawTrSurf::Set(argv[1],NewC2d);
249 di << "Apply BRepLib::SameRange() to shape or GeomLib::SameRange() to pcurve:" << "\n";
250 di << "> samerange shape" << "\n";
252 di << "> samerange newcurve curve2d first last newfirst newlast" << "\n";
258 // ########################################
259 // ## DECLARATIONS ##
260 // ########################################
262 static int initactor = 0;
264 void SWDRAW_ShapeTool::InitCommands (Draw_Interpretor& theCommands)
266 if (initactor) return; initactor = 1;
271 theCommands.Add ("anaedges","nom shape",
272 __FILE__,XSHAPE_edge,g);
273 theCommands.Add ("expwire","nom wire [nom face]",
274 __FILE__,XSHAPE_explorewire,g);
276 theCommands.Add ("ssolid","nom shell + nouveau nom solid",
277 __FILE__,XSHAPE_ssolid,g);
279 theCommands.Add ("edgeregul","shape val",
280 __FILE__,XSHAPE_edgeregul,g);
282 theCommands.Add ("samerange","{ shape | result curve2d first last newfirst newlast }",
283 __FILE__,samerange,g);