1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_Constraint.h>
4 #include <SketchSolver_Error.h>
6 #include <PlaneGCSSolver_AttributeBuilder.h>
7 #include <PlaneGCSSolver_Tools.h>
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Point.h>
14 #include <SketchPlugin_ConstraintAngle.h>
15 #include <SketchPlugin_ConstraintCoincidence.h>
16 #include <SketchPlugin_ConstraintCollinear.h>
17 #include <SketchPlugin_ConstraintDistance.h>
18 #include <SketchPlugin_ConstraintEqual.h>
19 #include <SketchPlugin_ConstraintHorizontal.h>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintMiddle.h>
22 #include <SketchPlugin_ConstraintMirror.h>
23 #include <SketchPlugin_ConstraintParallel.h>
24 #include <SketchPlugin_ConstraintPerpendicular.h>
25 #include <SketchPlugin_ConstraintRadius.h>
26 #include <SketchPlugin_ConstraintRigid.h>
27 #include <SketchPlugin_ConstraintTangent.h>
28 #include <SketchPlugin_ConstraintVertical.h>
30 #include <GeomAPI_Dir2d.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_ResultConstruction.h>
38 SketchSolver_Constraint::SketchSolver_Constraint(
39 ConstraintPtr theConstraint)
40 : myBaseConstraint(theConstraint),
41 myType(CONSTRAINT_UNKNOWN)
45 void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
47 myStorage = theStorage;
48 blockEvents(theEvensBlocked);
49 // Process constraint according to its type
53 void SketchSolver_Constraint::blockEvents(bool isBlocked)
55 myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
59 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
61 const std::string& aType = theConstraint->getKind();
62 if (aType == SketchPlugin_ConstraintCoincidence::ID())
63 return CONSTRAINT_COINCIDENCE;
64 else if (aType == SketchPlugin_ConstraintRigid::ID())
65 return CONSTRAINT_FIXED;
66 else if (aType == SketchPlugin_ConstraintHorizontal::ID())
67 return CONSTRAINT_HORIZONTAL;
68 else if (aType == SketchPlugin_ConstraintVertical::ID())
69 return CONSTRAINT_VERTICAL;
70 else if (aType == SketchPlugin_ConstraintAngle::ID())
71 return CONSTRAINT_ANGLE;
72 else if (aType == SketchPlugin_ConstraintDistance::ID())
73 return CONSTRAINT_DISTANCE;
74 else if (aType == SketchPlugin_ConstraintEqual::ID())
75 return CONSTRAINT_EQUAL;
76 else if (aType == SketchPlugin_ConstraintLength::ID())
77 return CONSTRAINT_PT_PT_DISTANCE;
78 else if (aType == SketchPlugin_ConstraintMirror::ID())
79 return CONSTRAINT_SYMMETRIC;
80 else if (aType == SketchPlugin_ConstraintParallel::ID())
81 return CONSTRAINT_PARALLEL;
82 else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
83 return CONSTRAINT_PERPENDICULAR;
84 else if (aType == SketchPlugin_ConstraintRadius::ID())
85 return CONSTRAINT_RADIUS;
86 else if (aType == SketchPlugin_ConstraintTangent::ID())
87 return CONSTRAINT_TANGENT;
88 else if (aType == SketchPlugin_ConstraintCollinear::ID())
89 return CONSTRAINT_COLLINEAR;
90 else if (aType == SketchPlugin_ConstraintMiddle::ID())
91 return CONSTRAINT_MIDDLE_POINT;
92 return CONSTRAINT_UNKNOWN;
95 void SketchSolver_Constraint::process()
98 if (!myBaseConstraint || !myStorage) {
99 // Not enough parameters are assigned
103 SketchSolver_ConstraintType aConstrType = getType();
104 EntityWrapperPtr aValue;
105 std::vector<EntityWrapperPtr> anAttributes;
106 getAttributes(aValue, anAttributes);
107 if (!myErrorMsg.empty())
109 if (anAttributes.empty()) {
110 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
113 if (aConstrType == CONSTRAINT_UNKNOWN)
114 aConstrType = getType();
116 ConstraintWrapperPtr aNewConstraint = PlaneGCSSolver_Tools::createConstraint(
117 myBaseConstraint, aConstrType,
118 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
119 myStorage->addConstraint(myBaseConstraint, aNewConstraint);
124 void SketchSolver_Constraint::update()
128 // Get list of attributes of the constraint and compare it with previously stored.
129 // If the lists are different, fully rebuild constraint
130 std::set<EntityWrapperPtr> anAttributes;
131 for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
132 AttributePtr anAttr =
133 myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
137 if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
138 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
139 FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
141 // Workaround for the Length constraint: add points of line, not line itself
142 anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
143 anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
146 anAttributes.insert(myStorage->entity(anAttr));
149 std::set<EntityWrapperPtr>::iterator aFound;
150 std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
151 for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
152 anAttributes.erase(*anAttrIt);
154 if (!anAttributes.empty()) {
160 AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
162 myStorage->update(aValueAttr);
167 bool SketchSolver_Constraint::remove()
170 myType = CONSTRAINT_UNKNOWN;
171 myStorage->unsubscribeUpdates(this);
172 return myStorage->removeConstraint(myBaseConstraint);
175 void SketchSolver_Constraint::getAttributes(
176 EntityWrapperPtr& theValue,
177 std::vector<EntityWrapperPtr>& theAttributes)
179 static const int anInitNbOfAttr = 4;
180 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
181 myAttributes.clear();
183 DataPtr aData = myBaseConstraint->data();
185 myType = TYPE(myBaseConstraint);
187 AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
189 PlaneGCSSolver_AttributeBuilder aValueBuilder;
190 theValue = aValueBuilder.createAttribute(aValueAttr);
191 myStorage->addEntity(aValueAttr, theValue);
194 int aPtInd = 0; // index of first point in the list of attributes
195 int aEntInd = 2; // index of first entity in the list of attributes
196 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
197 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
198 for (; anIter != aConstrAttrs.end(); anIter++) {
199 AttributeRefAttrPtr aRefAttr =
200 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
201 if (!aRefAttr || !aRefAttr->isInitialized()) {
202 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
206 myStorage->update(*anIter, true);
207 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
208 myAttributes.push_back(anEntity);
210 SketchSolver_EntityType aType = anEntity->type();
211 if (aType == ENTITY_UNKNOWN)
213 else if (aType == ENTITY_POINT)
214 theAttributes[aPtInd++] = anEntity; // the point is created
215 else { // another entity (not a point) is created
216 if (aEntInd < anInitNbOfAttr)
217 theAttributes[aEntInd] = anEntity;
219 theAttributes.push_back(anEntity);