Create new Product in CATIA with Python

2019-06-04 15:27发布

I am automating the creation of a new product with a Python script and have run into a problem with the interactive events getting stuck at the "Part Number" dialog. This does not occur when creating a new part, just a new product. Here is the applicable portion of the script (CATIA is open):

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")
...

The problem is that I am developing a small tool for others to use and it is not very useful if it hangs up.

Clicking on "Cancel" gets rid of the dialog box, but no interactive actions occur afterwards. Clicking on "Ok" resolves the problem, but it would be preferable for the script to be able to prepare the product as a final result without interaction in order to restrict user error and improve ease of use.

I know that I can create a product and manipulate it (i.e. add parts, add new products, etc.), then successfully save it. So the processes are being executed, they just aren't being displayed anymore. I just can't seem to find a way to get past the "Part Number" dialog box. I even tried naming it programmatically, which worked but didn't kill the dialog box.

Opening an existing product works very well, and any scripting processes can continue without problems. However, programmatically creating the product, saving, and closing causes CATIA to lock up... so the option of saving and re-opening as an existing product is out the window.

I also referenced the v5Automation.chm, but I couldn't find a way of interacting with dialog boxes.

I also tried .Update() on the new product and it's parts. Some other assurances were CATIA.Visible = True and CATIA.RefreshDisplay = True.

Disclaimer: I know that VBA can be used and does not pose this problem. I am looking for a solution to this problem using Python (2 or 3, doesn't matter).

3条回答
太酷不给撩
2楼-- · 2019-06-04 16:14

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.

查看更多
神经病院院长
3楼-- · 2019-06-04 16:14

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)
查看更多
啃猪蹄的小仙女
4楼-- · 2019-06-04 16:23

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.

查看更多
登录 后发表回答