7fd59977 |
1 | #include "stdafx.h" |
2 | |
3 | #include "Sample2D_Face.h" |
4 | |
5c1f974e |
5 | IMPLEMENT_STANDARD_HANDLE(Sample2D_Face,AIS_InteractiveObject) |
6 | IMPLEMENT_STANDARD_RTTIEXT(Sample2D_Face,AIS_InteractiveObject) |
7fd59977 |
7 | |
5c1f974e |
8 | Sample2D_Face::Sample2D_Face (const TopoDS_Shape& theFace) |
9 | :AIS_InteractiveObject() |
7fd59977 |
10 | { |
5c1f974e |
11 | myshape = theFace; |
12 | myFORWARDColor = Quantity_NOC_BLUE1; |
13 | myREVERSEDColor = Quantity_NOC_YELLOW; |
14 | myINTERNALColor = Quantity_NOC_RED1; |
15 | myEXTERNALColor = Quantity_NOC_MAGENTA1; |
16 | myWidthIndex = 1; |
17 | myTypeIndex = 1; |
18 | |
19 | myForwardNum=0; |
20 | myReversedNum=0; |
21 | myInternalNum=0; |
22 | myExternalNum=0; |
23 | |
24 | SetAutoHilight(Standard_False); |
25 | |
26 | FillData(Standard_True); |
7fd59977 |
27 | } |
28 | |
5c1f974e |
29 | void Sample2D_Face::DrawMarker(const Handle(Geom2d_TrimmedCurve)& theCurve, const Handle(Prs3d_Presentation)& thePresentation) |
7fd59977 |
30 | { |
5c1f974e |
31 | Standard_Real aCenterParam = (theCurve->FirstParameter()+theCurve->LastParameter())/2; |
32 | gp_Pnt2d p; |
33 | gp_Vec2d v; |
34 | theCurve->D1(aCenterParam,p,v); |
35 | if (v.Magnitude() > gp::Resolution()) |
36 | { |
37 | gp_Vec aDir(v.X(),v.Y(),0.); |
38 | gp_Pnt aPoint(p.X(),p.Y(),0.); |
39 | aDir.Normalize(); |
40 | aDir.Reverse(); |
41 | gp_Dir aZ(0,0,1); |
42 | gp_Pnt aLeft(aPoint.Translated(aDir.Rotated(gp_Ax1(aPoint,aZ), M_PI/6)*5)) ; |
43 | gp_Pnt aRight(aPoint.Translated(aDir.Rotated(gp_Ax1(aPoint,aZ), M_PI*11/6)*5)); |
44 | |
45 | Handle(Graphic3d_ArrayOfPolylines) anArrow = new Graphic3d_ArrayOfPolylines(3); |
46 | anArrow->AddVertex(aLeft); |
47 | anArrow->AddVertex(aPoint); |
48 | anArrow->AddVertex(aRight); |
49 | |
50 | Prs3d_Root::CurrentGroup(thePresentation)->AddPrimitiveArray(anArrow); |
51 | } |
52 | } |
7fd59977 |
53 | |
5c1f974e |
54 | void Sample2D_Face::FillData(Standard_Boolean isSizesRecompute) |
55 | { |
56 | if(myshape.IsNull() || myshape.ShapeType()!=TopAbs_FACE) return; |
57 | |
58 | Standard_Real f,l; |
59 | TopExp_Explorer ex; |
60 | TopoDS_Face aFace = TopoDS::Face(myshape); |
61 | |
62 | //count number of verteces and bounds in primitive arrays |
63 | if(isSizesRecompute) |
64 | { |
65 | mySeq_FORWARD.Clear(); |
66 | mySeq_REVERSED.Clear(); |
67 | mySeq_INTERNAL.Clear(); |
68 | mySeq_EXTERNAL.Clear(); |
69 | |
70 | myshape.Orientation(TopAbs_FORWARD); |
71 | ex.Init(myshape,TopAbs_EDGE); |
72 | while (ex.More()) |
73 | { |
74 | BRepAdaptor_Curve2d aCurveOnEdge(TopoDS::Edge(ex.Current()),aFace); |
75 | GCPnts_QuasiUniformDeflection anEdgeDistrib(aCurveOnEdge,1.e-2); |
76 | if(anEdgeDistrib.IsDone()) |
77 | switch (ex.Current().Orientation()) |
78 | { |
79 | case TopAbs_FORWARD: { |
80 | myForwardNum+=anEdgeDistrib.NbPoints(); |
81 | myForwardBounds++; |
82 | break; |
83 | } |
84 | case TopAbs_REVERSED: { |
85 | myReversedNum+=anEdgeDistrib.NbPoints(); |
86 | myReversedBounds++; |
87 | break; |
88 | } |
89 | case TopAbs_INTERNAL: { |
90 | myInternalNum+=anEdgeDistrib.NbPoints(); |
91 | myInternalBounds++; |
92 | break; |
93 | } |
94 | case TopAbs_EXTERNAL: { |
95 | myExternalNum+=anEdgeDistrib.NbPoints(); |
96 | myExternalBounds++; |
97 | break; |
98 | } |
99 | default : break; |
100 | }//end switch |
101 | ex.Next(); |
102 | } |
103 | } |
104 | |
105 | myForwardArray = new Graphic3d_ArrayOfPolylines(myForwardNum,myForwardBounds); |
106 | myReversedArray = new Graphic3d_ArrayOfPolylines(myReversedNum, myReversedBounds); |
107 | myInternalArray = new Graphic3d_ArrayOfPolylines(myInternalNum, myInternalBounds); |
108 | myExternalArray = new Graphic3d_ArrayOfPolylines(myExternalNum, myExternalBounds); |
109 | |
110 | //fill primitive arrays |
111 | ex.Init(myshape,TopAbs_EDGE); |
112 | while (ex.More()) { |
113 | const Handle(Geom2d_Curve) aCurve = BRep_Tool::CurveOnSurface |
114 | (TopoDS::Edge(ex.Current()),aFace,f,l); |
115 | |
116 | Handle(Geom2d_TrimmedCurve) aTrimmedCurve = new Geom2d_TrimmedCurve(aCurve,f,l); |
117 | TopoDS_Edge CurrentEdge= TopoDS::Edge(ex.Current()); |
118 | if(!aTrimmedCurve.IsNull()) |
119 | { |
120 | Handle(Geom_Curve) aCurve3d = GeomLib::To3d(gp_Ax2(gp_Pnt(0,0,0),gp_Dir(0,0,1)),aTrimmedCurve); |
121 | BRepAdaptor_Curve2d aCurveOnEdge(CurrentEdge,aFace); |
122 | GCPnts_QuasiUniformDeflection anEdgeDistrib(aCurveOnEdge,1.e-2); |
123 | if(anEdgeDistrib.IsDone()) |
124 | { |
125 | switch (ex.Current().Orientation()) |
126 | { |
127 | case TopAbs_FORWARD: { |
128 | myForwardArray->AddBound(anEdgeDistrib.NbPoints()); |
129 | for(Standard_Integer i=1;i<=anEdgeDistrib.NbPoints();++i) |
130 | { |
131 | myForwardArray->AddVertex(anEdgeDistrib.Value(i)); |
132 | } |
133 | if(isSizesRecompute) |
134 | mySeq_FORWARD.Append(aCurve3d); |
135 | break; |
136 | } |
137 | case TopAbs_REVERSED: { |
138 | myReversedArray->AddBound(anEdgeDistrib.NbPoints()); |
139 | for(Standard_Integer i=1;i<=anEdgeDistrib.NbPoints();++i) |
140 | { |
141 | myReversedArray->AddVertex(anEdgeDistrib.Value(i)); |
142 | } |
143 | if(isSizesRecompute) |
144 | mySeq_REVERSED.Append(aCurve3d); |
145 | break; |
146 | } |
147 | case TopAbs_INTERNAL: { |
148 | myInternalArray->AddBound(anEdgeDistrib.NbPoints()); |
149 | for(Standard_Integer i=1;i<=anEdgeDistrib.NbPoints();++i) |
150 | { |
151 | myInternalArray->AddVertex(anEdgeDistrib.Value(i)); |
152 | } |
153 | if(isSizesRecompute) |
154 | mySeq_INTERNAL.Append(aCurve3d); |
155 | break; |
156 | } |
157 | case TopAbs_EXTERNAL: { |
158 | myExternalArray->AddBound(anEdgeDistrib.NbPoints()); |
159 | for(Standard_Integer i=1;i<=anEdgeDistrib.NbPoints();++i) |
160 | { |
161 | myExternalArray->AddVertex(anEdgeDistrib.Value(i)); |
162 | } |
163 | if(isSizesRecompute) |
164 | mySeq_EXTERNAL.Append(aCurve3d); |
165 | break; |
166 | } |
167 | default : break; |
168 | }//end switch |
169 | } |
170 | }//end else |
171 | ex.Next(); |
172 | } |
173 | } |
7fd59977 |
174 | |
5c1f974e |
175 | void Sample2D_Face::Compute ( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager, |
176 | const Handle(Prs3d_Presentation)& thePresentation, |
177 | const Standard_Integer theMode) |
178 | { |
179 | thePresentation->Clear(); |
180 | myDrawer->SetWireDraw(1); |
181 | |
182 | if(myshape.IsNull() || myshape.ShapeType()!=TopAbs_FACE) return; |
183 | |
184 | Handle(Graphic3d_AspectLine3d) aLineAspect_FORWARD = |
185 | new Graphic3d_AspectLine3d(myFORWARDColor, Aspect_TOL_SOLID,1); |
186 | Handle(Graphic3d_AspectLine3d) aLineAspect_REVERSED = |
187 | new Graphic3d_AspectLine3d(myREVERSEDColor, Aspect_TOL_SOLID,1); |
188 | Handle(Graphic3d_AspectLine3d) aLineAspect_INTERNAL = |
189 | new Graphic3d_AspectLine3d(myINTERNALColor, Aspect_TOL_SOLID,1); |
190 | Handle(Graphic3d_AspectLine3d) aLineAspect_EXTERNAL = |
191 | new Graphic3d_AspectLine3d(myEXTERNALColor, Aspect_TOL_SOLID,1); |
192 | |
193 | Standard_Real f,l; |
194 | TopoDS_Face aFace = TopoDS::Face(myshape); |
195 | //estimating number of verteces in primitive arrays |
196 | TopExp_Explorer ex(myshape,TopAbs_EDGE); |
197 | ex.Init(myshape,TopAbs_EDGE); |
198 | while (ex.More()) |
199 | { |
200 | const Handle(Geom2d_Curve) aCurve = BRep_Tool::CurveOnSurface |
201 | (TopoDS::Edge(ex.Current()),aFace,f,l); |
202 | |
203 | Handle(Geom2d_TrimmedCurve) aTrimmedCurve = new Geom2d_TrimmedCurve(aCurve,f,l); |
204 | TopoDS_Edge aCurrentEdge= TopoDS::Edge(ex.Current()); |
205 | //make a 3D curve from 2D trimmed curve to display it |
206 | Handle(Geom_Curve) aCurve3d = GeomLib::To3d(gp_Ax2(gp_Pnt(0,0,0),gp_Dir(0,0,1)),aTrimmedCurve); |
207 | //make distribution of points |
208 | BRepAdaptor_Curve2d aCurveOnEdge(aCurrentEdge,aFace); |
209 | GCPnts_QuasiUniformDeflection anEdgeDistrib(aCurveOnEdge,1.e-2); |
210 | if(anEdgeDistrib.IsDone()) |
211 | { |
212 | switch (ex.Current().Orientation()) |
213 | { |
214 | case TopAbs_FORWARD: { |
215 | |
216 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_FORWARD); |
217 | DrawMarker(aTrimmedCurve, thePresentation); |
218 | break; |
219 | } |
220 | case TopAbs_REVERSED: { |
221 | |
222 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_REVERSED); |
223 | DrawMarker(aTrimmedCurve, thePresentation); |
224 | break; |
225 | } |
226 | case TopAbs_INTERNAL: { |
227 | |
228 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_INTERNAL); |
229 | DrawMarker(aTrimmedCurve, thePresentation); |
230 | |
231 | mySeq_INTERNAL.Append(aCurve3d); |
232 | break; |
233 | } |
234 | case TopAbs_EXTERNAL: { |
235 | |
236 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_EXTERNAL); |
237 | DrawMarker(aTrimmedCurve, thePresentation); |
238 | break; |
239 | } |
240 | default : break; |
241 | }//end switch |
242 | } |
243 | ex.Next(); |
244 | } |
245 | //add all primitives to the presentation |
246 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_FORWARD); |
247 | Prs3d_Root::CurrentGroup(thePresentation)->AddPrimitiveArray(myForwardArray); |
7fd59977 |
248 | |
5c1f974e |
249 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_REVERSED); |
250 | Prs3d_Root::CurrentGroup(thePresentation)->AddPrimitiveArray(myReversedArray); |
7fd59977 |
251 | |
5c1f974e |
252 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_INTERNAL); |
253 | Prs3d_Root::CurrentGroup(thePresentation)->AddPrimitiveArray(myInternalArray); |
7fd59977 |
254 | |
5c1f974e |
255 | Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect(aLineAspect_EXTERNAL); |
256 | Prs3d_Root::CurrentGroup(thePresentation)->AddPrimitiveArray(myExternalArray); |
257 | } |
7fd59977 |
258 | |
5c1f974e |
259 | //Method for advanced customizable selection of picked object |
260 | void Sample2D_Face::HilightSelected |
261 | ( const Handle(PrsMgr_PresentationManager3d)& thePM, |
262 | const SelectMgr_SequenceOfOwner& theOwners) |
263 | { |
264 | Handle( Prs3d_Presentation ) aSelectionPrs; |
7fd59977 |
265 | |
5c1f974e |
266 | aSelectionPrs = GetSelectPresentation( thePM ); |
7fd59977 |
267 | |
5c1f974e |
268 | Handle(Graphic3d_AspectLine3d) aLineAspect = |
269 | new Graphic3d_AspectLine3d(Quantity_NOC_ANTIQUEWHITE, Aspect_TOL_SOLID,2); |
270 | if( HasPresentation() ) |
271 | aSelectionPrs->SetTransformPersistence( Presentation()->TransformPersistenceMode(), Presentation()->TransformPersistencePoint() ); |
7fd59977 |
272 | |
5c1f974e |
273 | Standard_Integer aLength = theOwners.Length(); |
274 | Handle (SelectMgr_EntityOwner) anOwner; |
7fd59977 |
275 | |
5c1f974e |
276 | aSelectionPrs->Clear(); |
277 | FillData(); |
7fd59977 |
278 | |
5c1f974e |
279 | Prs3d_Root::NewGroup ( aSelectionPrs ); |
280 | Handle (Graphic3d_Group) aSelectGroup = Prs3d_Root::CurrentGroup ( aSelectionPrs); |
7fd59977 |
281 | |
5c1f974e |
282 | for(Standard_Integer i=1; i<=aLength; ++i) |
283 | { |
284 | anOwner = theOwners.Value(i); |
285 | //check priority of owner to add primitives in one of array |
286 | //containing primitives with certain type of orientation |
287 | switch(anOwner->Priority()) |
288 | { |
289 | case 7: |
290 | { |
291 | //add to objects with forward orientation |
292 | aSelectGroup->SetGroupPrimitivesAspect(aLineAspect); |
293 | aSelectGroup->AddPrimitiveArray(myForwardArray); |
294 | break; |
295 | } |
296 | case 6: |
297 | { |
298 | //add to objects with reversed orientation |
299 | aSelectGroup->SetGroupPrimitivesAspect(aLineAspect); |
300 | aSelectGroup->AddPrimitiveArray(myReversedArray); |
301 | break; |
302 | } |
303 | case 5: |
7fd59977 |
304 | { |
5c1f974e |
305 | //add to objects with internal orientation |
306 | aSelectGroup->SetGroupPrimitivesAspect(aLineAspect); |
307 | aSelectGroup->AddPrimitiveArray(myInternalArray); |
308 | break; |
309 | } |
310 | case 4: |
311 | { |
312 | //add to objects with external orientation |
313 | aSelectGroup->SetGroupPrimitivesAspect(aLineAspect); |
314 | aSelectGroup->AddPrimitiveArray(myExternalArray); |
315 | break; |
316 | } |
317 | } |
7fd59977 |
318 | |
5c1f974e |
319 | } |
320 | aSelectionPrs->Display(); |
7fd59977 |
321 | |
5c1f974e |
322 | } |
7fd59977 |
323 | |
5c1f974e |
324 | void Sample2D_Face::ClearSelected () |
325 | { |
326 | Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL ); |
327 | if( !aSelectionPrs.IsNull() ) |
328 | aSelectionPrs->Clear(); |
329 | } |
7fd59977 |
330 | |
331 | |
7fd59977 |
332 | |
5c1f974e |
333 | //Method for advanced customizable highlighting of picked object |
334 | void Sample2D_Face::HilightOwnerWithColor ( const Handle(PrsMgr_PresentationManager3d)& thePM, |
335 | const Quantity_NameOfColor theColor, |
336 | const Handle(SelectMgr_EntityOwner)& theOwner) |
337 | { |
338 | Handle( Prs3d_Presentation ) aHighlightPrs; |
339 | aHighlightPrs = GetHilightPresentation( thePM ); |
340 | if( HasPresentation() ) |
341 | aHighlightPrs->SetTransformPersistence( Presentation()->TransformPersistenceMode(), Presentation()->TransformPersistencePoint() ); |
342 | if(theOwner.IsNull()) |
343 | return; |
344 | aHighlightPrs->Clear(); |
345 | FillData(); |
346 | |
347 | //Direct highlighting |
348 | Prs3d_Root::NewGroup ( aHighlightPrs ); |
349 | Handle (Graphic3d_Group) aHilightGroup = Prs3d_Root::CurrentGroup(aHighlightPrs); |
350 | Handle(Graphic3d_AspectLine3d) aLineAspect = |
351 | new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID,2); |
352 | switch(theOwner->Priority()) |
353 | { |
354 | case 7: |
355 | { |
356 | aHilightGroup->SetGroupPrimitivesAspect(aLineAspect); |
357 | aHilightGroup->AddPrimitiveArray(myForwardArray); |
358 | break; |
7fd59977 |
359 | } |
5c1f974e |
360 | case 6: |
361 | { |
362 | aHilightGroup->SetGroupPrimitivesAspect(aLineAspect); |
363 | aHilightGroup->AddPrimitiveArray(myReversedArray); |
364 | break; |
365 | } |
366 | case 5: |
367 | { |
368 | aHilightGroup->SetGroupPrimitivesAspect(aLineAspect); |
369 | aHilightGroup->AddPrimitiveArray(myInternalArray); |
370 | break; |
371 | } |
372 | case 4: |
373 | { |
374 | aHilightGroup->SetGroupPrimitivesAspect(aLineAspect); |
375 | aHilightGroup->AddPrimitiveArray(myExternalArray); |
376 | break; |
377 | } |
378 | } |
379 | if( thePM->IsImmediateModeOn() ) |
380 | thePM->AddToImmediateList( aHighlightPrs ); |
7fd59977 |
381 | |
7fd59977 |
382 | } |
383 | |
384 | |
385 | |
5c1f974e |
386 | //for auto select |
387 | void Sample2D_Face::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, |
388 | const Standard_Integer theMode) |
389 | { |
390 | if(myshape.IsNull()) |
391 | return; |
392 | |
393 | if(mySeq_FORWARD.Length()==0 && |
394 | mySeq_REVERSED.Length()==0 && |
395 | mySeq_INTERNAL.Length()==0 && |
396 | mySeq_EXTERNAL.Length()==0) return; |
397 | |
398 | //create entity owner for every part of the face |
399 | //set different priorities for primitives of different orientation |
400 | Handle(SelectMgr_EntityOwner) anOwner_Forward = new SelectMgr_EntityOwner(this,7); |
401 | Handle(SelectMgr_EntityOwner) anOwner_Reversed = new SelectMgr_EntityOwner(this,6); |
402 | Handle(SelectMgr_EntityOwner) anOwner_Internal = new SelectMgr_EntityOwner(this,5); |
403 | Handle(SelectMgr_EntityOwner) anOwner_External = new SelectMgr_EntityOwner(this,4); |
404 | |
405 | //create a sensitive for every part |
406 | Handle(Select3D_SensitiveGroup) aForwardGroup = new Select3D_SensitiveGroup(anOwner_Forward); |
407 | Handle(Select3D_SensitiveGroup) aReversedGroup = new Select3D_SensitiveGroup(anOwner_Reversed); |
408 | Handle(Select3D_SensitiveGroup) aInternalGroup = new Select3D_SensitiveGroup(anOwner_Internal); |
409 | Handle(Select3D_SensitiveGroup) aExternalGroup = new Select3D_SensitiveGroup(anOwner_External); |
410 | |
411 | Standard_Integer aLength = mySeq_FORWARD.Length(); |
412 | for(Standard_Integer i=1;i<=aLength;++i) |
413 | { |
414 | Handle(Select3D_SensitiveCurve) aSensitveCurve = new Select3D_SensitiveCurve(anOwner_Forward,mySeq_FORWARD(i)); |
415 | aForwardGroup->Add(aSensitveCurve); |
416 | } |
417 | theSelection->Add(aForwardGroup); |
418 | |
419 | aLength = mySeq_REVERSED.Length(); |
420 | for(Standard_Integer i=1;i<=aLength;++i) |
421 | { |
422 | Handle(Select3D_SensitiveCurve) aSensitveCurve = new Select3D_SensitiveCurve(anOwner_Reversed,mySeq_REVERSED(i)); |
423 | aReversedGroup->Add(aSensitveCurve); |
424 | } |
425 | theSelection->Add(aReversedGroup); |
426 | |
427 | aLength = mySeq_INTERNAL.Length(); |
428 | for(Standard_Integer i=1;i<=aLength;++i) |
429 | { |
430 | Handle(Select3D_SensitiveCurve) aSensitveCurve = new Select3D_SensitiveCurve(anOwner_Internal,mySeq_INTERNAL(i)); |
431 | aInternalGroup->Add(aSensitveCurve); |
432 | } |
433 | theSelection->Add(aInternalGroup); |
434 | |
435 | aLength = mySeq_EXTERNAL.Length(); |
436 | for(Standard_Integer i=1;i<=aLength;++i) |
437 | { |
438 | Handle(Select3D_SensitiveCurve) aSensitveCurve = new Select3D_SensitiveCurve(anOwner_External,mySeq_EXTERNAL(i)); |
439 | aExternalGroup->Add(aSensitveCurve); |
440 | } |
441 | theSelection->Add(aExternalGroup); |
442 | } |
7fd59977 |
443 | |