1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/VInspector_Tools.hxx>
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>
32 #include <Standard_WarningsDisable.hxx>
33 #include <QStringList>
34 #include <Standard_WarningsRestore.hxx>
38 // =======================================================================
39 // function : GetShapeTypeInfo
41 // =======================================================================
42 TCollection_AsciiString VInspector_Tools::GetShapeTypeInfo (const TopAbs_ShapeEnum& theType)
44 Standard_SStream aSStream;
45 TopAbs::Print (theType, aSStream);
46 return aSStream.str().c_str();
49 // =======================================================================
50 // function : GetPointerInfo
52 // =======================================================================
53 TCollection_AsciiString VInspector_Tools::GetPointerInfo (const Handle(Standard_Transient)& thePointer, const bool isShortInfo)
55 std::ostringstream aPtrStr;
56 aPtrStr << thePointer.operator->();
58 return aPtrStr.str().c_str();
60 TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
61 for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
63 if (anInfoPtr.Value(aSymbolId) != '0')
65 anInfoPtr = anInfoPtr.SubString(aSymbolId, anInfoPtr.Length());
66 anInfoPtr.Prepend("0x");
70 return aPtrStr.str().c_str();
73 // =======================================================================
74 // function : SelectedOwners
76 // =======================================================================
77 int VInspector_Tools::SelectedOwners (const Handle(AIS_InteractiveContext)& theContext,
78 const Handle(AIS_InteractiveObject)& theObject,
79 const bool theShapeInfoOnly)
81 QStringList anObjects;
82 if (theContext.IsNull())
85 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
86 for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
88 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
89 if (anOwner.IsNull()) // TODO: check why it is possible
92 if (!theObject.IsNull())
94 Handle(AIS_InteractiveObject) anOwnerPresentation =
95 Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
96 if (anOwnerPresentation != theObject)
99 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
100 if (theShapeInfoOnly && BROwnr.IsNull())
103 Standard_Transient* anOwnerPtr = anOwner.operator->();
104 if (aSelectedIds.contains ((size_t)anOwnerPtr))
106 aSelectedIds.append ((size_t)anOwnerPtr);
108 anObjects.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
110 return anObjects.size();
113 // =======================================================================
114 // function : IsOwnerSelected
116 // =======================================================================
117 bool VInspector_Tools::IsOwnerSelected (const Handle(AIS_InteractiveContext)& theContext,
118 const Handle(SelectBasics_EntityOwner)& theOwner)
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;
127 // =======================================================================
128 // function : ContextOwners
130 // =======================================================================
131 NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ContextOwners (
132 const Handle(AIS_InteractiveContext)& theContext)
134 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
135 if (theContext.IsNull())
136 return aResultOwners;
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())
143 Handle(AIS_InteractiveObject) anIO = aIt.Value();
146 #if OCC_VERSION_HEX < 0x070201
147 for (anIO->Init(); anIO->More(); anIO->Next())
149 Handle(SelectMgr_Selection) aSelection = anIO->CurrentSelection();
150 if (aSelection.IsNull())
152 for (aSelection->Init(); aSelection->More(); aSelection->Next())
154 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
156 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anIO->Selections()); aSelIter.More(); aSelIter.Next())
158 Handle(SelectMgr_Selection) aSelection = aSelIter.Value();
159 if (aSelection.IsNull())
161 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
163 Handle(SelectMgr_SensitiveEntity) anEntity = aSelEntIter.Value();
165 if (anEntity.IsNull())
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))
172 aSelectedIds.append ((size_t)anOwnerPtr);
173 aResultOwners.Append (anOwner);
177 return aResultOwners;
180 // =======================================================================
181 // function : ActiveOwners
183 // =======================================================================
184 NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ActiveOwners (
185 const Handle(AIS_InteractiveContext)& theContext,
186 NCollection_List<Handle(SelectBasics_EntityOwner)>& theEmptySelectableOwners)
188 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
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);
199 anActiveOwners = aContext->MainSelector()->ActiveOwners();
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())
206 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
207 if (anOwner.IsNull())
210 Standard_Transient* anOwnerPtr = anOwner.operator->();
211 if (aSelectedIds.contains ((size_t)anOwnerPtr))
213 aSelectedIds.append ((size_t)anOwnerPtr);
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);
221 return aResultOwners;
224 // =======================================================================
225 // function : AddOrRemoveSelectedShapes
227 // =======================================================================
228 void VInspector_Tools::AddOrRemoveSelectedShapes (const Handle(AIS_InteractiveContext)& theContext,
229 const NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
231 // TODO: the next two rows are to be removed later
232 theContext->UnhilightSelected(false);
233 theContext->ClearSelected(false);
235 theContext->UnhilightSelected(Standard_False);
237 for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt(theOwners);
238 anOwnersIt.More(); anOwnersIt.Next())
240 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
241 theContext->AddOrRemoveSelected (anOwner, Standard_False);
243 theContext->UpdateCurrentViewer();
246 // =======================================================================
247 // function : AddOrRemovePresentations
249 // =======================================================================
250 void VInspector_Tools::AddOrRemovePresentations (const Handle(AIS_InteractiveContext)& theContext,
251 const NCollection_List<Handle(AIS_InteractiveObject)>& thePresentations)
253 // TODO: the next two rows are to be removed later
254 theContext->UnhilightSelected(false);
255 theContext->ClearSelected(false);
257 for (NCollection_List<Handle(AIS_InteractiveObject)>::Iterator anIOIt (thePresentations); anIOIt.More(); anIOIt.Next())
258 theContext->AddOrRemoveSelected (anIOIt.Value(), false);
260 theContext->UpdateCurrentViewer();
263 // =======================================================================
264 // function : GetInfo
266 // =======================================================================
267 QList<QVariant> VInspector_Tools::GetInfo (Handle(AIS_InteractiveObject)& theObject)
269 QList<QVariant> anInfo;
270 anInfo.append (theObject->DynamicType()->Name());
271 anInfo.append (VInspector_Tools::GetPointerInfo (theObject, true).ToCString());
273 Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (theObject);
274 if (aShapeIO.IsNull())
277 const TopoDS_Shape& aShape = aShapeIO->Shape();
278 if (!aShape.IsNull())
279 anInfo.append (VInspector_Tools::GetShapeTypeInfo (aShape.ShapeType()).ToCString());
284 // =======================================================================
285 // function : GetHighlightInfo
287 // =======================================================================
288 QList<QVariant> VInspector_Tools::GetHighlightInfo (const Handle(AIS_InteractiveContext)& theContext)
290 QList<QVariant> aValues;
291 if (theContext.IsNull())
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())
301 Handle(SelectMgr_EntityOwner) anOwner = theContext->DetectedOwner();
302 if (anOwner.IsNull())
304 Standard_Transient* anOwnerPtr = anOwner.operator->();
305 if (aSelectedIds.contains ((size_t)anOwnerPtr))
307 aSelectedIds.append ((size_t)anOwnerPtr);
308 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
311 QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
312 if (anIOInfo.size() == 3)
314 aSelectedNames.append (anIOInfo[0].toString());
315 aSelectedPointers.append (anIOInfo[1].toString());
316 aSelectedTypes.append (anIOInfo[2].toString());
318 aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
320 aValues.append (aSelectedNames.join (", "));
321 aValues.append (aSelectedPointers.join (", "));
322 aValues.append (aSelectedTypes.join (", "));
323 aValues.append (aSelectedOwners.join (", "));
328 // =======================================================================
329 // function : GetSelectedInfo
331 // =======================================================================
332 QList<QVariant> VInspector_Tools::GetSelectedInfo (const Handle(AIS_InteractiveContext)& theContext)
334 QList<QVariant> aValues;
335 if (theContext.IsNull())
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())
345 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
346 if (anOwner.IsNull())
348 Standard_Transient* anOwnerPtr = anOwner.operator->();
349 if (aSelectedIds.contains ((size_t)anOwnerPtr))
351 aSelectedIds.append ((size_t)anOwnerPtr);
352 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
356 QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
357 if (anIOInfo.size() == 3)
359 aSelectedNames.append (anIOInfo[0].toString());
360 aSelectedPointers.append (anIOInfo[1].toString());
361 aSelectedTypes.append (anIOInfo[2].toString());
363 aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
365 aValues.append (aSelectedNames.join (", "));
366 aValues.append (aSelectedPointers.join (", "));
367 aValues.append (aSelectedTypes.join (", "));
368 aValues.append (aSelectedOwners.join (", "));
372 // =======================================================================
373 // function : GetSelectedInfoPointers
375 // =======================================================================
376 QString VInspector_Tools::GetSelectedInfoPointers (const Handle(AIS_InteractiveContext)& theContext)
378 QList<QVariant> aSelectedInfo = VInspector_Tools::GetSelectedInfo (theContext);
379 return aSelectedInfo.size() > 2 ? aSelectedInfo[1].toString() : QString();
382 // =======================================================================
385 // =======================================================================
386 TCollection_AsciiString VInspector_Tools::ToName (const VInspector_SelectionType theType, const int theValue)
390 case VInspector_SelectionType_TypeOfUpdate:
394 case SelectMgr_TOU_Full: return "Full";
395 case SelectMgr_TOU_Partial: return "Partial";
396 case SelectMgr_TOU_None: return "None";
401 case VInspector_SelectionType_StateOfSelection:
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";
414 case VInspector_SelectionType_TypeOfBVHUpdate:
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";
431 // =======================================================================
432 // function : SelectionModeToName
434 // =======================================================================
435 TCollection_AsciiString VInspector_Tools::SelectionModeToName (int theMode, const Handle(AIS_InteractiveObject)& thePresentation)
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();
443 Handle(AIS_Trihedron) aTrihedronPresentation = Handle(AIS_Trihedron)::DownCast (thePresentation);
444 if (!aTrihedronPresentation.IsNull())
448 case 0: return "Trihedron";
449 case 1: return "Trihedron Origin";
450 case 2: return "Trihedron Axes";
451 case 3: return "Trihedron Planes";
456 return TCollection_AsciiString (theMode);
459 // =======================================================================
460 // function : OrientationToName
462 // =======================================================================
463 TCollection_AsciiString VInspector_Tools::OrientationToName (const TopAbs_Orientation& theOrientation)
465 Standard_SStream aSStream;
466 TopAbs::Print(theOrientation, aSStream);
467 return aSStream.str().c_str();
470 // =======================================================================
471 // function : LocationToName
473 // =======================================================================
474 TCollection_AsciiString VInspector_Tools::LocationToName (const TopLoc_Location& theLocation)
476 gp_Trsf aTrsf = theLocation.Transformation();
478 TCollection_AsciiString aValues;
479 for (int aRowId = 1; aRowId <= 3; aRowId++)
481 for (int aColId = 1; aColId <= 4; aColId++) {
482 aValues += TCollection_AsciiString (aTrsf.Value(aRowId, aColId));
492 // =======================================================================
493 // function : ReadShape
495 // =======================================================================
496 TopoDS_Shape VInspector_Tools::ReadShape (const TCollection_AsciiString& theFileName)
500 BRep_Builder aBuilder;
501 BRepTools::Read (aShape, theFileName.ToCString(), aBuilder);