【问题标题】:Catia select a feature from a specific instance in an assemblyCatia 从装配体中的特定实例中选择特征
【发布时间】:2021-09-14 14:04:29
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: python vba com catia


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我会建议一种不同的方法。而不是添加从某个地方获得的引用(可能是按名称),而是在迭代所有产品时将部件的实际实例添加到选择中。或者使用实例名称来获取正确的部分。

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

      如果您想复制特征,您必须深入研究 Instance 对象。

             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
      

      【讨论】:

      • 我喜欢类型检查。但这不是我想做的。我想复制零件的特征(如提取物)。这部分在产品中多次存在。当我将特征添加到选择中时,Catia 选择了将选择该特征的部件的任何(但始终相同)实例。
      • 我看到您正在为复制功能的定位而不是选择它而苦苦挣扎。而且你把用户作为一个选择功能,你知道了:)我现在记得我在 Catia 制作扫地机游戏时也使用了 LeafProduct 用于我的宏观课程,以了解我选择的身体属于哪个产品.
      • 该特征基于零件而不是装配体中的原点携带位置。因此,如果您正在构建 Self made allcatpart,您必须弄清楚这一点。
      • Catia Minesweeper :D 有趣!
      猜你喜欢
      • 2017-09-14
      • 2020-03-15
      • 2016-03-16
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多