Catia select 装配体中特定实例的特征

Catia select a feature from a specific instance in an assembly

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

主要产品:
-Product1(第 1 部分的实例)
-Product2(第 2 部分的实例)
-Product3(第 2 部分的实例)
-Product4(第 3 部分的实例)
...

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

我无法控制将选择哪个功能 CATIA.ActiveDocument.Selection.Add(myExtractReference)
Catia 始终选择 Product2 中的特征而不是 Product3 中的特征。所以粘贴的地物位置会不对!

有人知道这个问题并有解决办法吗?

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

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

这是一个简单的 VBA 迭代一个 lvl 树和 select 复制粘贴场景的示例。

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

       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

我可以从其他地方获得帮助。仍然想分享我的解决方案。它是用 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()