Create new Product in CATIA with Python

怎甘沉沦 提交于 2019-12-07 15:30:39

The only way I have found, so far, to circumvent this problem is to create a template product (in this case, just an empty product) and do a catDocs.NewFrom(<templateProductPath>) and add the product structure as necessary.

This post is old but since I found this page when having the same issue I figured I'll add my solution. I've found a handful of methods in CATIA that behave this way - works fine in CATIA VBA but not through the COM interface. The best solution I've found is to write a mini VBA function in a string and then call it in CATIA through Python. Here is an example:

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)

I was trying to replicate your issue, but I dind't encounter it. Products just created fine using incremental default names. Then I thought it was something related to Settings since the dialog is simlar to the one that optionally pops up when addin a new part. I discovered that I had the option Infrastructure > Product Infrastructure > Product structure > Part Number: Manual input unchecked.

I don't know how this was related to using VBA or not, but checking it created the issue and unchecking it removed the issue, while still sending the same command from Python.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!