CATIA 二次开发 C#

引入类库

在正确安装CATIA之后即可在项目中引用CATIA COM类库,其类库名称均以CATIA V5开头。在引用为类库后,我们即可进行开发工作。 


在VS2010中引用CATIACOM组件

开发示例

该示例演示了如何连接CATIA,并生成一个新的Product。

using INFITF;
using MECMOD;
using PARTITF;
using ProductStructureTypeLib;
using SPATypeLib;
using NavigatorTypeLib;

namespace CATIATest
{
    class Program
    {
        static void Main(string[] args)
        {
            // 连接CATIA
            Application Catia =(Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Catia.Application");
            // 获取当前活动ProductDocument
            ProductDocument pd = (ProductDocument)Catia.ActiveDocument;
            // 创建一个ID为newProduct的Product
            pd.Product.Products.AddNewProduct("newProduct");
        }
    }
}

注意】在调试之前请确保CATIA已经运行,否则无法连接到CATIA,程序无法运行。

加载CATIA文件

public void AddNewComponent(string filePath)
        {
            object[] files = new object[1] { filePath };
            this.ActiveProductDocument.Product.Products.AddComponentsFromFiles(files, "All");
        }

向当前Product中添加Part

 // 向当前Product中添加一个指定长度的圆柱体
        public void CreateCylinder(string name, double length)
        {
            // 添加一个新零件
            Product product = this.ActiveProductDocument.Product.Products.AddNewComponent("Part", name);
            // 绘制圆
            Part part = ((PartDocument)this._catia.Documents.Item(name + ".CATPart")).Part;
            Sketch sketch = (part.Bodies.GetItem("零件几何体") as Body).Sketches.Add(
                (Reference)part.OriginElements.PlaneXY);
            sketch.SetAbsoluteAxisData(new object[] { 0, 0, 0, 1, 0, 0, 0, 1, 0 });
            part.InWorkObject = sketch;
            Factory2D factory = sketch.OpenEdition();
            Axis2D axis = (Axis2D)sketch.GeometricElements.GetItem("绝对轴");
            (axis.GetItem("横向") as Line2D).ReportName = 1;
            (axis.GetItem("纵向") as Line2D).ReportName = 2;
            Circle2D circle = factory.CreateClosedCircle(0, 0, 5);
            circle.CenterPoint = (Point2D)axis.GetItem("原点");
            circle.ReportName = 3;
            sketch.CloseEdition();
            part.Update();
            // 绘制圆柱
            (part.ShapeFactory as ShapeFactory).AddNewPad(sketch, length);
            part.Update();
        }

查找Product

// 根据产品名称获取指定Product
        public Product GetProduct(string productId)
        {
            return (Product)this.ActiveProductDocument.Product.Products.GetItem(productId);
        }

移除Product

 protected void RemoveProduct(Product product)
        {
            Selection selection = this.ActiveProductDocument.Selection;
            selection.Clear();
            selection.Add(product);
            selection.Delete();
        }

碰撞检测

 // 检测两个产品间是否存在冲突
        public bool ConflictCheck(Product product1, Product product2)
        {
            Groups groups = this.ActiveProductDocument.Product.GetTechnologicalObject("Groups") as Groups;
            Group first = groups.Add();
            Group second = groups.Add();
            first.AddExplicit(product1);
            second.AddExplicit(product2);

            Clash clash = (this.ActiveProductDocument.Product.GetTechnologicalObject("Clashes") as Clashes).Add();
            clash.ComputationType = CatClashComputationType.catClashComputationTypeBetweenTwo;
            clash.InterferenceType = CatClashInterferenceType.catClashInterferenceTypeContact;
            clash.FirstGroup = first;
            clash.SecondGroup = second;
            clash.Compute();
            Conflicts conflicts = clash.Conflicts;

            groups.Remove(first.get_Name());
            groups.Remove(second.get_Name());

            return conflicts.Count != 0;
        }

自动调整视图

 protected void AdapteAllView()
        {
            this._catia.ActiveWindow.ActiveViewer.Reframe();
        }

其他功能探索

一般来说,对于CATIA中能够手工解决的问题均应能够通过编程的方式处理,我们可以通过以下三种途径获取编程帮助以及Sample。

  1. V5 Automation API 手册(推荐)
  2. CATIA 宏录制功能
  3. Baidu 以及 Google

方式一:在安装好CATIA后,再其安装目录中%Install Root%\Dassault Systemes\B20\intel_a\code\bin中已经包含了V5 Automation API .chm帮助文档。该文档中包含了全部的可用API及类继承结构,同时提供了丰富的代码示例供读者参考。 


CATIA帮助文档

方式二:通过启用CATIA的宏录制功能,可以获得所有操作的VBScript代码,我们可以参考该代码进行CSharp代码的开发。下图通过CATIA录制了一个绘制圆柱的过程,右侧窗口内显示的即为生成的VBScript代码。 


CATIA宏录制功能





  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值