0023821: Improve qmake project files for Qt samples
[occt.git] / samples / mfc / standard / Common / Primitive / Sample2D_Face.cpp
1 #include "stdafx.h"
2
3 #include "Sample2D_Face.h"
4
5 IMPLEMENT_STANDARD_HANDLE(Sample2D_Face,AIS2D_InteractiveObject)
6 IMPLEMENT_STANDARD_RTTIEXT(Sample2D_Face,AIS2D_InteractiveObject)
7
8 Sample2D_Face::Sample2D_Face (TopoDS_Face& aFace)
9     :AIS2D_InteractiveObject(),myFace(aFace)
10
11 {
12 myMarkerIndex        = 1;
13 myMarkerWidth        = 5;
14 myMarkerHeight       = 5;
15 myFORWARDColorIndex  = 2;
16 myREVERSEDColorIndex = 3;
17 myINTERNALColorIndex = 4;
18 myEXTERNALColorIndex = 5;
19 myWidthIndex         = 1;
20 myTypeIndex          = 1;
21 }
22
23
24 #include "TopExp_Explorer.hxx"
25 #include "BRep_Tool.hxx"
26 #include "TopoDS.hxx"
27 #include "TopoDS_Edge.hxx"
28 #include "TopoDS.hxx"
29 #include "TopoDS_Vertex.hxx"
30 #include "TopExp.hxx"
31 #include "Graphic2d_Marker.hxx"
32 #include "Graphic2d_PolylineMarker.hxx"
33 #include "gp.hxx"
34 #include "Prs2d_AspectLine.hxx"
35 #include "Graphic2d_TypeOfPolygonFilling.hxx"
36
37 void Sample2D_Face::SetContext(const Handle(AIS2D_InteractiveContext)& theContext) 
38 {
39     if(myFace.IsNull() || myFace.ShapeType()!=TopAbs_FACE) return;
40
41     AIS2D_InteractiveObject::SetContext(theContext);    
42
43     Handle(Prs2d_AspectLine) aLineAspect_FORWARD = new Prs2d_AspectLine;  
44     
45         aLineAspect_FORWARD->SetTypeOfFill(Graphic2d_TOPF_EMPTY);
46     aLineAspect_FORWARD->SetTypeIndex(myTypeIndex);
47     aLineAspect_FORWARD->SetWidthIndex(myWidthIndex);
48     aLineAspect_FORWARD->SetColorIndex(myFORWARDColorIndex);
49
50         Handle(Prs2d_AspectLine) aLineAspect_REVERSED = new Prs2d_AspectLine;  
51     aLineAspect_REVERSED->SetTypeOfFill(Graphic2d_TOPF_EMPTY);
52     aLineAspect_REVERSED->SetTypeIndex(myTypeIndex);
53     aLineAspect_REVERSED->SetWidthIndex(myWidthIndex);
54     aLineAspect_REVERSED->SetColorIndex(myREVERSEDColorIndex);
55
56         Handle(Prs2d_AspectLine) aLineAspect_INTERNAL = new Prs2d_AspectLine;  
57     aLineAspect_INTERNAL->SetTypeOfFill(Graphic2d_TOPF_EMPTY);
58     aLineAspect_INTERNAL->SetTypeIndex(myTypeIndex);
59     aLineAspect_INTERNAL->SetWidthIndex(myWidthIndex);
60     aLineAspect_INTERNAL->SetColorIndex(myINTERNALColorIndex);
61
62         Handle(Prs2d_AspectLine) aLineAspect_EXTERNAL = new Prs2d_AspectLine;  
63     aLineAspect_EXTERNAL->SetTypeOfFill(Graphic2d_TOPF_EMPTY);
64     aLineAspect_EXTERNAL->SetTypeIndex(myTypeIndex);
65     aLineAspect_EXTERNAL->SetWidthIndex(myWidthIndex);
66     aLineAspect_EXTERNAL->SetColorIndex(myEXTERNALColorIndex);
67
68     // pcurves of a face
69
70     Handle(Graphic2d_SetOfCurves) segment_FORWARD;
71     segment_FORWARD = new Graphic2d_SetOfCurves(this); 
72
73     Handle(Graphic2d_SetOfCurves) segment_REVERSED;
74     segment_REVERSED = new Graphic2d_SetOfCurves(this); 
75
76     Handle(Graphic2d_SetOfCurves) segment_INTERNAL;
77     segment_INTERNAL = new Graphic2d_SetOfCurves(this); 
78
79     Handle(Graphic2d_SetOfCurves) segment_EXTERNAL;
80     segment_EXTERNAL = new Graphic2d_SetOfCurves(this); 
81
82     Standard_Real f,l;
83     Standard_Integer i = 1;
84     myFace.Orientation(TopAbs_FORWARD);
85     TopExp_Explorer ex(myFace,TopAbs_EDGE);
86     while (ex.More()) {
87       const Handle(Geom2d_Curve) Curve = BRep_Tool::CurveOnSurface
88                  (TopoDS::Edge(ex.Current()),TopoDS::Face(myFace),f,l);
89
90       Handle(Geom2d_TrimmedCurve) c = new Geom2d_TrimmedCurve(Curve,f,l);
91
92       if (c.IsNull())
93       {
94         // null curve --> EDGE have a null lenght
95         // --> get first and last Vertices
96         TopoDS_Edge CurrentEdge= TopoDS::Edge(ex.Current());
97
98         TopoDS_Vertex FirstVertex = TopExp::FirstVertex(CurrentEdge);
99         TopoDS_Vertex LastVertex  = TopExp::LastVertex(CurrentEdge);
100
101         if (FirstVertex != LastVertex)
102           Standard_Failure::Raise("Null Edge pcurve But different vertices");
103
104          gp_Pnt aPnt  = BRep_Tool::Pnt(FirstVertex);
105
106
107         Handle(Graphic2d_Marker) aMarker= 
108            new Graphic2d_Marker(this,myMarkerIndex,
109                                 aPnt.X() ,aPnt.Y(),
110                                 myMarkerWidth,myMarkerHeight,0);
111
112         switch (ex.Current().Orientation())
113         {
114           case TopAbs_FORWARD:  aMarker->SetColorIndex (myFORWARDColorIndex);   break;
115           case TopAbs_REVERSED: aMarker->SetColorIndex (myREVERSEDColorIndex);  break;
116           case TopAbs_INTERNAL: aMarker->SetColorIndex (myINTERNALColorIndex);  break;
117           case TopAbs_EXTERNAL: aMarker->SetColorIndex (myEXTERNALColorIndex);  break;
118           default : break;
119         }
120         ex.Next();
121       }
122       else
123       {
124         gp_Pnt2d p1,p2;
125         gp_Vec2d v;
126         c->D1(l,p1,v);
127         Graphic2d_Array1OfVertex aListVertex(1,3);
128         if (v.Magnitude() > gp::Resolution()) {
129             Standard_Real L = 2;
130             Standard_Real H = 1;
131             gp_Dir2d d(v);
132             p2.SetCoord(- L*d.X() - H*d.Y(),- L*d.Y() + H*d.X());
133             aListVertex(1)=Graphic2d_Vertex(p2.X(),p2.Y());
134             
135             p2.SetCoord(- L*d.X() + H*d.Y(),- L*d.Y() - H*d.X());
136             aListVertex(2)=Graphic2d_Vertex(0.,0.);
137             aListVertex(3)=Graphic2d_Vertex(p2.X(),p2.Y()); 
138         }
139
140         Handle(Graphic2d_PolylineMarker) aMarker= 
141           new Graphic2d_PolylineMarker(this,p1.X(),p1.Y(),aListVertex);
142
143                 // Display the Curve
144
145                 switch (ex.Current().Orientation())
146         {
147                 case TopAbs_FORWARD: {
148              segment_FORWARD->Add(c); 
149              SetAspect(aLineAspect_FORWARD, aMarker);  
150              break;
151                 }
152                 case TopAbs_REVERSED: {
153              segment_REVERSED->Add(c); 
154              SetAspect(aLineAspect_REVERSED, aMarker); 
155              break;
156                 }
157                 case TopAbs_INTERNAL: {
158              segment_INTERNAL->Add(c); 
159              SetAspect(aLineAspect_INTERNAL, aMarker);
160              break;
161                 }
162                 case TopAbs_EXTERNAL: {
163              segment_EXTERNAL->Add(c); 
164              SetAspect(aLineAspect_EXTERNAL, aMarker);
165              break;
166                 }
167           default : break;
168         }
169       }
170       ex.Next();
171     }
172
173
174
175         SetAspect(aLineAspect_FORWARD, segment_FORWARD);
176         SetAspect(aLineAspect_REVERSED, segment_REVERSED);
177     SetAspect(aLineAspect_INTERNAL, segment_INTERNAL);
178         SetAspect(aLineAspect_EXTERNAL, segment_EXTERNAL);
179 }
180
181
182
183