机加工(CAM)仿真初探

来源:互联网 发布:朋友圈营销 知乎 编辑:程序博客网 时间:2024/04/19 11:11

最近接触了好几个想做点机加工方面开发的客户。虽然Autodesk有CAM解决方案,甚至云上的CAM 360,但有些用户还是需要自行定制加工过程,尤其走刀的仿真。

Inventor API的Transient Brep和Client Graphics可以帮助得以实现。其实很简单,刀具每走一步,让刀具的实体和基本体进行布尔运算,得到更新的体,然后用Client Graphics体现出来。我尝试做了个小例子,供大家参考。觉得还应有完善的空间。

这个例子,基于附件中的零件,一个体模拟刀具,一个模拟基本体,然后刀具将从处开始向内圆周运动,切削基本体直到圆心。


Inventor.Application oInvApp;Asset toolAppearance;Asset baseAppearance;void CamTest(){    //get active Inventor process        oInvApp =         System.Runtime.InteropServices.Marshal.        GetActiveObject("Inventor.Application")         as Inventor.Application;    //get active document    PartDocument oPartDoc =        oInvApp.ActiveDocument as PartDocument;                 PartComponentDefinition oPartDef =        oPartDoc.ComponentDefinition;    //Transient Brep object    TransientBRep oTBrep =        oInvApp.TransientBRep;    //Transient Geometry    TransientGeometry oTG=        oInvApp.TransientGeometry;    //base body    SurfaceBody oBaseBody =         oPartDef.SurfaceBodies[1];    //tool body    SurfaceBody oToolBody =        oPartDef.SurfaceBodies[2];    //make base body and tool body invisible    //we just show client graphics    oBaseBody.Visible = false;    oToolBody.Visible = false;    //make a copy of base body and tool body    //we will use them as client graphics    SurfaceBody oBaseBody_Copy =         oTBrep.Copy(oBaseBody);    SurfaceBody oToolBody_Copy =        oTBrep.Copy(oToolBody);    //Client Graphics    ClientGraphics oClientGraphics;    try    {        //delete if the Client Graphics exists        oClientGraphics =            oPartDef.ClientGraphicsCollection["CAMDemo"];        // An existing client graphics object        // was successfully obtained so clean up.        oClientGraphics.Delete();        // update the display to see the results.        oApp.ActiveView.Update();                   }    catch    {        //get the appearance for tool        toolAppearance =             oPartDoc.Assets["Gold - Metal"];        baseAppearance =            oPartDoc.Assets["Plate"];        // Create the ClientGraphics        oClientGraphics =             oPartDef.ClientGraphicsCollection.Add("CAMDemo");        //add graphics node for base body        GraphicsNode _oNode_BaseBody =             oClientGraphics.AddNode(1);        //add graphics of base body         SurfaceGraphics _Gra_BaseBody =             _oNode_BaseBody.AddSurfaceGraphics(oBaseBody_Copy);        _oNode_BaseBody.Appearance = baseAppearance;             //add graphics node for tool body        GraphicsNode _oNode_ToolBody =            oClientGraphics.AddNode(2);        //add graphics of tool body         SurfaceGraphics _Gra_ToolBody =            _oNode_ToolBody.AddSurfaceGraphics(oToolBody_Copy);        _oNode_ToolBody.Appearance = toolAppearance;                    //CAM Simulation starts:        //move tool inside the base along Z         Matrix oMovingM =            oTG.CreateMatrix();        oMovingM.SetTranslation(oTG.CreateVector(0, 0, -1));        //move tool inside base body: depth = 1        eachCAM(oMovingM,              ref oBaseBody_Copy,            ref oToolBody_Copy,             ref _oNode_BaseBody,            ref _oNode_ToolBody,            ref _Gra_BaseBody,            ref _Gra_ToolBody);        // diameter of the tool body        double toolDia = 0.5;        // how many circle loops the tool will move        int loopCount = 10;                        //from which diamater the tool starts to move        double max_CAM_Dia = 4.0;                   //last matrix of the tool body                        Matrix oLastMovingM = oTG.CreateMatrix();        oLastMovingM.SetToIdentity();        //tool moves from max diameter to 0        for (int circleLoop = loopCount;             circleLoop > 0 ;             circleLoop--)        {            //diameter of this loop            double thisDia = max_CAM_Dia * circleLoop / loopCount;            //the perimeter of this circle            double perimeterofLoop = Math.PI * thisDia;            //how many steps the tool needs to move for this loop            int circleStepCount =                 (int)(perimeterofLoop / toolDia) *2;                                //moving of each circle            for (int angleIndex = 1;                 angleIndex < circleStepCount;                angleIndex++)            {                               //get the location the tool will move to                      double x = thisDia / 2.0 *                     Math.Cos(angleIndex * Math.PI * 2 / circleStepCount);                double y = thisDia/ 2.0 *                     Math.Sin(angleIndex * Math.PI * 2 / circleStepCount );                                        //transform matrix                Matrix oEachMovingM = oTG.CreateMatrix();                oEachMovingM.SetTranslation(oTG.CreateVector(x, y, 0));                //the last location of the tool affects                //so invert and update with the new matrix                oLastMovingM.Invert();                oLastMovingM.TransformBy(oEachMovingM);                                        oMovingM = oLastMovingM;                //move                eachCAM(oMovingM,                                              ref oBaseBody_Copy,                    ref oToolBody_Copy,                    ref _oNode_BaseBody,                    ref _oNode_ToolBody,                    ref _Gra_BaseBody,                    ref _Gra_ToolBody);                                        //record the current matrix for next use                oLastMovingM = oEachMovingM;            }        }     }  }// each step of movingvoid eachCAM(Matrix _oMovingM,                              ref SurfaceBody _BaseBody,            ref SurfaceBody _ToolBody,            ref GraphicsNode _BaseBodyNode,            ref GraphicsNode _ToolBodyNode,                                ref SurfaceGraphics _BaseGrap,            ref SurfaceGraphics _ToolGrap                               ){    TransientBRep oTBrep = oInvApp.TransientBRep;    // we need to recreate the graphics.        _BaseGrap.Delete();        _ToolGrap.Delete();                 //transform the tool body        oTBrep.Transform(_ToolBody, _oMovingM);                 //remove material of the base body        oTBrep.DoBoolean(_BaseBody, _ToolBody,             BooleanTypeEnum.kBooleanTypeDifference);                 //add the update bodies again        _BaseGrap = _BaseBodyNode.AddSurfaceGraphics(_BaseBody);        _ToolGrap = _ToolBodyNode.AddSurfaceGraphics(_ToolBody);    //assign the appearance to the tool graphics                      _BaseBodyNode.Appearance = baseAppearance;        _ToolBodyNode.Appearance =toolAppearance;    //update the view        oInvApp.ActiveView.Update(); }


注意,此gif里出现的图像残留,是制作此gif时产生的。用Inventor测试时,没有此现象。

0 0