Creating a Knowledge Advisor Reaction: BeforeUpdate Event

This task explains how to use the BeforeUpdate event in a reaction feature. In the scenario below, the user optimizes the position of a point each time he modifies the length of the cable (spline). The user creates his geometry and inserts all the components in a User Defined Feature (UDF).
This UDF contains the geometry of a cable going through 3 points:
The two points located at both extremities are to be specified in input.
The coordinate of the third point is optimized in order to reach a target length for the cable. 

This target parameter is a published parameter of the UDF. The optimization is launched by using a VBMacro with argument, called in a Reaction to the "Before Update" event of the UDF.  

The Reaction capabilities require the Knowledge Advisor product. 
  1. Open the KwrEvent_BeforeUpdate.CATPart file: It contains 3 points and a spline (called cable in this scenario).

  1. From the Start->Knowledgeware menu, access the Product Engineering Optimizer workbench and click the Optimize icon (). The Optimization window opens.
  2. Enter the following data in the Optimization window:
    Problem tab  
      Optimization type Minimization
    Optimized parameter distance
    Free parameters Open_body.1\Point.3\Pointcoordinates.1\Z
    Algorithm Simulated Annealing
    Termination criteria Maximum number of updates: 100
    Consecutive updates without improvements: 20
    Maximum time (minutes): 5
    Constraints tab  
      New constraint `Open_body.1\Point.3\Point coordinates.1\Z` - max (Open_body.1\Point.2.coord(3) ,Open_body.1\Point.1.coord(3)) <= 0mm 
  3. Click Run optimization, and click OK once the optimization process is over.
  4. From the Start->Knowledgeware menu, access the Knowledge Advisor workbench and click the Macros with argument icon (). The Script Editor opens. Enter the following data in the editor and click OK:
    Argument optim
    Script body optim.Run false
  5. Click the Reaction icon (). The Reaction dialog box opens. 
In the Source type field, select Owner.
In the Available events list, select BeforeUpdate.
In the Action field, select Knowledgeware action.
  1.  Click the Edit action... button, paste the following script in the editor, and click OK twice:
if (CableLength <= distance (Open_body.1\Point.1 ,Open_body.1\Point.2 ))
Message ("Incompatible inputs: cable length is less than distance between points!")
else
{
`VB Script.1` .Run(Relations\Optimizations.1\Optimization.1 )
}
  1. Double-click the root of the specification tree and select the Insert->UserFeature->UserFeature Creation... command. The UserFeature Definition window opens.
  2. In the Name field, enter the name of the UDF: Cable1 in this scenario.
  3. Select the Spline, Point3, Reaction.1, VB Script.1, Optimization.1, the 4 formulas, and the parameters: they are displayed in the UserFeature definition window (see below.)

 

Note that the UDF becomes the owner of the reaction. This reaction will be fired before the update of the UDF instance.
  1. Click the Parameters tab, select CableLength, click the Published name check box and click OK.

  2. Save the file and close it.
  3. Create a new .CATPart file, access the Generative Shape Design workbench,  and create 3 points. 
  4. Select the Insert->Instantiate From Document... command. The File Selection panel opens. Select the KwrEvent_BeforeUpdate.CATPart file and click the Open button.
  5. The Insert object dialog box opens. Select Point.1 and Point.2 in the geometry or in the specification tree and click OK. The cable (UDF) is instantiated and the optimization is launched before the update.
  6. Repeat steps 14 and 15: select Point.2 and Point.3 when instantiating the UDF: the cable lengths are optimized.

  1. Double-click the CableLength=400mm parameter and change its value to 200mm. This cable length is optimized once again just before the update.
 To know more about the Reaction feature window, see Using the Reaction Feature Window.

Back Up Next