1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_Constraint.h>
4 #include <SketchSolver_Group.h>
5 #include <SketchSolver_Error.h>
6 #include <SketchSolver_Manager.h>
8 #include <PlaneGCSSolver_AttributeBuilder.h>
10 #include <SketchPlugin_Arc.h>
11 #include <SketchPlugin_Circle.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Point.h>
15 #include <SketchPlugin_ConstraintAngle.h>
16 #include <SketchPlugin_ConstraintCoincidence.h>
17 #include <SketchPlugin_ConstraintCollinear.h>
18 #include <SketchPlugin_ConstraintDistance.h>
19 #include <SketchPlugin_ConstraintEqual.h>
20 #include <SketchPlugin_ConstraintHorizontal.h>
21 #include <SketchPlugin_ConstraintLength.h>
22 #include <SketchPlugin_ConstraintMiddle.h>
23 #include <SketchPlugin_ConstraintMirror.h>
24 #include <SketchPlugin_ConstraintParallel.h>
25 #include <SketchPlugin_ConstraintPerpendicular.h>
26 #include <SketchPlugin_ConstraintRadius.h>
27 #include <SketchPlugin_ConstraintRigid.h>
28 #include <SketchPlugin_ConstraintTangent.h>
29 #include <SketchPlugin_ConstraintVertical.h>
31 #include <GeomAPI_Dir2d.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Point2D.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_ResultConstruction.h>
39 SketchSolver_Constraint::SketchSolver_Constraint(
40 ConstraintPtr theConstraint)
41 : myBaseConstraint(theConstraint),
42 myType(CONSTRAINT_UNKNOWN)
46 void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
48 myStorage = theStorage;
49 blockEvents(theEvensBlocked);
50 // Process constraint according to its type
54 void SketchSolver_Constraint::blockEvents(bool isBlocked)
56 myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
60 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
62 const std::string& aType = theConstraint->getKind();
63 if (aType == SketchPlugin_ConstraintCoincidence::ID())
64 return CONSTRAINT_COINCIDENCE;
65 else if (aType == SketchPlugin_ConstraintRigid::ID())
66 return CONSTRAINT_FIXED;
67 else if (aType == SketchPlugin_ConstraintHorizontal::ID())
68 return CONSTRAINT_HORIZONTAL;
69 else if (aType == SketchPlugin_ConstraintVertical::ID())
70 return CONSTRAINT_VERTICAL;
71 else if (aType == SketchPlugin_ConstraintAngle::ID())
72 return CONSTRAINT_ANGLE;
73 else if (aType == SketchPlugin_ConstraintDistance::ID())
74 return CONSTRAINT_DISTANCE;
75 else if (aType == SketchPlugin_ConstraintEqual::ID())
76 return CONSTRAINT_EQUAL;
77 else if (aType == SketchPlugin_ConstraintLength::ID())
78 return CONSTRAINT_PT_PT_DISTANCE;
79 else if (aType == SketchPlugin_ConstraintMirror::ID())
80 return CONSTRAINT_SYMMETRIC;
81 else if (aType == SketchPlugin_ConstraintParallel::ID())
82 return CONSTRAINT_PARALLEL;
83 else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
84 return CONSTRAINT_PERPENDICULAR;
85 else if (aType == SketchPlugin_ConstraintRadius::ID())
86 return CONSTRAINT_RADIUS;
87 else if (aType == SketchPlugin_ConstraintTangent::ID())
88 return CONSTRAINT_TANGENT;
89 else if (aType == SketchPlugin_ConstraintCollinear::ID())
90 return CONSTRAINT_COLLINEAR;
91 else if (aType == SketchPlugin_ConstraintMiddle::ID())
92 return CONSTRAINT_MIDDLE_POINT;
93 return CONSTRAINT_UNKNOWN;
96 void SketchSolver_Constraint::process()
99 if (!myBaseConstraint || !myStorage) {
100 // Not enough parameters are assigned
104 SketchSolver_ConstraintType aConstrType = getType();
105 EntityWrapperPtr aValue;
106 std::vector<EntityWrapperPtr> anAttributes;
107 getAttributes(aValue, anAttributes);
108 if (!myErrorMsg.empty())
110 if (anAttributes.empty()) {
111 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
114 if (aConstrType == CONSTRAINT_UNKNOWN)
115 aConstrType = getType();
117 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
118 ConstraintWrapperPtr aNewConstraint = aBuilder->createConstraint(
119 myBaseConstraint, aConstrType,
120 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
121 myStorage->addConstraint(myBaseConstraint, aNewConstraint);
126 void SketchSolver_Constraint::update()
130 // Get list of attributes of the constraint and compare it with previously stored.
131 // If the lists are different, fully rebuild constraint
132 std::set<EntityWrapperPtr> anAttributes;
133 for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
134 AttributePtr anAttr =
135 myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
139 if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
140 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
141 FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
143 // Workaround for the Length constraint: add points of line, not line itself
144 anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
145 anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
148 anAttributes.insert(myStorage->entity(anAttr));
151 std::set<EntityWrapperPtr>::iterator aFound;
152 std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
153 for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
154 anAttributes.erase(*anAttrIt);
156 if (!anAttributes.empty()) {
162 AttributeDoublePtr aValueAttr = myBaseConstraint->real(SketchPlugin_Constraint::VALUE());
164 myStorage->update(aValueAttr);
169 bool SketchSolver_Constraint::remove()
172 myType = CONSTRAINT_UNKNOWN;
173 myStorage->unsubscribeUpdates(this);
174 return myStorage->removeConstraint(myBaseConstraint);
177 void SketchSolver_Constraint::getAttributes(
178 EntityWrapperPtr& theValue,
179 std::vector<EntityWrapperPtr>& theAttributes)
181 static const int anInitNbOfAttr = 4;
182 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
183 myAttributes.clear();
185 DataPtr aData = myBaseConstraint->data();
186 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
188 myType = TYPE(myBaseConstraint);
190 AttributeDoublePtr aValueAttr = aData->real(SketchPlugin_Constraint::VALUE());
192 PlaneGCSSolver_AttributeBuilder aValueBuilder;
193 theValue = aValueBuilder.createAttribute(aValueAttr);
194 myStorage->addEntity(aValueAttr, theValue);
197 int aPtInd = 0; // index of first point in the list of attributes
198 int aEntInd = 2; // index of first entity in the list of attributes
199 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
200 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
201 for (; anIter != aConstrAttrs.end(); anIter++) {
202 AttributeRefAttrPtr aRefAttr =
203 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
204 if (!aRefAttr || !aRefAttr->isInitialized()) {
205 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
209 myStorage->update(*anIter, true);
210 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
211 myAttributes.push_back(anEntity);
213 SketchSolver_EntityType aType = anEntity->type();
214 if (aType == ENTITY_UNKNOWN)
216 else if (aType == ENTITY_POINT)
217 theAttributes[aPtInd++] = anEntity; // the point is created
218 else { // another entity (not a point) is created
219 if (aEntInd < anInitNbOfAttr)
220 theAttributes[aEntInd] = anEntity;
222 theAttributes.push_back(anEntity);