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 #if OCC_VERSION_HEX < 0x060901
21 #include <AIS_LocalContext.hxx>
23 #include <AIS_Selection.hxx>
24 #include <AIS_Shape.hxx>
25 #include <AIS_Trihedron.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRepTools.hxx>
28 #include <gp_Trsf.hxx>
29 #include <SelectMgr_StateOfSelection.hxx>
30 #include <SelectMgr_TypeOfUpdate.hxx>
31 #include <SelectMgr_TypeOfBVHUpdate.hxx>
32 #include <Standard_Version.hxx>
33 #include <StdSelect_BRepOwner.hxx>
35 #include <Standard_WarningsDisable.hxx>
36 #include <QStringList>
37 #include <Standard_WarningsRestore.hxx>
41 // =======================================================================
42 // function : GetShapeTypeInfo
44 // =======================================================================
45 TCollection_AsciiString VInspector_Tools::GetShapeTypeInfo (const TopAbs_ShapeEnum& theType)
47 Standard_SStream aSStream;
48 TopAbs::Print (theType, aSStream);
49 return aSStream.str().c_str();
52 // =======================================================================
53 // function : GetPointerInfo
55 // =======================================================================
56 TCollection_AsciiString VInspector_Tools::GetPointerInfo (const Handle(Standard_Transient)& thePointer, const bool isShortInfo)
58 std::ostringstream aPtrStr;
59 aPtrStr << thePointer.operator->();
61 return aPtrStr.str().c_str();
63 TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
64 for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
66 if (anInfoPtr.Value(aSymbolId) != '0')
68 anInfoPtr = anInfoPtr.SubString(aSymbolId, anInfoPtr.Length());
69 anInfoPtr.Prepend("0x");
73 return aPtrStr.str().c_str();
76 // =======================================================================
77 // function : SelectedOwners
79 // =======================================================================
80 int VInspector_Tools::SelectedOwners (const Handle(AIS_InteractiveContext)& theContext,
81 const Handle(AIS_InteractiveObject)& theObject,
82 const bool theShapeInfoOnly)
84 QStringList anObjects;
85 if (theContext.IsNull())
88 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
89 for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
91 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
92 if (anOwner.IsNull()) // TODO: check why it is possible
95 if (!theObject.IsNull())
97 Handle(AIS_InteractiveObject) anOwnerPresentation =
98 Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
99 if (anOwnerPresentation != theObject)
102 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
103 if (theShapeInfoOnly && BROwnr.IsNull())
106 Standard_Transient* anOwnerPtr = anOwner.operator->();
107 if (aSelectedIds.contains ((size_t)anOwnerPtr))
109 aSelectedIds.append ((size_t)anOwnerPtr);
111 anObjects.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
113 return anObjects.size();
116 // =======================================================================
117 // function : IsOwnerSelected
119 // =======================================================================
120 bool VInspector_Tools::IsOwnerSelected (const Handle(AIS_InteractiveContext)& theContext,
121 const Handle(SelectBasics_EntityOwner)& theOwner)
123 bool anIsSelected = false;
124 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (theOwner);
125 for (theContext->InitSelected(); theContext->MoreSelected() && !anIsSelected; theContext->NextSelected())
126 anIsSelected = theContext->SelectedOwner() == anOwner;
130 // =======================================================================
131 // function : ContextOwners
133 // =======================================================================
134 NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ContextOwners (
135 const Handle(AIS_InteractiveContext)& theContext)
137 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
138 if (theContext.IsNull())
139 return aResultOwners;
141 AIS_ListOfInteractive aListOfIO;
142 theContext->DisplayedObjects (aListOfIO);
143 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
144 for (AIS_ListIteratorOfListOfInteractive aIt(aListOfIO); aIt.More(); aIt.Next())
146 Handle(AIS_InteractiveObject) anIO = aIt.Value();
149 #if OCC_VERSION_HEX < 0x070201
150 for (anIO->Init(); anIO->More(); anIO->Next())
152 Handle(SelectMgr_Selection) aSelection = anIO->CurrentSelection();
153 if (aSelection.IsNull())
155 for (aSelection->Init(); aSelection->More(); aSelection->Next())
157 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
159 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anIO->Selections()); aSelIter.More(); aSelIter.Next())
161 Handle(SelectMgr_Selection) aSelection = aSelIter.Value();
162 if (aSelection.IsNull())
164 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
166 Handle(SelectMgr_SensitiveEntity) anEntity = aSelEntIter.Value();
168 if (anEntity.IsNull())
170 const Handle(SelectBasics_SensitiveEntity)& aBase = anEntity->BaseSensitive();
171 Handle(SelectBasics_EntityOwner) anOwner = aBase->OwnerId();
172 Standard_Transient* anOwnerPtr = anOwner.operator->();
173 if (aSelectedIds.contains ((size_t)anOwnerPtr))
175 aSelectedIds.append ((size_t)anOwnerPtr);
176 aResultOwners.Append (anOwner);
180 return aResultOwners;
183 // =======================================================================
184 // function : ActiveOwners
186 // =======================================================================
187 NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ActiveOwners (
188 const Handle(AIS_InteractiveContext)& theContext,
189 NCollection_List<Handle(SelectBasics_EntityOwner)>& theEmptySelectableOwners)
191 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
193 // only local context is processed: TODO for global context
194 Handle(AIS_InteractiveContext) aContext = theContext;
195 if (aContext.IsNull())
196 return aResultOwners;
197 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
198 // OCCT BUG:1 - equal pointer owners are appears in the list
199 #if OCC_VERSION_HEX > 0x060901
200 aContext->MainSelector()->ActiveOwners (anActiveOwners);
202 anActiveOwners = aContext->MainSelector()->ActiveOwners();
204 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
205 Handle(SelectMgr_EntityOwner) anOwner;
206 for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
207 anOwnersIt.More(); anOwnersIt.Next())
209 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
210 if (anOwner.IsNull())
213 Standard_Transient* anOwnerPtr = anOwner.operator->();
214 if (aSelectedIds.contains ((size_t)anOwnerPtr))
216 aSelectedIds.append ((size_t)anOwnerPtr);
218 aResultOwners.Append (anOwner);
219 Handle(SelectMgr_SelectableObject) aSelectable = anOwner->Selectable();
220 if (aSelectable.IsNull() ||
221 !theContext->IsDisplayed(Handle(AIS_InteractiveObject)::DownCast (aSelectable)))
222 theEmptySelectableOwners.Append (anOwner);
224 return aResultOwners;
227 // =======================================================================
228 // function : AddOrRemoveSelectedShapes
230 // =======================================================================
231 void VInspector_Tools::AddOrRemoveSelectedShapes (const Handle(AIS_InteractiveContext)& theContext,
232 const NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
234 // TODO: the next two rows are to be removed later
235 theContext->UnhilightSelected(false);
236 theContext->ClearSelected(false);
238 theContext->UnhilightSelected(Standard_False);
240 //TODO: processing in local context only
241 #if OCC_VERSION_HEX < 0x060901
242 Handle(AIS_LocalContext) aLContext = theContext->LocalContext();
243 TCollection_AsciiString aSelectionName = aLContext->SelectionName();
244 aLContext->UnhilightPicked(Standard_False);
247 for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt(theOwners);
248 anOwnersIt.More(); anOwnersIt.Next())
250 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
251 #if OCC_VERSION_HEX > 0x060901
252 theContext->AddOrRemoveSelected (anOwner, Standard_False);
254 AIS_Selection::Selection(aSelectionName.ToCString())->Select(anOwner);
255 anOwner->SetSelected(Standard_True);
258 theContext->UpdateCurrentViewer();
261 // =======================================================================
262 // function : AddOrRemovePresentations
264 // =======================================================================
265 void VInspector_Tools::AddOrRemovePresentations (const Handle(AIS_InteractiveContext)& theContext,
266 const NCollection_List<Handle(AIS_InteractiveObject)>& thePresentations)
268 // TODO: the next two rows are to be removed later
269 theContext->UnhilightSelected(false);
270 theContext->ClearSelected(false);
272 for (NCollection_List<Handle(AIS_InteractiveObject)>::Iterator anIOIt (thePresentations); anIOIt.More(); anIOIt.Next())
273 theContext->AddOrRemoveSelected (anIOIt.Value(), false);
275 theContext->UpdateCurrentViewer();
278 // =======================================================================
279 // function : GetInfo
281 // =======================================================================
282 QList<QVariant> VInspector_Tools::GetInfo (Handle(AIS_InteractiveObject)& theObject)
284 QList<QVariant> anInfo;
285 anInfo.append (theObject->DynamicType()->Name());
286 anInfo.append (VInspector_Tools::GetPointerInfo (theObject, true).ToCString());
288 Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (theObject);
289 if (aShapeIO.IsNull())
292 const TopoDS_Shape& aShape = aShapeIO->Shape();
293 if (!aShape.IsNull())
294 anInfo.append (VInspector_Tools::GetShapeTypeInfo (aShape.ShapeType()).ToCString());
299 // =======================================================================
300 // function : GetHighlightInfo
302 // =======================================================================
303 QList<QVariant> VInspector_Tools::GetHighlightInfo (const Handle(AIS_InteractiveContext)& theContext)
305 QList<QVariant> aValues;
306 if (theContext.IsNull())
309 QStringList aSelectedNames;
310 QStringList aSelectedPointers;
311 QStringList aSelectedTypes;
312 QStringList aSelectedOwners;
313 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
314 for (theContext->InitDetected(); theContext->MoreDetected(); theContext->NextDetected())
316 Handle(SelectMgr_EntityOwner) anOwner = theContext->DetectedOwner();
317 if (anOwner.IsNull())
319 Standard_Transient* anOwnerPtr = anOwner.operator->();
320 if (aSelectedIds.contains ((size_t)anOwnerPtr))
322 aSelectedIds.append ((size_t)anOwnerPtr);
323 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
326 QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
327 if (anIOInfo.size() == 3)
329 aSelectedNames.append (anIOInfo[0].toString());
330 aSelectedPointers.append (anIOInfo[1].toString());
331 aSelectedTypes.append (anIOInfo[2].toString());
333 aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
335 aValues.append (aSelectedNames.join (", "));
336 aValues.append (aSelectedPointers.join (", "));
337 aValues.append (aSelectedTypes.join (", "));
338 aValues.append (aSelectedOwners.join (", "));
343 // =======================================================================
344 // function : GetSelectedInfo
346 // =======================================================================
347 QList<QVariant> VInspector_Tools::GetSelectedInfo (const Handle(AIS_InteractiveContext)& theContext)
349 QList<QVariant> aValues;
350 if (theContext.IsNull())
353 QStringList aSelectedNames;
354 QStringList aSelectedPointers;
355 QStringList aSelectedTypes;
356 QStringList aSelectedOwners;
357 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
358 for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
360 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
361 if (anOwner.IsNull())
363 Standard_Transient* anOwnerPtr = anOwner.operator->();
364 if (aSelectedIds.contains ((size_t)anOwnerPtr))
366 aSelectedIds.append ((size_t)anOwnerPtr);
367 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
371 QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
372 if (anIOInfo.size() == 3)
374 aSelectedNames.append (anIOInfo[0].toString());
375 aSelectedPointers.append (anIOInfo[1].toString());
376 aSelectedTypes.append (anIOInfo[2].toString());
378 aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
380 aValues.append (aSelectedNames.join (", "));
381 aValues.append (aSelectedPointers.join (", "));
382 aValues.append (aSelectedTypes.join (", "));
383 aValues.append (aSelectedOwners.join (", "));
387 // =======================================================================
388 // function : GetSelectedInfoPointers
390 // =======================================================================
391 QString VInspector_Tools::GetSelectedInfoPointers (const Handle(AIS_InteractiveContext)& theContext)
393 QList<QVariant> aSelectedInfo = VInspector_Tools::GetSelectedInfo (theContext);
394 return aSelectedInfo.size() > 2 ? aSelectedInfo[1].toString() : QString();
397 // =======================================================================
400 // =======================================================================
401 TCollection_AsciiString VInspector_Tools::ToName (const VInspector_SelectionType theType, const int theValue)
405 case VInspector_SelectionType_TypeOfUpdate:
409 case SelectMgr_TOU_Full: return "Full";
410 case SelectMgr_TOU_Partial: return "Partial";
411 case SelectMgr_TOU_None: return "None";
416 case VInspector_SelectionType_StateOfSelection:
420 case SelectMgr_SOS_Activated: return "Activated";
421 case SelectMgr_SOS_Deactivated: return "Deactivated";
422 case SelectMgr_SOS_Sleeping: return "Sleeping";
423 case SelectMgr_SOS_Any: return "Any";
424 case SelectMgr_SOS_Unknown: return "Unknown";
429 case VInspector_SelectionType_TypeOfBVHUpdate:
433 case SelectMgr_TBU_Add: return "Add";
434 case SelectMgr_TBU_Remove: return "Remove";
435 case SelectMgr_TBU_Renew: return "Renew";
436 case SelectMgr_TBU_Invalidate: return "Invalidate";
437 case SelectMgr_TBU_None: return "None";
446 // =======================================================================
447 // function : SelectionModeToName
449 // =======================================================================
450 TCollection_AsciiString VInspector_Tools::SelectionModeToName (int theMode, const Handle(AIS_InteractiveObject)& thePresentation)
452 // types are obtained by comment of SelectMgr_Selection class;
453 Handle(AIS_Shape) aShapePresentation = Handle(AIS_Shape)::DownCast (thePresentation);
454 if (!aShapePresentation.IsNull())
455 return VInspector_Tools::GetShapeTypeInfo (AIS_Shape::SelectionType (theMode)).ToCString();
458 Handle(AIS_Trihedron) aTrihedronPresentation = Handle(AIS_Trihedron)::DownCast (thePresentation);
459 if (!aTrihedronPresentation.IsNull())
463 case 0: return "Trihedron";
464 case 1: return "Trihedron Origin";
465 case 2: return "Trihedron Axes";
466 case 3: return "Trihedron Planes";
471 return TCollection_AsciiString (theMode);
474 // =======================================================================
475 // function : OrientationToName
477 // =======================================================================
478 TCollection_AsciiString VInspector_Tools::OrientationToName (const TopAbs_Orientation& theOrientation)
480 Standard_SStream aSStream;
481 TopAbs::Print(theOrientation, aSStream);
482 return aSStream.str().c_str();
485 // =======================================================================
486 // function : LocationToName
488 // =======================================================================
489 TCollection_AsciiString VInspector_Tools::LocationToName (const TopLoc_Location& theLocation)
491 gp_Trsf aTrsf = theLocation.Transformation();
493 TCollection_AsciiString aValues;
494 for (int aRowId = 1; aRowId <= 3; aRowId++)
496 for (int aColId = 1; aColId <= 4; aColId++) {
497 aValues += TCollection_AsciiString (aTrsf.Value(aRowId, aColId));
507 // =======================================================================
508 // function : ReadShape
510 // =======================================================================
511 TopoDS_Shape VInspector_Tools::ReadShape (const TCollection_AsciiString& theFileName)
515 BRep_Builder aBuilder;
516 BRepTools::Read (aShape, theFileName.ToCString(), aBuilder);