使用 VBA 宏在 CATIA V5R24 中实例化 PowerCopy

Instantiate PowerCopy in CATIA V5R24 using VBA macros

我想为强力复制创建宏。我从这个 link 中获取了代码和模型 不幸的是它不起作用(我做了一些小修改)。我在这个 中发现了类似的问题,在他的情况下它有效。

 Private Sub CommandButton1_Click()
' Instantiation of a PowerCopy Reference "SurfacicHoles"
' SurfacicHoles is stored in the CATPart "e:\tmp\PowerCopyReference.CATPart"
' It has
' 3 inputs: FirstHole, Support,and SecondHole
' 2 published parameters: Radius1 and Radius2
'------------------------------------------------------------------

'------------------------------------------------------------------
Dim CATIA As Object
Set CATIA = GetObject(, "CATIA.Application")
Dim SysS As Object
Set SysS = CATIA.SystemService

Dim SpassString As String

'CATIA.SystemService.Print ("Retrieve the current part")
SpassString = SysS.Print("Retrive the current part")

Dim PartDocumentDest As PartDocument
Set PartDocumentDest = CATIA.ActiveDocument

Dim PartDest As Part
Set PartDest = PartDocumentDest.Part

'------------------------------------------------------------------
'CATIA.SystemService.Print "Retrieve the factory of the current part"
SpassString = SysS.Print("Retrieve the factory of the current part")


Dim factory As InstanceFactory
Set factory = PartDest.GetCustomerFactory("InstanceFactory")
'Debug.Print factory.Name

'------------------------------------------------------------------
'CATIA.SystemService.Print "BeginInstanceFactory"
SpassString = SysS.Print("BeginInstanceFactory")


factory.BeginInstanceFactory "SurfacicHoles", "C:\PowerCopyReference.CATPart"
'------------------------------------------------------------------
'CATIA.SystemService.Print "Begin Instantiation"
SpassString = SysS.Print("Begin Instantiation")

factory.BeginInstantiate
'------------------------------------------------------------------
'CATIA.SystemService.Print "Set Inputs"
SpassString = SysS.Print("Set Inputs")

Dim FirstHole As Object
Set FirstHole = PartDest.FindObjectByName("Point.1")

Dim Support As Object
Set Support = PartDest.FindObjectByName("Surface.1")

Dim SecondHole As Object
Set SecondHole = PartDest.FindObjectByName("Point.2")

factory.PutInputData "FirstHole", FirstHole
factory.PutInputData "Support", Support
factory.PutInputData "SecondHole", SecondHole
'------------------------------------------------------------------
'CATIA.SystemService.Print "Modify Parameters"
SpassString = SysS.Print("Modify Parameters")

Dim param1 As Parameter
Set param1 = factory.GetParameter("Radius1")
param1.ValuateFromString ("25mm")

Dim param2 As Parameter
Set param2 = factory.GetParameter("Radius2")
param2.ValuateFromString ("15mm")
'------------------------------------------------------------------
'CATIA.SystemService.Print "Instantiate"
SpassString = SysS.Print("Instantiate")

Dim Instance As ShapeInstance
Set Instance = factory.Instantiate
'------------------------------------------------------------------
'CATIA.SystemService.Print "End of Instantiation"
SpassString = SysS.Print("End of Instantiation")


factory.EndInstantiate
'------------------------------------------------------------------
'CATIA.SystemService.Print "Release the reference document"
SpassString = SysS.Print("Release the reference document")

factory.EndInstanceFactory
'------------------------------------------------------------------
'CATIA.SystemService.Print "Update"
SpassString = SysS.Print("Update")

PartDest.Update

End Sub

这一步出现错误

factory.BeginInstanceFactory "SurfacicHoles", "C:\PowerCopyReference.CATPart"

Run-time error '-2147467259(80004005)': Automation error. Unspecified error

Windows 7 64 位

今天我从我的管理层那里得到了新信息...我们的一些工厂没有获得 KT1 许可证...在那种情况下还有其他方法可以使用自动电源复制吗?

实例工厂一旦用 BeginInstanceFactory 打开,它必须被相应的 EndInstanceFactory 关闭。

现在发生了什么,尤其是在开发中,您执行了一个 BeginInstanceFactory,然后在调用 EndInstanceFactory 之前的某个地方出现了问题,您必须重试。

但是实例工厂在您的 CATIA 会话中仍然打开,如果您立即再次尝试您的宏,您将在 BeginInstanceFactory 处收到错误。

因此,作为最佳实践,我一直在做的是在调用 BeginInstanceFactory 之前调用 EndInstanceFactory(当然也是在调用结束时)。如果不需要调用,它将被忽略。但是,如果需要(如果工厂由于先前的失败 运行 而在您的会话中仍然打开),它应该将所有内容恢复到预期状态,以便再次打开工厂。

总结一下,试试这个:

...
Set factory = PartDest.GetCustomerFactory("InstanceFactory")
factory.EndInstanceFactory
factory.BeginInstanceFactory "SurfacicHoles","C:\PowerCopyReference.CATPart"
...