📄 mainfrm.cpp
字号:
SoSeparator *coords_sep = new SoSeparator();
m_coords_trans = new SoTranslation();
coords_sep->addChild(m_coords_trans);
SoFont *font = new SoFont();
coords_sep->addChild(font);
coords_text = new SoText2();
coords_sep->addChild(coords_text);
m_root->addChild(coords_sep);
// CRect rect_viewer;
// m_wndDialogBar.GetDlgItem(IDC_VIEWER)->GetWindowRect(rect_viewer);
// int xRight=0,yRight=0;
// xRight=rect_viewer.Width()/2;
font->name.setValue("Times-Roman");
m_coords_trans->translation.setValue(1,1,0);
coords_text->justification = SoText2::RIGHT;
coords_text->string.set1Value(0,"");
coords_text->string.set1Value(1,"Current relative coordinates:X 000,Y 000,Z 000");
coords_text->string.set1Value(2,"Current absolute coordinates:X 000,Y 000,Z 000");
IvfSetSceneGraph(m_root);
//------------------------------------------------------------------------------------------------
// 判断有没有正在激活(也就是显示)的文档.
CDocument *pDocument = this->GetActiveFrame()->GetActiveDocument();
if(pDocument != NULL)
{
// 检测到有打开的NC代码,在此添加处理代码.
CNCSimulaSysView *pActiveView = (CNCSimulaSysView*)this->GetActiveFrame()->GetActiveView();
CBCGPEditCtrl *pEditCtrl = pActiveView->GetEditCtrl();
// pEditCtrl->EnableWindow(false);
// 读取编辑视图的全部内容和行数.
CString strContent;
pEditCtrl->GetWindowTextA(strContent);
int iLineCount = pEditCtrl->GetLineCount();
// 仿真开始和其它标志位.
m_bSimulaHaveStart = true;
m_ArrayCodeByLine.RemoveAll();
// 读取每一行的内容并分别进行处理.
for(int i=1;i<=iLineCount;i++)
{
CString strLine;
if(i<iLineCount)
{
int iSplit = strContent.Find('\n');
strLine = strContent.Left(iSplit);
strContent = strContent.Right(strContent.GetLength()-iSplit-1);
}
else
{
strLine = strContent;
}
// 将分行后的代码存储到m_ArrayCodeByLine结构中.
m_ArrayCodeByLine.Add(strLine);
}
// 调用函数进行插补和动态显示.
InterpolationAndDisplay();
}
else
{
AfxMessageBox(_T("程序检测到没有打开的NC代码."));
}
// pEditCtrl->EnableWindow(true);
}
else
{
SetTimer(1,60,NULL);
}
}
if(nID == ID_SIMULA_PAUSE)
{
m_uID_simulatoolbar = ID_SIMULA_PAUSE;
KillTimer(1);
}
}
void CMainFrame::OnUpdateSimulaToolbarItemSel(CCmdUI* pCmdUI)
{
pCmdUI->Enable(false);
if((m_bSimulaHaveStart && pCmdUI->m_nID==ID_SIMULA_PLAY && m_uID_simulatoolbar==ID_SIMULA_PAUSE) || (!m_bSimulaHaveStart && pCmdUI->m_nID==ID_SIMULA_PLAY))
{
pCmdUI->Enable(true);
}
if(m_bSimulaHaveStart && pCmdUI->m_nID==ID_SIMULA_PAUSE && m_uID_simulatoolbar == ID_SIMULA_PLAY)
{
pCmdUI->Enable(true);
}
}
void CMainFrame::InterpolationAndDisplay(void)
{
CNCSimulaSysView *pActiveView = (CNCSimulaSysView*)this->GetActiveFrame()->GetActiveView();
CBCGPEditCtrl *pEditCtrl = pActiveView->GetEditCtrl();
for(int i=0;i<=2;i++)
{
m_fCoordinate_relative[i]=0.0f;
}
// 在此添加每一行的处理代码.
// 调用CNCInterpolation类中的LineInterpolation判断是否需要插补,如果需要则计算出各个坐标点.
if(m_ArrayCodeByLine.GetSize()-1>=m_iLineNowInterpolation)
{
pEditCtrl->SelectLine(m_iLineNowInterpolation);
// 将每一行NC代码解析来的所有进给方向存于m_ArrayForwardDirection_CodeLineInterpolation.
bool bNeedInterpolation = m_NCCodeParse.CodeLineParse(m_ArrayCodeByLine[m_iLineNowInterpolation],false,m_ArrayForwardDirection_CodeLineInterpolation);
// 调用函数计算每一步进给后的相对坐标和绝对坐标,并绘制刀具轨迹.
if(m_ArrayForwardDirection_CodeLineInterpolation.GetSize()!=0)
{
SetTimer(1,60,NULL);
}
m_iLineNowInterpolation++;
}
else
{
m_iLineNowInterpolation = 0;
m_bSimulaHaveStart = false;
}
}
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(m_ArrayForwardDirection_CodeLineInterpolation.GetSize()!=0 && m_iNowStep<=m_ArrayForwardDirection_CodeLineInterpolation.GetSize()-1)
{
SoSeparator *line_sep=new SoSeparator();
SoCoordinate3 *coords = new SoCoordinate3();
line_sep->addChild(coords);
SoMaterial *mat = new SoMaterial();
line_sep->addChild(mat);
SoLineSet *lines = new SoLineSet();
line_sep->addChild(lines);
SoTranslation *trans = new SoTranslation();
if(m_ArrayForwardDirection_CodeLineInterpolation[m_iNowStep]==_T("X+"))
{
// 画线段.
coords->point.set1Value(0,0, 0, 0);
coords->point.set1Value(1,m_fScale_display, 0, 0);
// 添加一个位移以便是下条线段的起点是本条线段的终点.
trans->translation.setValue(m_fScale_display, 0, 0);
// 更新相对和绝对坐标.
m_fCoordinate_relative[0]+=1;
m_fCoordinate_absolute[0]+=1;
// 移动刀具.
SbVec3f pos_increase;
pos_increase.setValue(m_fScale_display, 0, 0);
m_tools_trans->translation.setValue(m_tools_trans->translation.getValue()+pos_increase);
}
if(m_ArrayForwardDirection_CodeLineInterpolation[m_iNowStep]==_T("X-"))
{
coords->point.set1Value(0,0, 0, 0);
coords->point.set1Value(1,-1*m_fScale_display, 0, 0);
trans->translation.setValue(-1*m_fScale_display, 0, 0);
m_fCoordinate_relative[0]-=1;
m_fCoordinate_absolute[0]-=1;
SbVec3f pos_increase;
pos_increase.setValue(-1*m_fScale_display, 0, 0);
m_tools_trans->translation.setValue(m_tools_trans->translation.getValue()+pos_increase);
}
if(m_ArrayForwardDirection_CodeLineInterpolation[m_iNowStep]==_T("Y+"))
{
coords->point.set1Value(0,0, 0, 0);
coords->point.set1Value(1,0, m_fScale_display, 0);
trans->translation.setValue(0, m_fScale_display, 0);
m_fCoordinate_relative[1]+=1;
m_fCoordinate_absolute[1]+=1;
SbVec3f pos_increase;
pos_increase.setValue(0, m_fScale_display, 0);
m_tools_trans->translation.setValue(m_tools_trans->translation.getValue()+pos_increase);
}
if(m_ArrayForwardDirection_CodeLineInterpolation[m_iNowStep]==_T("Y-"))
{
coords->point.set1Value(0,0, 0, 0);
coords->point.set1Value(1,0, -1*m_fScale_display, 0);
trans->translation.setValue(0, -1*m_fScale_display, 0);
m_fCoordinate_relative[1]-=1;
m_fCoordinate_absolute[1]-=1;
SbVec3f pos_increase;
pos_increase.setValue(0, -1*m_fScale_display, 0);
m_tools_trans->translation.setValue(m_tools_trans->translation.getValue()+pos_increase);
}
if(m_ArrayForwardDirection_CodeLineInterpolation[m_iNowStep]==_T("Z+"))
{
coords->point.set1Value(0,0, 0, 0);
coords->point.set1Value(1,0, 0, m_fScale_display);
trans->translation.setValue(0, 0, m_fScale_display);
m_fCoordinate_relative[2]+=1;
m_fCoordinate_absolute[2]+=1;
SbVec3f pos_increase;
pos_increase.setValue(0, 0, m_fScale_display);
m_tools_trans->translation.setValue(m_tools_trans->translation.getValue()+pos_increase);
}
if(m_ArrayForwardDirection_CodeLineInterpolation[m_iNowStep]==_T("Z-"))
{
coords->point.set1Value(0,0, 0, 0);
coords->point.set1Value(1,0, 0, -1*m_fScale_display);
trans->translation.setValue(0, 0, -1*m_fScale_display);
m_fCoordinate_relative[2]-=1;
m_fCoordinate_absolute[2]-=1;
SbVec3f pos_increase;
pos_increase.setValue(0, 0, -1*m_fScale_display);
m_tools_trans->translation.setValue(m_tools_trans->translation.getValue()+pos_increase);
}
lines->numVertices.set1Value(0, 2);
simula_sep->addChild(line_sep);
simula_sep->addChild(trans);
// 修改显示的坐标值.
CString str_relative=_T("Current relative coordinates:");
CString str_absolute=_T("Current absolute coordinates:");
char temp[26];
sprintf(temp,"%f",m_fCoordinate_relative[0]);
str_relative = str_relative+_T("X: ")+temp;
sprintf(temp,"%f",m_fCoordinate_relative[1]);
str_relative = str_relative+_T(",Y: ")+temp;
sprintf(temp,"%f",m_fCoordinate_relative[2]);
str_relative = str_relative+_T(",Z: ")+temp;
char c_relative[100];
_tcscpy(c_relative, str_relative.GetBuffer(str_relative.GetLength())) ;
sprintf(temp,"%f",m_fCoordinate_absolute[0]);
str_absolute = str_absolute+_T("X: ")+temp;
sprintf(temp,"%f",m_fCoordinate_absolute[1]);
str_absolute = str_absolute+_T(",Y: ")+temp;
sprintf(temp,"%f",m_fCoordinate_absolute[2]);
str_absolute = str_absolute+_T(",Z: ")+temp;
char c_absolute[100];
_tcscpy(c_absolute, str_absolute.GetBuffer(str_absolute.GetLength())) ;
coords_text->string.set1Value(1,c_relative);
coords_text->string.set1Value(2,c_absolute);
m_iNowStep++;
}
else
{
m_ArrayForwardDirection_CodeLineInterpolation.RemoveAll();
KillTimer(1);
m_iNowStep = 0;
InterpolationAndDisplay();
}
__super::OnTimer(nIDEvent);
}
void CMainFrame::InitDynamicDisplayModule(void)
{
// BEGIN_IVWGEN
static int cArgs[]= {
// Decoration
TRUE,
// URL Display
TRUE,
// Viewpoint
TRUE,
// URL Fetch
TRUE,
};
CIvfApp::IvfInitSoWin( &m_wndDialogBar);
CWnd *pWnd = m_wndDialogBar.GetDlgItem(IDC_VIEWER);
IvfCreateComponent(pWnd,(void *)cArgs);
// END_IVWGEN
}
void CMainFrame::SynSimulaProcess(void)
{
// 弹出Open Inventor的DialogBar.
m_wndDialogBar.ShowControlBar(true,false,false);
// 使坐标记录数组复位.
for(int i=0;i<3;i++)
{
m_fCoordinate_relative[i] = 0.0f;
m_fCoordinate_absolute[i] = 0.0f;
}
// 初始化根节点及其它.
m_root->removeAllChildren();
// m_root->ref();
// 添加第一个相机节点.
SoOrthographicCamera *camera_ortho_simula = new SoOrthographicCamera();
camera_ortho_simula->position.setValue(0, 0, 90);
camera_ortho_simula->height=500;
camera_ortho_simula->aspectRatio=1.0;
m_root->addChild(camera_ortho_simula);
// 仿真节点.
simula_sep=new SoSeparator();
m_root->addChild(simula_sep);
// 添加坐标轴节点.
SoSeparator *axis_sep = new SoSeparator();
SoInput *axis_in = new SoInput();
axis_in->openFile("axis.iv");
axis_sep = SoDB::readAll(axis_in);
simula_sep->addChild(axis_sep);
// 添加刀具分割符节点.
SoSeparator *tools_sep = new SoSeparator();
m_tools_trans=new SoTranslation();
tools_sep->addChild(m_tools_trans);
SoCube *cube = new SoCube();
tools_sep->addChild(cube);
cube->height=7;
m_tools_trans->translation.setValue(0,0,0);
simula_sep->addChild(tools_sep);
// 添加第二个相机节点.
SoOrthographicCamera *camera_ortho_coords = new SoOrthographicCamera();
// camera_ortho_coords->position.setValue(0, 0, 90);
// camera_ortho_coords->height=500;
// camera_ortho_coords->aspectRatio=1.0;
m_root->addChild(camera_ortho_coords);
// 添加坐标显示节点的内容.
SoSeparator *coords_sep = new SoSeparator();
m_coords_trans = new SoTranslation();
coords_sep->addChild(m_coords_trans);
SoFont *font = new SoFont();
coords_sep->addChild(font);
coords_text = new SoText2();
coords_sep->addChild(coords_text);
m_root->addChild(coords_sep);
// CRect rect_viewer;
// m_wndDialogBar.GetDlgItem(IDC_VIEWER)->GetWindowRect(rect_viewer);
// int xRight=0,yRight=0;
// xRight=rect_viewer.Width()/2;
font->name.setValue("Times-Roman");
m_coords_trans->translation.setValue(1,1,0);
coords_text->justification = SoText2::RIGHT;
coords_text->string.set1Value(0,"");
coords_text->string.set1Value(1,"Current relative coordinates:X 000,Y 000,Z 000");
coords_text->string.set1Value(2,"Current absolute coordinates:X 000,Y 000,Z 000");
IvfSetSceneGraph(m_root);
//------------------------------------------------------------------------------------------------
// 判断有没有正在激活(也就是显示)的文档.
CDocument *pDocument = this->GetActiveFrame()->GetActiveDocument();
if(pDocument != NULL)
{
// 检测到有打开的NC代码,在此添加处理代码.
CNCSimulaSysView *pActiveView = (CNCSimulaSysView*)this->GetActiveFrame()->GetActiveView();
CBCGPEditCtrl *pEditCtrl = pActiveView->GetEditCtrl();
// pEditCtrl->EnableWindow(false);
// 读取编辑视图的全部内容和行数.
CString strContent;
pEditCtrl->GetWindowTextA(strContent);
int iLineCount = pEditCtrl->GetLineCount();
m_ArrayCodeByLine.RemoveAll();
// 读取每一行的内容并分别进行处理.
for(int i=1;i<=iLineCount;i++)
{
CString strLine;
if(i<iLineCount)
{
int iSplit = strContent.Find('\n');
strLine = strContent.Left(iSplit);
strContent = strContent.Right(strContent.GetLength()-iSplit-1);
}
else
{
strLine = strContent;
}
// 将分行后的代码存储到m_ArrayCodeByLine结构中.
m_ArrayCodeByLine.Add(strLine);
}
// 调用函数进行插补和动态显示.
InterpolationAndDisplay();
// pEditCtrl->EnableWindow(true);
}
else
{
AfxMessageBox(_T("程序检测到没有打开的NC代码."));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -