AnyCAD(MFC版)三种格式(IGES,STEP,STL)三维零件的读取

注:显示请参考利用AnyCAD在MFC中对话框中增加三维显示

零件读取代码

类型 相关类
IGES TopoDataExchangeIges
STEP TopoDataExchangeStep
STL TopoDataExchangeStl

IGES读取与测试

void CAnyCADTest1Dlg::OnBnClickedImportIges()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog igesDlg(TRUE);
	igesDlg.m_ofn.lpstrTitle = _T("读取IGES文件");
	igesDlg.m_ofn.lpstrFilter = _T("IGES(*.iges*.iges;*.igsAll Files*.*)"); 
	if (!igesDlg.DoModal())
	{
		AfxMessageBox(_T("模型导入失败!")); 
		return;
	}
   
#ifdef UNICODE
	USES_CONVERSION;
	string path = T2A(igesDlg.GetPathName());
#else
	string path = igesDlg.GetPathName().GetBuffer(0);
#endif

	TopoDataExchangeIges iges;				//复合类型(Topo_COMPOUND),24个顶点,6个面,24条边
	m_igesShape = iges.Read(path);

//	auto node = m_Window3D.ShowGeometry(m_igesShape, ElementId(2));     //该句为显示读入模型的语句

    ///////////////////////////后面为一些测试//////////////////////////
	EnumTopoShapeType type = m_igesShape.GetShapeType();                //类型

	//对iges的face进行提取
	m_subGroup = GlobalInstance::topoExplor.ExplorFaces(m_igesShape);

	TopoShapeGroup edgeGroup = GlobalInstance::topoExplor.ExplorEdges(m_igesShape);
	EdgeClassifier edgeClassfier;
	edgeClassfier.Initialize(m_igesShape);
	TopoShapeGroup freeEdgeGroup = edgeClassfier.GetFreeEdges();
	int size = edgeGroup.Size();                            			//24
	int size1 = freeEdgeGroup.Size();									//23
	int size2 = edgeClassfier.GetSharedEdges().Size();					//0
	int size3 = edgeClassfier.GetStandaloneEdges().Size();				//0
	for (int i(0); i < size; ++i)
	{
		m_Window3D.ShowGeometry(edgeGroup.GetTopoShape(i), ElementId(i+1));
	}

	m_Window3D.GetView().RequestDraw(1);
}

STEP读取与测试

void CAnyCADTest1Dlg::OnBnClickedImportStep()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog stepDlg(TRUE);
	stepDlg.m_ofn.lpstrTitle = _T("读取STEP文件");
	stepDlg.m_ofn.lpstrFilter = _T("STEP(*.step*.stepAll Files*.*)");
	if (!stepDlg.DoModal())
	{
		AfxMessageBox(_T("模型导入失败!"));
		return;
	}

#ifdef UNICODE
	USES_CONVERSION;
	string path = T2A(stepDlg.GetPathName());
#else
	string path = stepDlg.GetPathName().GetBuffer(0);
#endif

	TopoDataExchangeStep step;		//实体(Topo_SOLID ),8个顶点,6个面,12条边
	m_stepShape = step.Read(path);

//	auto node = m_Window3D.ShowGeometry(m_stpShape, ElementId(2));      //该句为显示读入模型的语句

///////////////////////////后面为一些测试//////////////////////////
	EnumTopoShapeType type = m_stepShape.GetShapeType();        //类型

	//对step的face进行提取
	m_subGroup = GlobalInstance::topoExplor.ExplorFaces(m_stepShape);

	TopoShapeGroup edgeGroup = GlobalInstance::topoExplor.ExplorEdges(m_stepShape);
	EdgeClassifier edgeClassfier;
	edgeClassfier.Initialize(m_stepShape);
	TopoShapeGroup sharedEdgeGroup = edgeClassfier.GetSharedEdges();
	int size = edgeGroup.Size();                                //12
	int size1 = edgeClassfier.GetFreeEdges().Size();			//0
	int size2 = sharedEdgeGroup.Size();							//11
	int size3 = edgeClassfier.GetStandaloneEdges().Size();		//0
	for (int i(0); i < size2; ++i)
	{
		m_Window3D.ShowGeometry(sharedEdgeGroup.GetTopoShape(i), ElementId(i + 1));
	}
	
	m_Window3D.GetView().RequestDraw(1);
}

STL读取与测试

void CAnyCADTest1Dlg::OnBnClickedImportStl()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog stlDlg(TRUE);
	stlDlg.m_ofn.lpstrTitle = _T("读取STL文件");
	stlDlg.m_ofn.lpstrFilter = _T("STL(*.stl*.stlAll Files*.*)");
	if (!stlDlg.DoModal())
	{
		AfxMessageBox(_T("模型导入失败!"));
		return;
	}

#ifdef UNICODE
	USES_CONVERSION;
	string path = T2A(stlDlg.GetPathName());
#else
	string path = stlDlg.GetPathName().GetBuffer(0);
#endif

	TopoDataExchangeStl stl;		//三角面片(Topo_SHELL),8个顶点,12个面(三角面),18条边
	m_stlShape = stl.Read(path);

	//	auto node = m_Window3D.ShowGeometry(m_stpShape, ElementId(2));     //该句为显示读入模型的语句
    
    ///////////////////////////后面为一些测试//////////////////////////
	EnumTopoShapeType type = m_stlShape.GetShapeType();         //类型

	//对stl的face进行提取
	m_subGroup = GlobalInstance::topoExplor.ExplorFaces(m_stlShape);

	TopoShapeGroup edgeGroup = GlobalInstance::topoExplor.ExplorEdges(m_stlShape);
	EdgeClassifier edgeClassfier;
	edgeClassfier.Initialize(m_stlShape);
	TopoShapeGroup freeEdgeGroup = edgeClassfier.GetFreeEdges();
	int size = edgeGroup.Size();							//18
	int size1 = freeEdgeGroup.Size();						//0
	int size2 = edgeClassfier.GetSharedEdges().Size();		//17
	int size3 = edgeClassfier.GetStandaloneEdges().Size();	//0
	for (int i(0); i < size; ++i)
	{
		m_Window3D.ShowGeometry(edgeGroup.GetTopoShape(i), ElementId(i + 1));
	}

	m_Window3D.GetView().RequestDraw(1);
}

测试分析

  以上程序读取的均为长方体快,为同一长方体块的三种文件。
  以上程序中在读取完三种零件后对读取零件的一些属性进行了测试,主要包含以下属性:
    (1) type:读取得到的零件的类型;
    (2) 边数:包括两种 ——
           a)通过GlobalInstance::topoExplor.ExplorEdges获取的物体所有边界,个数为size;
           b)通过edgeClassfier.GetFreeEdges获取的物体边界,其中又包括三种边界free edge,shared edge,standalone edge,对应的个数为size1,size2,size3。
  
  测试代码结果如下表所示(表格中后两列是后期补充的实验,测试模型顶点个数及面个数):

零件名称 size size1 size2 size3 type 顶点个数 面个数
IGES 24 23 0 0 Topo_COMPOUND 24 6
STEP 12 0 11 0 Topo_SOLID 8 6
STL 18 0 17 0 Topo_SHELL 8 12

  从表中可以看出,
    (1) 通过edgeClassfier.GetFreeEdges获取的边界个数(size1+size2+size3)总比通过GlobalInstance::topoExplor.ExplorEdges获取的边界个数(size)少1,通过绘制结果可以看出少的是GlobalInstance::topoExplor.ExplorEdges获取的最后一个边界。
    (2) IGES获取的零件类型为复合类型,边界个数与顶点个数均为24,相当于每个面单独统计一次边界和顶点,导致测得的长方体边界数为实际的两倍,顶点个数为实际的三倍;
    (3) STEP获取的零件类型为实体类型,边界个数与顶点个数均与实际长方体边界个数和顶点个数相同;
    (4) STL获取的零件类型为面片类型,且为三角面片。而长方体表面为四边形,因此存储为STL模型时自动对长方体表面进行划分,每个面添加了一条边界从而将四边形转化为三角形面片,因此得到的面个数为12,边界个数为18。通过图像也可以看出(如下图)。

AnyCAD(MFC版)三种格式(IGES,STEP,STL)三维零件的读取-冯金伟博客园