1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
32 #include <SketchPlugin_Line.h>
33 #include <SketchPlugin_ConstraintCoincidence.h>
34 #include <SketchPlugin_ConstraintHorizontal.h>
35 #include <SketchPlugin_ConstraintVertical.h>
39 const double tolerance = 1e-7;
42 SketchPlugin_Rectangle::SketchPlugin_Rectangle()
43 : SketchPlugin_SketchEntity()
47 void SketchPlugin_Rectangle::initDerivedClassAttributes()
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());
57 data()->addAttribute(NOT_TO_DUMP_LIST_ID(), ModelAPI_AttributeRefList::typeId());
58 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), NOT_TO_DUMP_LIST_ID());
60 AttributeIntArrayPtr isHVList = intArray(ISHV_LIST_ID());
61 isHVList->setSize(4, false);
63 isHVList->setValue(i++, 0, false);
66 void SketchPlugin_Rectangle::updateLines()
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()));
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};
83 bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value();
85 /// Update coordinates of rectangle lines
86 for(unsigned i = 0; i < aNbLines; i++)
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);
99 void SketchPlugin_Rectangle::execute()
101 SketchPlugin_Sketch* aSketch = sketch();
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()));
110 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
111 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
113 if(!aStartAttr->isInitialized() || !aEndAttr->isInitialized()) {
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());
124 /// Create 1-4 lines to compose the rectangle
125 for( unsigned i = 0; i < 3; i++)
127 FeaturePtr aLine = aSketch->addFeature(SketchPlugin_Line::ID());
128 aLinesList->append(aLine);
129 aNotToDumpList->append(aLine);
132 aNbLines = aLinesList->size();
133 /// Create constraints to keep the rectangle
134 for( unsigned i = 0; i < aNbLines; i++)
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()));
148 /// Update coordinates of created lines
152 /// Add horizontal and vertical constraint for the lines which already have result
153 for(unsigned i = 0; i< aNbLines; i++)
155 if(isHVList->value(i))
157 FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
158 ResultPtr aLineResult = aLine->lastResult();
159 if(!aLineResult.get())
161 std::string aHVName = SketchPlugin_ConstraintHorizontal::ID();
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);
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) {
180 GeomShapePtr aRectangleShape;
183 for(unsigned i = 0; i< aNbLines; i++)
185 FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
186 ResultPtr aLineResult = aLine->lastResult();
187 if(!aLineResult.get())
189 aSubs.push_back(aLineResult->shape());
192 aRectangleShape = aSubs.empty() ? GeomShapePtr() : GeomAlgoAPI_CompoundBuilder::compound(aSubs);
194 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 0);
195 aResult->setShape(aRectangleShape);
196 aResult->setIsInHistory(false);
197 setResult(aResult, 1);
201 bool SketchPlugin_Rectangle::isFixed() {
202 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
205 void SketchPlugin_Rectangle::attributeChanged(const std::string& theID)
207 if (theID == START_ID() || theID == END_ID())
209 AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID());
210 AttributeRefListPtr aNotToDumpList = reflist(NOT_TO_DUMP_LIST_ID());
211 unsigned aNbLines = aLinesList->size();
214 SketchPlugin_Sketch* aSketch = sketch();
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);
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()));
229 if (aStartPoint->isInitialized() && aEndPoint->isInitialized())
232 if (theID == AUXILIARY_ID())
234 bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value();
235 AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID());
236 unsigned aNbLines = aLinesList->size();
238 /// Update coordinates of rectangle lines
239 for(unsigned i = 0; i < aNbLines; i++)
241 FeaturePtr aLine = std::dynamic_pointer_cast<ModelAPI_Feature>(aLinesList->object(i));
242 aLine->data()->boolean(AUXILIARY_ID())->setValue(anAuxiliary);