1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Builder.cpp
4 // Created: 25 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Builder.h"
8 #include <SketchPlugin_ConstraintAngle.h>
9 #include <SketchSolver_ConstraintAngle.h>
10 #include <SketchSolver_ConstraintCoincidence.h>
11 #include <SketchSolver_ConstraintDistance.h>
12 #include <SketchSolver_ConstraintEqual.h>
13 #include <SketchSolver_ConstraintFillet.h>
14 #include <SketchSolver_ConstraintLength.h>
15 #include <SketchSolver_ConstraintMirror.h>
16 #include <SketchSolver_ConstraintRigid.h>
17 #include <SketchSolver_ConstraintTangent.h>
18 #include <SketchSolver_ConstraintMultiRotation.h>
19 #include <SketchSolver_ConstraintMultiTranslation.h>
20 #include <SketchSolver_ConstraintMovement.h>
21 #include <SketchSolver_Error.h>
23 #include <GeomAPI_Edge.h>
24 #include <GeomDataAPI_Dir.h>
25 #include <GeomDataAPI_Point.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <Events_Error.h>
28 #include <ModelAPI_Attribute.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_ResultConstruction.h>
34 #include <SketchPlugin_Arc.h>
35 #include <SketchPlugin_Circle.h>
36 #include <SketchPlugin_Line.h>
37 #include <SketchPlugin_Point.h>
38 #include <SketchPlugin_ConstraintCoincidence.h>
39 #include <SketchPlugin_ConstraintDistance.h>
40 #include <SketchPlugin_ConstraintEqual.h>
41 #include <SketchPlugin_ConstraintFillet.h>
42 #include <SketchPlugin_ConstraintHorizontal.h>
43 #include <SketchPlugin_ConstraintLength.h>
44 #include <SketchPlugin_ConstraintMirror.h>
45 #include <SketchPlugin_ConstraintParallel.h>
46 #include <SketchPlugin_ConstraintPerpendicular.h>
47 #include <SketchPlugin_ConstraintRadius.h>
48 #include <SketchPlugin_ConstraintRigid.h>
49 #include <SketchPlugin_ConstraintTangent.h>
50 #include <SketchPlugin_ConstraintVertical.h>
51 #include <SketchPlugin_MultiRotation.h>
52 #include <SketchPlugin_MultiTranslation.h>
56 // Initialization of constraint builder self pointer
57 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
59 SketchSolver_Builder* SketchSolver_Builder::getInstance()
62 mySelf = new SketchSolver_Builder();
66 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
68 SolverConstraintPtr aResult;
69 DataPtr aData = theConstraint->data();
70 if (!aData || !aData->isValid())
74 // Verify attributes of constraint and generate errors
75 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
76 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
77 for (; anIter != anAttrList.end(); anIter++) {
78 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
80 if (aRefAttr->isObject() && aRefAttr->object()) {
81 ResultConstructionPtr aRC =
82 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
84 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
88 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
90 std::list<ObjectPtr> aList = aRefList->list();
91 std::list<ObjectPtr>::iterator aListIter = aList.begin();
92 for (; aListIter != aList.end(); aListIter++) {
93 ResultConstructionPtr aRC =
94 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
95 if (*aListIter && !aRC)
96 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
102 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
103 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
104 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
105 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
106 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
107 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
108 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
109 return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
110 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
111 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
112 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
113 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
114 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
115 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
116 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
117 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
118 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
119 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
120 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
121 return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
122 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
123 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
124 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
125 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
126 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
127 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
128 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
129 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
130 } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
131 return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
132 } else if (theConstraint->getKind() == SketchPlugin_ConstraintAngle::ID()) {
133 return SolverConstraintPtr(new SketchSolver_ConstraintAngle(theConstraint));
138 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
140 DataPtr aData = theFixedFeature->data();
141 if (!aData || !aData->isValid())
142 return SolverConstraintPtr();
143 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
146 SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature)
148 DataPtr aData = theFixedFeature->data();
149 if (!aData || !aData->isValid())
150 return SolverConstraintPtr();
151 return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature));
156 bool SketchSolver_Builder::createWorkplane(
157 CompositeFeaturePtr theSketch,
158 std::vector<Slvs_Entity>& theEntities,
159 std::vector<Slvs_Param>& theParameters)
161 DataPtr aSketchData = theSketch->data();
162 if (!aSketchData || !aSketchData->isValid())
163 return false; // the sketch is incorrect
165 // Get parameters of workplane
166 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
167 SketchPlugin_Sketch::DIRX_ID());
168 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
169 SketchPlugin_Sketch::NORM_ID());
170 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
171 SketchPlugin_Sketch::ORIGIN_ID());
172 // Create SolveSpace entity corresponding to the sketch origin
173 if (!createEntity(anOrigin, theEntities, theParameters))
175 Slvs_hEntity anOriginID = theEntities.back().h;
176 // Create SolveSpace entity corresponding the the sketch normal
177 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
179 Slvs_hEntity aNormalID = theEntities.back().h;
182 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
183 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
184 theEntities.push_back(aWorkplane);
188 bool SketchSolver_Builder::createEntity(
189 AttributePtr theAttribute,
190 std::vector<Slvs_Entity>& theEntities,
191 std::vector<Slvs_Param>& theParameters)
193 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
194 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
197 std::shared_ptr<GeomDataAPI_Point> aPoint =
198 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
200 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
201 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
202 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
203 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
204 aParamID-2, aParamID-1, aParamID));
208 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
209 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
211 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
212 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
213 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
214 aParamID-1, aParamID));
217 // Scalar value (used for the distance entities)
218 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
220 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
221 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
222 SLVS_E_UNKNOWN, aParamID));
225 // unknown attribute type
229 bool SketchSolver_Builder::createEntity(
230 FeaturePtr theFeature,
231 std::vector<Slvs_Entity>& theEntities,
232 std::vector<Slvs_Param>& theParameters)
234 if (!theFeature->data()->isValid())
237 // SketchPlugin features
238 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
239 SketchPlugin_Feature>(theFeature);
243 // Verify the feature by its kind
244 const std::string& aFeatureKind = aFeature->getKind();
245 DataPtr aData = aFeature->data();
247 if (aFeatureKind == SketchPlugin_Line::ID()) {
248 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
249 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
250 if (!aStart->isInitialized() || !aEnd->isInitialized())
252 if (!createEntity(aStart, theEntities, theParameters) ||
253 !createEntity(aEnd, theEntities, theParameters))
255 Slvs_hEntity aLineID = theEntities.back().h + 1;
256 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
257 aLineID-2, aLineID-1));
260 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
261 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
262 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
263 if (!aCenter->isInitialized() || !aRadius->isInitialized())
265 if (!createEntity(aCenter, theEntities, theParameters) ||
266 !createEntity(aRadius, theEntities, theParameters))
268 Slvs_hEntity aCircID = theEntities.back().h;
269 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
270 SLVS_E_UNKNOWN, aCircID-1));
273 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
274 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
275 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
276 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
277 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
279 if (!createEntity(aCenter, theEntities, theParameters) ||
280 !createEntity(aStart, theEntities, theParameters) ||
281 !createEntity(aEnd, theEntities, theParameters))
283 Slvs_hEntity anArcID = theEntities.back().h;
284 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
285 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
287 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
288 else if (aFeatureKind == SketchPlugin_Point::ID()) {
289 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
290 if (!aPoint->isInitialized() ||
291 !createEntity(aPoint, theEntities, theParameters))
293 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
294 // No need to add another entity.
299 bool SketchSolver_Builder::createNormal(
300 AttributePtr theNormal,
301 AttributePtr theDirX,
302 std::vector<Slvs_Entity>& theEntities,
303 std::vector<Slvs_Param>& theParameters)
305 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
306 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
307 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
308 !aNorm->isInitialized())
310 // calculate Y direction
311 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
313 // quaternion parameters of normal vector
314 double qw, qx, qy, qz;
315 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
317 double aNormCoord[4] = { qw, qx, qy, qz };
319 // Create parameters of the normal
320 Slvs_hParam aCurParam = theParameters.back().h;
321 for (int i = 0; i < 4; i++)
322 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
325 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
326 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
327 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
328 theEntities.push_back(aNormal);