本文主要是介绍获取CATIA几何图形集下的所有层级,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
编辑于江苏南京2023年3月21号。
最近想到一个需求,获取几何图形集下所有子集的树结构,通过树结构可以做很多应用,例如刷编码、排序、和数据库绑定做数据结构等。用下面的图形集做了测试。
1.定义数据类存放数据
public class TreeNode{/// <summary>/// 存储几何图形集的名称/// </summary>public string Name { get; set; }/// <summary>/// 存储几何图形集的子图形集/// </summary>public IList<TreeNode> Children { get; set; }}
2.获取层级数据
2.1 获取目标文件夹
var refCurve = CatiaHelper.SelectionObject<HybridBody>(catApp);///存放层级数据List<TreeNode> list1 = new List<TreeNode>();//遍历,通过递归的方法寻找到所有的层级。通过数据类中Children存放子集if (refCurve.HybridBodies.Count > 0){foreach (HybridBody index in refCurve.HybridBodies){TreeNode tree1 = new TreeNode();list1.Add(FindAllChildren(index, tree1));}}treeView.ItemsSource = list1;
2.2 递归
通过递归的方法获取所有的层级,递归代码如下
/// <summary>/// 获取所有的子集/// </summary>/// <param name="hybridBody">目标文件集</param>/// <param name="tree">树形数据</param>/// <returns>存放所有子集的树形数据</returns>private static TreeNode FindAllChildren(HybridBody hybridBody, TreeNode tree){if (hybridBody != null){tree.Name = hybridBody.get_Name();if (hybridBody.HybridBodies.Count > 0){tree.Children = new List<TreeNode>();for (int i = 1; i <= hybridBody.HybridBodies.Count; i++){TreeNode t1 = new TreeNode{Name = hybridBody.HybridBodies.Item(i).get_Name()};t1 = FindAllChildren(hybridBody.HybridBodies.Item(i), t1);tree.Children.Add(t1);}}return tree;}else{return tree;}}
3.前端显示数据
用WPF写一个窗体,用TreeView控件可以将数据绑定到控件上。
WPF代码如下
<Window x:Class="TreeView.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:TreeView"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><Button Content="获取层级" Margin="10" Height="40" Width="80" Click="Button_Click"/><TreeView Name="treeView"><!--默认展开所有节点--><TreeView.ItemContainerStyle><Style TargetType="TreeViewItem"><Setter Property="IsExpanded" Value="True"/></Style></TreeView.ItemContainerStyle><!--绑定数据源--><TreeView.ItemTemplate><HierarchicalDataTemplate ItemsSource="{Binding Children}"><StackPanel Orientation="Horizontal"><Image Source="pic/齐柏林飞船.png" Height="20" Width="20"></Image><TextBlock Text="{Binding Name}"/></StackPanel></HierarchicalDataTemplate></TreeView.ItemTemplate></TreeView></StackPanel></Grid>
</Window>
4.后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using INFITF;
using MECMOD;
using InstanceBallBySelect;namespace TreeView
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow :System.Windows.Window{INFITF.Application catApp;PartDocument oCATIADoc;Part rootPart;public MainWindow(){InitializeComponent();catApp = (INFITF.Application)Marshal.GetActiveObject("Catia.Application");oCATIADoc = (PartDocument)catApp.ActiveDocument;rootPart = oCATIADoc.Part;//TreeDemo();}private void Button_Click(object sender, RoutedEventArgs e){var refCurve = CatiaHelper.SelectionObject<HybridBody>(catApp);///存放层级数据List<TreeNode> list1 = new List<TreeNode>();//遍历,通过递归的方法寻找到所有的层级。通过数据类中Children存放子集if (refCurve.HybridBodies.Count > 0){foreach (HybridBody index in refCurve.HybridBodies){TreeNode tree1 = new TreeNode();list1.Add(FindAllChildren(index, tree1));}}treeView.ItemsSource = list1;}/// <summary>/// 获取所有的子集/// </summary>/// <param name="hybridBody">目标文件集</param>/// <param name="tree">树形数据</param>/// <returns>存放所有子集的树形数据</returns>private static TreeNode FindAllChildren(HybridBody hybridBody, TreeNode tree){if (hybridBody != null){tree.Name = hybridBody.get_Name();if (hybridBody.HybridBodies.Count > 0){tree.Children = new List<TreeNode>();for (int i = 1; i <= hybridBody.HybridBodies.Count; i++){TreeNode t1 = new TreeNode{Name = hybridBody.HybridBodies.Item(i).get_Name()};t1 = FindAllChildren(hybridBody.HybridBodies.Item(i), t1);tree.Children.Add(t1);}}return tree;}else{return tree;}}}
}
整体的效果如图
5.结语
装配体的层级关系可以按照类似的方法获取。
另,WPF的控件自由度比较高,可以自定义样式实现不同的功能。如将样例中的飞船换成构件的缩略图,在每个子项中填加按键反向操控Catia等。
这篇关于获取CATIA几何图形集下的所有层级的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!