【问题标题】:Create new Product in CATIA with Python使用 Python 在 CATIA 中创建新产品
【发布时间】:2015-11-07 01:01:29
【问题描述】:

我正在使用 Python 脚本自动创建新产品,但遇到了交互事件卡在“部件号”对话框中的问题。创建新零件时不会发生这种情况,而只是创建新产品。这是脚本的适用部分(CATIA 已打开):

import win32com.client.dynamic
CATIA = win32com.client.Dispatch("CATIA.Application")
catDocs = CATIA.Documents

# Create a new product
newProductDoc = catDocs.Add("Product")

# "Part Number" window appears, requesting a name for the product
# Interactive processes will not proceed
newProduct = newProductDoc.Product
newProductSet = newProduct.Products
newPart = newProductSet.AddNewComponent("Part", "dummyPart")
...

问题是我正在开发一个小工具供别人使用,如果挂掉就不是很有用了。

单击“取消”会退出对话框,但之后不会发生任何交互操作。单击“确定”可以解决问题,但最好让脚本能够在没有交互的情况下准备产品作为最终结果,以限制用户错误并提高易用性。

我知道我可以创建一个产品并对其进行操作(即添加零件、添加新产品等),然后成功保存它。所以进程正在执行,它们只是不再显示。我似乎无法找到通过“零件编号”对话框的方法。我什至尝试过以编程方式命名它,虽然有效,但并没有杀死对话框。

打开现有产品效果很好,任何脚本编写过程都可以继续进行而不会出现问题。但是,以编程方式创建产品、保存和关闭会导致 CATIA 锁定……因此,作为现有产品保存和重新打开的选项不在窗口中。

我还引用了 v5Automation.chm,但我找不到与对话框交互的方法。

我还在新产品及其零件上尝试了.Update()。其他一些保证是CATIA.Visible = TrueCATIA.RefreshDisplay = True

免责声明:我知道可以使用 VBA 并且不会造成此问题。我正在寻找使用 Python 解决这个问题的方法(2 或 3,没关系)。

【问题讨论】:

    标签: python python-3.x automation catia


    【解决方案1】:

    到目前为止,我发现解决此问题的唯一方法是创建一个模板产品(在这种情况下,只是一个空产品)并执行 catDocs.NewFrom(<templateProductPath>) 并根据需要添加产品结构。

    【讨论】:

      【解决方案2】:

      这篇文章很旧,但因为我在遇到同样问题时发现了这个页面,所以我想我会添加我的解决方案。我在 CATIA 中发现了一些以这种方式运行的方法——在 CATIA VBA 中可以正常工作,但不能通过 COM 接口。我发现的最佳解决方案是在字符串中编写一个迷你 VBA 函数,然后通过 Python 在 CATIA 中调用它。这是一个例子:

      import random
      import win32com.client
      
      CATIA = win32com.client.GetActiveObject('CATIA.Application')
      CATVBALanguage = 1
      
      # This should work, but CATIA leaves up the dialog window and it can affect
      # the rest of the code execution
      # NewProductDocument = CATIA.Documents.Add('Product')
      
      # Instead, write the code in VBA and then have CATIA execute it.  You can
      # pass in arguments and capture the results as demonstrated below.
      CREATE_PRODUCT_VBA_CODE = '''
          Public Function create_product(part_number as  CATBSTR) as Document
              Set create_product = CATIA.Documents.Add("Product")
              create_product.Product.PartNumber = part_number
          End Function
      '''
      PART_NUMBER = 'test_product_{}'.format(random.randint(1, 100))
      NewProductDocument = CATIA.SystemService.Evaluate(
          CREATE_PRODUCT_VBA_CODE,   # String with the VBA code to execute
          CATVBALanguage,            # 1 to indicate this string is VBA code
          'create_product',          # VBA function to call and return result from
          [PART_NUMBER]              # Array of arguments, in order for VBA function
      )
      
      # Can still interact with this returned object as if we had created it
      print(NewProductDocument.Product.PartNumber)
      

      【讨论】:

      • @Scott BI 正在寻找同样的问题,我在使用 dispatch 之前使用了您的解决方案并且在那里遇到错误,现在我在 GetActiveObject com_error 上遇到错误:(-2147221005,'无效的类字符串',无,无),你知道吗?
      【解决方案3】:

      我试图复制您的问题,但没有遇到。使用增量默认名称创建的产品很好。 然后我认为它与设置有关,因为该对话框与添加新部件时可选弹出的对话框相似。 我发现我有选项 Infrastructure > Product Infrastructure > Product structure > Part Number: Manual input unchecked

      我不知道这与是否使用 VBA 有什么关系,但选中它会产生问题,取消选中它会消除问题,同时仍然从 Python 发送相同的命令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-04
        • 2020-09-15
        • 2023-01-20
        • 2021-10-02
        • 2015-09-27
        • 1970-01-01
        相关资源
        最近更新 更多