+ //Add a new Spline object
+ Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
+
+ //Add a new Spline function for interpolation type
+ Handle(GEOM_Function) aFunction =
+ aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOLATION);
+ if (aFunction.IsNull()) return NULL;
+
+ //Check if the function is set correctly
+ if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
+
+ GEOMImpl_ISpline aCI (aFunction);
+
+ aCI.SetConstructorType(POINT_CONSTRUCTOR);
+
+ Handle(TColStd_HSequenceOfTransient) aPoints = new TColStd_HSequenceOfTransient;
+ std::list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
+ for (; it != thePoints.end(); it++) {
+ Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
+ if (aRefPnt.IsNull()) {
+ return NULL;
+ }
+ aPoints->Append(aRefPnt);
+ }
+ aCI.SetPoints(aPoints);
+
+ aCI.SetIsClosed(theIsClosed);
+ aCI.SetDoReordering(theDoReordering);
+
+ //Compute the Spline value
+ try {
+ OCC_CATCH_SIGNALS;
+ if (!GetSolver()->ComputeFunction(aFunction)) {
+ SetErrorCode("Spline driver failed");
+ return NULL;
+ }
+ }
+ catch (Standard_Failure) {
+ Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+ SetErrorCode(aFail->GetMessageString());
+ return NULL;
+ }
+
+ //Make a Python command
+ GEOM::TPythonDump pd (aFunction);
+ pd << aSpline << " = geompy.MakeInterpol([";
+
+ it = thePoints.begin();
+ pd << (*it++);
+ while (it != thePoints.end()) {
+ pd << ", " << (*it++);
+ }
+ pd << "], " << theIsClosed << ", " << theDoReordering << ")";
+
+ SetErrorCode(OK);
+ return aSpline;
+}
+
+
+//=============================================================================
+/*!
+ * MakeSplineInterpolWithTangents
+ */
+//=============================================================================
+Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineInterpolWithTangents
+ (std::list<Handle(GEOM_Object)> thePoints,
+ Handle(GEOM_Object) theFirstVec,
+ Handle(GEOM_Object) theLastVec)
+{
+ SetErrorCode(KO);
+
+ //Add a new Spline object
+ Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
+
+ //Add a new Spline function for interpolation type
+ Handle(GEOM_Function) aFunction =
+ aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOL_TANGENTS);
+ if (aFunction.IsNull()) return NULL;
+
+ //Check if the function is set correctly
+ if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
+
+ GEOMImpl_ISpline aCI (aFunction);
+
+ aCI.SetConstructorType(POINT_CONSTRUCTOR);
+
+ Handle(TColStd_HSequenceOfTransient) aPoints = new TColStd_HSequenceOfTransient;
+ std::list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
+ for (; it != thePoints.end(); it++) {
+ Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
+ if (aRefPnt.IsNull()) {
+ SetErrorCode("NULL point for Interpolation");
+ return NULL;
+ }
+ aPoints->Append(aRefPnt);
+ }
+ aCI.SetPoints(aPoints);
+
+ Handle(GEOM_Function) aVec1 = theFirstVec->GetLastFunction();
+ Handle(GEOM_Function) aVec2 = theLastVec->GetLastFunction();
+
+ if (aVec1.IsNull() || aVec2.IsNull()) return NULL;
+
+ aCI.SetFirstVector(aVec1);
+ aCI.SetLastVector(aVec2);
+
+ //Compute the Spline value
+ try {
+ OCC_CATCH_SIGNALS;
+ if (!GetSolver()->ComputeFunction(aFunction)) {
+ SetErrorCode("Spline driver failed");
+ return NULL;
+ }
+ }
+ catch (Standard_Failure) {
+ Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+ SetErrorCode(aFail->GetMessageString());
+ return NULL;
+ }
+
+ //Make a Python command
+ GEOM::TPythonDump pd (aFunction);
+ pd << aSpline << " = geompy.MakeInterpolWithTangents([";
+
+ it = thePoints.begin();
+ pd << (*it++);
+ while (it != thePoints.end()) {
+ pd << ", " << (*it++);
+ }
+ pd << "], " << theFirstVec << ", " << theLastVec << ")";
+
+ SetErrorCode(OK);
+ return aSpline;
+}
+
+//=============================================================================
+/*!
+ * MakeCurveParametric
+ */
+//=============================================================================
+Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCurveParametric
+ (const char* thexExpr, const char* theyExpr, const char* thezExpr,
+ double theParamMin, double theParamMax, double theParamStep,
+ CurveType theCurveType,
+ int theParamNbStep, bool theNewMethod)
+{
+ TCollection_AsciiString aPyScript;
+ aPyScript +="from math import * \n";
+ aPyScript +="def X(t): \n";
+ aPyScript +=" return ";
+ aPyScript += thexExpr;
+ aPyScript += "\n";
+ aPyScript +="def Y(t): \n";
+ aPyScript +=" return ";
+ aPyScript += theyExpr;
+ aPyScript += "\n";
+
+ aPyScript +="def Z(t): \n";
+ aPyScript +=" return ";
+ aPyScript += thezExpr;
+ aPyScript += "\n";
+
+ if (theNewMethod)
+ {
+ aPyScript +="def coordCalculator(tmin, tmax, nstep): \n";
+ aPyScript +=" coords = [] \n";
+ aPyScript +=" tstep = (tmax - tmin) / nstep \n";
+ aPyScript +=" n = 0 \n";
+ aPyScript +=" while n <= nstep : \n";
+ aPyScript +=" t = tmin + n*tstep \n";
+ aPyScript +=" coords.append([X(t), Y(t), Z(t)]) \n";
+ aPyScript +=" n = n+1 \n";
+ aPyScript +=" return coords \n";
+ }
+ else
+ {
+ aPyScript +="def coordCalculator(tmin, tmax, tstep): \n";
+ aPyScript +=" coords = [] \n";
+ aPyScript +=" while tmin <= tmax : \n";
+ aPyScript +=" coords.append([X(tmin), Y(tmin), Z(tmin)]) \n";
+ aPyScript +=" tmin = tmin + tstep \n";
+ aPyScript +=" return coords \n";
+ }
+
+ SetErrorCode(KO);
+
+ if(theParamMin >= theParamMax) {
+ SetErrorCode("The minimum value of the parameter must be less than maximum value !!!");
+ return NULL;
+ }
+
+ if(!theNewMethod && theParamStep <= 0.0) {
+ SetErrorCode("Value of the step must be positive !!!");
+ return NULL;
+ }
+ else if(theNewMethod && theParamNbStep < 0) {
+ SetErrorCode("The number of steps must be positive !!!");
+ return NULL;
+ }
+
+ /* Initialize the Python interpreter */
+ if (! Py_IsInitialized()) {
+ SetErrorCode("Python interpreter is not initialized !!! ");
+ return NULL;
+ }
+
+ PyGILState_STATE gstate;
+ gstate = PyGILState_Ensure();
+
+ PyObject* main_mod = PyImport_AddModule("__main__");
+ PyObject* main_dict = PyModule_GetDict(main_mod);
+
+ PyObject* obj = PyRun_String(aPyScript.ToCString(), Py_file_input, main_dict, NULL);
+
+ if (obj == NULL) {
+ SetErrorCode("Error during executing of python script !!!");
+ PyErr_Print();
+ PyGILState_Release(gstate);
+ return NULL;
+ } else {
+ Py_DECREF(obj);
+ }
+
+ PyObject * func = NULL;
+ func = PyObject_GetAttrString(main_mod, "coordCalculator");
+
+ if (func == NULL){
+ SetErrorCode("Can't get function from python module !!!");
+ PyGILState_Release(gstate);
+ return NULL;
+ }
+
+ PyObject* coords;
+ if (theNewMethod)
+ coords = PyObject_CallFunction(func,(char*)"(d, d, i)", theParamMin, theParamMax, theParamNbStep );
+ else
+ coords = PyObject_CallFunction(func,(char*)"(d, d, d)", theParamMin, theParamMax, theParamStep );
+
+ PyObject* new_stderr = NULL;
+
+ if (coords == NULL){
+ fflush(stderr);
+ std::string err_description="";
+ new_stderr = newPyStdOut(err_description);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ MESSAGE("Can't evaluate coordCalculator()" << " error is " << err_description);
+ SetErrorCode("Can't evaluate the expressions, please check them !!!");
+ PyGILState_Release(gstate);
+ return NULL;
+ }
+
+ int lsize = PyList_Size( coords );
+
+ if(lsize <= 0) {
+ SetErrorCode("Empty list of the points, please check input parameters !!!");
+ return NULL;
+ }
+
+ Handle(TColStd_HArray1OfReal) aCoordsArray = new TColStd_HArray1OfReal (1, lsize * 3);
+
+ int k=1;
+ for ( Py_ssize_t i = 0; i < lsize; ++i ) {
+ PyObject* coord = PyList_GetItem( coords, i );
+ if (coord != NULL) {
+ for ( Py_ssize_t j = 0; j < PyList_Size(coord); ++j) {
+ PyObject* item = PyList_GetItem(coord, j);
+ aCoordsArray->SetValue(k, PyFloat_AsDouble(item));
+ k++;
+ }
+ }
+ }
+
+ Py_DECREF(coords);
+
+ PyGILState_Release(gstate);
+
+ Handle(GEOM_Object) aCurve;
+ Handle(GEOM_Function) aFunction;
+ TCollection_AsciiString aCurveType;
+
+ switch(theCurveType) {
+ case Polyline: {
+ //Add a new Polyline object
+ aCurve = GetEngine()->AddObject(GetDocID(), GEOM_POLYLINE);
+
+ //Add a new Polyline function for creation a polyline relatively to points set
+ aFunction = aCurve->AddFunction(GEOMImpl_PolylineDriver::GetID(), POLYLINE_POINTS);
+ if (aFunction.IsNull()) return NULL;
+
+ //Check if the function is set correctly
+ if (aFunction->GetDriverGUID() != GEOMImpl_PolylineDriver::GetID()) return NULL;
+
+ GEOMImpl_IPolyline aCI (aFunction);
+
+ aCI.SetLength(lsize);
+ aCI.SetConstructorType(COORD_CONSTRUCTOR);
+ aCI.SetIsClosed(false);
+ aCI.SetCoordinates(aCoordsArray);
+ aCurveType = "GEOM.Polyline";
+ break;
+ }
+ case Bezier: {
+ //Add a new Spline object
+ aCurve = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
+ //Add a new Spline function for creation a bezier curve relatively to points set
+ aFunction =
+ aCurve->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_BEZIER);
+ if (aFunction.IsNull()) return NULL;
+
+ //Check if the function is set correctly
+ if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
+
+ GEOMImpl_ISpline aCI (aFunction);
+
+ aCI.SetConstructorType(COORD_CONSTRUCTOR);
+ aCI.SetIsClosed(false);
+ aCI.SetCoordinates(aCoordsArray);
+ aCurveType = "GEOM.Bezier";
+ break;
+ }
+ case Interpolation: {
+ //Add a new Spline object
+ aCurve = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
+
+ //Add a new Spline function for creation a bezier curve relatively to points set
+ aFunction = aCurve->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOLATION);
+ if (aFunction.IsNull()) return NULL;
+
+ //Check if the function is set correctly
+ if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
+
+ GEOMImpl_ISpline aCI (aFunction);
+ aCI.SetConstructorType(COORD_CONSTRUCTOR);
+ aCI.SetIsClosed(false);
+ aCI.SetDoReordering(false);
+ aCI.SetCoordinates(aCoordsArray);
+ aCurveType = "GEOM.Interpolation";
+ break;
+ }
+ }
+
+ GEOMImpl_ICurveParametric aIP(aFunction);
+ aIP.SetExprX (thexExpr);
+ aIP.SetExprY (theyExpr);
+ aIP.SetExprZ (thezExpr);
+ aIP.SetParamMin (theParamMin);
+ aIP.SetParamMax (theParamMax);
+ if ( theNewMethod )
+ aIP.SetParamNbStep(theParamNbStep);
+ else
+ aIP.SetParamStep (theParamStep);
+
+ //Compute the Curve value
+ try {
+ OCC_CATCH_SIGNALS;
+ if (!GetSolver()->ComputeFunction(aFunction)) {
+ SetErrorCode("Curve driver failed !!!");
+ return NULL;
+ }
+ }
+ catch (Standard_Failure) {
+ Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+ SetErrorCode(aFail->GetMessageString());
+ return NULL;
+ }
+
+ //Make a Python command
+ GEOM::TPythonDump pd (aFunction);
+ pd << aCurve << " = geompy.MakeCurveParametric(";
+ pd << "\"" << thexExpr << "\", ";
+ pd << "\"" << theyExpr << "\", ";
+ pd << "\"" << thezExpr << "\", ";
+
+ pd << theParamMin <<", ";
+ pd << theParamMax <<", ";
+ if (theNewMethod)
+ pd << theParamNbStep <<", ";
+ else
+ pd << theParamStep <<", ";
+ pd << aCurveType.ToCString() <<", ";
+ pd << theNewMethod <<")";
+
+ SetErrorCode(OK);
+ return aCurve;
+}
+
+//=============================================================================
+/*!
+ * MakeSketcher
+ */
+//=============================================================================
+Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcher (const char* theCommand,
+ std::list<double> theWorkingPlane)
+{
+ SetErrorCode(KO);
+
+ if (!theCommand || strcmp(theCommand, "") == 0) return NULL;
+
+ //Add a new Sketcher object
+ Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_SKETCHER);
+
+ //Add a new Sketcher function
+ Handle(GEOM_Function) aFunction =
+ aSketcher->AddFunction(GEOMImpl_SketcherDriver::GetID(), SKETCHER_NINE_DOUBLS);
+ if (aFunction.IsNull()) return NULL;
+
+ //Check if the function is set correctly
+ if (aFunction->GetDriverGUID() != GEOMImpl_SketcherDriver::GetID()) return NULL;
+
+ GEOMImpl_ISketcher aCI (aFunction);
+
+ TCollection_AsciiString aCommand((char*) theCommand);
+ aCI.SetCommand(aCommand);
+
+ int ind = 1;
+ std::list<double>::iterator it = theWorkingPlane.begin();
+ for (; it != theWorkingPlane.end(); it++, ind++)
+ aCI.SetWorkingPlane(ind, *it);
+
+ //Compute the Sketcher value
+ try {
+ OCC_CATCH_SIGNALS;
+ if (!GetSolver()->ComputeFunction(aFunction)) {
+ SetErrorCode("Sketcher driver failed");
+ return NULL;
+ }
+ }
+ catch (Standard_Failure) {
+ Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+ SetErrorCode(aFail->GetMessageString());
+ return NULL;
+ }
+
+ //Make a Python command
+ GEOM::TPythonDump pd (aFunction);
+ pd << aSketcher << " = geompy.MakeSketcher(\"" << aCommand.ToCString() << "\", [";
+
+ it = theWorkingPlane.begin();
+ pd << (*it++);
+ while (it != theWorkingPlane.end()) {
+ pd << ", " << (*it++);
+ }
+ pd << "])";
+
+ SetErrorCode(OK);
+ return aSketcher;
+}
+
+//=============================================================================
+/*!
+ * MakeSketcherOnPlane
+ */
+//=============================================================================
+Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcherOnPlane
+ (const char* theCommand,
+ Handle(GEOM_Object) theWorkingPlane)
+{
+ SetErrorCode(KO);
+
+ if (!theCommand || strcmp(theCommand, "") == 0) return NULL;