1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Solver.cpp
4 // Created: 07 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Solver.h"
8 #include <Events_LongOp.h>
10 SketchSolver_Solver::SketchSolver_Solver()
12 // Nullify all elements of the set of equations
13 myEquationsSystem.param = 0;
14 myEquationsSystem.params = 0;
15 myEquationsSystem.entity = 0;
16 myEquationsSystem.entities = 0;
17 myEquationsSystem.constraint = 0;
18 myEquationsSystem.constraints = 0;
19 myEquationsSystem.failed = 0;
20 myEquationsSystem.faileds = 0;
22 myEquationsSystem.dragged[0] = 0;
23 myEquationsSystem.dragged[1] = 0;
24 myEquationsSystem.dragged[2] = 0;
25 myEquationsSystem.dragged[3] = 0;
27 // If the set of constraints is inconsistent,
28 // the failed field will contain wrong constraints
29 myEquationsSystem.calculateFaileds = 1;
32 SketchSolver_Solver::~SketchSolver_Solver()
34 if (myEquationsSystem.constraint)
35 delete[] myEquationsSystem.constraint;
36 if (myEquationsSystem.failed)
37 delete[] myEquationsSystem.failed;
40 void SketchSolver_Solver::setParameters(Slvs_Param* theParameters, int theSize)
42 myEquationsSystem.param = theParameters;
43 myEquationsSystem.params = theSize;
47 void SketchSolver_Solver::setDraggedParameters(const Slvs_hParam* theDragged)
49 for (unsigned int i = 0; i < 4; i++)
50 myEquationsSystem.dragged[i] = theDragged[i];
53 void SketchSolver_Solver::setEntities(Slvs_Entity* theEntities, int theSize)
55 myEquationsSystem.entity = theEntities;
56 myEquationsSystem.entities = theSize;
59 void SketchSolver_Solver::setConstraints(Slvs_Constraint* theConstraints, int theSize)
61 if (!myEquationsSystem.constraint) {
62 myEquationsSystem.constraint = new Slvs_Constraint[theSize];
63 myEquationsSystem.constraints = theSize;
65 else if (myEquationsSystem.constraints != theSize) {
66 delete[] myEquationsSystem.constraint;
67 myEquationsSystem.constraint = new Slvs_Constraint[theSize];
68 myEquationsSystem.constraints = theSize;
70 memcpy(myEquationsSystem.constraint, theConstraints, theSize * sizeof(Slvs_Constraint));
74 int SketchSolver_Solver::solve()
76 if (myEquationsSystem.constraints <= 0)
77 return SLVS_RESULT_EMPTY_SET;
79 Events_LongOp::start(this);
80 Slvs_Solve(&myEquationsSystem, myGroupID);
81 Events_LongOp::end(this);
83 return myEquationsSystem.result;
86 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
88 if (myEquationsSystem.result != SLVS_RESULT_OKAY)
91 if (theParameters.size() != myEquationsSystem.params)
92 return false; // number of parameters is not the same
94 std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
95 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) {
96 if (myEquationsSystem.param[i].h != aParamIter->h)
97 return false; // sequence of parameters was changed
98 aParamIter->val = myEquationsSystem.param[i].val;