pythoncatia_python的CATIA宏

I would like to use my macro (.CATScript) to open the catia interface and make the changes listed in the macro script to the .CATpart and give output as .stp file. Is it possible to use python to realise this function?

There was an example in Run a Catia macro with a python script, but it didn't work in my case. I edited the code as below and gave it a run.

import win32com.client

catapp = win32com.client.Dispatch("CATIA.Application")

catapp.StartCommand('Macro_schweller_model_lsopt.CATScript')

The error I had was

File "C:\FK-Programme\python36-32\Anaconda\Install\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch

IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)

com_error: (-2147221005, 'Ungültige Klassenzeichenfolge', None, None)

My .CATscript looks like this

Sub CATMain()

Dim FileToOpen as String

Dim partDocument1 As Document

Dim part1 As Part

Dim AnglePara As Parameter

Dim parameters1 As Parameters

Dim AmplitudePara As Parameter

Dim WavelengthPara As Parameter

FileToOpen = "E:\Datei\Results\Optimization\LS_OPT_results\Optimization_model_1\Schweller_fully_corrugated.CATPart"

Set partDocument1 = CATIA.Documents.Open(FileToOpen)

Set part1 = partDocument1.Part

Set parameters1 = part1.Parameters

Set AnglePara = parameters1.RootParameterSet.DirectParameters.Item("Angle")

AnglePara.Value = -7

Set AmplitudePara = parameters1.RootParameterSet.DirectParameters.Item("Amplitude")

AmplitudePara.Value = 30

Set WavelengthPara = parameters1.RootParameterSet.DirectParameters.Item("Wavelength")

WavelengthPara.Value = 30

CATIA.DisplayFileAlerts = False

partDocument1.Part.Update

partDocument1.ExportData "E:\Datei\Results\Optimization\LS_OPT_results\Optimization_model_1\Schweller.stp", "stp"

End Sub

解决方案

The StartCommand method can (as far as I know) only start macros assigned to toolbar buttons. I advise you to instead use the SystemService.ExecuteScript method, which allows you to run the script directly. Your example would then be modified to look something like this:

import win32com.client

catapp = win32com.client.Dispatch("CATIA.Application")

catapp.SystemService.ExecuteScript(

# Macro library name/path

r"C:\Path\To\Directory\Containing\The\Script",

# Type of macro library (document/directory/VBA project)

1, # directory

# Macro name

"Macro_schweller_model_lsopt.CATScript",

# Function name

"CATMain",

# Arguments

tuple(),

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值