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_ConstraintMultiRotation.h>
17 #include <SketchSolver_ConstraintMultiTranslation.h>
18 #include <SketchSolver_Error.h>
20 #include <GeomAPI_Edge.h>
21 #include <GeomDataAPI_Dir.h>
22 #include <GeomDataAPI_Point.h>
23 #include <GeomDataAPI_Point2D.h>
24 #include <Events_Error.h>
25 #include <ModelAPI_Attribute.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeRefAttr.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_ResultConstruction.h>
31 #include <SketchPlugin_Arc.h>
32 #include <SketchPlugin_Circle.h>
33 #include <SketchPlugin_Line.h>
34 #include <SketchPlugin_Point.h>
35 #include <SketchPlugin_ConstraintCoincidence.h>
36 #include <SketchPlugin_ConstraintDistance.h>
37 #include <SketchPlugin_ConstraintEqual.h>
38 #include <SketchPlugin_ConstraintFillet.h>
39 #include <SketchPlugin_ConstraintHorizontal.h>
40 #include <SketchPlugin_ConstraintLength.h>
41 #include <SketchPlugin_ConstraintMirror.h>
42 #include <SketchPlugin_ConstraintParallel.h>
43 #include <SketchPlugin_ConstraintPerpendicular.h>
44 #include <SketchPlugin_ConstraintRadius.h>
45 #include <SketchPlugin_ConstraintRigid.h>
46 #include <SketchPlugin_ConstraintTangent.h>
47 #include <SketchPlugin_ConstraintVertical.h>
48 #include <SketchPlugin_MultiRotation.h>
49 #include <SketchPlugin_MultiTranslation.h>
53 // Initialization of constraint builder self pointer
54 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
56 SketchSolver_Builder* SketchSolver_Builder::getInstance()
59 mySelf = new SketchSolver_Builder();
63 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
65 SolverConstraintPtr aResult;
66 DataPtr aData = theConstraint->data();
67 if (!aData || !aData->isValid())
71 // Verify attributes of constraint and generate errors
72 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
73 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
74 for (; anIter != anAttrList.end(); anIter++) {
75 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
77 if (aRefAttr->isObject() && aRefAttr->object()) {
78 ResultConstructionPtr aRC =
79 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
81 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
85 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
87 std::list<ObjectPtr> aList = aRefList->list();
88 std::list<ObjectPtr>::iterator aListIter = aList.begin();
89 for (; aListIter != aList.end(); aListIter++) {
90 ResultConstructionPtr aRC =
91 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
92 if (*aListIter && !aRC)
93 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
99 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
100 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
101 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
102 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
103 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
104 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
105 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
106 return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
107 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
108 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
109 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
110 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
111 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
112 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
113 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
114 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
115 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
116 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
117 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
118 return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
119 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
120 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
121 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
122 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
123 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
124 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
125 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
126 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
127 } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
128 return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
133 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
135 DataPtr aData = theFixedFeature->data();
136 if (!aData || !aData->isValid())
137 return SolverConstraintPtr();
138 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
143 bool SketchSolver_Builder::createWorkplane(
144 CompositeFeaturePtr theSketch,
145 std::vector<Slvs_Entity>& theEntities,
146 std::vector<Slvs_Param>& theParameters)
148 DataPtr aSketchData = theSketch->data();
149 if (!aSketchData || !aSketchData->isValid())
150 return false; // the sketch is incorrect
152 // Get parameters of workplane
153 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
154 SketchPlugin_Sketch::DIRX_ID());
155 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
156 SketchPlugin_Sketch::NORM_ID());
157 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
158 SketchPlugin_Sketch::ORIGIN_ID());
159 // Create SolveSpace entity corresponding to the sketch origin
160 if (!createEntity(anOrigin, theEntities, theParameters))
162 Slvs_hEntity anOriginID = theEntities.back().h;
163 // Create SolveSpace entity corresponding the the sketch normal
164 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
166 Slvs_hEntity aNormalID = theEntities.back().h;
169 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
170 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
171 theEntities.push_back(aWorkplane);
175 bool SketchSolver_Builder::createEntity(
176 AttributePtr theAttribute,
177 std::vector<Slvs_Entity>& theEntities,
178 std::vector<Slvs_Param>& theParameters)
180 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
181 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
184 std::shared_ptr<GeomDataAPI_Point> aPoint =
185 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
187 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
188 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
189 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
190 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
191 aParamID-2, aParamID-1, aParamID));
195 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
196 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
198 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
199 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
200 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
201 aParamID-1, aParamID));
204 // Scalar value (used for the distance entities)
205 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
207 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
208 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
209 SLVS_E_UNKNOWN, aParamID));
212 // unknown attribute type
216 bool SketchSolver_Builder::createEntity(
217 FeaturePtr theFeature,
218 std::vector<Slvs_Entity>& theEntities,
219 std::vector<Slvs_Param>& theParameters)
221 if (!theFeature->data()->isValid())
224 // SketchPlugin features
225 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
226 SketchPlugin_Feature>(theFeature);
230 // Verify the feature by its kind
231 const std::string& aFeatureKind = aFeature->getKind();
232 DataPtr aData = aFeature->data();
234 if (aFeatureKind == SketchPlugin_Line::ID()) {
235 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
236 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
237 if (!aStart->isInitialized() || !aEnd->isInitialized())
239 if (!createEntity(aStart, theEntities, theParameters) ||
240 !createEntity(aEnd, theEntities, theParameters))
242 Slvs_hEntity aLineID = theEntities.back().h + 1;
243 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
244 aLineID-2, aLineID-1));
247 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
248 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
249 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
250 if (!aCenter->isInitialized() || !aRadius->isInitialized())
252 if (!createEntity(aCenter, theEntities, theParameters) ||
253 !createEntity(aRadius, theEntities, theParameters))
255 Slvs_hEntity aCircID = theEntities.back().h;
256 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
257 SLVS_E_UNKNOWN, aCircID-1));
260 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
261 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
262 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
263 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
264 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
266 if (!createEntity(aCenter, theEntities, theParameters) ||
267 !createEntity(aStart, theEntities, theParameters) ||
268 !createEntity(aEnd, theEntities, theParameters))
270 Slvs_hEntity anArcID = theEntities.back().h;
271 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
272 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
274 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
275 else if (aFeatureKind == SketchPlugin_Point::ID()) {
276 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
277 if (!aPoint->isInitialized() ||
278 !createEntity(aPoint, theEntities, theParameters))
280 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
281 // No need to add another entity.
286 bool SketchSolver_Builder::createNormal(
287 AttributePtr theNormal,
288 AttributePtr theDirX,
289 std::vector<Slvs_Entity>& theEntities,
290 std::vector<Slvs_Param>& theParameters)
292 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
293 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
294 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance))
296 // calculate Y direction
297 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
299 // quaternion parameters of normal vector
300 double qw, qx, qy, qz;
301 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
303 double aNormCoord[4] = { qw, qx, qy, qz };
305 // Create parameters of the normal
306 Slvs_hParam aCurParam = theParameters.back().h;
307 for (int i = 0; i < 4; i++)
308 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
311 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
312 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
313 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
314 theEntities.push_back(aNormal);