logplus/app/fdsa4_test/test0531.cpp

425 lines
17 KiB
C++
Raw Normal View History

#include "memrdwt.h"
#include "math.h"
#include "basefun.h"
#include "DepthProgress.h"
#include <qvector.h>
//#include"cloudalgorithmaccess.h"
#include <QTextCodec>
#include <QCoreApplication>
#include <QtNetwork/QNetworkProxyFactory>
#include <QLibrary>
#include <QtNetwork/QTcpSocket>
#include "pythonhandler.h"
// #undef slots
// #if defined(_DEBUG)
// #define IS_DEBUG
// #undef _DEBUG//防止加载python312_d.lib 先取消掉_DEBUG
// #endif
// #include <Python.h>
// #include <numpy/arrayobject.h>
// #if defined(IS_DEBUG)
// #define _DEBUG
// #endif
// #define slots
#include <QDebug>
/****************************************************数据定义****************************************************************
******************************begin****************************/
extern "C"{
_declspec (dllexport) struct INC_STRU INC={6,
"FWGN_W:波名1,raw;;\
NWGN_W:2,raw;;\
RWGN_W:3,raw;;\
GR:,API;;\
NWGN_C:线2,raw;;\
RWGN_C:线3,raw;;\
"
};//输入曲线定义 6:输入6条数据, "数据英文:数据中文,数据单位;;"
_declspec (dllexport) struct INP_STRU INP; ///使用GetINValue(RGN_C, 6, 1);函数需要定义该变量
_declspec (dllexport) struct OUTC_STRU OUTC={2,
"ATT:波名1,raw;;\
THCAV:线1,raw;;\
"
};//输出曲线定义 2:输出2条数据, "数据英文:数据中文,数据单位;;"(注:先写波形数据,再写曲线数据)
//_declspec (dllexport) struct OUTP_STRU OUTP;
_declspec (dllexport) struct HD_STRU HD={0.0,9999.0,0.0,0.0,0.0,0.0,0.0,"test0531.INP","test0531.TLP"};//改成自己的工程名
__declspec (dllexport) struct CONC_STRU CONC={4,
"CTH:参数名1,mm;;;;10.5;\
CT:2,%;;;;1;\
IT:3,mm;;;;9.7;\
MT:4,%;;;;1;"
};//常量参数定义 4:4个参数, "数据英文:数据中文,单位;;;;参数默认值;"
_declspec (dllexport) struct CON_STRU CON={10.5, 1, 9.7, 1};//再次设置默认值(需要与前面相同)
_declspec (dllexport) struct ErrorInf errorinf;
//定义输出数据类型(注:需要与前面输出定义一一对应)
__declspec (dllexport) struct LogType LogType={WAVE_OBJECT, CURVE_OBJECT};//{WAVE_OBJECT, CURVE_OBJECT}
__declspec( dllexport ) CMemRdWt MemRdWt;
_declspec (dllexport) int test0531();//改成自己的工程名
}
/****************************************************end****************************************************************/
struct Process_Struct{
int Process();
};
//DLL入口函数
int test0531()//改成自己的工程名
{
Process_Struct Process;
return Process.Process();
}
//固井结构体(注:此处为表格输出示例)
typedef struct ss_struct
{
int GNO;
float GSDEP,GEDEP;
int GRESULT;
} GUJING;
//结论结构体
typedef struct s2_struct
{
int NO;
float SDEP,EDEP;
QString R;
} JIELUN;
void callPython(float FGN_C, float NGN_C, float RGN_C, float* FGN_W, float* NGN_W, float* Att0all, int nlen)
{
QVariantList args;
QVariantList arg1;
QVariantList arg2;
// 填充列表
for (int i = 0; i < nlen; ++i) {
arg1 << (double)FGN_W[i];
arg2 << (double)NGN_W[i];
}
args.append(QVariant::fromValue(arg1)); // 直接将 innerList1 作为元素添加
args.append(QVariant::fromValue(arg2)); // 使用 << 操作符
args << (double)FGN_C << (double)NGN_C << (double)RGN_C;
PythonHandler::getInstance()->executeScript("fdsa4_test", "process_arrays", args, Att0all, nlen);
// static PyObject * pmodule = NULL;
// static PyObject * pmodule2 = NULL;
// if (!pmodule) // 防止重复加载报错cannot load module more than once per process
// {
// Py_Initialize();
// if(!Py_IsInitialized())
// {
// qDebug() << "init err";
// }
// PyRun_SimpleString("import os");
// PyRun_SimpleString("import sys");
// // PyRun_SimpleString("sys.path.append('./app/fdsa4_test')");// 设置运行路径
// PyRun_SimpleString("sys.path.append('D:/jiayl0909/logPlus/build/Bin/app/fdsa4_test')");// 设置运行路径
// PyRun_SimpleString("sys.path.append(os.path.dirname(os.path.dirname(os.getcwd())))");
// PyRun_SimpleString("print(os.getcwd())");
// PyRun_SimpleString("print(os.path.dirname(os.path.dirname(os.getcwd())))");
// PyRun_SimpleString("print(sys.path)");
// // pmodule2 = PyImport_ImportModule("math_algorithm");
// pmodule = PyImport_ImportModule("math_algorithm");
// }
// if(PyErr_Occurred()){
// PyObject*pType,*pValue,*pTraceback;
// PyErr_Fetch(&pType,&pValue,&pTraceback);PyErr_NormalizeException(&pType,&pValue,&pTraceback);
// PyObject* pstr=PyObject_Str(pValue);
// const char* pstrErrorMessage =PyUnicode_AsUTF8(pstr);
// printf("Error message:%s\n",pstrErrorMessage);
// }
// if(!pmodule)
// {
// qDebug() << "can not open the file";
// return;
// }
// // return;
// PyObject* pFunc2 = PyObject_GetAttrString(pmodule, "process_arrays");
// // 创建两个Python列表数组
// PyObject* pArgs = PyTuple_New(5);//五个参数
// PyObject* pList1 = PyList_New(0);
// PyObject* pList2 = PyList_New(0);
// // 填充列表
// for (int i = 0; i < nlen; ++i) {
// PyList_Append(pList1, PyFloat_FromDouble(FGN_W[i])); // 可以调用GetINValue(AC,1,1);添加曲线数据
// PyList_Append(pList2, PyFloat_FromDouble(NGN_W[i]));
// }
// // 将列表放入参数元组中
// PyTuple_SetItem(pArgs, 0, pList1);
// PyTuple_SetItem(pArgs, 1, pList2);
// PyTuple_SetItem(pArgs, 2, PyFloat_FromDouble(FGN_C));
// PyTuple_SetItem(pArgs, 3, PyFloat_FromDouble(NGN_C));
// PyTuple_SetItem(pArgs, 4, PyFloat_FromDouble(RGN_C));
// // 调用函数
// PyObject* pValue = PyObject_CallObject(pFunc2, pArgs);
// if (pValue != NULL) {
// // // 处理结果或打印结果例如转换为C++数据类型)
// // for (Py_ssize_t i = 0; i < PyList_Size(pValue); ++i) {
// // PyObject* item = PyList_GetItem(pValue, i);
// // printf("%ld ", PyLong_AsLong(item)); // 对于Python 3使用 PyLong_AsLong
// // }
// // printf("\n");
// // 处理返回的元组,这里有两个数组元素
// PyObject* array1 = PyTuple_GetItem(pValue, 0);
// double dR = PyFloat_AsDouble(PyTuple_GetItem(pValue, 1)); // 获取double
// // // 将numpy数组转换为C++数组例如使用numpy的API
// // PyArrayObject* arr1 = (PyArrayObject*)PyArray_FromAny(array1, NULL, 0, 0, NPY_ARRAY_CARRAY, NULL);
// PyArrayObject* arr1 = (PyArrayObject*)array1;
// double* data1 = (double*)PyArray_DATA(arr1);
// int len = PyArray_DIM(arr1, 0); // 获取数组长度
// if (len>nlen)
// len = nlen;
// for (int i = 0; i < len; i++) {
// Att0all[i] = data1[i];
// }
// Py_DECREF(arr1);
// } else {
// PyErr_Print();
// }
// // 清理资源
// Py_DECREF(pValue);
// Py_DECREF(pArgs);
// // while(Py_REFCNT(pmodule)>0)
// // Py_DECREF(pmodule);
// // Py_Finalize();
}
//核心处理逻辑
int Process_Struct::Process()
{
if(HD.Sdep >= HD.EndDep)
{
// AfxMessageBox("当前处理井段深度有误!\n已停止计算");
AfxMessageBox("The current depth of the processing interval is incorrect!\n Calculation has been stopped");
return 0;
}
/*******************************************************写数据前的准备操作*******************************************************
******************************begin****************************/
//1初始化参数、输入
MemRdWt.Const();
MemRdWt.In();
//2波形类型的输出数据初始化检查
char outname[2][16];
int OUTindex = 0, WaveIndex = 0;
MemRdWt.GetOutCurveName(OUTindex, outname[OUTindex]);//获取输出数据名称参数1是序列值从0开始波形/曲线均使用GetOutCurveName函数
int index1 = MemRdWt.OpenWave(outname[OUTindex]);//检查是否存在outname[OUTindex]波形
if(index1 < 0) //返回索引小于0说明没有该波形需要创建
{
//创建方式:
Slf_WAVE myWave;
strcpy(myWave.Name, outname[OUTindex]);
strcpy(myWave.AliasName, outname[OUTindex]);
strcpy(myWave.DepthUnit, "m");
myWave.CodeLen = 4;
myWave.RepCode = 4;
myWave.DefVal = 0;
myWave.StartDepth = HD.Sdep;
myWave.EndDepth = HD.Edep;
myWave.DepLevel = HD.Rlev;
strcpy(myWave.DepthUnit, "m");
myWave.StartTime = 0; //起始记录时间
myWave.TimeLevel = 5; //时间采样间隔
myWave.ArrayNum = 1; //阵列数
myWave.TimeSamples = 36; //时间采样总数
strcpy(myWave.TimeUnit, "ns"); //时间单位
index1 = MemRdWt.OpenWave((Slf_WAVE *)&myWave); //创建波形(注:此时返回索引应>1代表创建成功
}
if(index1 < 0)
{
// AfxMessageBox("阵列数据创建失败!\n已停止计算");
AfxMessageBox("Array data creation failed!\nCalculation stopped");
return 0;
}else{
WaveIndex = index1;
}
//3曲线类型的输出数据初始化检查
OUTindex = 1;//按extern "C"中定义的输出数据顺序设置
MemRdWt.GetOutCurveName(OUTindex, outname[OUTindex]);//获取输出数据名称参数1是序列值从0开始
index1 = MemRdWt.OpenCurve(outname[1]);//index1 = MemRdWt.OpenCurve("EEE");//检查数据是否存在//outname[OUTindex]
if(index1 < 0) //返回索引小于0说明没有这条曲线需要创建
{
//创建方式:
Slf_CURVE myCurve; //定义曲线对象
strcpy(myCurve.Name, outname[1]); //设置名称
strcpy(myCurve.AliasName, outname[1]); //设置别名
strcpy(myCurve.Unit, "m"); //设置数据单位
myCurve.CodeLen = 4; //设置字节长度4float
myCurve.RepCode = 4; //设置数据类型4float
myCurve.DefVal = 0; //设置默认值
myCurve.StartDepth = HD.Sdep; //设置起始深度
myCurve.EndDepth = HD.Edep; //设置结束深度
myCurve.DepLevel = HD.Rlev; //设置采样间隔
strcpy(myCurve.DepthUnit, "m"); //设置深度单位
index1 = MemRdWt.OpenCurve((Slf_CURVE *)&myCurve); //创建曲线(注:此时返回索引应>1代表创建成功
}
if(index1 < 0)
{
// AfxMessageBox("波形数据创建失败!\n已停止计算");
AfxMessageBox("Array data creation failed!\nCalculation stopped");
return 0;
}
/*********************************************************end************************************************************/
//4平台进度条设置
DepthProgress mmProgress;
mmProgress.CreatProgress(HD.Stdep, HD.EndDep, "程序处理中...");
//5深度循环
while(HD.Dep <= HD.EndDep)//逐深度处理
{
mmProgress.SetDepth(HD.Dep);//设置平台进度条
/********************************************************获取常量参数示例***************************************************
******************************begin****************************/
//GetPosValue函数的参数3为序列值要和extern "C"的CONC内容对应上从1开始
float CTH, CT, IT, MT;
GetPosValue(CTH, CON, 1, 1);
GetPosValue(CT, CON, 2, 1);
GetPosValue(IT, CON, 3, 1);
GetPosValue(MT, CON, 4, 1);
/***************************************************************end*************************************************************/
/********************************************************读数据示例***************************************************************
*****************************begin****************************/
//1读wave数据
char inname[3][36];
int index[4] = {-1};
//获取输入数据对应的波列名称参数1是序列值从0开始
MemRdWt.GetInCurveName(0, inname[0]);
MemRdWt.GetInCurveName(1, inname[1]);
MemRdWt.GetInCurveName(2, inname[2]);
//获取波列数据对应的index,再读数据
index[0] = MemRdWt.OpenWave(inname[0]);
index[1] = MemRdWt.OpenWave(inname[1]);
index[2] = MemRdWt.OpenWave(inname[2]);
if(index[0] < 0 || index[1] < 0 || index[2] < 0){
// AfxMessageBox("波列数据获取失败\n已停止计算");
AfxMessageBox("Waveform data acquisition failed\nCalculation stopped");
return 0;
}
float FGN_W[60], NGN_W[60], RGN_W[60]; //必须根据wave数据的大小声明
int a = 0;
int b = 0; int c = 0;
c = MemRdWt.ReadWaveToFloatBuf(index[2], HD.Dep, 1, RGN_W);
b = MemRdWt.ReadWaveToFloatBuf(index[1], HD.Dep, 1, NGN_W);
a = MemRdWt.ReadWaveToFloatBuf(index[0], HD.Dep, 1, FGN_W);//读取wave数据从HD.Dep深度开始读1个深度位置的wave数据至FGN_W中
//2读曲线数据
//GetINValue函数的参数2为序列值要和extern "C"的INC内容的顺序对应上
float FGN_C, NGN_C, RGN_C;
GetINValue(FGN_C, 4, 1);
GetINValue(NGN_C, 5, 1);
GetINValue(RGN_C, 6, 1);
/********************************************************end*************************************************************/
/*******************************************处理程序示例(注:改成自己的算法)******************************************
*****************************begin****************************/
float Att0all[36];
float THCAV = 0.0;
bool bPython = true;
if (bPython)
{
callPython(FGN_C, NGN_C, RGN_C, FGN_W, NGN_W, Att0all, 36);
}
else
{
for(int i = 0; i < 36; i++)
Att0all[i] = FGN_W[i] + NGN_W[i];
THCAV = FGN_C + NGN_C + RGN_C;
}
/********************************************************end********************************************************/
/*******************************************************向平台写数据示例**********************************************
*****************************begin****************************/
//1写wave数据
MemRdWt.WriteWave(WaveIndex, HD.Dep, 1, &Att0all);
//2写curve数据
int THCAV_index = MemRdWt.OpenCurve(outname[1]);//outname[1]
if(THCAV_index > 0)
MemRdWt.WriteCurve(THCAV_index, HD.Dep, 1, &THCAV);
/*******************************************************end**********************************************************/
MemRdWt.In(); //继续深度循环
}
/*************************************************向平台写表格示例*************************************************************
*****************************begin****************************/
//1固井结论表格
int itable1 = MemRdWt.OpenOG_RESULT("固井质量");//例如表格取名为“固井质量”
MemRdWt.SetTableRecordCount(itable1, 0); //清空原有表格数据
GUJING *CCNN2 = new GUJING[5];
for(int i = 0; i < 5; i++)
{
CCNN2[i].GNO = i + 1;
CCNN2[i].GSDEP = 2000 + i * 10;
CCNN2[i].GEDEP = 2000 + (i + 1) * 10;
CCNN2[i].GRESULT = 1;
int temp = MemRdWt.WriteTable(itable1, i + 1, &CCNN2[i]);
}
//2其他表格
struct Slf_RST{
int Order;
float Depth;
float CorrDepth;
};
struct Slf_RST m_Result;
itable1 = MemRdWt.OpenTable("ABCD");
if (itable1 < 0)
{
itable1 = MemRdWt.Open_Set_Table("ABCD",0,3,
"NO,DEPTH,DDEP",
"4,4,4",//字段长度
"1,4,4",//字段类型
"0,0,0");//字段备注,1-枚举
}
MemRdWt.SetTableRecordCount(itable1,3); //设置表格有3行数据
for(int j = 0; j < 3; j++)
{
memset(&m_Result, 0, sizeof(Slf_RST));
m_Result.Order = j + 1;
m_Result.Depth = 10;
m_Result.CorrDepth = 20 + j;
MemRdWt.WriteTable(itable1, j + 1, &m_Result);
}
MemRdWt.CloseTable(itable1);
/******************************************************end*************************************************************/
return 1;
}