Instantiating a User Feature From a VB Macro

You can instantiate User Features from VB Macros. Two different protocols are available to instantiate User Features.

This task shows you how to:

Related Topics
Creating a User Feature

First Instantiation Protocol

The first protocol is dedicated to User Feature instantiation only. It is defined by a single method: AddInstance. For more information about this method, See the Automation documentation.

  1. Create a 3D Shape Representation called Macro-Representation-Template identical to the one below:



  2. Click and select Knowledgeware > Templates > Product Knowledge Template workbench.

  3. Click Create a UserFeature . The UserFeature Definition dialog box is displayed.


    • Enter the name of the User Feature: CircularHoleUDF.
    • Select the components in the specification tree: Circle.1 and Split.1.
    • Click the Parameters tab and publish the Circle.1\Radius parameter. Rename Circle.1\Radius into Radius.
    • Click OK when done to validate your modifications. The User Feature is created.

  4. Create a second 3D Shape Representation called Macro-Representation-Template identical to the one below:



  5. Save the created 3D Shape Representations in the database.

  6. Select Tools > Macro > Macros.

  7. Click Macro libraries.... In the Macro libraries window, click Create new library. Enter the name of the Resources zip and click Finish. Click Close.

  8. In the Macros window, click Create... The Create a new macro dialog box is displayed.

  9. Select the macro language (Script in this scenario), and enter the name of the macro. Click OK.

  10. Click Edit and enter the following script in the editor:

    Sub CATMain()
    							'-----------------------------------------------------------------------
    							CATIA.SystemService.Print "UI Active the Representation 
    							containing the UDF reference"
    							Set StartObject = CATIA.ActiveEditor.ActiveObject
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "Retrieve the UDF reference 
    							named CircularHoleUDF"
    							Set reference = StartObject.FindObjectByName("CircularHoleUDF")
    							If (TypeName(reference)="Nothing") Then
    							MsgBox "No UDF",0,"Error","",0
    							End If
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "UI Active the Representation 
    							Reference where the instantiation is performed"
    							Dim theWindows As Windows
    							Set theWindows = CATIA.Windows
    							Dim destinationWindow As Window
    							Set destinationWindow = theWindows.Item(1)
    							destinationWindow.Activate
    							Set DestinationObject = CATIA.ActiveEditor.ActiveObject
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "Retrieve the Instance factory 
    							on this part container"
    							Set factory = DestinationObject.GetCustomerFactory("InstanceFactory")
    							'----------------------------------------------------------------------
    							CATIA.SystemService.Print "Instantiate the UDF"
    							Set instance = factory.AddInstance(reference)
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "Valuate the first input named 
    							Surface"
    							Set input1 = DestinationObject.FindObjectByName("Fill.1")
    							instance.PutInput "Surface", input1
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "Valuate the second input 
    							named Center"
    							Set input2 = DestinationObject.FindObjectByName("input.2")
    							instance.PutInput "Center", input2
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "Modify the parameter named 
    							Radius"
    							Set param1 = instance.GetParameter("Radius")
    							param1.ValuateFromString("5mm")
    							'------------------------------------------------------------------------
    							CATIA.SystemService.Print "Launch a global Update"
    							DestinationObject.Update
    							'------------------------------------------------------------------------
    							End Sub

    The User Feature is instantiated.

    Note:

    • This method is to be used when instantiating the reference only once.
    • You must open the Representation Reference containing the User Feature reference in the current editor and then switch the windows to perform the instantiation. Once done, the Representation Reference containing the User Feature Reference remains open in the CATIA session until you close it.

Second Instantiation Protocol

The second protocol is dedicated to User Features and Power Copies instantiation. It is defined by several methods that must be called in a defined order

  1. .Create a 3D Shape Representation called UDF-TST-Macro-RepresentationTemplate containing geometry and identical to the one below:



  2. Click Create a UserFeature . The UserFeature Definition dialog box is displayed.


    • Enter the name of the User Feature: CircularHoleUDF.
    • Select the inputs in the specification tree: Circle.1 and Split.1.
    • Click the Parameters tab and publish the Circle.1\Radius parameter.
    • Click OK when done to validate your modifications. The User Feature is created.

  3. Save the created 3D Shape Representation in the database and close it.

  4. Select Tools > Macro > Macros.

  5. Click Macro libraries.... In the Macro libraries window, click Create new library. Enter the name of the Resources zip and click Finish. Click Close.

  6. In the Macros window, click Create.... The Create a new macro dialog box is displayed.

  7. Select the macro language (Script in this scenario), and enter the name of the macro. Click OK.

  8. Click Edit and enter the following script in the editor:

    ' This 
    							sample shows how to instantiate a User Feature named 
    							"CircularHoleUDF"
    							' which is stored in a 3D Shape Representation Reference 
    							named 
    							' "UDF-TST-Macro-RepresentationTemplate" and with a 
    							V_version = "---".
    							' There are 2 inputs: "Surface" and "Center" and 1 published 
    							parameter
    							' "Radius". To run this sample, open in session the 
    							3D Shape Representation
    							' where you want to instantiate the User Feature. It 
    							must contain at least a
    							' Fill.1 surface and a input.2 point(this point must 
    							lay on the Fill).
    							' Go to Tools \ Macro \ Macros ... and run this script.
    							
    							Sub CATMain()
    							'---------------------------------------------------------------------------
    							CATIA.SystemService.Print "Retrieve the Representation 
    							Reference where the instantiation is performed"
    							Set PartDocumentDest = CATIA.ActiveEditor.ActiveObject
    							CATIA.SystemService.Print "PartDocumentDest.TypeName 
    							= " & TypeName(PartDocumentDest)
    							If TypeName(PartDocumentDest)<> "Part" Then
    							MsgBox "Error: TypeName activeObject is not a part"
    							End If
    							'---------------------------------------------------------------------------
    							CATIA.SystemService.Print "Retrieve the Instance factory 
    							on this part container"
    							Set factory = PartDocumentDest.GetCustomerFactory("InstanceFactory")
    							'---------------------------------------------------------------------------
    							CATIA.SystemService.Print "Initialize the process with 
    							the name of the Rep Template and the PLMExternalID of 
    							the Representation Reference where it stands"
    							factory.BeginInstanceFactory "CircularHoleUDF", "UDF-TST-Macro-RepresentationTemplate", 
    							"---"
    							'----------------------------------------------------------------------------
    							CATIA.SystemService.Print "Begin the instantiation"
    							factory.BeginInstantiate
    							'----------------------------------------------------------------------------
    							CATIA.SystemService.Print "Valuate the first input named 
    							Surface"
    							Set input1 = PartDocumentDest.FindObjectByName("Fill.1")
    							factory.PutInputData "Surface", input1
    							'---------------------------------------------------------------------------
    							CATIA.SystemService.Print "Valuate the second input 
    							named Center"
    							Set input2 = PartDocumentDest.FindObjectByName("input.2")
    							factory.PutInputData "Center", input2
    							'---------------------------------------------------------------------------
    							CATIA.SystemService.Print "Modify the parameter named 
    							Radius"
    							Set param1 = factory.GetParameter("Radius")
    							param1.ValuateFromString("5mm")
    							'----------------------------------------------------------------------------
    							CATIA.SystemService.Print "Instantiate the User Feature"
    							Set Instance = factory.Instantiate
    							'----------------------------------------------------------------------------
    							CATIA.SystemService.Print "End the instantiation process"
    							factory.EndInstantiate
    							'-----------------------------------------------------------------------------
    							CATIA.SystemService.Print "Release the 3D Shape Representation 
    							containing the Rep Template reference"
    							factory.EndInstanceFactory
    							'-----------------------------------------------------------------------------
    							CATIA.SystemService.Print "Launch a global Update"
    							PartDocumentDest.Update
    							'----------------------------------------------------------------------------
    							End Sub
     

  9. Close the script editor, select your macro and click Run in the Macros dialog box. The User Feature is instantiated.

    Indicate the PLM_ExternalID and the V_version of the Representation Reference containing the Template. The Representation Reference will be loaded in session without the editor, and will be unloaded at the end of the instantiation. This protocol is recommended for the instantiation of a Template in a Knowledge Loop.

    If you are a DS Passport customer, you can read the