0028661: BRepOffsetAPI_MakePipeShell throws an exception Standard_NoSuchObject: NColl...
[occt.git] / src / BRepTest / BRepTest_OtherCommands.cxx
CommitLineData
b311480e 1// Created on: 1995-04-13
2// Created by: Jean Yves LEBEY
3// Copyright (c) 1995-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
17#include <BRepTest.hxx>
18
91322f44 19#include <Draw.hxx>
7fd59977 20#include <Draw_Interpretor.hxx>
21#include <DBRep.hxx>
a15d6ace 22#include <DrawTrSurf.hxx>
23
24#include <string.h>
25#include <stdio.h>
26
27#include <Standard_ErrorHandler.hxx>
28#include <Precision.hxx>
29#include <TCollection_AsciiString.hxx>
30#include <gp_Lin.hxx>
31#include <gp_Pnt.hxx>
32#include <gp_Dir.hxx>
33#include <gp_Ax1.hxx>
34
35#include <ElCLib.hxx>
36
37#include <TColgp_SequenceOfPnt.hxx>
38
39#include <GeomAbs_JoinType.hxx>
40#include <Geom_Line.hxx>
41
42#include <IntCurvesFace_Intersector.hxx>
43
44#include <TopAbs.hxx>
45#include <TopAbs_Orientation.hxx>
46
47#include <TopoDS.hxx>
7fd59977 48#include <TopoDS_Shape.hxx>
49#include <TopoDS_Compound.hxx>
50#include <TopoDS_CompSolid.hxx>
51#include <TopoDS_Solid.hxx>
52#include <TopoDS_Shell.hxx>
53#include <TopoDS_Face.hxx>
54#include <TopoDS_Wire.hxx>
55#include <TopoDS_Edge.hxx>
56#include <TopoDS_Vertex.hxx>
a15d6ace 57#include <TopoDS_Iterator.hxx>
7fd59977 58
a15d6ace 59#include <BRep_Builder.hxx>
60#include <BRep_Tool.hxx>
7fd59977 61
7fd59977 62#include <TopExp_Explorer.hxx>
a15d6ace 63
64#include <TopTools_ListOfShape.hxx>
65#include <TopTools_ListIteratorOfListOfShape.hxx>
7fd59977 66#include <TopTools_MapOfShape.hxx>
67
a15d6ace 68#include <LocOpe_CSIntersector.hxx>
69#include <LocOpe_SequenceOfLin.hxx>
70#include <LocOpe_PntFace.hxx>
71#include <BRepFeat_MakeDPrism.hxx>
7fd59977 72
a15d6ace 73#include <BRepTools.hxx>
74#include <BRepIntCurveSurface_Inter.hxx>
75#include <BRepOffset.hxx>
76#include <BRepOffset_MakeOffset.hxx>
77#include <BRepClass3d_SolidClassifier.hxx>
004e8466 78#include <GeomAdaptor_Curve.hxx>
a15d6ace 79
80static
81 void SampleEdges (const TopoDS_Shape& theShape,
82 TColgp_SequenceOfPnt& theSeq);
83static
84 TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape,
85 const TopoDS_Shape& basis,
86 const gp_Ax1& ax1);
87static
88 void PrintState (Draw_Interpretor& aDI,
89 const TopAbs_State& aState);
90//
91static Standard_Integer emptyshape(Draw_Interpretor&, Standard_Integer, const char** );
92static Standard_Integer subshape (Draw_Interpretor&, Standard_Integer, const char** );
93static Standard_Integer brepintcs (Draw_Interpretor&, Standard_Integer, const char** );
94static Standard_Integer MakeBoss (Draw_Interpretor&, Standard_Integer, const char** );
95static Standard_Integer MakeShell (Draw_Interpretor&, Standard_Integer, const char** );
96static Standard_Integer xbounds (Draw_Interpretor&, Standard_Integer, const char** );
97static Standard_Integer xclassify (Draw_Interpretor&, Standard_Integer, const char** );
7fd59977 98
99//=======================================================================
a15d6ace 100//function : OtherCommands
101//purpose :
7fd59977 102//=======================================================================
a15d6ace 103void BRepTest::OtherCommands(Draw_Interpretor& theCommands)
104{
105 static Standard_Boolean done = Standard_False;
106 if (done) return;
107 done = Standard_True;
108
109 const char* g = "TOPOLOGY other commands";
110
111 theCommands.Add("shape",
112 "shape name V/E/W/F/Sh/So/CS/C; make a empty shape",__FILE__,emptyshape,g);
7fd59977 113
a15d6ace 114 theCommands.Add("subshape",
115 "subshape name V/E/W/F/Sh/So/CS/C index; get subsshape <index> of given type"
116 ,__FILE__,subshape,g);
117
118 theCommands.Add("BRepIntCS",
004e8466 119 "Calcul d'intersection entre face et curve : BRepIntCS curve1 [curve2 ...] shape [res] [tol]"
a15d6ace 120 ,__FILE__,brepintcs,g);
121
122 theCommands.Add("makeboss", "create a boss on the shape myS", __FILE__, MakeBoss, g);
123 theCommands.Add("mksh", "create a shell on Shape", __FILE__, MakeShell, g);
124 theCommands.Add("xbounds", "xbounds face", __FILE__, xbounds, g);
125 theCommands.Add("xclassify", "use xclassify Solid [Tolerance=1.e-7]", __FILE__, xclassify, g);
126
127
128}
129//=======================================================================
130//function : emptyshape
131//purpose : shape : shape name V/E/W/F/SH/SO/CS/C
132//=======================================================================
133Standard_Integer emptyshape(Draw_Interpretor& , Standard_Integer n, const char** a)
7fd59977 134{
135 if (n <= 1) return 1;
136
137 BRep_Builder B;
138 TopoDS_Shape S;
139
140 if (n == 3) {
141 TCollection_AsciiString as(a[2]); as.LowerCase();
142 const char* a2 = as.ToCString();
143
144 if ( ! strcmp(a2,"c") ) {
145 TopoDS_Compound SS; B.MakeCompound(SS); S = SS;
146 }
147 else if ( ! strcmp(a2,"cs") ) {
148 TopoDS_CompSolid SS; B.MakeCompSolid(SS); S = SS;
149 }
150 else if ( ! strcmp(a2,"so") ) {
151 TopoDS_Solid SS; B.MakeSolid(SS); S = SS;
152 }
153 else if ( ! strcmp(a2,"sh") ) {
154 TopoDS_Shell SS; B.MakeShell(SS); S = SS;
155 }
156 else if ( ! strcmp(a2,"f") ) {
157 TopoDS_Face SS; B.MakeFace(SS); S = SS;
158 }
159 else if ( ! strcmp(a2,"w") ) {
160 TopoDS_Wire SS; B.MakeWire(SS); S = SS;
161 }
162 else if ( ! strcmp(a2,"e") ) {
163 TopoDS_Edge SS; B.MakeEdge(SS); S = SS;
164 }
165 else if ( ! strcmp(a2,"v") ) {
166 TopoDS_Vertex SS; B.MakeVertex(SS); S = SS;
167 }
168 else {
169 return 1;
170 }
171 }
172
173 const char *shapename = a[1];
174 DBRep::Set(shapename,S);
175 return 0;
176}
177
178//=======================================================================
a15d6ace 179//function : subshape
180//purpose :
7fd59977 181//=======================================================================
a15d6ace 182Standard_Integer subshape(Draw_Interpretor& di, Standard_Integer n, const char** a)
7fd59977 183{
184 if (n <= 2) return 1;
185
186
187 TopoDS_Shape S = DBRep::Get(a[1]);
188 if (S.IsNull()) return 0;
189 char newname[1024];
190 strcpy(newname,a[1]);
191 char* p = newname;
192 while (*p != '\0') p++;
193 *p = '_';
194 p++;
195 Standard_Integer i = 0;
196 if (n == 3) {
91322f44 197 Standard_Integer isub = Draw::Atoi(a[2]);
7fd59977 198 TopoDS_Iterator itr(S);
199 while (itr.More()) {
200 i++;
201 if ( i == isub ) {
91322f44 202 Sprintf(p,"%d",i);
7fd59977 203 DBRep::Set(newname,itr.Value());
204 di.AppendElement(newname);
205 break;
206 }
207 itr.Next();
208 }
209 }
210 else {
211 // explode a type
212 TopAbs_ShapeEnum typ;
213 switch (a[2][0]) {
214
215 case 'C' :
216 case 'c' :
217 typ = TopAbs_COMPSOLID;
218 break;
219
220 case 'S' :
221 case 's' :
222 if ((a[2][1] == 'O')||(a[2][1] == 'o'))
223 typ = TopAbs_SOLID;
224 else if ((a[2][1] == 'H')||(a[2][1] == 'h'))
225 typ = TopAbs_SHELL;
226 else
227 return 1;
228 break;
229
230 case 'F' :
231 case 'f' :
232 typ = TopAbs_FACE;
233 break;
234
235 case 'W' :
236 case 'w' :
237 typ = TopAbs_WIRE;
238 break;
239
240 case 'E' :
241 case 'e' :
242 typ = TopAbs_EDGE;
243 break;
244
245 case 'V' :
246 case 'v' :
247 typ = TopAbs_VERTEX;
248 break;
249
250 default :
251 return 1;
252 }
253
91322f44 254 Standard_Integer isub = Draw::Atoi(a[3]);
7fd59977 255 TopTools_MapOfShape M;
256 M.Add(S);
257 TopExp_Explorer ex(S,typ);
258 while (ex.More()) {
259 if (M.Add(ex.Current())) {
260 i++;
261 if ( i == isub ) {
91322f44 262 Sprintf(p,"%d",i);
7fd59977 263 DBRep::Set(newname,ex.Current());
264 di.AppendElement(newname);
265 break;
266 }
267 }
268 ex.Next();
269 }
270 }
271 return 0;
272}
7fd59977 273//=======================================================================
a15d6ace 274//function : brepintcs
275//purpose :
7fd59977 276//=======================================================================
004e8466 277Standard_Integer brepintcs(Draw_Interpretor& di, Standard_Integer n, const char** a)
7fd59977 278{
004e8466 279 if (n <= 2)
280 {
281 cout<<"Invalid input arguments. Should be: curve1 [curve2 ...] shape [result] [tol]"<<endl;
282 return 1;
283 }
284 Standard_Integer indshape = 2;
285 TopoDS_Shape S;
286 for( ; indshape <= n-1 ; indshape++)
287 {
288 S = DBRep::Get(a[indshape]);
289 if(!S.IsNull())
290 break;
291 }
292 if (S.IsNull())
293 {
294 cout<<"Invalid input shape"<<endl;
295 return 1;
296 }
7fd59977 297
004e8466 298 BRepIntCurveSurface_Inter theAlg;
299 double tol=1e-6;
300 if( indshape < n-1)
301 {
302 Standard_Real preci = atof(a[n-1]);
303 if(preci >= Precision::Confusion())
304 tol = preci;
305 }
306 int nbpi=0;
307 gp_Pnt curp;
308 TopoDS_Compound aComp;
309 BRep_Builder aB;
310 aB.MakeCompound(aComp);
311 if (indshape == 2) {
7fd59977 312 Handle(Geom_Curve) C= DrawTrSurf::GetCurve(a[1]);
313 if (C.IsNull()) return 2;
314 GeomAdaptor_Curve acur(C);
315 theAlg.Init(S, acur, tol);
004e8466 316
7fd59977 317 for (; theAlg.More(); theAlg.Next()) {
318 curp=theAlg.Pnt();
004e8466 319 TopoDS_Vertex aV;
320
321 aB.MakeVertex(aV, curp, 0);
322 aB.Add(aComp, aV);
7fd59977 323 nbpi++;
004e8466 324 di<<"Point "<<nbpi<<" : "<<curp.X()<<" "<<curp.Y()<<" "<<curp.Z()<<"\n";
7fd59977 325 char name[64];
326 char* temp = name; // pour portage WNT
91322f44 327 Sprintf(temp, "%s_%d", "brics", nbpi);
7fd59977 328 DrawTrSurf::Set(temp, curp);
329 }
330 }
331 else {
004e8466 332 theAlg.Load(S,tol );
333 for (Standard_Integer il = 1; il<indshape ; il++)
334 {
335 Handle(Geom_Curve) hl= DrawTrSurf::GetCurve(a[il]);
7fd59977 336 if (!hl.IsNull()) {
004e8466 337 theAlg.Init(hl);
338 for (; theAlg.More(); theAlg.Next()) {
339 curp=theAlg.Pnt();
340 nbpi++;
341 TopoDS_Vertex aV;
342 aB.MakeVertex(aV, curp, 0);
343 aB.Add(aComp, aV);
344 di<<"Point "<<nbpi<<" : "<<curp.X()<<" "<<curp.Y()<<" "<<curp.Z()<<"\n";
345 char name[64];
346 char* temp = name; // pour portage WNT
347 Sprintf(temp, "%s_%d", "brics", nbpi);
348 DrawTrSurf::Set(temp, curp);
349 }
7fd59977 350 }
351 }
352 }
004e8466 353 if(!nbpi)
586db386 354 di<<"Points of intersections are not found\n";
004e8466 355 if(indshape < n-1)
356 DBRep::Set(a[n-1], aComp);
7fd59977 357 //POP pour NT
358 return 0;
359}
7fd59977 360//=======================================================================
a15d6ace 361//function : MakeBoss
362//purpose :
7fd59977 363//=======================================================================
a15d6ace 364Standard_Integer MakeBoss(Draw_Interpretor& , Standard_Integer , const char** a)
7fd59977 365{
7fd59977 366 TopoDS_Shape myS = DBRep::Get( a[2] );
367
368 TopoDS_Shape myBasis = DBRep::Get( a[3] ) ;
369
370 Standard_Real ang = -0.05235987901687622;
371
372
373 TopoDS_Face basis = TopoDS::Face(myBasis);
374
375 BRepFeat_MakeDPrism DPRISM(myS, basis, basis, ang, 1, Standard_True);
376
377 TopoDS_Shape myFaceOnShape;
378 gp_Pnt Pnt(0.0, 0.0, 50.0);
379 gp_Dir Dir(-0.0, -0.0, -1.0);
380 gp_Ax1 ax(Pnt, Dir);
381
382 myFaceOnShape = NextFaceForPrism(myS, myBasis, ax);
383
384 DPRISM.Perform (myFaceOnShape);
385 DPRISM.Build();
386
387 if( DPRISM.IsDone() ) DBRep::Set( a[1], DPRISM.Shape() );
388
389 return 0;
390}
7fd59977 391//=======================================================================
a15d6ace 392//function : MakeShell
393//purpose :
7fd59977 394//=======================================================================
a15d6ace 395Standard_Integer MakeShell(Draw_Interpretor& , Standard_Integer , const char** a)
7fd59977 396{
397
398 TopoDS_Shape aShape = DBRep::Get( a[1] );
399 TopTools_ListOfShape Lst;
400 TopExp_Explorer Exp(aShape, TopAbs_FACE);
401 TopoDS_Shape InputShape(DBRep::Get( a[2] ));
402 TopoDS_Face F = TopoDS::Face(InputShape);
403// TopoDS_Face F = TopoDS::Face(DBRep::Get( a[2] ));
404
91322f44 405 Standard_Real Off = -Draw::Atof( a[3] );
7fd59977 406
407 BRepOffset_MakeOffset Offset;
408
409 Offset.Initialize( aShape, Off, 1.0e-3, BRepOffset_Skin,
410 Standard_True , Standard_False , GeomAbs_Arc );
411 Offset.AddFace( F );
412 Offset.MakeThickSolid();
413
414 if( Offset.IsDone() ) {
415 // SaveShape::Save(Offset.Shape(), "ss");
416 DBRep::Set( a[1], Offset.Shape() );
417 }
418 return 0;
419}
bd05fabf
S
420//=======================================================================
421//function : xbounds
422//purpose :
423//=======================================================================
424Standard_Integer xbounds(Draw_Interpretor& di, Standard_Integer n, const char** a)
425{
426 if (n<2) {
586db386 427 di << "Usage : " << a[0] << " face\n";
bd05fabf
S
428 return 0;
429 }
430 //
431
432 Standard_Real aUMin, aUMax, aVMin, aVMax;
433 TopoDS_Shape aS;
434 TopoDS_Face aF;
435 //
436 aS=DBRep::Get(a[1]);
437 if (aS.IsNull()) {
438 di << " null shapes is not allowed here\n";
439 return 0;
440 }
441 if (aS.ShapeType()!=TopAbs_FACE) {
442 di << " shape" << a[1] <<" must be a face\n";
443 return 0;
444 }
445 //
446 aF=*((TopoDS_Face*)&aS);
447 //
448 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
449 //
450 TCollection_AsciiString aStr;
451 TCollection_AsciiString sUMin(aUMin);
452 TCollection_AsciiString sUMax(aUMax);
453 TCollection_AsciiString sVMin(aVMin);
454 TCollection_AsciiString sVMax(aVMax);
455 //
456 aStr=aStr+sUMin + "\n";
457 aStr=aStr+sUMax + "\n";
458 aStr=aStr+sVMin + "\n";
459 aStr=aStr+sVMax + "\n";
460 di <<aStr.ToCString();
461 //
462 return 0;
463}
7fd59977 464//=======================================================================
a15d6ace 465//function : xclassify
7fd59977 466//purpose :
467//=======================================================================
a15d6ace 468Standard_Integer xclassify (Draw_Interpretor& aDI, Standard_Integer n, const char** a)
7fd59977 469{
a15d6ace 470 if (n < 2) {
471 aDI<<" use xclassify Solid [Tolerance=1.e-7]\n";
472 return 1;
473 }
474
475 TopoDS_Shape aS = DBRep::Get(a[1]);
476 if (aS.IsNull()) {
477 aDI<<" Null Shape is not allowed here\n";
478 return 0;
479 }
480
481 if (aS.ShapeType()!=TopAbs_SOLID) {
482 aDI<< " Shape type must be SOLID\n";
483 return 0;
484 }
485 //
486 Standard_Real aTol=1.e-7;
487 TopAbs_State aState = TopAbs_UNKNOWN;
488 //
489 aTol=1.e-7;
490 if (n==3) {
91322f44 491 aTol=Draw::Atof(a[2]);
a15d6ace 492 }
493 //
494 BRepClass3d_SolidClassifier aSC(aS);
495 aSC.PerformInfinitePoint(aTol);
496
497 aState = aSC.State();
498 PrintState(aDI, aState);
499 //
500 return 0;
501}
502//=======================================================================
503//function : PrintState
504//purpose :
505//=======================================================================
506void PrintState (Draw_Interpretor& aDI,
507 const TopAbs_State& aState)
508{
509 aDI<<"state is: ";
510 switch (aState) {
511 case TopAbs_IN:
512 aDI<<"IN\n";
513 break;
514 case TopAbs_OUT:
515 aDI<<"OUT\n";
516 break;
517 case TopAbs_ON:
518 aDI<<"ON\n";
519 break;
520 case TopAbs_UNKNOWN:
521 default:
522 aDI<<"UNKNOWN\n";
523 break;
524 }
7fd59977 525}
7fd59977 526//=======================================================================
527//function : NextFaceForPrism
528//purpose : Search a face from <shape> which intersects with a line of
529// direction <ax1> and location a point of <basis>.
530//=======================================================================
a15d6ace 531TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape,
532 const TopoDS_Shape& basis,
533 const gp_Ax1& ax1)
7fd59977 534{
535 TopoDS_Face nextFace;
536
537 TColgp_SequenceOfPnt seqPnts;
538 SampleEdges(basis, seqPnts);
539
540 for (Standard_Integer i=1; i<=seqPnts.Length(); i++) {
541 const gp_Pnt& pt = seqPnts(i);
542 // find a axis through a face
543 gp_Dir dir = ax1.Direction();
544 gp_Ax1 ax1b(pt, dir);
545
546 LocOpe_CSIntersector ASI(shape);
547 LocOpe_SequenceOfLin slin;
548 slin.Append(ax1b);
549 ASI.Perform(slin);
550
551 if (ASI.IsDone()) {
552 Standard_Integer no=1, IndFrom, IndTo;
553 TopAbs_Orientation theOr;
554 Standard_Real min = 1.e-04, Tol = -Precision::Confusion();
555 if (ASI.LocalizeAfter (no, min, Tol, theOr, IndFrom, IndTo)) {
556 nextFace = ASI.Point(no, IndFrom).Face();
557 break;
558 }
559 }
560 }
561
562 return nextFace;
563}
564
7fd59977 565
566//=======================================================================
567//function : SampleEdges
568//purpose : Sampling of <theShape>.
569//design : Collect the vertices and points on the edges
570//=======================================================================
a15d6ace 571void SampleEdges (const TopoDS_Shape& theShape, TColgp_SequenceOfPnt& theSeq)
7fd59977 572{
573
574 theSeq.Clear();
575
576
577 TopTools_MapOfShape theMap;
578 TopExp_Explorer exp;
579
580 // Adds all vertices/pnt
581 for (exp.Init(theShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
582 if (theMap.Add(exp.Current())) {
583 theSeq.Append (BRep_Tool::Pnt(TopoDS::Vertex(exp.Current())));
584 }
585 }
586
587 // Computes points on edge, but does not take the extremities into account
588 Standard_Integer NECHANT = 5;
589 Handle(Geom_Curve) C;
590 Standard_Real f,l,prm;
591 for (exp.Init (theShape,TopAbs_EDGE); exp.More(); exp.Next()) {
592 const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
593 if (theMap.Add(edg)) {
594 if (!BRep_Tool::Degenerated(edg)) {
595 C = BRep_Tool::Curve(edg,f,l);
596 for (Standard_Integer i=1; i < NECHANT; i++) {
597 prm = ((NECHANT-i)*f+i*l)/NECHANT;
598 theSeq.Append (C->Value(prm));
599 }
600 }
601 }
602 }
603}
604