08241205296a06f5fed33d0937bce52a9fd34549
[occt.git] / src / BRepTest / BRepTest_ChamferCommands.cxx
1 // File:        BRepTest_ChamferCommands.cxx
2 // Created:     Mon Sep 25 14:07:55 1995
3 // Author:      Stagiaire Flore Lautheanne
4 //              <fla@phylox>
5
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
9
10 #include <Draw_Interpretor.hxx>
11 #include <BRepTest.hxx>
12 #include <DBRep.hxx>
13 #include <BRepFilletAPI_MakeChamfer.hxx>
14 #include <TopAbs_ShapeEnum.hxx>
15 #include <TopoDS.hxx>
16 #include <TopoDS_Face.hxx>
17 #include <TopoDS_Edge.hxx>
18 #include <TopoDS_Vertex.hxx>
19 #include <TopoDS_Shape.hxx>
20
21 #include <Precision.hxx>
22 #ifdef HAVE_STRINGS_H
23 # include <strings.h>
24 #endif
25
26
27
28 //=========================================================================
29 // function : chamfer
30 // purpose  : command to construct chamfers on several edges
31 //            Here the chamfer is propagated on tangential edges to the 
32 //            required edge
33 //=========================================================================
34  
35 static Standard_Integer chamfer(Draw_Interpretor& di,
36                                 Standard_Integer narg, 
37                                 const char** a)
38      
39 {
40   // check the argument number of the command
41   if (narg == 1) {
42     //cout <<" help for chamf : "<< endl;
43     //cout <<"   Construction by equal distances from edge          :  chamf newname shape edge face S dist"<< endl;
44     //cout <<"   Construction by two distances from edge            :  chamf newname shape edge face dist1 dist2"<< endl;
45     //cout <<"   Construction by distance from edge and given angle :  chamf newname shape edge face A dist angle"<< endl;
46     di <<" help for chamf : "<< "\n";
47     di <<"   Construction by equal distances from edge          :  chamf newname shape edge face S dist"<< "\n";
48     di <<"   Construction by two distances from edge            :  chamf newname shape edge face dist1 dist2"<< "\n";
49     di <<"   Construction by distance from edge and given angle :  chamf newname shape edge face A dist angle"<< "\n";
50   }
51   else {
52     if (narg < 7) return 1;
53   
54     TopoDS_Shape S = DBRep::Get(a[2]);
55     if (S.IsNull()) return 1;
56
57     TopoDS_Edge E;
58     TopoDS_Face F;
59     Standard_Real d1,d2, angle;
60     Standard_Integer i      = 3;
61     Standard_Integer Method = 1;
62     Standard_Integer NbArg  = 3;
63
64     BRepFilletAPI_MakeChamfer aMCh(S);
65
66     if (!strcasecmp(a[i + 2], "S") ) Method = 0;
67
68     if (Method != 0) {
69       if (!strcasecmp(a[i + 2], "A")) {
70         Method = 2;
71         NbArg++;
72       }
73     }
74
75     while (i + NbArg < narg) {
76       TopoDS_Shape aLocalEdge(DBRep::Get(a[i], TopAbs_EDGE));
77       E = TopoDS::Edge(aLocalEdge);
78       TopoDS_Shape aLocalFace(DBRep::Get(a[i + 1], TopAbs_FACE));
79       F = TopoDS::Face(aLocalFace);
80 //      E = TopoDS::Edge(DBRep::Get(a[i], TopAbs_EDGE));
81 //      F = TopoDS::Face(DBRep::Get(a[i + 1], TopAbs_FACE));
82       if (Method == 0) {
83         if (!E.IsNull() && !F.IsNull() && (aMCh.Contour(E) == 0) )  {
84           d1 = atof(a[i + 3]);
85
86           if ( d1 > Precision::Confusion()) 
87             aMCh.Add(d1,E ,F);
88         }
89         i += 4;
90       }
91       else if (Method == 1) {
92         if (!E.IsNull() && !F.IsNull() && (aMCh.Contour(E) == 0) )  {
93           d1 = atof(a[i + 2]);
94           d2 = atof(a[i + 3]);
95       
96           if (   (d1 > Precision::Confusion())
97               && (d2 > Precision::Confusion()) )
98             aMCh.Add(d1,d2,E,F);
99         }
100         i += 4;
101       }
102       else {
103         if (!E.IsNull() && !F.IsNull() && (aMCh.Contour(E) == 0) )  {
104           d1     = atof(a[i + 3]);
105           angle  = atof(a[i + 4]);
106           angle *= PI / 180.;
107       
108           if (   (d1 > Precision::Confusion())
109               && (angle > Precision::Confusion())
110               && (PI /  2.-  angle > Precision::Confusion()) )
111             aMCh.AddDA(d1, angle, E, F);
112         }
113         i += 5;
114       }
115
116       if ( i < narg) {
117         Method = 1;
118         NbArg  = 3;
119         if (!strcasecmp(a[i + 2], "S") ) Method = 0;
120
121         if (Method != 0) {
122           if (!strcasecmp(a[i + 2], "A")) {
123             Method = 2;
124             NbArg++;
125           }
126         }
127
128       }
129     }
130
131     // compute the chamfer and display the result
132     if (aMCh.NbContours() == 0 )
133       {
134         //cout<<"No suitable edges to chamfer"<<endl;
135         di<<"No suitable edges to chamfer"<<"\n";
136         return 1;
137       }
138     else aMCh.Build();
139
140     if (aMCh.IsDone()){
141       DBRep::Set(a[1],aMCh);
142       return 0;
143     }
144     else {
145       //cout<<"compute of chamfer failed"<<endl;
146       di<<"compute of chamfer failed"<<"\n";
147       return 1;
148     }
149   }
150       return 0;
151
152 }
153
154
155
156 //=======================================================================
157 //function : ChamferCommands
158 //purpose  : 
159 //=======================================================================
160
161 void  BRepTest::ChamferCommands(Draw_Interpretor& theCommands)
162 {
163   static Standard_Boolean done = Standard_False;
164   if (done) return;
165   done = Standard_True;
166
167   DBRep::BasicCommands(theCommands);
168
169   const char* g = "TOPOLOGY Fillet construction commands";
170
171 //  theCommands.Add("chamf",
172 //                "chamf newname shape edge1 face1 dist1_1 dist1_2 edge2 face2 dist2_1 dist2_2 ... ",__FILE__,chamfer,g);
173
174   theCommands.Add("chamf",
175                   "for help call chamf without arguments",__FILE__,chamfer,g);
176
177 }