Salome HOME
move rectangle plugin from python addons to C++.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Rectangle.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Rectangle.h"
21 #include "SketchPlugin_Sketch.h"
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31
32 #include <SketchPlugin_Line.h>
33 #include <SketchPlugin_ConstraintCoincidence.h>
34 #include <SketchPlugin_ConstraintHorizontal.h>
35 #include <SketchPlugin_ConstraintVertical.h>
36
37 #include <cmath>
38
39 const double tolerance = 1e-7;
40
41
42 SketchPlugin_Rectangle::SketchPlugin_Rectangle()
43   : SketchPlugin_SketchEntity()
44 {
45 }
46
47 void SketchPlugin_Rectangle::initDerivedClassAttributes()
48 {
49   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
50   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
51   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
53   data()->addAttribute(LINES_LIST_ID(), ModelAPI_AttributeRefList::typeId());
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LINES_LIST_ID());
55   data()->addAttribute(ISHV_LIST_ID(), ModelAPI_AttributeIntArray::typeId());
56
57   data()->addAttribute(NOT_TO_DUMP_LIST_ID(), ModelAPI_AttributeRefList::typeId());
58   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), NOT_TO_DUMP_LIST_ID());
59
60   AttributeIntArrayPtr isHVList = intArray(ISHV_LIST_ID());
61   isHVList->setSize(4, false);
62   for(int i = 0; i< 4;)
63     isHVList->setValue(i++, 0, false);
64  }
65
66 void SketchPlugin_Rectangle::updateLines()
67 {
68   // Retrieving list of already created lines
69   AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID());
70   unsigned  aNbLines = aLinesList->size();
71   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
72       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
73   std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
74       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
75
76   double xMin = std::min(aStartPoint->x(),  aEndPoint->x());
77   double xMax = std::max(aStartPoint->x(),  aEndPoint->x());
78   double yMin = std::min(aStartPoint->y(),  aEndPoint->y());
79   double yMax = std::max(aStartPoint->y(),  aEndPoint->y());
80   std::vector<double> aX = {xMin, xMax, xMax, xMin};
81   std::vector<double> aY = {yMin, yMin, yMax, yMax};
82
83   bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value();
84
85   /// Update coordinates of rectangle lines
86   for(unsigned  i = 0; i < aNbLines; i++)
87   {
88     FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
89     std::shared_ptr<GeomDataAPI_Point2D> aLineStart =
90         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLine->attribute(SketchPlugin_Line::START_ID()));
91     std::shared_ptr<GeomDataAPI_Point2D> aLineEnd =
92         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLine->attribute(SketchPlugin_Line::END_ID()));
93        aLineStart->setValue(aX[i], aY[i]);
94     aLineEnd->setValue(aX[(i+1)%4], aY[(i+1)%4]);
95     aLine->data()->boolean(AUXILIARY_ID())->setValue(anAuxiliary);
96   }
97 }
98
99 void SketchPlugin_Rectangle::execute()
100 {
101   SketchPlugin_Sketch* aSketch = sketch();
102   if(!aSketch) {
103     return;
104   }
105
106   // Compute a Rectangle in 3D view.
107   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
108       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
109
110   std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
111       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
112
113   if(!aStartAttr->isInitialized() || !aEndAttr->isInitialized()) {
114     return;
115   }
116
117   AttributeRefListPtr aLinesList =  reflist(LINES_LIST_ID());
118   unsigned aNbLines = aLinesList->size();
119   AttributeIntArrayPtr isHVList = intArray(ISHV_LIST_ID());
120   AttributeRefListPtr aNotToDumpList = reflist(NOT_TO_DUMP_LIST_ID());
121
122   if(aNbLines == 1)
123   {
124     /// Create 1-4 lines to compose the rectangle
125     for( unsigned i = 0; i < 3; i++)
126     {
127       FeaturePtr aLine = aSketch->addFeature(SketchPlugin_Line::ID());
128       aLinesList->append(aLine);
129       aNotToDumpList->append(aLine);
130     }
131     updateLines();
132     aNbLines = aLinesList->size();
133     /// Create constraints to keep the rectangle
134     for( unsigned i = 0; i < aNbLines; i++)
135     {
136       FeaturePtr  aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
137       /// connect neighbor lines by coincidence
138       unsigned iPrev = (i+3)%4;
139       FeaturePtr aPrevLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(iPrev));
140       FeaturePtr aCoincidence = aSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
141       aNotToDumpList->append(aCoincidence);
142       AttributeRefAttrPtr aRefAttrA = aCoincidence->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A());
143       AttributeRefAttrPtr aRefAttrB = aCoincidence->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B());
144       aRefAttrA->setAttr(aPrevLine->attribute(SketchPlugin_Line::END_ID()));
145       aRefAttrB->setAttr(aLine->attribute(SketchPlugin_Line::START_ID()));
146     }
147
148     /// Update coordinates of created lines
149     updateLines();
150   }
151
152   /// Add horizontal and vertical constraint for the lines which already have result
153   for(unsigned i = 0; i< aNbLines; i++)
154   {
155     if(isHVList->value(i))
156       continue;
157     FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
158     ResultPtr aLineResult = aLine->lastResult();
159     if(!aLineResult.get())
160       continue;
161     std::string aHVName = SketchPlugin_ConstraintHorizontal::ID();
162     if(i%2)
163       aHVName = SketchPlugin_ConstraintVertical::ID();
164     FeaturePtr aHVConstraint = aSketch->addFeature(aHVName);
165     aNotToDumpList->append(aHVConstraint);
166     AttributeRefAttrPtr aRefAttrA = aHVConstraint->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A());
167     aRefAttrA->setObject(aLine->lastResult());
168     isHVList->setValue(i, 1, false);
169   }
170
171   double aDiag = std::pow(aStartAttr->x() - aEndAttr->x(), 2.0);
172   aDiag += std::pow(aStartAttr->y() - aEndAttr->y(), 2.0);
173   aDiag = std::sqrt(aDiag);
174   if(aDiag < tolerance) {
175     return;
176   }
177
178   // store results.
179
180   GeomShapePtr aRectangleShape;
181   ListOfShape aSubs;
182
183   for(unsigned i = 0; i< aNbLines; i++)
184   {
185     FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
186     ResultPtr aLineResult = aLine->lastResult();
187     if(!aLineResult.get())
188       continue;
189     aSubs.push_back(aLineResult->shape());
190   }
191
192   aRectangleShape = aSubs.empty() ? GeomShapePtr() : GeomAlgoAPI_CompoundBuilder::compound(aSubs);
193
194   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 0);
195   aResult->setShape(aRectangleShape);
196   aResult->setIsInHistory(false);
197   setResult(aResult, 1);
198 }
199
200
201 bool SketchPlugin_Rectangle::isFixed() {
202   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
203 }
204
205 void SketchPlugin_Rectangle::attributeChanged(const std::string& theID)
206 {
207   if (theID == START_ID() || theID == END_ID())
208   {
209     AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID());
210     AttributeRefListPtr aNotToDumpList =  reflist(NOT_TO_DUMP_LIST_ID());
211     unsigned  aNbLines = aLinesList->size();
212     if(aNbLines == 0)
213     {
214       SketchPlugin_Sketch* aSketch = sketch();
215       if(!aSketch) {
216         return;
217       }
218       /// Create first line to be able to create a coincidence with selected point/feature
219       FeaturePtr aLine = aSketch->addFeature(SketchPlugin_Line::ID());
220       aLinesList->append(aLine);
221       aNotToDumpList->append(aLine);
222     }
223
224     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
225         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
226     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
227         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
228
229     if (aStartPoint->isInitialized() && aEndPoint->isInitialized())
230       updateLines();    
231   }
232   if (theID == AUXILIARY_ID())
233   {
234     bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value();
235     AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID());
236     unsigned aNbLines = aLinesList->size();
237
238     /// Update coordinates of rectangle lines
239     for(unsigned  i = 0; i < aNbLines; i++)
240     {
241       FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
242       aLine->data()->boolean(AUXILIARY_ID())->setValue(anAuxiliary);
243     }
244   }
245 }