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_ConstraintMovement.h>
19 #include <SketchSolver_Error.h>
21 #include <GeomAPI_Edge.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomDataAPI_Point.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <Events_Error.h>
26 #include <ModelAPI_Attribute.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <ModelAPI_ResultConstruction.h>
32 #include <SketchPlugin_Arc.h>
33 #include <SketchPlugin_Circle.h>
34 #include <SketchPlugin_Line.h>
35 #include <SketchPlugin_Point.h>
36 #include <SketchPlugin_ConstraintCoincidence.h>
37 #include <SketchPlugin_ConstraintDistance.h>
38 #include <SketchPlugin_ConstraintEqual.h>
39 #include <SketchPlugin_ConstraintFillet.h>
40 #include <SketchPlugin_ConstraintHorizontal.h>
41 #include <SketchPlugin_ConstraintLength.h>
42 #include <SketchPlugin_ConstraintMirror.h>
43 #include <SketchPlugin_ConstraintParallel.h>
44 #include <SketchPlugin_ConstraintPerpendicular.h>
45 #include <SketchPlugin_ConstraintRadius.h>
46 #include <SketchPlugin_ConstraintRigid.h>
47 #include <SketchPlugin_ConstraintTangent.h>
48 #include <SketchPlugin_ConstraintVertical.h>
49 #include <SketchPlugin_MultiRotation.h>
50 #include <SketchPlugin_MultiTranslation.h>
54 // Initialization of constraint builder self pointer
55 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
57 SketchSolver_Builder* SketchSolver_Builder::getInstance()
60 mySelf = new SketchSolver_Builder();
64 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
66 SolverConstraintPtr aResult;
67 DataPtr aData = theConstraint->data();
68 if (!aData || !aData->isValid())
72 // Verify attributes of constraint and generate errors
73 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
74 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
75 for (; anIter != anAttrList.end(); anIter++) {
76 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
78 if (aRefAttr->isObject() && aRefAttr->object()) {
79 ResultConstructionPtr aRC =
80 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
82 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
86 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
88 std::list<ObjectPtr> aList = aRefList->list();
89 std::list<ObjectPtr>::iterator aListIter = aList.begin();
90 for (; aListIter != aList.end(); aListIter++) {
91 ResultConstructionPtr aRC =
92 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
93 if (*aListIter && !aRC)
94 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
100 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
101 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
102 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
103 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
104 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
105 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
106 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
107 return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
108 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
109 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
110 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
111 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
112 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
113 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
114 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
115 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
116 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
117 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
118 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
119 return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
120 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
121 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
122 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
123 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
124 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
125 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
126 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
127 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
128 } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
129 return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
134 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
136 DataPtr aData = theFixedFeature->data();
137 if (!aData || !aData->isValid())
138 return SolverConstraintPtr();
139 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
142 SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature)
144 DataPtr aData = theFixedFeature->data();
145 if (!aData || !aData->isValid())
146 return SolverConstraintPtr();
147 return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature));
152 bool SketchSolver_Builder::createWorkplane(
153 CompositeFeaturePtr theSketch,
154 std::vector<Slvs_Entity>& theEntities,
155 std::vector<Slvs_Param>& theParameters)
157 DataPtr aSketchData = theSketch->data();
158 if (!aSketchData || !aSketchData->isValid())
159 return false; // the sketch is incorrect
161 // Get parameters of workplane
162 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
163 SketchPlugin_Sketch::DIRX_ID());
164 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
165 SketchPlugin_Sketch::NORM_ID());
166 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
167 SketchPlugin_Sketch::ORIGIN_ID());
168 // Create SolveSpace entity corresponding to the sketch origin
169 if (!createEntity(anOrigin, theEntities, theParameters))
171 Slvs_hEntity anOriginID = theEntities.back().h;
172 // Create SolveSpace entity corresponding the the sketch normal
173 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
175 Slvs_hEntity aNormalID = theEntities.back().h;
178 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
179 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
180 theEntities.push_back(aWorkplane);
184 bool SketchSolver_Builder::createEntity(
185 AttributePtr theAttribute,
186 std::vector<Slvs_Entity>& theEntities,
187 std::vector<Slvs_Param>& theParameters)
189 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
190 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
193 std::shared_ptr<GeomDataAPI_Point> aPoint =
194 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
196 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
197 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
198 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
199 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
200 aParamID-2, aParamID-1, aParamID));
204 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
205 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
207 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
208 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
209 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
210 aParamID-1, aParamID));
213 // Scalar value (used for the distance entities)
214 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
216 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
217 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
218 SLVS_E_UNKNOWN, aParamID));
221 // unknown attribute type
225 bool SketchSolver_Builder::createEntity(
226 FeaturePtr theFeature,
227 std::vector<Slvs_Entity>& theEntities,
228 std::vector<Slvs_Param>& theParameters)
230 if (!theFeature->data()->isValid())
233 // SketchPlugin features
234 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
235 SketchPlugin_Feature>(theFeature);
239 // Verify the feature by its kind
240 const std::string& aFeatureKind = aFeature->getKind();
241 DataPtr aData = aFeature->data();
243 if (aFeatureKind == SketchPlugin_Line::ID()) {
244 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
245 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
246 if (!aStart->isInitialized() || !aEnd->isInitialized())
248 if (!createEntity(aStart, theEntities, theParameters) ||
249 !createEntity(aEnd, theEntities, theParameters))
251 Slvs_hEntity aLineID = theEntities.back().h + 1;
252 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
253 aLineID-2, aLineID-1));
256 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
257 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
258 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
259 if (!aCenter->isInitialized() || !aRadius->isInitialized())
261 if (!createEntity(aCenter, theEntities, theParameters) ||
262 !createEntity(aRadius, theEntities, theParameters))
264 Slvs_hEntity aCircID = theEntities.back().h;
265 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
266 SLVS_E_UNKNOWN, aCircID-1));
269 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
270 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
271 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
272 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
273 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
275 if (!createEntity(aCenter, theEntities, theParameters) ||
276 !createEntity(aStart, theEntities, theParameters) ||
277 !createEntity(aEnd, theEntities, theParameters))
279 Slvs_hEntity anArcID = theEntities.back().h;
280 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
281 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
283 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
284 else if (aFeatureKind == SketchPlugin_Point::ID()) {
285 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
286 if (!aPoint->isInitialized() ||
287 !createEntity(aPoint, theEntities, theParameters))
289 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
290 // No need to add another entity.
295 bool SketchSolver_Builder::createNormal(
296 AttributePtr theNormal,
297 AttributePtr theDirX,
298 std::vector<Slvs_Entity>& theEntities,
299 std::vector<Slvs_Param>& theParameters)
301 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
302 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
303 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance))
305 // calculate Y direction
306 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
308 // quaternion parameters of normal vector
309 double qw, qx, qy, qz;
310 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
312 double aNormCoord[4] = { qw, qx, qy, qz };
314 // Create parameters of the normal
315 Slvs_hParam aCurParam = theParameters.back().h;
316 for (int i = 0; i < 4; i++)
317 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
320 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
321 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
322 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
323 theEntities.push_back(aNormal);