Search code examples
vbacatia

Instantiate PowerCopy in CATIA V5R24 using VBA macros


I would like to create macro for power copy. I took code and models from this link unfortunately it doesn't work (I have made small modifications). Similar problem I have found in this subject, and in his case it works.

 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

At this step appears an error

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

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

Windows 7 64bit

Today I have got new information from my management... that some of our factories doesn't have access to KT1 license... In that case is there any other way to use automatic power copy ?


Solution

  • Once an instance factory is opened with BeginInstanceFactory, it must be closed by a corresponding EndInstanceFactory.

    Now what happens, particularly in development, is you do an BeginInstanceFactory, then somewhere before the call to EndInstanceFactory something breaks, and you have to try again.

    However the Instance Factory is still open in your CATIA session, and if you try your macro again right away you will get an error at BeginInstanceFactory.

    So what I ALWAYS do just as a best practice, is call EndInstanceFactory BEFORE BeginInstanceFactory is called (and of course at the end as well). If the call is not required it will be ignored. But if it is required (If the factory is still open in your session because of a previous failed run) it should put everything back into the expected state for the factory to be opened again.

    So to summarize, try this:

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