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_ConstraintCoincidence.h>
10 #include <SketchSolver_ConstraintDistance.h>
11 #include <SketchSolver_ConstraintEqual.h>
12 #include <SketchSolver_ConstraintFillet.h>
13 #include <SketchSolver_ConstraintLength.h>
14 #include <SketchSolver_ConstraintMirror.h>
15 #include <SketchSolver_ConstraintRigid.h>
16 #include <SketchSolver_ConstraintTangent.h>
17 #include <SketchSolver_ConstraintMultiRotation.h>
18 #include <SketchSolver_ConstraintMultiTranslation.h>
19 #include <SketchSolver_ConstraintMovement.h>
20 #include <SketchSolver_Error.h>
22 #include <GeomAPI_Edge.h>
23 #include <GeomDataAPI_Dir.h>
24 #include <GeomDataAPI_Point.h>
25 #include <GeomDataAPI_Point2D.h>
26 #include <Events_Error.h>
27 #include <ModelAPI_Attribute.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_ResultConstruction.h>
33 #include <SketchPlugin_Arc.h>
34 #include <SketchPlugin_Circle.h>
35 #include <SketchPlugin_Line.h>
36 #include <SketchPlugin_Point.h>
37 #include <SketchPlugin_ConstraintCoincidence.h>
38 #include <SketchPlugin_ConstraintDistance.h>
39 #include <SketchPlugin_ConstraintEqual.h>
40 #include <SketchPlugin_ConstraintFillet.h>
41 #include <SketchPlugin_ConstraintHorizontal.h>
42 #include <SketchPlugin_ConstraintLength.h>
43 #include <SketchPlugin_ConstraintMirror.h>
44 #include <SketchPlugin_ConstraintParallel.h>
45 #include <SketchPlugin_ConstraintPerpendicular.h>
46 #include <SketchPlugin_ConstraintRadius.h>
47 #include <SketchPlugin_ConstraintRigid.h>
48 #include <SketchPlugin_ConstraintTangent.h>
49 #include <SketchPlugin_ConstraintVertical.h>
50 #include <SketchPlugin_MultiRotation.h>
51 #include <SketchPlugin_MultiTranslation.h>
55 // Initialization of constraint builder self pointer
56 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
58 SketchSolver_Builder* SketchSolver_Builder::getInstance()
61 mySelf = new SketchSolver_Builder();
65 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
67 SolverConstraintPtr aResult;
68 DataPtr aData = theConstraint->data();
69 if (!aData || !aData->isValid())
73 // Verify attributes of constraint and generate errors
74 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
75 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
76 for (; anIter != anAttrList.end(); anIter++) {
77 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
79 if (aRefAttr->isObject() && aRefAttr->object()) {
80 ResultConstructionPtr aRC =
81 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
83 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
87 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
89 std::list<ObjectPtr> aList = aRefList->list();
90 std::list<ObjectPtr>::iterator aListIter = aList.begin();
91 for (; aListIter != aList.end(); aListIter++) {
92 ResultConstructionPtr aRC =
93 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
94 if (*aListIter && !aRC)
95 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
101 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
102 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
103 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
104 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
105 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
106 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
107 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
108 return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
109 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
110 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
111 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
112 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
113 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
114 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
115 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
116 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
117 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
118 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
119 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
120 return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
121 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
122 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
123 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
124 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
125 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
126 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
127 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
128 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
129 } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
130 return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
131 } else if (theConstraint->getKind() == SketchPlugin_ConstraintAngle::ID()) {
132 return SolverConstraintPtr(new SketchSolver_ConstraintAngle(theConstraint));
137 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
139 DataPtr aData = theFixedFeature->data();
140 if (!aData || !aData->isValid())
141 return SolverConstraintPtr();
142 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
145 SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature)
147 DataPtr aData = theFixedFeature->data();
148 if (!aData || !aData->isValid())
149 return SolverConstraintPtr();
150 return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature));
155 bool SketchSolver_Builder::createWorkplane(
156 CompositeFeaturePtr theSketch,
157 std::vector<Slvs_Entity>& theEntities,
158 std::vector<Slvs_Param>& theParameters)
160 DataPtr aSketchData = theSketch->data();
161 if (!aSketchData || !aSketchData->isValid())
162 return false; // the sketch is incorrect
164 // Get parameters of workplane
165 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
166 SketchPlugin_Sketch::DIRX_ID());
167 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
168 SketchPlugin_Sketch::NORM_ID());
169 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
170 SketchPlugin_Sketch::ORIGIN_ID());
171 // Create SolveSpace entity corresponding to the sketch origin
172 if (!createEntity(anOrigin, theEntities, theParameters))
174 Slvs_hEntity anOriginID = theEntities.back().h;
175 // Create SolveSpace entity corresponding the the sketch normal
176 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
178 Slvs_hEntity aNormalID = theEntities.back().h;
181 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
182 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
183 theEntities.push_back(aWorkplane);
187 bool SketchSolver_Builder::createEntity(
188 AttributePtr theAttribute,
189 std::vector<Slvs_Entity>& theEntities,
190 std::vector<Slvs_Param>& theParameters)
192 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
193 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
196 std::shared_ptr<GeomDataAPI_Point> aPoint =
197 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
199 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
200 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
201 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
202 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
203 aParamID-2, aParamID-1, aParamID));
207 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
208 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
210 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
211 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
212 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
213 aParamID-1, aParamID));
216 // Scalar value (used for the distance entities)
217 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
219 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
220 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
221 SLVS_E_UNKNOWN, aParamID));
224 // unknown attribute type
228 bool SketchSolver_Builder::createEntity(
229 FeaturePtr theFeature,
230 std::vector<Slvs_Entity>& theEntities,
231 std::vector<Slvs_Param>& theParameters)
233 if (!theFeature->data()->isValid())
236 // SketchPlugin features
237 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
238 SketchPlugin_Feature>(theFeature);
242 // Verify the feature by its kind
243 const std::string& aFeatureKind = aFeature->getKind();
244 DataPtr aData = aFeature->data();
246 if (aFeatureKind == SketchPlugin_Line::ID()) {
247 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
248 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
249 if (!aStart->isInitialized() || !aEnd->isInitialized())
251 if (!createEntity(aStart, theEntities, theParameters) ||
252 !createEntity(aEnd, theEntities, theParameters))
254 Slvs_hEntity aLineID = theEntities.back().h + 1;
255 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
256 aLineID-2, aLineID-1));
259 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
260 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
261 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
262 if (!aCenter->isInitialized() || !aRadius->isInitialized())
264 if (!createEntity(aCenter, theEntities, theParameters) ||
265 !createEntity(aRadius, theEntities, theParameters))
267 Slvs_hEntity aCircID = theEntities.back().h;
268 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
269 SLVS_E_UNKNOWN, aCircID-1));
272 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
273 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
274 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
275 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
276 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
278 if (!createEntity(aCenter, theEntities, theParameters) ||
279 !createEntity(aStart, theEntities, theParameters) ||
280 !createEntity(aEnd, theEntities, theParameters))
282 Slvs_hEntity anArcID = theEntities.back().h;
283 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
284 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
286 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
287 else if (aFeatureKind == SketchPlugin_Point::ID()) {
288 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
289 if (!aPoint->isInitialized() ||
290 !createEntity(aPoint, theEntities, theParameters))
292 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
293 // No need to add another entity.
298 bool SketchSolver_Builder::createNormal(
299 AttributePtr theNormal,
300 AttributePtr theDirX,
301 std::vector<Slvs_Entity>& theEntities,
302 std::vector<Slvs_Param>& theParameters)
304 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
305 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
306 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
307 !aNorm->isInitialized())
309 // calculate Y direction
310 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
312 // quaternion parameters of normal vector
313 double qw, qx, qy, qz;
314 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
316 double aNormCoord[4] = { qw, qx, qy, qz };
318 // Create parameters of the normal
319 Slvs_hParam aCurParam = theParameters.back().h;
320 for (int i = 0; i < 4; i++)
321 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
324 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
325 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
326 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
327 theEntities.push_back(aNormal);