Salome HOME
Issue #1369: Added feature "Create Vertex"
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Vertex.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        BuildPlugin_Vertex.cpp
4 // Created:     18 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Vertex.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12
13 #include <Events_Error.h>
14
15 #include <GeomAPI_DataMapOfShapeShape.h>
16 #include <GeomAPI_PlanarEdges.h>
17 #include <GeomAPI_ShapeExplorer.h>
18
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAlgoAPI_WireBuilder.h>
21
22 #include <algorithm>
23
24 //=================================================================================================
25 BuildPlugin_Vertex::BuildPlugin_Vertex()
26 {
27 }
28
29 //=================================================================================================
30 void BuildPlugin_Vertex::initAttributes()
31 {
32   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
33 }
34
35 //=================================================================================================
36 void BuildPlugin_Vertex::execute()
37 {
38   // Get base objects list.
39   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
40   if(!aSelectionList.get()) {
41     setError("Error: Could not get selection list.");
42     return;
43   }
44   if(aSelectionList->size() == 0) {
45     setError("Error: Empty selection list.");
46     return;
47   }
48
49   // Collect base shapes.
50   ListOfShape aListOfShapes;
51   int aResultIndex = 0;
52   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
53     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
54     GeomShapePtr aShape = aSelection->value();
55     if(!aShape.get()) {
56       ResultPtr aContext = aSelection->context();
57       if(!aContext.get()) {
58         setError("Error: Attribute has empty context.");
59         return;
60       }
61
62       aShape = aContext->shape();
63     }
64     if(!aShape.get()) {
65       setError("Error: Empty shape selected.");
66       return;
67     }
68
69     if(aShape->shapeType() != GeomAPI_Shape::VERTEX) {
70       setError("Error: Selected shape has wrong type. Only vertices acceptable.");
71       return;
72     }
73
74     // Store result.
75     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
76     aResultBody->store(aShape);
77     setResult(aResultBody, aResultIndex);
78     ++aResultIndex;
79   }
80
81   removeResults(aResultIndex);
82 }