initial upload
This commit is contained in:
259
archive/3dtools2msh/3dtools2msh.h
Normal file
259
archive/3dtools2msh/3dtools2msh.h
Normal file
@@ -0,0 +1,259 @@
|
||||
#ifndef _3DTOOLS2MSH_H
|
||||
#define _3DTOOLS2MSH_H
|
||||
#include "iostream"
|
||||
#include "fstream"
|
||||
#include "sstream"
|
||||
#include "string.h"
|
||||
#include "iomanip"
|
||||
#define MESH "-M"
|
||||
#define VALUE "-V"
|
||||
#define OUTFILE "-G"
|
||||
#define FILTER "-F"
|
||||
using namespace std;
|
||||
|
||||
string& replace_all(string& str,const string& old_value,const string& new_value,int &num) //替换str中所有lod_value为new_value,返回被替换的lod_value的个数
|
||||
{
|
||||
int count = 0;
|
||||
for(string::size_type pos(0);pos!=string::npos;pos+=new_value.length()){
|
||||
if((pos=str.find(old_value,pos))!=string::npos){
|
||||
str.replace(pos,old_value.length(),new_value);
|
||||
count++;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
num = count;
|
||||
return str;
|
||||
}
|
||||
|
||||
int typeget(char *str, const char* str2,string &str3)//提出头端的子句
|
||||
{
|
||||
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
||||
if (strp == NULL)
|
||||
{
|
||||
return 0; // 如果没有找到,返回
|
||||
}
|
||||
string temp = str;
|
||||
str3=temp.substr(strlen(str2));//提出除str2外的子句
|
||||
return 1;
|
||||
}
|
||||
|
||||
int nameget(char *str, const char* str2,string &str3)//提出尾端的子句
|
||||
{
|
||||
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
||||
if (strp == NULL)
|
||||
{
|
||||
return 0; // 如果没有找到,返回
|
||||
}
|
||||
string temp = str;
|
||||
str3=temp.substr(0,strlen(str)-strlen(str2));//提出除str2外的子句
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct NODE
|
||||
{
|
||||
double x,y,z;
|
||||
};
|
||||
|
||||
struct CUBE
|
||||
{
|
||||
int index[8];
|
||||
double phys;
|
||||
};
|
||||
|
||||
class _3dtools2msh
|
||||
{
|
||||
public:
|
||||
_3dtools2msh();
|
||||
~_3dtools2msh();
|
||||
int meshin(string);
|
||||
int physin(string);
|
||||
int findindex();
|
||||
int mshout(string,double);
|
||||
private:
|
||||
double xst,yst,zst;
|
||||
int xnum,ynum,znum;
|
||||
double xinter,yinter,zinter;
|
||||
|
||||
NODE* node;
|
||||
CUBE* cube;
|
||||
int nodenum;
|
||||
int cubenum;
|
||||
};
|
||||
|
||||
_3dtools2msh::_3dtools2msh()
|
||||
{
|
||||
node = NULL;
|
||||
cube = NULL;
|
||||
}
|
||||
|
||||
_3dtools2msh::~_3dtools2msh()
|
||||
{
|
||||
if(node!=NULL) delete[] node;
|
||||
if(cube!=NULL) delete[] cube;
|
||||
}
|
||||
|
||||
int _3dtools2msh::meshin(string filename)
|
||||
{
|
||||
stringstream stemp;
|
||||
string temp;
|
||||
int space_num;
|
||||
int xnum_t,ynum_t,znum_t;
|
||||
const char* meshname = filename.c_str();
|
||||
ifstream inmesh;
|
||||
inmesh.open(meshname);
|
||||
if (!inmesh)
|
||||
{
|
||||
cout<<filename<<" not found!"<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
inmesh>>ynum>>xnum>>znum>>yst>>xst>>zst;
|
||||
inmesh>>temp;
|
||||
replace_all(temp,"*"," ",space_num);
|
||||
stemp.clear();
|
||||
stemp.str(temp);
|
||||
stemp>>ynum_t>>yinter;
|
||||
if (ynum!=ynum_t)
|
||||
{
|
||||
cout<<"syntax error: "<<filename<<endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
inmesh>>temp;
|
||||
replace_all(temp,"*"," ",space_num);
|
||||
stemp.clear();
|
||||
stemp.str(temp);
|
||||
stemp>>xnum_t>>xinter;
|
||||
if (xnum!=xnum_t)
|
||||
{
|
||||
cout<<"syntax error: "<<filename<<endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
inmesh>>temp;
|
||||
replace_all(temp,"*"," ",space_num);
|
||||
stemp.clear();
|
||||
stemp.str(temp);
|
||||
stemp>>znum_t>>zinter;
|
||||
if (znum!=znum_t)
|
||||
{
|
||||
cout<<"syntax error: "<<filename<<endl;
|
||||
return 1;
|
||||
}
|
||||
inmesh.close();
|
||||
|
||||
nodenum = (xnum+1)*(ynum+1)*(znum+1);
|
||||
node = new NODE [nodenum];
|
||||
for (int i = 0; i < nodenum; i++)
|
||||
{
|
||||
node[i].z = zst + zinter*(i%(znum+1));
|
||||
node[i].y = yst + yinter*((i/(znum+1))%(ynum+1));
|
||||
node[i].x = xst + xinter*(i/((znum+1)*(ynum+1)));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int _3dtools2msh::physin(string filename)
|
||||
{
|
||||
cubenum = xnum*ynum*znum;
|
||||
cube = new CUBE [cubenum];
|
||||
for (int i = 0; i < cubenum; i++)
|
||||
{
|
||||
cube[i].phys = 0.0;
|
||||
}
|
||||
|
||||
const char* cubename = filename.c_str();
|
||||
ifstream incube;
|
||||
incube.open(cubename);
|
||||
if (!incube)
|
||||
{
|
||||
cout<<filename<<" not found!"<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < cubenum; i++)
|
||||
{
|
||||
incube>>cube[i].phys;
|
||||
}
|
||||
incube.close();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int _3dtools2msh::findindex()
|
||||
{
|
||||
for (int i = 0; i < xnum; i++)
|
||||
{
|
||||
for (int j = 0; j < ynum; j++)
|
||||
{
|
||||
for (int k = 0; k < znum; k++)
|
||||
{
|
||||
cube[k+j*znum+i*ynum*znum].index[0]=k+j*(znum+1)+i*(ynum+1)*(znum+1);
|
||||
cube[k+j*znum+i*ynum*znum].index[1]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+znum+1;
|
||||
cube[k+j*znum+i*ynum*znum].index[2]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+znum+1+(ynum+1)*(znum+1);
|
||||
cube[k+j*znum+i*ynum*znum].index[3]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+(ynum+1)*(znum+1);
|
||||
cube[k+j*znum+i*ynum*znum].index[4]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1;
|
||||
cube[k+j*znum+i*ynum*znum].index[5]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1+znum+1;
|
||||
cube[k+j*znum+i*ynum*znum].index[6]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1+znum+1+(ynum+1)*(znum+1);
|
||||
cube[k+j*znum+i*ynum*znum].index[7]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1+(ynum+1)*(znum+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _3dtools2msh::mshout(string filename,double filter)
|
||||
{
|
||||
const char* mshname = filename.c_str();
|
||||
ofstream outmsh;
|
||||
outmsh.open(mshname);
|
||||
if (!outmsh)
|
||||
{
|
||||
cout<<"fail to create "<<filename<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
outmsh<<"$MeshFormat"<<endl<<"2.2 0 8"<<endl<<"$EndMeshFormat"<<endl<<"$Nodes"<<endl<<nodenum<<endl;
|
||||
for (int i = 0; i < nodenum; i++)
|
||||
{
|
||||
outmsh<<i<<" "<<setprecision(16)<<node[i].x<<" "<<node[i].y<<" "<<node[i].z<<endl;
|
||||
}
|
||||
outmsh<<"$EndNodes"<<endl<<"$Elements"<<endl<<cubenum<<endl;
|
||||
for (int i = 0; i < cubenum; i++)
|
||||
{
|
||||
outmsh<<i<<" 5 2 5 1 "<<cube[i].index[0]<<" "
|
||||
<<cube[i].index[1]<<" "
|
||||
<<cube[i].index[2]<<" "
|
||||
<<cube[i].index[3]<<" "
|
||||
<<cube[i].index[4]<<" "
|
||||
<<cube[i].index[5]<<" "
|
||||
<<cube[i].index[6]<<" "
|
||||
<<cube[i].index[7]<<endl;
|
||||
}
|
||||
int eleNum = 0;
|
||||
for (int i = 0; i < cubenum; i++)
|
||||
{
|
||||
if (cube[i].phys != filter)
|
||||
{
|
||||
eleNum++;
|
||||
}
|
||||
}
|
||||
outmsh<<"$EndElements"<<endl<<"$ElementData"<<endl;
|
||||
outmsh<<1<<endl<<"\"Recovered model\""<<endl<<1<<endl<<0.0<<endl<<3<<endl<<0<<endl<<1<<endl<<eleNum<<endl;
|
||||
for (int i = 0; i < cubenum; i++)
|
||||
{
|
||||
if (cube[i].phys != filter)
|
||||
{
|
||||
outmsh<<i<<" "<<setprecision(16)<<cube[i].phys<<endl;
|
||||
}
|
||||
}
|
||||
outmsh<<"$EndElementData";
|
||||
outmsh.close();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
5
archive/3dtools2msh/CMakeLists.txt
Normal file
5
archive/3dtools2msh/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_executable(3dtools2msh main.cpp)
|
||||
# 第二种方式指定目标文件的路径 这个方式目标文件夹中将只包含目标文件而没有中间文件
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
# 设置安装地址
|
||||
install(TARGETS 3dtools2msh RUNTIME DESTINATION selfpro)
|
84
archive/3dtools2msh/main.cpp
Normal file
84
archive/3dtools2msh/main.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "3dtools2msh.h"
|
||||
|
||||
void disp_help()
|
||||
{
|
||||
cout<<"3dtools2msh 0.1 - convert the files of Meshtools 3D to Gmsh .msh file"<<endl<<endl
|
||||
<<"syntax: 3dtools2msh -M<mesh-file> -V<model-file> [-F<filter-value>] [-G<output-file>]"<<endl
|
||||
<<" -M Meshtools 3D's mesh file"<<endl
|
||||
<<" -V Meshtools 3D's model file"<<endl
|
||||
<<" -F filter out value, the default is 9999"<<endl
|
||||
<<" -G specify output file's name, the input model file name will be used if -G is absent"<<endl
|
||||
<<"example: 3dtools2msh -Mexample.mesh -Vexample.model -Gexample.msh"<<endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
string _meshname="";
|
||||
string _modelname="";
|
||||
string _mshname="";
|
||||
string temp;
|
||||
double filterNum = 9999;
|
||||
if (argc==1)
|
||||
{
|
||||
disp_help();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (typeget(argv[i],MESH,temp))
|
||||
{
|
||||
if (temp=="")
|
||||
{
|
||||
cout<<"syntax error: "<<argv[i]<<endl;
|
||||
disp_help();
|
||||
return 0;
|
||||
}
|
||||
else _meshname=temp;
|
||||
}
|
||||
else if (typeget(argv[i],VALUE,temp))
|
||||
{
|
||||
if (temp=="")
|
||||
{
|
||||
cout<<"syntax error: "<<argv[i]<<endl;
|
||||
disp_help();
|
||||
return 0;
|
||||
}
|
||||
else _modelname=temp;
|
||||
}
|
||||
else if (typeget(argv[i],FILTER,temp))
|
||||
{
|
||||
if (temp=="")
|
||||
{
|
||||
cout<<"syntax error: "<<argv[i]<<endl;
|
||||
disp_help();
|
||||
return 0;
|
||||
}
|
||||
else filterNum=atof(temp.c_str());
|
||||
}
|
||||
else if (typeget(argv[i],OUTFILE,temp))
|
||||
{
|
||||
if (temp=="")
|
||||
{
|
||||
cout<<"syntax error: "<<argv[i]<<endl;
|
||||
disp_help();
|
||||
return 0;
|
||||
}
|
||||
else _mshname=temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"unrecognized argument "<<argv[i]<<endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(_mshname=="") _mshname=_modelname+"_convert_to.msh";
|
||||
_3dtools2msh testrun;
|
||||
testrun.meshin(_meshname);
|
||||
testrun.physin(_modelname);
|
||||
testrun.findindex();
|
||||
testrun.mshout(_mshname,filterNum);
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user