0025467: Visualization - Possibility to remove AIS_LocalContext class
[occt.git] / tools / VInspector / VInspector_Tools.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/VInspector_Tools.hxx>
17
18 #include <AIS_ListIteratorOfListOfInteractive.hxx>
19 #include <AIS_ListOfInteractive.hxx>
20 #include <AIS_Selection.hxx>
21 #include <AIS_Shape.hxx>
22 #include <AIS_Trihedron.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRepTools.hxx>
25 #include <gp_Trsf.hxx>
26 #include <SelectMgr_StateOfSelection.hxx>
27 #include <SelectMgr_TypeOfUpdate.hxx>
28 #include <SelectMgr_TypeOfBVHUpdate.hxx>
29 #include <Standard_Version.hxx>
30 #include <StdSelect_BRepOwner.hxx>
31
32 #include <Standard_WarningsDisable.hxx>
33 #include <QStringList>
34 #include <Standard_WarningsRestore.hxx>
35
36 #include <sstream>
37
38 // =======================================================================
39 // function : GetShapeTypeInfo
40 // purpose :
41 // =======================================================================
42 TCollection_AsciiString VInspector_Tools::GetShapeTypeInfo (const TopAbs_ShapeEnum& theType)
43 {
44   Standard_SStream aSStream;
45   TopAbs::Print (theType, aSStream);
46   return aSStream.str().c_str();
47 }
48
49 // =======================================================================
50 // function : GetPointerInfo
51 // purpose :
52 // =======================================================================
53 TCollection_AsciiString VInspector_Tools::GetPointerInfo (const Handle(Standard_Transient)& thePointer, const bool isShortInfo)
54 {
55   std::ostringstream aPtrStr;
56   aPtrStr << thePointer.operator->();
57   if (!isShortInfo)
58     return aPtrStr.str().c_str();
59
60   TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
61   for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
62   {
63     if (anInfoPtr.Value(aSymbolId) != '0')
64     {
65       anInfoPtr = anInfoPtr.SubString(aSymbolId, anInfoPtr.Length());
66       anInfoPtr.Prepend("0x");
67       return anInfoPtr;
68     }
69   }
70   return aPtrStr.str().c_str();
71 }
72
73 // =======================================================================
74 // function : SelectedOwners
75 // purpose :
76 // =======================================================================
77 int VInspector_Tools::SelectedOwners (const Handle(AIS_InteractiveContext)& theContext,
78                                       const Handle(AIS_InteractiveObject)& theObject,
79                                       const bool theShapeInfoOnly)
80 {
81   QStringList anObjects;
82   if (theContext.IsNull())
83     return 0;
84
85   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
86   for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
87   {
88     Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
89     if (anOwner.IsNull()) // TODO: check why it is possible
90       continue;
91
92     if (!theObject.IsNull())
93     {
94       Handle(AIS_InteractiveObject) anOwnerPresentation =
95                                     Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
96       if (anOwnerPresentation != theObject)
97         continue;
98     }
99     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
100     if (theShapeInfoOnly && BROwnr.IsNull())
101       continue;
102
103     Standard_Transient* anOwnerPtr = anOwner.operator->();
104     if (aSelectedIds.contains ((size_t)anOwnerPtr))
105       continue;
106     aSelectedIds.append ((size_t)anOwnerPtr);
107
108     anObjects.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
109   }
110   return anObjects.size();
111 }
112
113 // =======================================================================
114 // function : IsOwnerSelected
115 // purpose :
116 // =======================================================================
117 bool VInspector_Tools::IsOwnerSelected (const Handle(AIS_InteractiveContext)& theContext,
118                                         const Handle(SelectBasics_EntityOwner)& theOwner)
119 {
120   bool anIsSelected = false;
121   Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (theOwner);
122   for (theContext->InitSelected(); theContext->MoreSelected() && !anIsSelected; theContext->NextSelected())
123     anIsSelected = theContext->SelectedOwner() == anOwner;
124   return anIsSelected;
125 }
126
127 // =======================================================================
128 // function : ContextOwners
129 // purpose :
130 // =======================================================================
131 NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ContextOwners (
132                                                const Handle(AIS_InteractiveContext)& theContext)
133 {
134   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
135   if (theContext.IsNull())
136     return aResultOwners;
137
138   AIS_ListOfInteractive aListOfIO;
139   theContext->DisplayedObjects (aListOfIO);
140   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
141   for (AIS_ListIteratorOfListOfInteractive aIt(aListOfIO); aIt.More(); aIt.Next())
142   {
143     Handle(AIS_InteractiveObject) anIO = aIt.Value();
144     if (anIO.IsNull())
145       continue;
146 #if OCC_VERSION_HEX < 0x070201
147     for (anIO->Init(); anIO->More(); anIO->Next())
148     {
149       Handle(SelectMgr_Selection) aSelection = anIO->CurrentSelection();
150       if (aSelection.IsNull())
151         continue;
152       for (aSelection->Init(); aSelection->More(); aSelection->Next())
153       {
154         Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
155 #else
156     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anIO->Selections()); aSelIter.More(); aSelIter.Next())
157     {
158       Handle(SelectMgr_Selection) aSelection = aSelIter.Value();
159       if (aSelection.IsNull())
160         continue;
161       for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
162       {
163         Handle(SelectMgr_SensitiveEntity) anEntity = aSelEntIter.Value();
164 #endif
165         if (anEntity.IsNull())
166           continue;
167         const Handle(SelectBasics_SensitiveEntity)& aBase = anEntity->BaseSensitive();
168         Handle(SelectBasics_EntityOwner) anOwner = aBase->OwnerId();
169         Standard_Transient* anOwnerPtr = anOwner.operator->();
170         if (aSelectedIds.contains ((size_t)anOwnerPtr))
171           continue;
172         aSelectedIds.append ((size_t)anOwnerPtr);
173         aResultOwners.Append (anOwner);
174       }
175     }
176   }
177   return aResultOwners;
178 }
179
180 // =======================================================================
181 // function : ActiveOwners
182 // purpose :
183 // =======================================================================
184 NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ActiveOwners (
185                                 const Handle(AIS_InteractiveContext)& theContext,
186                                 NCollection_List<Handle(SelectBasics_EntityOwner)>& theEmptySelectableOwners)
187 {
188   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
189
190   // only local context is processed: TODO for global context
191   Handle(AIS_InteractiveContext) aContext = theContext;
192   if (aContext.IsNull())
193     return aResultOwners;
194   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
195   // OCCT BUG:1 - equal pointer owners are appears in the list
196 #if OCC_VERSION_HEX > 0x060901
197   aContext->MainSelector()->ActiveOwners (anActiveOwners);
198 #else
199   anActiveOwners = aContext->MainSelector()->ActiveOwners();
200 #endif
201   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
202   Handle(SelectMgr_EntityOwner) anOwner;
203   for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
204        anOwnersIt.More(); anOwnersIt.Next())
205   {
206     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
207     if (anOwner.IsNull())
208       continue;
209
210     Standard_Transient* anOwnerPtr = anOwner.operator->();
211     if (aSelectedIds.contains ((size_t)anOwnerPtr))
212       continue;
213     aSelectedIds.append ((size_t)anOwnerPtr);
214
215     aResultOwners.Append (anOwner);
216     Handle(SelectMgr_SelectableObject) aSelectable = anOwner->Selectable();
217     if (aSelectable.IsNull() ||
218         !theContext->IsDisplayed(Handle(AIS_InteractiveObject)::DownCast (aSelectable)))
219       theEmptySelectableOwners.Append (anOwner);
220   }
221   return aResultOwners;
222 }
223
224 // =======================================================================
225 // function : AddOrRemoveSelectedShapes
226 // purpose :
227 // =======================================================================
228 void VInspector_Tools::AddOrRemoveSelectedShapes (const Handle(AIS_InteractiveContext)& theContext,
229                                                   const NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
230 {
231   // TODO: the next two rows are to be removed later
232   theContext->UnhilightSelected(false);
233   theContext->ClearSelected(false);
234
235   theContext->UnhilightSelected(Standard_False);
236
237   for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt(theOwners);
238        anOwnersIt.More(); anOwnersIt.Next())
239   {
240     Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
241     theContext->AddOrRemoveSelected (anOwner, Standard_False);
242   }
243   theContext->UpdateCurrentViewer();
244 }
245
246 // =======================================================================
247 // function : AddOrRemovePresentations
248 // purpose :
249 // =======================================================================
250 void VInspector_Tools::AddOrRemovePresentations (const Handle(AIS_InteractiveContext)& theContext,
251                                                  const NCollection_List<Handle(AIS_InteractiveObject)>& thePresentations)
252 {
253   // TODO: the next two rows are to be removed later
254   theContext->UnhilightSelected(false);
255   theContext->ClearSelected(false);
256
257   for (NCollection_List<Handle(AIS_InteractiveObject)>::Iterator anIOIt (thePresentations); anIOIt.More(); anIOIt.Next())
258     theContext->AddOrRemoveSelected (anIOIt.Value(), false);
259
260   theContext->UpdateCurrentViewer();
261 }
262
263 // =======================================================================
264 // function : GetInfo
265 // purpose :
266 // =======================================================================
267 QList<QVariant> VInspector_Tools::GetInfo (Handle(AIS_InteractiveObject)& theObject)
268 {
269   QList<QVariant> anInfo;
270   anInfo.append (theObject->DynamicType()->Name());
271   anInfo.append (VInspector_Tools::GetPointerInfo (theObject, true).ToCString());
272
273   Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (theObject);
274   if (aShapeIO.IsNull())
275     return anInfo;
276
277   const TopoDS_Shape& aShape = aShapeIO->Shape();
278   if (!aShape.IsNull())
279     anInfo.append (VInspector_Tools::GetShapeTypeInfo (aShape.ShapeType()).ToCString());
280
281   return anInfo;
282 }
283
284 // =======================================================================
285 // function : GetHighlightInfo
286 // purpose :
287 // =======================================================================
288 QList<QVariant> VInspector_Tools::GetHighlightInfo (const Handle(AIS_InteractiveContext)& theContext)
289 {
290   QList<QVariant> aValues;
291   if (theContext.IsNull())
292     return aValues;
293
294   QStringList aSelectedNames;
295   QStringList aSelectedPointers;
296   QStringList aSelectedTypes;
297   QStringList aSelectedOwners;
298   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
299   for (theContext->InitDetected(); theContext->MoreDetected(); theContext->NextDetected())
300   {
301     Handle(SelectMgr_EntityOwner) anOwner = theContext->DetectedOwner();
302     if (anOwner.IsNull())
303       continue;
304     Standard_Transient* anOwnerPtr = anOwner.operator->();
305     if (aSelectedIds.contains ((size_t)anOwnerPtr))
306       continue;
307     aSelectedIds.append ((size_t)anOwnerPtr);
308     Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
309     if (anIO.IsNull())
310       continue;
311     QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
312     if (anIOInfo.size() == 3)
313     {
314       aSelectedNames.append (anIOInfo[0].toString());
315       aSelectedPointers.append (anIOInfo[1].toString());
316       aSelectedTypes.append (anIOInfo[2].toString());
317     }
318     aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
319   }
320   aValues.append (aSelectedNames.join (", "));
321   aValues.append (aSelectedPointers.join (", "));
322   aValues.append (aSelectedTypes.join (", "));
323   aValues.append (aSelectedOwners.join (", "));
324
325   return aValues;
326 }
327
328 // =======================================================================
329 // function : GetSelectedInfo
330 // purpose :
331 // =======================================================================
332 QList<QVariant> VInspector_Tools::GetSelectedInfo (const Handle(AIS_InteractiveContext)& theContext)
333 {
334   QList<QVariant> aValues;
335   if (theContext.IsNull())
336     return aValues;
337
338   QStringList aSelectedNames;
339   QStringList aSelectedPointers;
340   QStringList aSelectedTypes;
341   QStringList aSelectedOwners;
342   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
343   for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
344   {
345     Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
346     if (anOwner.IsNull())
347       continue;
348     Standard_Transient* anOwnerPtr = anOwner.operator->();
349     if (aSelectedIds.contains ((size_t)anOwnerPtr))
350       continue;
351     aSelectedIds.append ((size_t)anOwnerPtr);
352     Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
353     if (anIO.IsNull())
354       continue;
355
356     QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
357     if (anIOInfo.size() == 3)
358     {
359       aSelectedNames.append (anIOInfo[0].toString());
360       aSelectedPointers.append (anIOInfo[1].toString());
361       aSelectedTypes.append (anIOInfo[2].toString());
362     }
363     aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
364   }
365   aValues.append (aSelectedNames.join (", "));
366   aValues.append (aSelectedPointers.join (", "));
367   aValues.append (aSelectedTypes.join (", "));
368   aValues.append (aSelectedOwners.join (", "));
369   return aValues;
370 }
371
372 // =======================================================================
373 // function : GetSelectedInfoPointers
374 // purpose :
375 // =======================================================================
376 QString VInspector_Tools::GetSelectedInfoPointers (const Handle(AIS_InteractiveContext)& theContext)
377 {
378   QList<QVariant> aSelectedInfo = VInspector_Tools::GetSelectedInfo (theContext);
379   return aSelectedInfo.size() > 2 ? aSelectedInfo[1].toString() : QString();
380 }
381
382 // =======================================================================
383 // function : ToName
384 // purpose :
385 // =======================================================================
386 TCollection_AsciiString VInspector_Tools::ToName (const VInspector_SelectionType theType, const int theValue)
387 {
388   switch (theType)
389   {
390     case VInspector_SelectionType_TypeOfUpdate:
391     {
392       switch (theValue)
393       {
394         case SelectMgr_TOU_Full:    return "Full";
395         case SelectMgr_TOU_Partial: return "Partial";
396         case SelectMgr_TOU_None:    return "None";
397         default: break;
398       }
399     }
400     break;
401     case VInspector_SelectionType_StateOfSelection:
402     {
403       switch (theValue)
404       {
405         case SelectMgr_SOS_Activated:   return "Activated";
406         case SelectMgr_SOS_Deactivated: return "Deactivated";
407         case SelectMgr_SOS_Sleeping:    return "Sleeping";
408         case SelectMgr_SOS_Any:         return "Any";
409         case SelectMgr_SOS_Unknown:     return "Unknown";
410         default: break;
411       }
412     }
413     break;
414     case VInspector_SelectionType_TypeOfBVHUpdate:
415     {
416       switch (theValue)
417       {
418         case SelectMgr_TBU_Add:        return "Add";
419         case SelectMgr_TBU_Remove:     return "Remove";
420         case SelectMgr_TBU_Renew:      return "Renew";
421         case SelectMgr_TBU_Invalidate: return "Invalidate";
422         case SelectMgr_TBU_None:       return "None";
423         default: break;
424       }
425     }
426     default: break;
427   }
428   return "";
429 }
430
431 // =======================================================================
432 // function : SelectionModeToName
433 // purpose :
434 // =======================================================================
435 TCollection_AsciiString VInspector_Tools::SelectionModeToName (int theMode, const Handle(AIS_InteractiveObject)& thePresentation)
436 {
437   // types are obtained by comment of SelectMgr_Selection class;
438   Handle(AIS_Shape) aShapePresentation = Handle(AIS_Shape)::DownCast (thePresentation);
439   if (!aShapePresentation.IsNull())
440     return VInspector_Tools::GetShapeTypeInfo (AIS_Shape::SelectionType (theMode)).ToCString();
441   else
442   {
443     Handle(AIS_Trihedron) aTrihedronPresentation = Handle(AIS_Trihedron)::DownCast (thePresentation);
444     if (!aTrihedronPresentation.IsNull())
445     {
446       switch (theMode)
447       {
448         case 0: return "Trihedron";
449         case 1: return "Trihedron Origin";
450         case 2: return "Trihedron Axes";
451         case 3: return "Trihedron Planes";
452         default: break;
453       }
454     }
455   }
456   return TCollection_AsciiString (theMode);
457 }
458
459 // =======================================================================
460 // function : OrientationToName
461 // purpose :
462 // =======================================================================
463 TCollection_AsciiString VInspector_Tools::OrientationToName (const TopAbs_Orientation& theOrientation)
464 {
465   Standard_SStream aSStream;
466   TopAbs::Print(theOrientation, aSStream);
467   return aSStream.str().c_str();
468 }
469
470 // =======================================================================
471 // function : LocationToName
472 // purpose :
473 // =======================================================================
474 TCollection_AsciiString VInspector_Tools::LocationToName (const TopLoc_Location& theLocation)
475 {
476   gp_Trsf aTrsf = theLocation.Transformation();
477
478   TCollection_AsciiString aValues;
479   for (int aRowId = 1; aRowId <= 3; aRowId++)
480   {
481     for (int aColId = 1; aColId <= 4; aColId++) {
482       aValues += TCollection_AsciiString (aTrsf.Value(aRowId, aColId));
483       if (aColId != 4)
484         aValues += ",";
485     }
486     if (aRowId != 3)
487       aValues += "  ";
488   }
489   return aValues;
490 }
491
492 // =======================================================================
493 // function : ReadShape
494 // purpose :
495 // =======================================================================
496 TopoDS_Shape VInspector_Tools::ReadShape (const TCollection_AsciiString& theFileName)
497 {
498   TopoDS_Shape aShape;
499
500   BRep_Builder aBuilder;
501   BRepTools::Read (aShape, theFileName.ToCString(), aBuilder);
502
503   return aShape;
504 }