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 <SketchSolver_ConstraintCoincidence.h>
9 #include <SketchSolver_ConstraintDistance.h>
10 #include <SketchSolver_ConstraintEqual.h>
11 #include <SketchSolver_ConstraintFillet.h>
12 #include <SketchSolver_ConstraintLength.h>
13 #include <SketchSolver_ConstraintMirror.h>
14 #include <SketchSolver_ConstraintRigid.h>
15 #include <SketchSolver_ConstraintTangent.h>
16 #include <SketchSolver_ConstraintMultiTranslation.h>
17 #include <SketchSolver_Error.h>
19 #include <GeomAPI_Edge.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomDataAPI_Point.h>
22 #include <GeomDataAPI_Point2D.h>
23 #include <Events_Error.h>
24 #include <ModelAPI_Attribute.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeRefAttr.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_ResultConstruction.h>
30 #include <SketchPlugin_Arc.h>
31 #include <SketchPlugin_Circle.h>
32 #include <SketchPlugin_Line.h>
33 #include <SketchPlugin_Point.h>
34 #include <SketchPlugin_ConstraintCoincidence.h>
35 #include <SketchPlugin_ConstraintDistance.h>
36 #include <SketchPlugin_ConstraintEqual.h>
37 #include <SketchPlugin_ConstraintFillet.h>
38 #include <SketchPlugin_ConstraintHorizontal.h>
39 #include <SketchPlugin_ConstraintLength.h>
40 #include <SketchPlugin_ConstraintMirror.h>
41 #include <SketchPlugin_ConstraintParallel.h>
42 #include <SketchPlugin_ConstraintPerpendicular.h>
43 #include <SketchPlugin_ConstraintRadius.h>
44 #include <SketchPlugin_ConstraintRigid.h>
45 #include <SketchPlugin_ConstraintTangent.h>
46 #include <SketchPlugin_ConstraintVertical.h>
47 #include <SketchPlugin_MultiTranslation.h>
51 // Initialization of constraint builder self pointer
52 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
54 SketchSolver_Builder* SketchSolver_Builder::getInstance()
57 mySelf = new SketchSolver_Builder();
61 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
63 SolverConstraintPtr aResult;
64 DataPtr aData = theConstraint->data();
65 if (!aData || !aData->isValid())
69 // Verify attributes of constraint and generate errors
70 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
71 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
72 for (; anIter != anAttrList.end(); anIter++) {
73 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
75 if (aRefAttr->isObject() && aRefAttr->object()) {
76 ResultConstructionPtr aRC =
77 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
79 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
83 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
85 std::list<ObjectPtr> aList = aRefList->list();
86 std::list<ObjectPtr>::iterator aListIter = aList.begin();
87 for (; aListIter != aList.end(); aListIter++) {
88 ResultConstructionPtr aRC =
89 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
90 if (*aListIter && !aRC)
91 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
97 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
98 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
99 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
100 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
101 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
102 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
103 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
104 return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
105 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
106 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
107 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
108 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
109 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
110 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
111 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
112 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
113 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
114 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
115 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
116 return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
117 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
118 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
119 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
120 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
121 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
122 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
123 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
124 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
129 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
131 DataPtr aData = theFixedFeature->data();
132 if (!aData || !aData->isValid())
133 return SolverConstraintPtr();
134 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
139 bool SketchSolver_Builder::createWorkplane(
140 CompositeFeaturePtr theSketch,
141 std::vector<Slvs_Entity>& theEntities,
142 std::vector<Slvs_Param>& theParameters)
144 DataPtr aSketchData = theSketch->data();
145 if (!aSketchData || !aSketchData->isValid())
146 return false; // the sketch is incorrect
148 // Get parameters of workplane
149 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
150 SketchPlugin_Sketch::DIRX_ID());
151 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
152 SketchPlugin_Sketch::NORM_ID());
153 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
154 SketchPlugin_Sketch::ORIGIN_ID());
155 // Create SolveSpace entity corresponding to the sketch origin
156 if (!createEntity(anOrigin, theEntities, theParameters))
158 Slvs_hEntity anOriginID = theEntities.back().h;
159 // Create SolveSpace entity corresponding the the sketch normal
160 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
162 Slvs_hEntity aNormalID = theEntities.back().h;
165 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
166 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
167 theEntities.push_back(aWorkplane);
171 bool SketchSolver_Builder::createEntity(
172 AttributePtr theAttribute,
173 std::vector<Slvs_Entity>& theEntities,
174 std::vector<Slvs_Param>& theParameters)
176 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
177 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
180 std::shared_ptr<GeomDataAPI_Point> aPoint =
181 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
183 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
184 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
185 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
186 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
187 aParamID-2, aParamID-1, aParamID));
191 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
192 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
194 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
195 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
196 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
197 aParamID-1, aParamID));
200 // Scalar value (used for the distance entities)
201 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
203 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
204 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
205 SLVS_E_UNKNOWN, aParamID));
208 // unknown attribute type
212 bool SketchSolver_Builder::createEntity(
213 FeaturePtr theFeature,
214 std::vector<Slvs_Entity>& theEntities,
215 std::vector<Slvs_Param>& theParameters)
217 if (!theFeature->data()->isValid())
220 // SketchPlugin features
221 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
222 SketchPlugin_Feature>(theFeature);
226 // Verify the feature by its kind
227 const std::string& aFeatureKind = aFeature->getKind();
228 DataPtr aData = aFeature->data();
230 if (aFeatureKind == SketchPlugin_Line::ID()) {
231 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
232 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
233 if (!aStart->isInitialized() || !aEnd->isInitialized())
235 if (!createEntity(aStart, theEntities, theParameters) ||
236 !createEntity(aEnd, theEntities, theParameters))
238 Slvs_hEntity aLineID = theEntities.back().h + 1;
239 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
240 aLineID-2, aLineID-1));
243 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
244 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
245 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
246 if (!aCenter->isInitialized() || !aRadius->isInitialized())
248 if (!createEntity(aCenter, theEntities, theParameters) ||
249 !createEntity(aRadius, theEntities, theParameters))
251 Slvs_hEntity aCircID = theEntities.back().h;
252 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
253 SLVS_E_UNKNOWN, aCircID-1));
256 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
257 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
258 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
259 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
260 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
262 if (!createEntity(aCenter, theEntities, theParameters) ||
263 !createEntity(aStart, theEntities, theParameters) ||
264 !createEntity(aEnd, theEntities, theParameters))
266 Slvs_hEntity anArcID = theEntities.back().h;
267 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
268 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
270 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
271 else if (aFeatureKind == SketchPlugin_Point::ID()) {
272 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
273 if (!aPoint->isInitialized() ||
274 !createEntity(aPoint, theEntities, theParameters))
276 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
277 // No need to add another entity.
282 bool SketchSolver_Builder::createNormal(
283 AttributePtr theNormal,
284 AttributePtr theDirX,
285 std::vector<Slvs_Entity>& theEntities,
286 std::vector<Slvs_Param>& theParameters)
288 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
289 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
290 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance))
292 // calculate Y direction
293 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
295 // quaternion parameters of normal vector
296 double qw, qx, qy, qz;
297 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
299 double aNormCoord[4] = { qw, qx, qy, qz };
301 // Create parameters of the normal
302 Slvs_hParam aCurParam = theParameters.back().h;
303 for (int i = 0; i < 4; i++)
304 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
307 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
308 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
309 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
310 theEntities.push_back(aNormal);