⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnode.h

📁 是一个G代码转换的源程序
💻 H
📖 第 1 页 / 共 4 页
字号:
#include <ostream>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <ctype.h>
#include <math.h>
using namespace std;

const double Epsilon=0.00001;
const double pi=3.141592653589793238462643;

class CompCoord
{
	public:
		CompCoord &operator=(const CompCoord &other)
		{
			if (this==&other)
				return *this;
			xq=other.xq;yq=other.yq;xz=other.xz;yz=other.yz;
			xc=other.xc;yc=other.yc;r=other.r;
			Typ=other.Typ;
			return *this;
		}
		float xq,yq,xz,yz;
		float xc,yc,r;
		int Typ;
};

class Coord
{
	public:
		Coord &operator=(const Coord &other)
		{
			if (this==&other)
				return *this;
			x=other.x;y=other.y;z=other.z;r=other.r;
			xc=other.xc;yc=other.yc;
			I=other.I;J=other.J;
			Typ=other.Typ;
			Axial=other.Axial;
			AxialComp=other.AxialComp;
			RadialComp=other.RadialComp;
			Radius=other.Radius;
			AR=other.AR;
			return *this;
		}
		friend ostream& operator<<(ostream &ostr,const Coord &Value)
		{
			ostr<<Value.AR<<" "<<Value.Typ<<" "<<Value.AxialComp<<" "<<Value.Axial<<" "
				<<Value.RadialComp<<" "<<Value.Radius<<" "
				<<Value.x<<" "<<Value.y<<" "<<Value.z<<" "
				<<Value.r<<" "<<Value.I<<" "<<Value.J<<" "
				<<Value.xc<<" "<<Value.yc;
			return ostr;
		}
		float x,y,z,r;
		float xc,yc;
		float I,J;
		int Typ;			// 1直线,2顺圆弧,3逆圆弧,0快速定位
		float Axial;		// 轴向补偿值
		int AxialComp;		// 轴向补偿代号,0取消,1正向补偿,2负向补偿
		int RadialComp;		// 半径补偿代号,0取消,1左刀补,2右刀补
		float Radius;		// 刀具的半径值
		int AR;				// 1绝对坐标,2相对坐标
};

class GNode
{
	public:
		GNode()
		{}

		GNode(vector<string> f):file(f)
		{}
		
		vector<string> getfile() const
		{
			return file;
		}
		
		vector<Coord> getContoutLine() const
		{
			return ContoutLine;
		}

		vector<Coord> getProfileCurve() const
		{
			return ProfileCurve;
		}

		vector<Coord> generateContoutLine();
		
		vector<Coord> generateProfileCurve();

	private:
		vector<string> file;
		vector<Coord> ContoutLine;
		vector<Coord> ProfileCurve;
		void getNumber(const string &str,float &number,int &length);
		string strclearfirstspace(const string &str);
		void clearfirstspace(vector<string> &f);		// 清空每一行前的空格
		void AComp(vector<Coord> &Value);				// 进行轴向补偿
		vector<Coord> RtoA(const vector<Coord> &Value);		// 相对坐标转绝对坐标
		void IJtoR(vector<Coord> &Value);				// 圆弧中I与J转成R
		double arccos(double x,double y);
		double Angle(double xq, double yq, double xz, double yz);
		void CalCenter(const float &xq,const float &yq,const float &xz,const float &yz,
			           const float &r1,const int &Typ,float &xc,float &yc);
		// 进行左右刀补充
		vector<Coord> Compensate(vector<Coord> TValue);
		void CirCenter(vector<Coord> &Value);

		// 曲线与曲线插补
		void CurCurComp(const CompCoord &cur1,const CompCoord &cur2,const float &rc,
			            const int &RadialComp,vector<CompCoord> &result);

		// 直线与直线转接计算
		void LLComp(const float &xq1,const float &yq1,const float &xz1,const float &yz1,
			        const float &xq2,const float &yq2,const float &xz2,const float &yz2,
					float rc,const int &RadialComp,vector<CompCoord> &result);	
		// 单条直线偏置点计算
		void LineComp(const float &xq,const float &yq,const float &xz,const float &yz,
					  float rc,const int &RadialComp,
                      float &lrxq,float &lryq,float &lrxz,float &lryz);
		// 直线与直线伸长与缩短型的转接点计算
		void LL1Radius(double angle1,double angle2,double rc,const int &RadialComp,
					   float &ACx1,float &ACy1);
		// 直线与直线插入型的转接计算
		void LL2Radius(double angle1,double angle2,double rc,const int &RadialComp,
					   float &AC1x,float &AC1y,float &AC2x,float &AC2y);
		// 圆弧与圆弧的转接计算
		void CCComp(const float &Xq1,const float &Yq1,const float &Xz1,const float &Yz1,
					const float &Ox1,const float &Oy1,float r1,const int &Typ1,
					const float &Xq2,const float &Yq2,const float &Xz2,const float &Yz2,
					const float &Ox2,const float &Oy2,float r2,const int &Typ2,
					float rc,const int &RadialComp,vector<CompCoord> &result);
		// 单条圆弧偏置点计算
		void ARCRadius(const float &Xq,const float &Yq,const float &Xz,const float &Yz,
					   const float &Ox,const float &Oy,float r,const int &RadialComp,const int &Typ,
					   float &rc,float &lrxq,float &lryq,float &lrxz,float &lryz);
		// 圆弧与圆弧伸长型的转接计算
		void CC1Radius(double Angle1,double Angle2,float rc,const int &RadialComp,
					   float &ABx,float &ABy,float &AC1x,float &AC1y,float &AC2x,float &AC2y);
		// 圆弧与圆弧插入型的转接计算
		void CC2Radius(double Angle1,double Angle2,float rc,const int &RadialComp,
					   float &ABx,float &ABy,float &AC1x,float &AC1y,float &AC2x,float &AC2y,float &ADx,float &ADy);
		// 直线与圆弧转接计算
		void LCComp(const float &Xq1,const float &Yq1,const float &Xz1,const float &Yz1,
                    const float &Xq2,const float &Yq2,const float &Xz2,const float &Yz2,
					const float &Ox2,const float &Oy2,float r2,const int &Typ2,
					float rc,const int &RadialComp,vector<CompCoord> &result);
		// 直线与圆弧伸长型转接计算
		void LC1Radius(double Angle1,double Angle2,float rc,const int &RadialComp,
			           float &AC1x,float &AC1y,float &AC2x,float &AC2y);
		// 直线与圆弧插入型转接计算
		void LC2Radius(double Angle1,double Angle2,float rc,const int &RadialComp,
			           float &AC1x,float &AC1y,float &AC2x,float &AC2y,float &ADx,float &ADy);
		// 圆弧与直线转接计算
		void CLComp(const float &Xq1,const float &Yq1,const float &Xz1,const float &Yz1,
					const float &Ox1,const float &Oy1,float r1,const int &Typ1,
					const float &Xq2,const float &Yq2,const float &Xz2,const float &Yz2,
					float rc,const int &RadialComp,vector<CompCoord> &result);
		// 圆弧与直线伸长型转接计算
		void CL1Radius(double Angle1,double Angle2,float rc,const int &RadialComp,
			           float &ABx,float &ABy,float &AC1x,float &AC1y);
		// 圆弧与直线插入型转接计算
		void CL2Radius(double Angle1,double Angle2,float rc,const int &RadialComp,
			           float &ABx,float &ABy,float &AC1x,float &AC1y,float &AC2x,float &AC2y);
		// 计算两圆弧求交点
		void JC1JC2(const float &xc1,const float &yc1,float r1,
					const float &xc2,const float &yc2,float r2,
					float &x1,float &y1,float &x2,float &y2);
		// 计算直线与圆弧的交点
		void LineJC(const float &xq,const float &yq,const float &xz,const float &yz,
			        const float &Ox,const float &Oy,float r,
					float &x1,float &y1,float &x2,float &y2);
		// 计算圆弧交点出的切线角度
		double dqcy(const float &xj,const float &yj,const float &Ox,const float &Oy,const int &Typ);
};


void GNode::getNumber(const string &str,float &number,int &length)
{
	char ch;
	number=0;
	string tmp="";
	ch=str[length];
	do
	{
		length++;
		ch=str[length];
		if (length>str.size())
			break;
		if (isdigit(ch) || ch=='.' || ch=='-')
			tmp=tmp+ch;
	}while (ch != ' ' && ch !=';' && ch !='\n');
	char num[128];
	strcpy(num,tmp.c_str());
	number=float(atof(num));
}

string GNode::strclearfirstspace(const string &str)
{
	string tmpstr;
	tmpstr="";
	int i=0;
	while (str[i] == ' ')
		i++;
	int j=0;
	for (i;i<str.length();i++)
		tmpstr=tmpstr+str[i];
	
	return tmpstr;
}

void GNode::clearfirstspace(vector<string> &f)
{
	string str,strtmp;
	for (int i=0;i<f.size();i++)
	{
		str=f[i];
		strtmp=strclearfirstspace(str);
		f[i]=strtmp;
	}
}

void GNode::AComp(vector<Coord> &Value)
{
	for (int i=0;i<Value.size();i++)
	{
		if (Value[i].AxialComp==1)
		{
			Value[i].z=Value[i].z+Value[i].Axial;
			Value[i].AxialComp=0;
		}
		else if (Value[i].AxialComp==2)
		{
			Value[i].z=Value[i].z-Value[i].Axial;
			Value[i].AxialComp=0;
		}
	}
}

vector<Coord> GNode::RtoA(const vector<Coord> &Value)
{
	vector<Coord> TValue;
	Coord TmpValue;
	for (int i=0;i<Value.size();i++)
	{
		TmpValue=Value[i];
		if (Value[i].AR==2)
		{
			if (i>0)
			{
				if (fabs(Value[i-1].x-Value[i].x)>Epsilon)
					TmpValue.x=TValue[i-1].x+Value[i].x;
				else
					TmpValue.x=TValue.back().x;
				if (fabs(Value[i-1].y-Value[i].y)>Epsilon)
					TmpValue.y=TValue[i-1].y+Value[i].y;
				else
					TmpValue.y=TValue.back().y;
				if (fabs(Value[i-1].z-Value[i].z)>Epsilon)
					TmpValue.z=TValue[i-1].z+Value[i].z;
				else
					TmpValue.z=TValue.back().z;
				TmpValue.AR=1;
			}
		}
		TValue.push_back(TmpValue);
	}
	return TValue;
}

void GNode::IJtoR(vector<Coord> &Value)
{
	for (int i=1;i<Value.size();i++)
	{
		if (Value[i].Typ==2 || Value[i].Typ==3)
		{
			if (!(fabs(Value[i].I)<Epsilon && fabs(Value[i].J)<Epsilon))
			{
				double xc,yc;
				xc=Value[i-1].x+Value[i].I;
				yc=Value[i-1].y+Value[i].J;
				double angle,sa,ea;
				sa=Angle(xc,yc,Value[i-1].x,Value[i-1].y);
				ea=Angle(xc,yc,Value[i].x,Value[i].y);
				if (fabs(ea)<Epsilon)
					ea=360;
				if (fabs(sa-360)<Epsilon)
					sa=0;
				if (Value[i].Typ==2)
					angle=360-(ea-sa);
				else
					angle=ea-sa;
				if (angle<0)
					angle=360-angle;
				double r;
				r=sqrtf(Value[i].I*Value[i].I+Value[i].J*Value[i].J);
				if (angle>180)
					r=-r;
				Value[i].r=float(r);
			}
		}
		Value[i].I=0;
		Value[i].J=0;
	}
}

double GNode::arccos(double x,double y)
{
	if (y>0)
		return acos(x)*180.0/pi;
	else
		return (2*pi-acos(x))*180.0/pi;
}

double GNode::Angle(double xq, double yq, double xz, double yz)
{
	double r,x;
	xz=xz-xq;
	yz=yz-yq;
	r=sqrt(xz*xz+yz*yz);
	x=xz/r;
	if (yz>0)
		return acos(x)*180.0/pi;
	else
		return (2*pi-acos(x))*180.0/pi;
}


vector<Coord> GNode::Compensate(vector<Coord> TValue)
{
	vector<Coord> Value;
	deque<Coord> z1Value,z2Value;
	Coord tmpValue;
	float xq1,yq1,xz1,yz1,r1,xc1,yc1;
	float xq2,yq2,xz2,yz2,r2,xc2,yc2;
	float rc;
	float lrxq,lryq,lrxz,lryz;
	int Typ1,Typ2;
	int RadialComp;
	bool exitCurv1;
	bool exitCurv2;
	vector<CompCoord> result;
	CompCoord cur1,cur2;
	int i=0;
	while (i<TValue.size())
	{
		RadialComp=TValue[i].RadialComp;
		if (RadialComp==0 || RadialComp==-1)
		{
			Value.push_back(TValue[i]);
			i++;
		}
		else
		{
			exitCurv2=false;
			exitCurv1=false;
			if (TValue[i-1].RadialComp==0 || TValue[i-1].RadialComp==-1)	//	找第一条线的起点和终点
			{
				exitCurv1=false;
				xq1=TValue[i].x;yq1=TValue[i].y;
				z1Value.push_back(TValue[i]);
				while (i<TValue.size()-1)			
				{
					i++;
					z1Value.push_back(TValue[i]);							// 队列的第1个存放的是起点,队列的最后一个存放的是终点
					if (!(fabs(xq1-TValue[i].x)<Epsilon && fabs(yq1-TValue[i].y)<Epsilon))
					{
						exitCurv1=true;
						break;
					}
				}
			}
			if (TValue[i].RadialComp==1 || TValue[i].RadialComp==2)			// 找第二条线的终点
			{
				xz2=TValue[i].x;yz2=TValue[i].y;
				z2Value.push_back(TValue[i]);
				while (i<TValue.size())
				{
					i++;
					if (TValue[i].RadialComp==0 || TValue[i].RadialComp==-1)
						break;
					z2Value.push_back(TValue[i]);
					if (!(fabs(xz2-TValue[i].x)<Epsilon && fabs(yz2-TValue[i].y)<Epsilon))
					{
						exitCurv2=true;
						break;
					}
				}
			}
			if (exitCurv1)
			{
				xq1=z1Value.front().x;yq1=z1Value.front().y;xz1=z1Value.back().x;yz1=z1Value.back().y;
				xc1=z1Value.back().xc;yc1=z1Value.back().yc;r1=z1Value.back().r;Typ1=z1Value.back().Typ;
				rc=z1Value.back().Radius;RadialComp=z1Value.back().RadialComp;
				if (Typ1==1)
					LineComp(xq1,yq1,xz1,yz1,rc,RadialComp,lrxq,lryq,lrxz,lryz);
				else if (Typ1==2 || Typ1==3)
					ARCRadius(xq1,yq1,xz1,yz1,xc1,yc1,r1,RadialComp,Typ1,rc,lrxq,lryq,lrxz,lryz);
				tmpValue=z1Value.front();
				tmpValue.RadialComp=0;
				tmpValue.x=lrxq;
				tmpValue.y=lryq;
				if (Typ1==2 || Typ1==3)
					tmpValue.r=r1+rc;
				Value.push_back(tmpValue);
				z1Value.pop_front();
				while (z1Value.size()>1)
				{
					tmpValue=z1Value.front();
					tmpValue.RadialComp=0;
					tmpValue.x=Value.back().x;
					tmpValue.y=Value.back().y;
					tmpValue.r=Value.back().r;
					Value.push_back(tmpValue);
					z1Value.pop_front();
				}
				z1Value.pop_front();
			}
			else
			{
				while (z1Value.size()>1)
				{
					tmpValue=z1Value.front();
					tmpValue.RadialComp=0;
					tmpValue.x=Value.back().x;
					tmpValue.y=Value.back().y;
					tmpValue.y=Value.back().r;
					Value.push_back(tmpValue);
					z1Value.pop_front();
				}
			}
			if (exitCurv2)
			{
				if (exitCurv1)
				{
					cur1.xq=xq1;cur1.yq=yq1;cur1.xz=xz1;cur1.yz=yz1;
					cur1.r=r1;cur1.xc=xc1;cur1.yc=yc1;cur1.Typ=Typ1;
					z1Value.pop_front();
					exitCurv1=false;
				}
				else
					cur1=cur2;
				xq2=z2Value.front().x;yq2=z2Value.front().y;xz2=z2Value.back().x;yz2=z2Value.back().y;
				xc2=z2Value.back().xc;yc2=z2Value.back().yc;r2=z2Value.back().r;Typ2=z2Value.back().Typ;
				rc=z2Value.back().Radius;RadialComp=z2Value.back().RadialComp;
				cur2.xq=xq2;cur2.yq=yq2;cur2.xz=xz2;cur2.yz=yz2;
				cur2.r=r2;cur2.xc=xc2;cur2.yc=yc2;cur2.Typ=Typ2;
				CurCurComp(cur1,cur2,rc,RadialComp,result);
				for (int j=0;j<result.size()-1;j++)
				{
					tmpValue=z2Value.front();
					tmpValue.RadialComp=0;
					tmpValue.x=result[j].xz;
					tmpValue.y=result[j].yz;
					tmpValue.Typ=result[j].Typ;
					if (result[j].Typ==2 || result[j].Typ==3)
						tmpValue.r=result[j].r;
					else
						tmpValue.r=0;
					Value.push_back(tmpValue);
				}
				z2Value.pop_front();
				while (z2Value.size()>1)
				{
					tmpValue=z2Value.front();
					tmpValue.RadialComp=0;
					tmpValue.x=Value.back().x;
					tmpValue.y=Value.back().y;
					tmpValue.y=Value.back().r;
					Value.push_back(tmpValue);
					z2Value.pop_front();
				}
				z2Value.pop_front();
			}
			else if (!z2Value.empty())
			{
				xq2=cur2.xq;yq2=cur2.yq;xz2=z2Value.front().x;yz2=z2Value.front().y;
				xc2=z2Value.front().xc;yc2=z2Value.front().yc;r2=z2Value.front().r;Typ2=z2Value.front().Typ;
				rc=z2Value.front().Radius;RadialComp=z2Value.front().RadialComp;	
				if (Typ2==1)
					LineComp(xq2,yq2,xz2,yz2,rc,RadialComp,lrxq,lryq,lrxz,lryz);
				else if (Typ2==2 || Typ2==3)
					ARCRadius(xq2,yq2,xz2,yz2,xc2,yc2,r2,RadialComp,Typ2,rc,lrxq,lryq,lrxz,lryz);
				while (!z2Value.empty())
				{
					tmpValue=z2Value.front();
					tmpValue.RadialComp=0;
					tmpValue.x=lrxz;
					tmpValue.y=lryz;
					if (Typ2==2 || Typ2==3)
						tmpValue.r=tmpValue.r+rc;
					z2Value.pop_front();						
					if (Typ2==2 || Typ2==3)
						tmpValue.y=r2+rc;
					Value.push_back(tmpValue);
					z2Value.pop_front();
				}
			}
		}
	}
	return Value;
}


void GNode::CurCurComp(const CompCoord &cur1,const CompCoord &cur2,const float &rc,
			           const int &RadialComp,vector<CompCoord> &result)
{
	float xq1,yq1,xz1,yz1,xc1,yc1,r1;
	float xq2,yq2,xz2,yz2,xc2,yc2,r2;
	int Typ1,Typ2;
	xq1=cur1.xq;yq1=cur1.yq;xz1=cur1.xz;yz1=cur1.yz;xc1=cur1.xc;yc1=cur1.yc;
	r1=cur1.r;Typ1=cur1.Typ;
	xq2=cur2.xq;yq2=cur2.yq;xz2=cur2.xz;yz2=cur2.yz;xc2=cur2.xc;yc2=cur2.yc;
	r2=cur2.r;Typ2=cur2.Typ;
	if (Typ1==1 && Typ2==1)
		LLComp(xq1,yq1,xz1,yz1,xq2,yq2,xz2,yz2,rc,RadialComp,result);
	else if ((Typ1==2 || Typ1==3) && (Typ2==2 || Typ2==3))
		CCComp(xq1,yq1,xz1,yz1,xc1,yc1,r1,Typ1,xq2,yq2,xz2,yz2,xc2,yc2,r2,Typ2,rc,RadialComp,result);
	else if ((Typ1==2 || Typ1==3) && Typ2==1)
		CLComp(xq1,yq1,xz1,yz1,xc1,yc1,r1,Typ1,xq2,yq2,xz2,yz2,rc,RadialComp,result);
	else if (Typ1==1 && (Typ2==2 || Typ2==3))
		LCComp(xq1,yq1,xz1,yz1,xq2,yq2,xz2,yz2,xc2,yc2,r2,Typ2,rc,RadialComp,result);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -