Midas GTS NX 的四面体模型转为 3DEC的模型 c++源码 (Tetrahedron to 3DEC)

Midas GTS NX 的四面体模型导入3DEC

1.使用方法
(1)数据导出之前:网格->工具->重新编号:单元、节点



(2)数据格式

(3)双击打开 Tet to 3DEC.exe ,输入节点文件名称:nl,和单元文件名称:el,回车,程序将自动开始读取。 

(注意:Tet to 3DEC.exe 和 节点、单元文件放在同一个文件夹






2.//完整程序:

https://pan.baidu.com/s/1KjOMmTp_4sf0uT8nG8Lt4w

3.//可以直接运行的程序:

https://pan.baidu.com/s/1xf7MGYdqCNurxvMft_GxEw


4.//以下是完整代码

/************************************************************************/
/* 将Midas GTS NX 的四面体模型 转为 3DEC模型 [email protected],2018.4.23 */
/************************************************************************/
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;


class CVert  // 节点
{
public:
	double x, y, z; 
	int id, pointattribute;
	CVert() {};
};

 class CElem  // 单元
 {
 public:
	int pt012[4];  
	int id, tettattribute;
	CElem() {};
};


void printinformation()
{
	
	cout<<"//************************************************************************//"<<endl;
	cout<<"   中国地质大学(北京) 工程技术学院  [email protected]   2018.4.22  "<<endl;
	cout<<"                 将Midas GTS NX 的四面体模型 转为 3DEC模型                      "<<endl;
	cout<<"                                                                              "<<endl;
	cout<<" 1、将节点文件,整理成4列,分别为节点ID,X, Y, Z"<<endl;
	cout<<"\t\tnl.txt文件第一行加上节点数量,如下:"<<endl;
	cout<<"\t\t1180	"<<endl;
	cout<<"\t\t1	30.000000	100.000000	75.000000"<<endl;
	cout<<"\t\t2	40.000000	100.000000	75.000000"<<endl;
	cout<<"\t\t3	20.000000	100.000000	75.000000"<<endl;
	cout<<"\t\t................................................"<<endl;
	cout<<" 2、将mIDAS网格单元文件,整理成6列,分别为"<<endl;
	cout<<"\t\t1)单元ID,材料属性,节点ID1~4; "<<endl;
	cout<<"\t\t2)材料属性统一改成数字,如1,2,3等"<<endl;
	cout<<"\t\t3)el.txt文件第一行加上单元数量,如下:"<<endl;
	cout<<"\t\t4834	"<<endl;					
	cout<<"\t\t1	1	357	26	17	25"<<endl;
	cout<<"\t\t2	1	347	344	31	22"<<endl;
	cout<<"\t\t3	1	373	330	25	34"<<endl;
	cout<<"\t\t................................................"<<endl;	
	cout<<"//************************************************************************//"<<endl;

}



//tet to 3dec, 判断四面体一个面上的三个点是不是逆时针,[email protected]
int anticlockwise(CVert PTS0, CVert PTS1, CVert PTS2, CVert PTS3)
{
	double x01, x12, y01, y12, z01, z12;//向量01,向量12
	double vx, vy, vz;//法向量
	double px, py, pz;//向量03
	double cospv = 0;

	/**/
	x01 =  PTS1.x - PTS0.x; y01 =  PTS1.y - PTS0.y; z01 =  PTS1.z -  PTS0.z;
	x12 =  PTS2.x - PTS1.x; y12 =  PTS2.y - PTS1.y; z12 =  PTS2.z -  PTS1.z;
	vx = y01*z12 - z01*y12; vy = z01*x12 - x01*z12; vz = x01*y12 - y01*x12;
	px = PTS3.x - PTS0.x; py = PTS3.y - PTS0.y; pz = PTS3.z - PTS0.z;
	cospv = (vx*px + vy*py + vz*pz);


	if (cospv <= 0)  return 1;
	else
		return 0;
}

/************************************************************************/
/* 将四面体转为3dec文件,  [email protected],2016.1028 */
/************************************************************************/
int main()
{
	printinformation();

	//读入文件
	int id, id0, id1, id2, id3;
	double x, y, z;	

	CVert * PTS;  // 存放所有块体的节点
	CElem * TET;  // 存放所有块体的单元

	vector<int>GroupNum;
	bool gp = false;

	int nVert, nTET;  //块体的节点和单元数量
	string filenamenode;
	string filenameele;
	string filename3dec;


	cout<<"\n请输入节点文件*.txt: ";
	cin>>filenamenode;
	ifstream finnode(filenamenode + ".txt");

	finnode >> nVert;
	PTS = new CVert[nVert];
	//读入点
	for (int i = 0; i < nVert; i++)
	{
		finnode >> id >> x >> y >> z;
		PTS[i].x = x;
		PTS[i].y = y;
		PTS[i].z = z;
	}
	finnode.close();

	cout<<"\t读入节点数量: "<< nVert <<endl; 


	//读入四面体单元
	cout<<"\n请输入四面体单元文件*.txt: ";
	cin>>filenameele;
	ifstream finele(filenameele + ".txt");

	GroupNum.push_back(0);

	finele >> nTET;
	TET = new CElem[nTET];
	for(int i = 0; i < nTET; i++)
	{
		gp = false;

		finele >> id >> id >> id0 >> id1>> id2>> id3;
		TET[i].pt012[0] = id0 - 1;
		TET[i].pt012[1] = id1 - 1;
		TET[i].pt012[2] = id2 - 1;
		TET[i].pt012[3] = id3 - 1;

		TET[i].tettattribute = id;


		for (int j = 0; j < GroupNum.size(); j++)
		{
			if (id == GroupNum[j]) 
			{
				gp = true; 
				break; 
			}
		}
		if (!gp) GroupNum.push_back(id);

	}
	finele.close();	
	
	cout<<"\t读入单元数量: "<< nTET <<endl; 
	cout<<"\t模型区域数量: "<< GroupNum.size() - 1 <<endl; 

	/***************************************************************************************************************************************************/
	/* tet to 3dec*/
	ofstream fout3dec(filenamenode + "_" + filenameele + "_Tet_3DEC.txt");

	for (int i = 1; i < GroupNum.size(); i++) 
	{
		
		for (int j = 0; j < nTET; j++)
		{
			id0 = TET[j].pt012[0]; 
			id1 = TET[j].pt012[1]; 
			id2 = TET[j].pt012[2]; 
			id3 = TET[j].pt012[3];
			id = TET[j].tettattribute;

			if (id != GroupNum[i])	continue;			

			fout3dec <<"poly &"<<"\n";	

			//123
			if (anticlockwise(PTS[id1], PTS[id2], PTS[id3], PTS[id0]) == 0)
			{
				fout3dec.setf(ios::fixed, ios::floatfield);  // 设定为 fixed 模式,以小数点表示浮点数  
				fout3dec.precision(6);  // 设置精度  

				fout3dec <<"face"<<"  "<<PTS[id1].x<<","<<PTS[id1].y<<","<< PTS[id1].z<<"  "<<PTS[id2].x<<","<<PTS[id2].y<<","<< PTS[id2].z;
				fout3dec <<"  "<< PTS[id3].x<<","<<PTS[id3].y<<","<< PTS[id3].z<<" &"<<"\n";
			}else
			{
				if (anticlockwise(PTS[id2], PTS[id1], PTS[id3], PTS[id0]) == 0)
				{
					fout3dec.setf(ios::fixed, ios::floatfield);  
					fout3dec.precision(6);   

					fout3dec <<"face"<<"  "<<PTS[id2].x<<","<<PTS[id2].y<<","<< PTS[id2].z<<"  "<<PTS[id1].x<<","<<PTS[id1].y<<","<< PTS[id1].z;
					fout3dec <<"  "<< PTS[id3].x<<","<<PTS[id3].y<<","<< PTS[id3].z<<" &"<<"\n";
				}
			}   

			//023
			if (anticlockwise(PTS[id0], PTS[id2], PTS[id3], PTS[id1]) == 0)	
			{
				fout3dec.setf(ios::fixed, ios::floatfield);  
				fout3dec.precision(6);   

				fout3dec <<"face"<<"  "<<PTS[id0].x<<","<<PTS[id0].y<<","<< PTS[id0].z<<"  "<<PTS[id2].x<<","<<PTS[id2].y<<","<< PTS[id2].z;
				fout3dec <<"  "<< PTS[id3].x<<","<<PTS[id3].y<<","<< PTS[id3].z<<" &"<<"\n";
			}else
			{
				if (anticlockwise(PTS[id2], PTS[id0], PTS[id3], PTS[id1]) == 0)
				{
					fout3dec.setf(ios::fixed, ios::floatfield);   
					fout3dec.precision(6);  

					fout3dec <<"face"<<"  "<<PTS[id2].x<<","<<PTS[id2].y<<","<< PTS[id2].z<<"  "<<PTS[id0].x<<","<<PTS[id0].y<<","<< PTS[id0].z;
					fout3dec <<"  "<< PTS[id3].x<<","<<PTS[id3].y<<","<< PTS[id3].z<<" &"<<"\n";
				}
			}

			//013	
			if (anticlockwise(PTS[id0], PTS[id1], PTS[id3], PTS[id2]) == 0)	
			{
				fout3dec.setf(ios::fixed, ios::floatfield);   
				fout3dec.precision(6);  

				fout3dec <<"face"<<"  "<<PTS[id0].x<<","<<PTS[id0].y<<","<< PTS[id0].z<<"  "<<PTS[id1].x<<","<<PTS[id1].y<<","<< PTS[id1].z;
				fout3dec <<"  "<< PTS[id3].x<<","<<PTS[id3].y<<","<< PTS[id3].z<<" &"<<"\n";
			}else
			{
				if (anticlockwise(PTS[id1], PTS[id0], PTS[id3], PTS[id2]) == 0)
				{
					fout3dec.setf(ios::fixed, ios::floatfield);  
					fout3dec.precision(6);   

					fout3dec <<"face"<<"  "<<PTS[id1].x<<","<<PTS[id1].y<<","<< PTS[id1].z<<"  "<<PTS[id0].x<<","<<PTS[id0].y<<","<< PTS[id0].z;
					fout3dec <<"  "<< PTS[id3].x<<","<<PTS[id3].y<<","<< PTS[id3].z<<" &"<<"\n";
				}
			}

			//012
			if (anticlockwise(PTS[id0], PTS[id1], PTS[id2], PTS[id3]) == 0)
			{
				fout3dec.setf(ios::fixed, ios::floatfield);   
				fout3dec.precision(6);

				fout3dec <<"face"<<"  "<<PTS[id0].x<<","<<PTS[id0].y<<","<< PTS[id0].z<<"  "<<PTS[id1].x<<","<<PTS[id1].y<<","<< PTS[id1].z;
				fout3dec <<"  "<< PTS[id2].x<<","<<PTS[id2].y<<","<< PTS[id2].z<<"\n";
			}else
			{
				if (anticlockwise(PTS[id1], PTS[id0], PTS[id2], PTS[id3]) == 0)
				{
					fout3dec.setf(ios::fixed, ios::floatfield);   
					fout3dec.precision(6);  

					fout3dec <<"face"<<"  "<<PTS[id1].x<<","<<PTS[id1].y<<","<< PTS[id1].z<<"  "<<PTS[id0].x<<","<<PTS[id0].y<<","<< PTS[id0].z;
					fout3dec <<"  "<< PTS[id2].x<<","<<PTS[id2].y<<","<< PTS[id2].z<<"\n";
				}
			}

		}
		
		fout3dec<< " mark region "<< GroupNum[i]<<"\n";
		fout3dec<< " hide region "<< GroupNum[i]<<"\n";
	}

	fout3dec<< "seek \n";
	
	fout3dec.close();

	cout<<"\n转换完成!\n";

	delete [] PTS;	
	delete [] TET;

	GroupNum.clear();
	GroupNum.shrink_to_fit();
	/***************************************************************************************************************************************************/

	cout<<"\n结果已经保存至: "<< filenamenode + "_" + filenameele + "_Tet_3DEC.txt\n";
		



	system("pause");
	return 0;

}

猜你喜欢

转载自blog.csdn.net/yiyu7234/article/details/80040712
DEC