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_ConstraintLength.h>
11 #include <SketchSolver_ConstraintRigid.h>
13 #include <GeomAPI_Edge.h>
14 #include <GeomDataAPI_Dir.h>
15 #include <GeomDataAPI_Point.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
21 #include <ModelAPI_ResultConstruction.h>
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Point.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintDistance.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_ConstraintFillet.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMirror.h>
34 #include <SketchPlugin_ConstraintParallel.h>
35 #include <SketchPlugin_ConstraintPerpendicular.h>
36 #include <SketchPlugin_ConstraintRadius.h>
37 #include <SketchPlugin_ConstraintRigid.h>
38 #include <SketchPlugin_ConstraintTangent.h>
39 #include <SketchPlugin_ConstraintVertical.h>
42 // Initialization of constraint builder self pointer
43 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
45 SketchSolver_Builder* SketchSolver_Builder::getInstance()
48 mySelf = new SketchSolver_Builder();
52 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
54 SolverConstraintPtr aResult;
55 DataPtr aData = theConstraint->data();
56 if (!aData || !aData->isValid())
59 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
60 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
61 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
62 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
63 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
64 //// return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
65 } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
66 //// return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
67 } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
68 return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
69 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
70 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
71 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
72 //// return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
73 } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
74 return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
75 } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
76 return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
77 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
78 //// return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
79 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
80 //// return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
81 } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
82 return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
83 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
84 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
89 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
91 DataPtr aData = theFixedFeature->data();
92 if (!aData || !aData->isValid())
93 return SolverConstraintPtr();
94 return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
99 bool SketchSolver_Builder::createWorkplane(
100 CompositeFeaturePtr theSketch,
101 std::vector<Slvs_Entity>& theEntities,
102 std::vector<Slvs_Param>& theParameters)
104 DataPtr aSketchData = theSketch->data();
105 if (!aSketchData || !aSketchData->isValid())
106 return false; // the sketch is incorrect
108 // Get parameters of workplane
109 std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
110 SketchPlugin_Sketch::DIRX_ID());
111 std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
112 SketchPlugin_Sketch::NORM_ID());
113 std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
114 SketchPlugin_Sketch::ORIGIN_ID());
115 // Create SolveSpace entity corresponding to the sketch origin
116 if (!createEntity(anOrigin, theEntities, theParameters))
118 Slvs_hEntity anOriginID = theEntities.back().h;
119 // Create SolveSpace entity corresponding the the sketch normal
120 if (!createNormal(aNorm, aDirX, theEntities, theParameters))
122 Slvs_hEntity aNormalID = theEntities.back().h;
125 Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
126 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
127 theEntities.push_back(aWorkplane);
131 bool SketchSolver_Builder::createEntity(
132 AttributePtr theAttribute,
133 std::vector<Slvs_Entity>& theEntities,
134 std::vector<Slvs_Param>& theParameters)
136 Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
137 Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
140 std::shared_ptr<GeomDataAPI_Point> aPoint =
141 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
143 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
144 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
145 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
146 theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
147 aParamID-2, aParamID-1, aParamID));
151 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
152 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
154 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
155 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
156 theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
157 aParamID-1, aParamID));
160 // Scalar value (used for the distance entities)
161 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
163 theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
164 theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
165 SLVS_E_UNKNOWN, aParamID));
168 // unknown attribute type
172 bool SketchSolver_Builder::createEntity(
173 FeaturePtr theFeature,
174 std::vector<Slvs_Entity>& theEntities,
175 std::vector<Slvs_Param>& theParameters)
177 if (!theFeature->data()->isValid())
180 // SketchPlugin features
181 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
182 SketchPlugin_Feature>(theFeature);
186 // Verify the feature by its kind
187 const std::string& aFeatureKind = aFeature->getKind();
188 DataPtr aData = aFeature->data();
190 if (aFeatureKind == SketchPlugin_Line::ID()) {
191 AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
192 AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
193 if (!aStart->isInitialized() || !aEnd->isInitialized())
195 if (!createEntity(aStart, theEntities, theParameters) ||
196 !createEntity(aEnd, theEntities, theParameters))
198 Slvs_hEntity aLineID = theEntities.back().h + 1;
199 theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
200 aLineID-2, aLineID-1));
203 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
204 AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
205 AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
206 if (!aCenter->isInitialized() || !aRadius->isInitialized())
208 if (!createEntity(aCenter, theEntities, theParameters) ||
209 !createEntity(aRadius, theEntities, theParameters))
211 Slvs_hEntity aCircID = theEntities.back().h;
212 theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
213 SLVS_E_UNKNOWN, aCircID-1));
216 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
217 AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
218 AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
219 AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
220 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
222 if (!createEntity(aCenter, theEntities, theParameters) ||
223 !createEntity(aStart, theEntities, theParameters) ||
224 !createEntity(aEnd, theEntities, theParameters))
226 Slvs_hEntity anArcID = theEntities.back().h;
227 theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
228 SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
230 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
231 else if (aFeatureKind == SketchPlugin_Point::ID()) {
232 AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
233 if (!aPoint->isInitialized() ||
234 !createEntity(aPoint, theEntities, theParameters))
236 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
237 // No need to add another entity.
242 bool SketchSolver_Builder::createNormal(
243 AttributePtr theNormal,
244 AttributePtr theDirX,
245 std::vector<Slvs_Entity>& theEntities,
246 std::vector<Slvs_Param>& theParameters)
248 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
249 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
250 if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance))
252 // calculate Y direction
253 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
255 // quaternion parameters of normal vector
256 double qw, qx, qy, qz;
257 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
259 double aNormCoord[4] = { qw, qx, qy, qz };
261 // Create parameters of the normal
262 Slvs_hParam aCurParam = theParameters.back().h;
263 for (int i = 0; i < 4; i++)
264 theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
267 Slvs_hEntity aCurEntity = theEntities.back().h + 1;
268 Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
269 aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
270 theEntities.push_back(aNormal);