【问题标题】:Catia enumeration values when using Python (Original title:accessing CATIA object in python)使用Python时的Catia枚举值(原标题:在python中访问CATIA对象)
【发布时间】:2019-04-25 00:54:51
【问题描述】:

我正在尝试在 CATIA 中进行一些设计自动化。我正在使用 python,然后我在 CATIA 中记录宏并将那里的代码转换为 python 代码。现在我偶然发现了一个问题。

下面是我想翻译成 pyhton 代码的 CATIA 宏。

Language="VBSCRIPT"

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

Set product1 = productDocument1.Product

Set product1 = product1.ReferenceProduct

Set constraints1 = product1.Connections("CATIAConstraints")

Set reference1 = product1.CreateReferenceFromName("ContainerSchiff/Container1/!yz plane")

Set reference2 = product1.CreateReferenceFromName("ContainerSchiff/Container0/!Geometrical Set.1/Point.2")

----Here is the problem ----   Set constraint1 = constraints1.AddBiEltCst(catCstTypeDistance, reference1, reference2)

Set length1 = constraint1.Dimension

length1.Value = 300.000000

product1.Update 

End Sub

翻译时我不知道如何处理catCstTypeDistance

如果我保持原样,那么 python 显然会抱怨名称未定义。如果我将它作为字符串传递也会抱怨。下面是python的部分

else:
    add_container_skeleton(product1,i)
    product1.ReferenceProduct
    constraints1=product1.Connections("CATIAConstraints")
    Name1="ContainerSchiff/Container" + str(i-1) + "/!Container1/yz plane" 
    Name2="ContainerSchiff/Container" + str(i) + "/!Geometrical Set.1/Point.2"

    reference1= product1.CreateReferenceFromName(Name1)
    reference2 = product1.CreateReferenceFromName(Name2)

    constraint1 = constraints1.AddBiEltCst('catCstTypeDistance', reference1, reference2)

    length1 = constraint1.Dimension
    length1.Value = 300.000000

以及我收到的两条不同的错误消息。

File "C:\Users\Mange\Documents\LIU\Catia part 2\first_draft.py", line 179, in place_containers
    constraint1 = constraints1.AddBiEltCst('catCstTypeDistance', reference1, reference2)
  File "<COMObject Connections>", line 3, in AddBiEltCst
ValueError: invalid literal for int() with base 10: 'catCstTypeDistance'
File "C:\Users\Mange\Documents\LIU\Catia part 2\first_draft.py", line 179, in place_containers
    constraint1 = constraints1.AddBiEltCst(catCstTypeDistance(), reference1, reference2)
NameError: name 'catCstTypeDistance' is not defined

我怎样才能从 python 中“访问”这个东西/对象(?)?

【问题讨论】:

  • 这是一个很好的问题。标题可能更适合搜索,因为“评估 CATIA 对象”在 CATIA 自动化中具有特定含义——它指的是获取 CATIA 应用程序对象。更好的标题可能是“使用 Python 在 CATIA 中创建注释”或“使用 Python 时的 Catia 枚举值”

标签: python catia


【解决方案1】:

解决了。

当不在 CATIA 本身中时,catCstTypeDistance 必须替换为与约束类型对应的整数。哪个数字对应哪个约束类型可以看这里http://catiadoc.free.fr/online/interfaces/enum_CatConstraintType.htm

【讨论】:

    【解决方案2】:

    IMO 最好的方法,而不是使用整数(并花时间翻译它们)是使用 ma​​kepy 来提前绑定 COM 对象。 之后,您将在从类型库生成的相关 .py 文件中列出常量。 之后,通过导入:

    from win32com.client import constants as CATEnum
    

    并且只有在获得了 Application 对象之后:

    CATIA = Dispatch('CATIA.Application')
    

    您将能够访问它们:

    CATEnum.catCstTypeDistance
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多