You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.2 KiB
C++

#include "StdAfx.h"
#include "PltReader.h"
#include "ObjComposite.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "AuthorityMgr.h"
#include "MarkObjPropertieMgr.h"
#include "Layer.h"
#include "GlobalFunction.h"
#include "LogMgr.h"
CPltReader* gPltReader = new CPltReader;//plt 读取对象
CPltReader::CPltReader(void)
{
m_ScaleX = 0.024888;
m_ScaleY = 0.024875;
}
CPltReader::~CPltReader(void)
{
}
#if 1//属性设置函数
CMFCPropertyGridProperty *CPltReader::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("PltSet");;//存储路径
CString Name;
//-------------------------------------------------------------------------------//
PropertyName = _T("plt 读取");
CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName);
//-------------------------------------------------------------------------------//
if(gAuthorityMgr->CheckAuthority(_Authority_Factory))
{
//添加属性变量映射
Name = _T("m_ScaleX");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_ScaleX);
pPropertie->SetType(_PROP_TYPE_DOUBLE);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("plt 读取比例X");
Description = _T("用于将读取的plt 文件缩放到cad 对应大小的比例");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName,(_variant_t)m_ScaleX, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
if(gAuthorityMgr->CheckAuthority(_Authority_Factory))
{
//添加属性变量映射
Name = _T("m_ScaleY");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_ScaleY);
pPropertie->SetType(_PROP_TYPE_DOUBLE);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("plt 读取比例Y");
Description = _T("用于将读取的plt 文件缩放到cad 对应大小的比例");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName,(_variant_t)m_ScaleY, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
//-------------------------------------------------------------------------------//
return pGroup;
}
#endif
#if 1
//打开文件
void CPltReader::OpenFile()
{
CLayer &layer = gLayer;
CFileDialog FileOpen(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "plt 文件 (*.plt)|*.plt;");
if(IDOK == FileOpen.DoModal())
{
CString FilePath=FileOpen.GetPathName();
//解析plt 文件
if(AnalysePltFlie(FilePath))
{
//将vec 的数据生成一个合成对象
TranslatorToComposite(m_PtVec,layer.GetObjContainer());
GetCurViewPtr()->RefreshView();
//更新图形属性
gMarkObjPropertieMgr->UpdateSelMarkObjPropertie();
gLogMgr->WriteDebugLog("func :Open Plt File---->"+FilePath);
}
gLogMgr->WriteDebugLog("func :AnalysePltFlie---->Error");
}
}
//将vec 的数据生成一个合成对象
void CPltReader::TranslatorToComposite(vector<PointType> &vec,CObjContainer &ObjContainer)
{
if(vec.empty())
{
return;
}
//创建合成对象
CObjComposite *pObj = new CObjComposite;
vector<PointType>::iterator iter = vec.begin();
vector<PointType>::iterator iter_end = vec.end();
for(;iter!=iter_end;iter++)
{
Dbxy DbPt((*iter).first.x,(*iter).first.y);
CDataPoint pt(DbPt);
pObj->AddPt(pt,(*iter).second);
}
//移动到中间
{
DbRect rect = pObj->GetRect();
Dbxy pt = rect.GetCenterPt();
SObjOperatePar par;
par.OpType = _OP_MOVE;
par.MoveX = -pt.x;
par.MoveY = -pt.y;
pObj->Operate(par);
}
//缩放到cad 设置尺寸X
{
SObjOperatePar par;
par.Scale = m_ScaleX;
par.OpType = _OP_STRETCH;
DbRect rect = pObj->GetRect();
Dbxy pt = rect.GetCenterPt();
par.OldSize = rect.R - rect.L;
par.NewSize = par.OldSize*par.Scale;
par.Diff = par.NewSize-par.OldSize;
par.xy = _X;
pObj->Operate(par);
}
//缩放到cad 设置尺寸Y
{
SObjOperatePar par;
par.Scale = m_ScaleY;
par.OpType = _OP_STRETCH;
DbRect rect = pObj->GetRect();
Dbxy pt = rect.GetCenterPt();
par.OldSize = rect.T - rect.B;
par.NewSize = par.OldSize*par.Scale;
par.Diff = par.NewSize-par.OldSize;
par.xy = _Y;
pObj->Operate(par);
}
ObjContainer.AddObject(pObj);
}
#endif
#if 1
//解析plt 文件
bool CPltReader::AnalysePltFlie(CString FilePath)
{
m_PtVec.clear();
CStdioFile file;
if(file.Open(FilePath,CFile::modeRead,NULL)==0)
{
return false;
}
CString str;
bool flg = true;
int pos = 0;
while(file.ReadString(str))//读取一行
{
int len = str.GetLength();
while(pos<len)
{
CString TypeStr = FindNextP(str,pos);
if(TypeStr == "PU")
{
flg = true;
}
else if(TypeStr == "PD")
{
flg = false;
}
else if(TypeStr == "PA")//提取坐标值
{
int idx1 = str.Find(',',pos);//逗号的位置
int idx2 = str.Find(';',pos);//分号的位置
if(idx1<idx2 && idx1 != -1 && idx2 != -1)
{
CString strNum1 = str.Mid(pos+1,idx1 - (pos+1));
CString strNum2 = str.Mid(idx1+1,idx2 - (idx1+1));
int x = atoi(strNum1);
int y = atoi(strNum2);
//保存当前点
if(!(x == 0 && y == 0))//不要保存第一个0点
{
CPoint point(x,y);
m_PtVec.push_back(make_pair(point,flg));
}
}
}
else if(TypeStr == "END")
{
break;
}
}
}
return true;
}
//找到下一个P ,返回其类型
CString CPltReader::FindNextP(CString str,int &pos)
{
CString TypeStr;
int len = str.GetLength();
pos = str.Find('P',pos);
if(pos == -1)
{
TypeStr = "END";
}
else if(pos<len)
{
pos++;
if(str[pos]=='U')
{
TypeStr = "PU";
}
else if(str[pos]=='D')
{
TypeStr = "PD";
}
else if(str[pos]=='A')
{
TypeStr = "PA";
}
}
return TypeStr;
}
#endif