#include "stdafx.h" #include "CHPSApp.h" #include "CHPSDoc.h" #include "CHPSView.h" #include "SandboxHighlightOp.h" #include HPS::Selection::Level SandboxHighlightOperator::SelectionLevel = HPS::Selection::Level::Entity; SandboxHighlightOperator::SandboxHighlightOperator( CHPSView * in_cview) : HPS::SelectOperator(HPS::MouseButtons::ButtonLeft(), HPS::ModifierKeys()), cview(in_cview) {} SandboxHighlightOperator::~SandboxHighlightOperator() {} // Used to output the structure of Components when imported via Exchange. void SandboxHighlightOperator::DebugComponentHierarchy() { // we will use this and select an edge to display the hierarchy // Typical output in debugger console if you output "type" value in the debugger: /* ExchangeTopoEdge(20487) ExchangeTopoCoEdge(20486) ExchangeTopoLoop(20485) ExchangeTopoFace(20484) ExchangeTopoShell(20483) ExchangeTopoConnex(20482) ExchangeTopoBody(20481) ExchangeRIBRepModel(12289) ExchangePartDefinition(4099) ExchangeProductOccurrence(4098)*/ // Notice there are some differences with the structure from HOOPS Exchange structures. HPS::SelectionResults selection_results = GetActiveSelection(); size_t selected_count = selection_results.GetCount(); if (selected_count > 0) { HPS::CADModel cad_model = cview->GetDocument()->GetCADModel(); HPS::SelectionResultsIterator it = selection_results.GetIterator(); if (it.IsValid()) { HPS::ComponentPath component_path = cad_model.GetComponentPath(it.GetItem()); HPS::Component c = component_path.Front(); while (c.GetOwners().size() > 0) { auto type = c.GetComponentType(); c = c.GetOwners()[0]; } } } } // Will highlight the neighbour faces of selected elements void SandboxHighlightOperator::ShowNeighbours() { HPS::Canvas canvas = cview->GetCanvas(); HPS::HighlightOptionsKit highlight_options(HPS::HighlightOptionsKit::GetDefault()); highlight_options.SetStyleName("highlight_style"); highlight_options.SetSubentityHighlighting(SelectionLevel == HPS::Selection::Level::Subentity); highlight_options.SetOverlay(HPS::Drawing::Overlay::InPlace); HPS::SelectionResults selection_results = GetActiveSelection(); size_t selected_count = selection_results.GetCount(); if (selected_count > 0) { HPS::CADModel cad_model = cview->GetDocument()->GetCADModel(); HPS::SelectionResultsIterator it = selection_results.GetIterator(); HPS::ComponentPath component_path = cad_model.GetComponentPath(it.GetItem()); if (!component_path.Empty()) { // we check we did select a face and not an edge or something else if (HPS::Exchange::Component::ComponentType::ExchangeTopoFace == component_path.Front().GetComponentType()) { auto neighbours = GetNeighbours(component_path.Front()); for (int i = 0; i < neighbours.size(); i++) { auto face = neighbours[i]; auto keyPath = face.GetKeyPath(face); auto facePath = cad_model.GetComponentPath(keyPath[0]); facePath.Highlight(canvas, highlight_options); } canvas.Update(); } } } } // Will return the adjacent faces using topology HPS::ComponentArray SandboxHighlightOperator::GetNeighbours(HPS::Component a_face) { HPS::ComponentArray neighbours; auto model = cview->GetCanvas().GetFrontView().GetAttachedModel(); HPS::Exchange::CADModel cadModel = HPS::Exchange::CADModel(model); // Get list of edges. Edges are shared amongts adjacent faces auto edgesArray = a_face.GetAllSubcomponents(HPS::Component::ComponentType::ExchangeTopoEdge); for (int i = 0; i < edgesArray.size(); i++) { auto edge = edgesArray[i]; auto owners = edge.GetOwners(); // Recap of hierarchy: edge->coedge->loop->face // Test if this is not an open edge (no other neihgbour for this edge) if (owners.size() != 1) { for (int j = 0; j < owners.size(); j++) { auto coedge = owners[j]; auto loop = coedge.GetOwners()[0]; auto face = loop.GetOwners()[0]; // we don't want to add current face, so we need to test the returned face in the array. if (!face.Equals(a_face)) { neighbours.push_back(face); } } } } return neighbours; } //! [OnMouseDown] bool SandboxHighlightOperator::OnMouseDown( HPS::MouseState const & in_state) { auto sel_opts = GetSelectionOptions(); sel_opts.SetLevel(SandboxHighlightOperator::SelectionLevel); SetSelectionOptions(sel_opts); if (IsMouseTriggered(in_state) && HPS::SelectOperator::OnMouseDown(in_state)) { HighlightCommon(); DebugComponentHierarchy(); ShowNeighbours(); return true; } return false; } //! [OnMouseDown] bool SandboxHighlightOperator::OnTouchDown( HPS::TouchState const & in_state) { auto sel_opts = GetSelectionOptions(); sel_opts.SetLevel(SandboxHighlightOperator::SelectionLevel); SetSelectionOptions(sel_opts); if (HPS::SelectOperator::OnTouchDown(in_state)) { HighlightCommon(); return true; } return false; } void SandboxHighlightOperator::HighlightCommon() { cview->Unhighlight(); HPS::SelectionResults selection_results = GetActiveSelection(); size_t selected_count = selection_results.GetCount(); if (selected_count > 0) { HPS::CADModel cad_model = cview->GetDocument()->GetCADModel(); //! [build_highlight_style] HPS::HighlightOptionsKit highlight_options(HPS::HighlightOptionsKit::GetDefault()); highlight_options.SetStyleName("highlight_style"); highlight_options.SetSubentityHighlighting(SelectionLevel == HPS::Selection::Level::Subentity); highlight_options.SetOverlay(HPS::Drawing::Overlay::InPlace); //! [build_highlight_style] //! [highlight_cad_model] if (!cad_model.Empty()) { // since we have a CADModel, we want to highlight the components, not just the Visualize geometry HPS::SelectionResultsIterator it = selection_results.GetIterator(); HPS::Canvas canvas = cview->GetCanvas(); while (it.IsValid()) { HPS::ComponentPath component_path = cad_model.GetComponentPath(it.GetItem()); if (!component_path.Empty()) { // Make the selected component get highlighted in the model browser highlight_options.SetNotification(true); component_path.Highlight(canvas, highlight_options); // if we selected PMI, highlight the associated components (if any) HPS::Component const & leaf_component = component_path.Front(); if (leaf_component.HasComponentType(HPS::Component::ComponentType::ExchangePMIMask)) { // Only highlight the Visualize geometry for the associated components, don't highlight the associated components in the model browser highlight_options.SetNotification(false); for (auto const & reference : leaf_component.GetReferences()) HPS::ComponentPath(1, &reference).Highlight(canvas, highlight_options); } } it.Next(); } } //! [highlight_cad_model] //! [highlight_geometry] else { // since there is no CADModel, just highlight the Visualize geometry cview->GetCanvas().GetWindowKey().GetHighlightControl().Highlight(selection_results, highlight_options); HPS::Database::GetEventDispatcher().InjectEvent(HPS::HighlightEvent(HPS::HighlightEvent::Action::Highlight, selection_results, highlight_options)); } //! [highlight_geometry] } cview->Update(); }