几何尺寸与公差论坛

 找回密码
 注册
查看: 2420|回复: 0

【转帖】如何在导入文件时获得零件尺寸

[复制链接]
发表于 2007-6-6 10:43:57 | 显示全部楼层 |阅读模式
如何在导入文件时获得零件尺寸


上次不知道,去OCC的论坛上发帖问了,有人给了比较详细的回答,现在整理出来,希望对大家有用
The codes in red is the implemented methods.


There are 3 ways to retrieve the dimensions of the imported part.

Method 1, Interate the shape with the TopExp_Explorer to get what your want, and then down cast to get the underlying curve (BRep_Tool::Curve). Then, based on the underlying geometry of the curve, you can get the appropriate dimensions.
For example: To retrieve the  dimension of the shape constructed by myself to test the method, and refering to the imported shape, the step is the same.
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <Geom_Circle.hxx>
#include <gp_Circ.hxx>
void COCCVIewer3DDoc::OnCylinder()
{
    // TODO: Add your command handler code here
    myAISContext->CloseAllContexts();
    BRepPrimAPI_MakeCylinder C(20.,20,1.5*PI);
    Handle(AIS_Shape) aCylinder=new AIS_Shape(C.Shape());
    myAISContext->Display(aCylinder);
    FitAll(-1);
    CString s;
    TopExp_Explorer ex(C.Shape(),TopAbs_EDGE);
    for(ex.Current();ex.More();ex.Next())
    {
        Standard_Real first,last;
        Handle_Geom_Curve Cur=BRep_Tool::Curve(TopoDS::Edge(ex.Current()),first,last);
        Handle(Standard_Type) curveType=Cur->DynamicType();
        if(curveType==STANDARD_TYPE(Geom_Circle))
        {
            Handle(Geom_Circle) acircle=Handle(Geom_Circle):ownCast(Cur);
            gp_Circ circData=acircle->Circ();
            Standard_Real radius=circData.Radius();
            CString s1;
            s1.Format("%f ",radius);
            s+=s1;
        }
        CString s2f;
        s2f.Format("%f,%f",first,last);s+=s2f;
        s+="\n";
    }
    AfxMessageBox(s);
}
You would have to do similar things for the different types of curves you might encounter.

Method 2 , you can get the extents of an object in the global X,Y,and Z directions by using Bnd_Box and BRepBndLib, this method is only useful to retrieve the box's dimension.
eg:
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <TopExp_Explorer.hxx>
#include <ChFi3d_FilletShape.hxx>
#include <BRepBndLib.hxx>
void COCCVIewer3DDoc::OnBox()
{
    // TODO: Add your command handler code here
    myAISContext->CloseAllContexts();
    TopoDS_Solid box=BRepPrimAPI_MakeBox (gp_Pnt(0.0,.0,.0),200.,150.,100.);
    TopExp_Explorer ex(box,TopAbs_EDGE);
    BRepFilletAPI_MakeFillet MF(box);
    while (ex.More())
    {
        MF.Add(10,TopoDS::Edge(ex.Current()));
        ex.Next();
    }   
    Handle(AIS_Shape) aBox=new AIS_Shape(MF.Shape());
    myAISContext->SetMaterial (aBox, Graphic3d_NOM_GOLD, Standard_False);
    myAISContext->SetDisplayMode (aBox, 1, Standard_False);
    myAISContext->Display(aBox);

    TopoDS_Solid box2=BRepPrimAPI_MakeBox (gp_Pnt(200.0,200.0,100.0),200.,200.,200.);
    BRepFilletAPI_MakeFillet Rake(box2);
    ex.Init(box2,TopAbs_EDGE);
    ChFi3d_FilletShape Fsh=ChFi3d_Rational;
    Rake.SetFilletShape(Fsh);
    Rake.Add(8,50,TopoDS::Edge(ex.Current()));
    Handle(AIS_Shape) aBox2=new AIS_Shape(Rake.Shape());
    myAISContext->SetMaterial (aBox2, Graphic3d_NOM_BRONZE, Standard_False);
    myAISContext->SetDisplayMode (aBox2, 1, Standard_False);
    myAISContext->Display(aBox2);
    FitAll(-1);
    Bnd_Box bounds;
    BRepBndLib::Add(MF.Shape(),bounds);
    bounds.SetGap(0.0);
    Standard_Real xmin,ymin,zmin,xmax,ymax,zmax;
    bounds.Get(xmin,ymin,zmin,xmax,ymax,zmax);
    CString s;
    s.Format("%f,%f,%f",xmax-xmin,ymax-ymin,zmax-zmin);
    AfxMessageBox(s);
}

Method 3, you can always look at vertices and calculate distances from those.
NOTE: This method I am still trying to make it work. There are a lot of problems to solve.  Hope someone can join me to let this happen.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 01:45 , Processed in 0.035751 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表