1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Group.cpp
4 // Created: 27 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Group.h"
9 #include <SketchSolver_Builder.h>
10 #include <SketchSolver_Constraint.h>
11 #include <SketchSolver_ConstraintCoincidence.h>
12 #include <SketchSolver_Error.h>
14 #include <Events_Error.h>
15 #include <Events_Loop.h>
16 #include <GeomAPI_XY.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomDataAPI_Dir.h>
20 #include <GeomDataAPI_Point.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_ResultConstruction.h>
28 #include <SketchPlugin_Constraint.h>
29 #include <SketchPlugin_ConstraintFillet.h>
30 #include <SketchPlugin_ConstraintLength.h>
31 #include <SketchPlugin_ConstraintCoincidence.h>
32 #include <SketchPlugin_ConstraintMirror.h>
33 #include <SketchPlugin_ConstraintRigid.h>
34 #include <SketchPlugin_Feature.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_Circle.h>
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Point.h>
40 #include <SketchPlugin_Sketch.h>
46 /// \brief This class is used to give unique index to the groups
50 /// \brief Return vacant index
51 static Slvs_hGroup NEW_GROUP() { return ++myGroupIndex; }
52 /// \brief Removes the index
53 static void REMOVE_GROUP(const Slvs_hGroup& theIndex) {
54 if (myGroupIndex == theIndex)
61 static Slvs_hGroup myGroupIndex; ///< index of the group
64 Slvs_hGroup GroupIndexer::myGroupIndex = 0;
68 // ========================================================
69 // ========= SketchSolver_Group ===============
70 // ========================================================
72 SketchSolver_Group::SketchSolver_Group(
73 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
74 : myID(GroupIndexer::NEW_GROUP())
76 // Initialize workplane
77 myWorkplaneID = SLVS_E_UNKNOWN;
79 assert(addWorkplane(theWorkplane));
81 addWorkplane(theWorkplane);
85 SketchSolver_Group::~SketchSolver_Group()
87 myConstraints.clear();
88 GroupIndexer::REMOVE_GROUP(myID);
91 // ============================================================================
92 // Function: isBaseWorkplane
93 // Class: SketchSolver_Group
94 // Purpose: verify the group is based on the given workplane
95 // ============================================================================
96 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
98 return theWorkplane == mySketch;
101 // ============================================================================
102 // Function: isInteract
103 // Class: SketchSolver_Group
104 // Purpose: verify are there any entities in the group used by given constraint
105 // ============================================================================
106 bool SketchSolver_Group::isInteract(
107 std::shared_ptr<SketchPlugin_Feature> theFeature) const
109 // Empty group interacts with everything
110 if (isEmpty()) return true;
111 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
113 return myFeatureStorage->isInteract(aConstraint);
114 return myFeatureStorage->isInteract(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
117 // ============================================================================
118 // Function: getFeatureId
119 // Class: SketchSolver_Group
120 // Purpose: Find the identifier of the feature, if it already exists in the group
121 // ============================================================================
122 Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
124 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
125 if (!myFeatureStorage)
127 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theFeature);
128 if (aConstraints.empty())
130 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
131 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
132 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
133 if (aCIter == myConstraints.end())
135 aResult = aCIter->second->getId(theFeature);
136 if (aResult != SLVS_E_UNKNOWN)
139 return SLVS_E_UNKNOWN;
142 // ============================================================================
143 // Function: getAttributeId
144 // Class: SketchSolver_Group
145 // Purpose: Find the identifier of the attribute, if it already exists in the group
146 // ============================================================================
147 Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
149 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
150 if (!myFeatureStorage)
152 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theAttribute);
153 if (aConstraints.empty())
155 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
156 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
157 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
158 if (aCIter == myConstraints.end())
160 aResult = aCIter->second->getId(theAttribute);
161 if (aResult != SLVS_E_UNKNOWN)
164 return SLVS_E_UNKNOWN;
167 // ============================================================================
168 // Function: changeConstraint
169 // Class: SketchSolver_Group
170 // Purpose: create/update the constraint in the group
171 // ============================================================================
172 bool SketchSolver_Group::changeConstraint(
173 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
175 // There is no workplane yet, something wrong
176 if (myWorkplaneID == SLVS_E_UNKNOWN)
182 if (myConstraints.find(theConstraint) == myConstraints.end()) {
183 // Add constraint to the current group
184 SolverConstraintPtr aConstraint =
185 SketchSolver_Builder::getInstance()->createConstraint(theConstraint);
188 aConstraint->setGroup(this);
189 aConstraint->setStorage(myStorage);
190 if (!aConstraint->error().empty()) {
191 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
192 return false; // some attribute are not initialized yet, don't show message
193 Events_Error::send(aConstraint->error(), this);
196 // Additional verification of coincidence of several points
197 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
198 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
199 for (; aCIter != myConstraints.end(); aCIter++) {
200 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
201 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aCIter->second);
204 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoinc2 =
205 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
206 if (aCoincidence != aCoinc2 && aCoincidence->isCoincide(aCoinc2)) {
207 aCoincidence->attach(aCoinc2);
208 aConstraint = aCoincidence;
212 myConstraints[theConstraint] = aConstraint;
215 myConstraints[theConstraint]->update();
217 // Fix base features for fillet
218 if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
219 std::list<AttributePtr> anAttrList =
220 theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
221 std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
222 for (; anAttrIter != anAttrList.end(); anAttrIter++) {
223 AttributeRefAttrPtr aRefAttr =
224 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
225 if (!aRefAttr || !aRefAttr->isObject())
227 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
228 SolverConstraintPtr aConstraint =
229 SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
232 aConstraint->setGroup(this);
233 aConstraint->setStorage(myStorage);
234 myTempConstraints.insert(aConstraint);
238 if (!myFeatureStorage)
239 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
240 myFeatureStorage->changeConstraint(theConstraint);
246 bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
248 std::set<ConstraintPtr> aConstraints =
249 myFeatureStorage->getConstraints(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
250 if (aConstraints.empty())
252 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
253 for (; aCIter != aConstraints.end(); aCIter++) {
254 ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter);
255 aSolConIter->second->update();
260 void SketchSolver_Group::moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
262 updateFeature(theFeature);
263 // Temporary rigid constraint
264 SolverConstraintPtr aConstraint =
265 SketchSolver_Builder::getInstance()->createRigidConstraint(theFeature);
268 aConstraint->setGroup(this);
269 aConstraint->setStorage(myStorage);
270 myTempConstraints.insert(aConstraint);
273 // ============================================================================
274 // Function: addWorkplane
275 // Class: SketchSolver_Group
276 // Purpose: create workplane for the group
277 // ============================================================================
278 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
280 if (myWorkplaneID != SLVS_E_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
281 return false; // the workplane already exists or the function parameter is not Sketch
283 mySketch = theSketch;
288 // ============================================================================
289 // Function: updateWorkplane
290 // Class: SketchSolver_Group
291 // Purpose: update parameters of workplane
292 // ============================================================================
293 bool SketchSolver_Group::updateWorkplane()
295 if (!myStorage) // Create storage if not exists
296 myStorage = StoragePtr(new SketchSolver_Storage);
297 SketchSolver_Builder* aBuilder = SketchSolver_Builder::getInstance();
299 std::vector<Slvs_Entity> anEntities;
300 std::vector<Slvs_Param> aParams;
301 if (!aBuilder->createWorkplane(mySketch, anEntities, aParams))
304 if (myWorkplaneID == SLVS_E_UNKNOWN) {
305 myWorkplaneID = anEntities.back().h;
306 // Add new workplane elements
307 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
308 for (; aParIter != aParams.end(); aParIter++) {
309 aParIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
310 aParIter->group = myID;
311 aParIter->h = myStorage->addParameter(*aParIter);
313 std::vector<Slvs_Entity>::iterator anEntIter = anEntities.begin();
314 for (; anEntIter != anEntities.end(); anEntIter++) {
315 anEntIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
316 anEntIter->group = myID;
317 anEntIter->wrkpl = myWorkplaneID;
318 for (int i = 0; i < 4; i++)
319 if (anEntIter->param[i] != SLVS_E_UNKNOWN)
320 anEntIter->param[i] = aParams[anEntIter->param[i]-1].h;
321 for (int i = 0; i < 4; i++)
322 if (anEntIter->point[i] != SLVS_E_UNKNOWN)
323 anEntIter->point[i] = anEntities[anEntIter->point[i]-1].h;
324 anEntIter->h = myStorage->addEntity(*anEntIter);
327 // Update existent workplane
328 const Slvs_Entity& aWP = myStorage->getEntity(myWorkplaneID);
329 const Slvs_Entity& anOrigin = myStorage->getEntity(aWP.point[0]);
330 const Slvs_Entity& aNormal = myStorage->getEntity(aWP.normal);
331 // Get parameters and update them
332 Slvs_hParam aWPParams[7] = {
333 anOrigin.param[0], anOrigin.param[1], anOrigin.param[2],
334 aNormal.param[0], aNormal.param[1], aNormal.param[2], aNormal.param[3]
336 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
337 for (int i = 0; aParIter != aParams.end(); aParIter++, i++) {
338 Slvs_Param aParam = myStorage->getParameter(aWPParams[i]);
339 aParam.val = aParIter->val;
340 myStorage->updateParameter(aParam);
343 return myWorkplaneID > 0;
346 // ============================================================================
347 // Function: resolveConstraints
348 // Class: SketchSolver_Group
349 // Purpose: solve the set of constraints for the current group
350 // ============================================================================
351 bool SketchSolver_Group::resolveConstraints()
353 bool aResolved = false;
354 if (myStorage->isNeedToResolve() && !isEmpty()) {
355 myConstrSolver.setGroupID(myID);
356 myStorage->initializeSolver(myConstrSolver);
358 int aResult = myConstrSolver.solve();
359 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
360 myFeatureStorage->blockEvents(true);
361 ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
362 for (; aConstrIter != myConstraints.end(); aConstrIter++)
363 aConstrIter->second->refresh();
364 myFeatureStorage->blockEvents(false);
365 } else if (!myConstraints.empty())
366 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
370 removeTemporaryConstraints();
371 myStorage->setNeedToResolve(false);
375 // ============================================================================
376 // Function: mergeGroups
377 // Class: SketchSolver_Group
378 // Purpose: append specified group to the current group
379 // ============================================================================
380 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
382 // If specified group is empty, no need to merge
383 if (theGroup.isEmpty())
385 if (!myFeatureStorage)
386 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
388 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
389 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
390 changeConstraint(aConstrIter->first);
393 // ============================================================================
394 // Function: splitGroup
395 // Class: SketchSolver_Group
396 // Purpose: divide the group into several subgroups
397 // ============================================================================
398 void SketchSolver_Group::splitGroup(std::vector<SketchSolver_Group*>& theCuts)
400 // Obtain constraints, which should be separated
401 FeatureStoragePtr aNewFeatStorage(new SketchSolver_FeatureStorage);
402 std::vector<ConstraintPtr> anUnusedConstraints;
403 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
404 for ( ; aCIter != myConstraints.end(); aCIter++) {
405 std::list<ConstraintPtr> aBaseConstraints = aCIter->second->constraints();
406 std::list<ConstraintPtr>::iterator anIter = aBaseConstraints.begin();
407 for (; anIter != aBaseConstraints.end(); anIter++)
408 if (aNewFeatStorage->isInteract(*anIter)) {
409 aNewFeatStorage->changeConstraint(*anIter);
411 anUnusedConstraints.push_back(*anIter);
414 std::vector<SketchSolver_Group*>::iterator aCutsIter;
415 std::vector<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
416 for ( ; aUnuseIt != anUnusedConstraints.end(); aUnuseIt++) {
417 // Remove unused constraints
418 removeConstraint(*aUnuseIt);
419 // Try to append constraint to already existent group
420 for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); aCutsIter++)
421 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
422 (*aCutsIter)->changeConstraint(*aUnuseIt);
425 if (aCutsIter == theCuts.end()) {
427 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
428 aGroup->changeConstraint(*aUnuseIt);
429 theCuts.push_back(aGroup);
434 // ============================================================================
435 // Function: isConsistent
436 // Class: SketchSolver_Group
437 // Purpose: search removed entities and constraints
438 // ============================================================================
439 bool SketchSolver_Group::isConsistent()
441 if (!myFeatureStorage) // no one constraint is initialized yet
444 bool aResult = myFeatureStorage->isConsistent();
446 // remove invalid entities
447 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
448 while (aCIter != myConstraints.end()) {
449 std::list<ConstraintPtr> aConstraints = aCIter->second->constraints();
450 std::list<ConstraintPtr>::iterator anIt = aConstraints.begin();
451 for (; anIt != aConstraints.end(); anIt++)
452 if (!(*anIt)->data() || !(*anIt)->data()->isValid())
453 if (aCIter->second->remove(*anIt)) {
454 // the constraint is fully removed, detach it from the list
455 ConstraintConstraintMap::iterator aTmpIt = aCIter++;
456 myFeatureStorage->removeConstraint(aTmpIt->first);
457 myConstraints.erase(aTmpIt);
460 if (anIt == aConstraints.end())
467 // ============================================================================
468 // Function: removeTemporaryConstraints
469 // Class: SketchSolver_Group
470 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
471 // resolving the set of constraints
472 // ============================================================================
473 void SketchSolver_Group::removeTemporaryConstraints()
475 myTempConstraints.clear();
476 // Clean lists of removed entities in the storage
477 std::set<Slvs_hParam> aRemPar;
478 std::set<Slvs_hEntity> aRemEnt;
479 std::set<Slvs_hConstraint> aRemCon;
480 myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
481 myStorage->setNeedToResolve(false);
484 // ============================================================================
485 // Function: removeConstraint
486 // Class: SketchSolver_Group
487 // Purpose: remove constraint and all unused entities
488 // ============================================================================
489 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
491 myFeatureStorage->removeConstraint(theConstraint);
492 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
493 for (; aCIter != myConstraints.end(); aCIter++)
494 if (aCIter->second->hasConstraint(theConstraint)) {
495 if (!aCIter->second->remove(theConstraint)) // the constraint is not fully removed
496 aCIter = myConstraints.end();
499 if (aCIter != myConstraints.end())
500 myConstraints.erase(aCIter);