Language="VBSCRIPT"

'*******************************************************************************************
'*
'* Procedure that allow the user to insert a PP Instruction at a specified location
'* 
'*    Open CATProcess and load Machining workbench
'*    Execute the VB Script Macro
'*    Enter the name of the activity before which you want to insert the PP Instruction
'*      If no Name is defined the PP Instruction will be added at the end of the Program
'*
'*******************************************************************************************

Sub CATMain()
	
	Dim MfgDoc1 As Document
	Set MfgDoc1 = CATIA.ActiveDocument

'*** Retrieve current Process
	
	Dim ActivityRef As AnyObject
	Set ActivityRef = MfgDoc1.GetItem("Process")

'*** Retrieve current Set Up

	Dim Setup1 As ManufacturingSetup

	If (ActivityRef.IsSubTypeOf("PhysicalActivity")) Then

		Set childs = ActivityRef.ChildrenActivities
		quantity = childs.Count

		if quantity <= 0 then
			Exit Sub
		End if

		NumberOfPO = 0

		For I=1 To quantity
			Set child = childs.Item(I)
			If (child.IsSubTypeOf("ManufacturingSetup")) Then
				Set Setup1 = child
				NumberOfPO = NumberOfPO +1
				Exit For
			End If
		Next   
	End If

	if NumberOfPO <= 0 then
	  Exit Sub
	End if

'*** Retrieve current Program

	Set ProgramList = Setup1.Programs

	Dim Program1 As ManufacturingProgram
	Set Program1 = ProgramList.GetElement(1)	

'*** Box for user-defined referenced activity

	Dim DefName As String
	Dim RefActivityName As String
         RefActivityName = InputBox("Add PP Instruction before activity", "Set name", DefName)

'*** Create PP Instruction

	Dim PPInstr As ManufacturingActivity
	Set PPInstr = Program1.AddPPInstruction ("PPRINT/MY PP WORD")

'*** Move to the correct Position

	Dim ActCur As ManufacturingActivity
	Dim ActName As String

	Set ActInProg = Program1.ChildrenActivities
	quantity = ActInProg.Count

	For I=1 To quantity
		set ActCur = ActInProg.Item(I)

		If (ActCur.IsSubTypeOf("ManufacturingActivity")) Then
			ActName = ActCur.Name

			if ActName = RefActivityName then
				Program1.MoveOperation ActCur,PPInstr
			End if
		End If
	Next   
	
End Sub