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_ConstraintParametric.h>
22 #include <SketchSolver_Error.h>
24 #include <GeomAPI_Edge.h>
25 #include <GeomDataAPI_Dir.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <Events_Error.h>
29 #include <ModelAPI_Attribute.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeRefList.h>
33 #include <ModelAPI_ResultConstruction.h>
35 #include <SketchPlugin_Arc.h>
36 #include <SketchPlugin_Circle.h>
37 #include <SketchPlugin_Line.h>
38 #include <SketchPlugin_Point.h>
39 #include <SketchPlugin_ConstraintCoincidence.h>
40 #include <SketchPlugin_ConstraintDistance.h>
41 #include <SketchPlugin_ConstraintEqual.h>
42 #include <SketchPlugin_ConstraintFillet.h>
43 #include <SketchPlugin_ConstraintHorizontal.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintMirror.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 #include <SketchPlugin_ConstraintRigid.h>
50 #include <SketchPlugin_ConstraintTangent.h>
51 #include <SketchPlugin_ConstraintVertical.h>
52 #include <SketchPlugin_MultiRotation.h>
53 #include <SketchPlugin_MultiTranslation.h>
57 // Initialization of constraint builder self pointer
58 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
60 SketchSolver_Builder* SketchSolver_Builder::getInstance()
63 mySelf = new SketchSolver_Builder();
67 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
69 SolverConstraintPtr aResult;
70 DataPtr aData = theConstraint->data();
71 if (!aData || !aData->isValid())
75 // Verify attributes of constraint and generate errors
76 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
77 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
78 for (; anIter != anAttrList.end(); anIter++) {
79 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
81 if (aRefAttr->isObject() && aRefAttr->object()) {
82 ResultConstructionPtr aRC =
83 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
85 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
89 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
91 std::list<ObjectPtr> aList = aRefList->list();
92 std::list<ObjectPtr>::iterator aListIter = aList.begin();
93 for (; aListIter != aList.end(); aListIter++) {
94 ResultConstructionPtr aRC =
95 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
96 if (*aListIter && !aRC)
97 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
103 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
104 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
105 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
106 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
107 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
108 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
109 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
110 return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
111 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
112 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
113 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
114 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
115 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
116 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
117 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
118 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
119 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
120 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
121 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
122 return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
123 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
124 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
125 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
126 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
127 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
128 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
129 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
130 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
131 } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
132 return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
133 } else if (theConstraint->getKind() == SketchPlugin_ConstraintAngle::ID()) {
134 return SolverConstraintPtr(new SketchSolver_ConstraintAngle(theConstraint));
139 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
141 DataPtr aData = theFixedFeature->data();
142 if (!aData || !aData->isValid())
143 return SolverConstraintPtr();
144 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
147 SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature)
149 DataPtr aData = theFixedFeature->data();
150 if (!aData || !aData->isValid())
151 return SolverConstraintPtr();
152 return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature));
155 SolverConstraintPtr SketchSolver_Builder::createParametricConstraint(AttributePtr theAttribute)
157 return SolverConstraintPtr(new SketchSolver_ConstraintParametric(theAttribute));
162 bool SketchSolver_Builder::createWorkplane(
163 CompositeFeaturePtr theSketch,
164 std::vector<Slvs_Entity>& theEntities,
165 std::vector<Slvs_Param>& theParameters)
167 DataPtr aSketchData = theSketch->data();
168 if (!aSketchData || !aSketchData->isValid())
169 return false; // the sketch is incorrect
171 // Get parameters of workplane
172 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
173 SketchPlugin_Sketch::DIRX_ID());
174 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
175 SketchPlugin_Sketch::NORM_ID());
176 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
177 SketchPlugin_Sketch::ORIGIN_ID());
178 // Create SolveSpace entity corresponding to the sketch origin
179 if (!createEntity(anOrigin, theEntities, theParameters))
181 Slvs_hEntity anOriginID = theEntities.back().h;
182 // Create SolveSpace entity corresponding the the sketch normal
183 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
185 Slvs_hEntity aNormalID = theEntities.back().h;
188 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
189 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
190 theEntities.push_back(aWorkplane);
194 bool SketchSolver_Builder::createEntity(
195 AttributePtr theAttribute,
196 std::vector<Slvs_Entity>& theEntities,
197 std::vector<Slvs_Param>& theParameters)
199 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
200 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
203 std::shared_ptr<GeomDataAPI_Point> aPoint =
204 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
206 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
207 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
208 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
209 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
210 aParamID-2, aParamID-1, aParamID));
214 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
215 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
217 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
218 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
219 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
220 aParamID-1, aParamID));
223 // Scalar value (used for the distance entities)
224 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
226 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
227 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
228 SLVS_E_UNKNOWN, aParamID));
231 // unknown attribute type
235 bool SketchSolver_Builder::createEntity(
236 FeaturePtr theFeature,
237 std::vector<Slvs_Entity>& theEntities,
238 std::vector<Slvs_Param>& theParameters)
240 if (!theFeature->data()->isValid())
243 // SketchPlugin features
244 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
245 SketchPlugin_Feature>(theFeature);
249 // Verify the feature by its kind
250 const std::string& aFeatureKind = aFeature->getKind();
251 DataPtr aData = aFeature->data();
253 if (aFeatureKind == SketchPlugin_Line::ID()) {
254 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
255 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
256 if (!aStart->isInitialized() || !aEnd->isInitialized())
258 if (!createEntity(aStart, theEntities, theParameters) ||
259 !createEntity(aEnd, theEntities, theParameters))
261 Slvs_hEntity aLineID = theEntities.back().h + 1;
262 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
263 aLineID-2, aLineID-1));
266 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
267 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
268 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
269 if (!aCenter->isInitialized() || !aRadius->isInitialized())
271 if (!createEntity(aCenter, theEntities, theParameters) ||
272 !createEntity(aRadius, theEntities, theParameters))
274 Slvs_hEntity aCircID = theEntities.back().h;
275 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
276 SLVS_E_UNKNOWN, aCircID-1));
279 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
280 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
281 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
282 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
283 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
285 if (!createEntity(aCenter, theEntities, theParameters) ||
286 !createEntity(aStart, theEntities, theParameters) ||
287 !createEntity(aEnd, theEntities, theParameters))
289 Slvs_hEntity anArcID = theEntities.back().h;
290 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
291 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
293 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
294 else if (aFeatureKind == SketchPlugin_Point::ID()) {
295 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
296 if (!aPoint->isInitialized() ||
297 !createEntity(aPoint, theEntities, theParameters))
299 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
300 // No need to add another entity.
305 bool SketchSolver_Builder::createNormal(
306 AttributePtr theNormal,
307 AttributePtr theDirX,
308 std::vector<Slvs_Entity>& theEntities,
309 std::vector<Slvs_Param>& theParameters)
311 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
312 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
313 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
314 !aNorm->isInitialized())
316 // calculate Y direction
317 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
319 // quaternion parameters of normal vector
320 double qw, qx, qy, qz;
321 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
323 double aNormCoord[4] = { qw, qx, qy, qz };
325 // Create parameters of the normal
326 Slvs_hParam aCurParam = theParameters.back().h;
327 for (int i = 0; i < 4; i++)
328 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
331 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
332 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
333 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
334 theEntities.push_back(aNormal);