Catia 从装配中的特定实例中选择特征

问题描述

假设我有一个这样的程序集:

主营产品:
-Product1(Part1的实例)
-Product2(Part2的实例)
-Product3(Part2的实例)
-Product4(Part3的实例)
...

现在,我想将 Product3 中的一项功能复制/粘贴到另一个功能中。
但是我在以编程方式选择功能时遇到了问题,因为该功能的一部分有 2 个实例

我无法控制 CATIA.ActiveDocument.Selection.Add(myExtractReference)
将选择哪个功能 Catia 总是从 Product2 中选择特征,而不是从 Product3 中选择特征。所以粘贴的特征位置会出错!

有人知道这个问题并且有解决方案吗?

编辑: 我要复制的特征参考已经作为变量存在,因为它是新创建的(所选几何的提取物)

解决方法

我可以在其他地方获得帮助。仍然想分享我的解决方案。它是用 Python 编写的,但在 VBA 中几乎相同。 线索是访问 CATIA.Selection.Item(1).LeafProduct 以了解初始选择的位置。

import win32com.client
import pycatia

CATIA = win32com.client.dynamic.DumbDispatch('CATIA.Application')
c_doc = CATIA.ActiveDocument
c_sel = c_doc.Selection
c_prod   = c_doc.Product

# New part where the feature should be pasted
new_prod = c_prod.Products.AddNewComponent("Part","")
new_part_doc = new_prod.ReferenceProduct.Parent

# from user selection
sel_obj = c_sel.Item(1).Value

sel_prod_by_user = c_sel.Item(1).LeafProduct  #  reference to the actual product where the selection was made
doc_from_sel = sel_prod_by_user.ReferenceProduct.Parent  # part doc from selection
hb = doc_from_sel.Part.HybridBodies.Add()  # new hybrid body for the extract. will be deleted later on
extract = doc_from_sel.Part.HybridShapeFactory.AddNewExtract(sel_obj)
hb.AppendHybridShape(extract)
doc_from_sel.Part.Update()

# Add the extract to the selection and copy it
c_sel.Clear()
c_sel.Add(extract)
sel_prod_by_catia = c_sel.Item(1).LeafProduct  # reference to the product where Catia makes the selection
c_sel_copy()  # will call Selection.Copy from VBA. Buggy in Python.

# Paste the extract into the new part in a new hybrid body
c_sel.Clear()
new_hb = new_part_doc.Part.HybridBodies.Item(1)
c_sel.Add(new_hb)
c_sel.PasteSpecial("CATPrtResultWithOutLink")
new_part_doc.Part.Update()
new_extract = new_hb.HybridShapes.Item(new_hb.HybridShapes.Count)

# Redo changes in the part,where the selection was made
c_sel.Clear()
c_sel.Add(hb)
c_sel.Delete()

# Create axis systems from Position object of sel_prd_by_user and sel_prd_by_catia
prod_list = [sel_prod_by_user,sel_prod_by_catia]
axs_list = []
for prod in prod_list:
    pc_pos = pycatia.in_interfaces.position.Position(prod.Position) # conversion to pycata's Position object,necessary
                                                                    # in order to use Position.GetComponents
    ax_comp = pc_pos.get_components()
    axs = new_part_doc.Part.AxisSystems.Add()
    axs.PutOrigin(ax_comp[9:12])
    axs.PutXAxis(ax_comp[0:3])
    axs.PutYAxis(ax_comp[3:6])
    axs.PutZAxis(ax_comp[6:9])
    axs_list.append(axs)
    new_part_doc.Part.Update()

# Translate the extract from axis system derived from sel_prd_by_catia to sel_prd_by_user
extract_ref = new_part_doc.Part.CreateReferenceFromObject(new_extract)
tgt_ax_ref = new_part_doc.Part.CreateReferenceFromObject(axs_list[0])
ref_ax_ref = new_part_doc.Part.CreateReferenceFromObject(axs_list[1])
new_extract_translated = new_part_doc.Part.HybridShapeFactory.AddNewAxisToAxis(extract_ref,ref_ax_ref,tgt_ax_ref)
new_hb.AppendHybridShape(new_extract_translated)
new_part_doc.Part.Update()
,

我建议采用不同的方法。不是添加从某处(可能按名称)获得的引用,而是将零件的实际实例添加到选择中,同时遍历所有产品。或者使用实例名称来获取正确的部分。

这是一个简单的 VBA 示例,它迭代一个 lvl 树并选择复制粘贴场景。

如果你想复制特征,你必须深入研究实例对象。

       Public Sub CatMain()
    
            Dim ActiveDoc As ProductDocument
            Dim ActiveSel As Selection
            
            If TypeOf CATIA.ActiveDocument Is ProductDocument Then 'of all the checks that people are using I think this one is most elegant and reliable
                Set ActiveDoc = CATIA.ActiveDocument
                Set ActiveSel = ActiveDoc.Selection
            Else
                Exit Sub
            End If
    
            Dim Instance As Product
    
            For Each Instance In ActiveDoc.Product.Products 'object oriented for ideal for us in this scenario
                If Instance.Products.Count = 0 Then 'beware that products without parts have also 0 items and are therefore mistaken for parts
                  Call ActiveSel.Add(Instance)
                End If
            Next
    
            Call ActiveSel.Copy
            Call ActiveSel.Clear
    
            Dim NewDoc As ProductDocument
            Set NewDoc = CATIA.Documents.Add("CATProduct")
            Set ActiveSel = NewDoc.Selection
            Call ActiveSel.Add(NewDoc.Product)
            Call ActiveSel.Paste
            Call ActiveSel.Clear
    
        End Sub