'********************************************************************
'********************************************************************
'****  VBSCRIPT MACRO FOR THE GENERATION OF HTML DOCUMENTATION   ****
'****  	           FOR THE MANUFACTURING WORKSHOP                **** 
'****                  Dassault Systemes                         ****
'********************************************************************
'********************************************************************

Language="VBSCRIPT"

Dim OutRef, OutPath, OutIndex


RootActivityNameRef = "Process"

HTMLFileName = "MfgProcess"
HTMLPath = "/tmp/"


Sub CATMain()
   CATDocument RootActivityNameRef, HTMLFileName, HTMLPath    
End Sub



'--------------------------------------------------------------------
' Main Procedure
'--------------------------------------------------------------------
Sub CATDocument( RootActivityName , HtmlFilesName , HtmlFilesPath )

  CATIA.SystemService.Print "RootActivityName : " & RootActivityName
  CATIA.SystemService.Print "HtmlFilesName : " & HtmlFilesName
  CATIA.SystemService.Print "HtmlFilesPath : " & HtmlFilesPath
  Dim RootActivity	
  Dim RootActName
  RootActName = RootActivityName
  OutPath = HtmlFilesPath
  OutRef = HtmlFilesName
  'OutIndex = OutRef & ".html"
  OutIndex = ".html"
  CreateHtmlFile OutPath, OutRef, OutIndex

End Sub


'---------------------------------------------------------------
' Create the output file and its header
'---------------------------------------------------------------

Sub CreateHtmlFile(OutPath, OutRef, OutIndex)
  Out = OutPath & OutRef & OutIndex
  Dim indexfs
  Dim indexf
 ' Set indexfs = CreateObject("Scripting.FileSystemObject")
  Set indexfs = CATIA.FileSystem

 ' Set indexf = Indexfs.OpenTextFile(OutPath & OutIndex, 2, True)
  Set indexf1 = indexfs.CreateFile("E:\tmp\tbl.html", TRUE)
  MsgBox indexf1.Name
 
  Set indexf=indexf1.OpenAsTextStream("ForWriting")
	
  ' msgbox indexf.name

  Dim ProcessName
  ProcessName = "ManufacturingProcess"

  indexf.Write "<!doctype html public ""-//w3c//dtd html 4.0 transitional//en"">" & VBCRLf
  indexf.Write "<html>" & VBCRLf
  indexf.Write "<head>" & VBCRLf
  indexf.Write "   <meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"">" & VBCRLf
  indexf.Write "   <meta name=""GENERATOR"" content=""IPD v2"">" & VBCRLf
  indexf.Write "   <meta name=""Author"" content=""IPD v2"">" & VBCRLf
  indexf.Write "   <title>Documentation for Process: " & ProcessName & "</title>" & VBCRLf
  indexf.Write "</head>" & VBCRLf
  indexf.Write "<body background=""back.gif"" bgcolor=#CCCCCC>" & VBCRLf
  indexf.Write "<center><table border =  10 bgcolor=#EEEEFF>" & VBCRLf
  indexf.Write "<tr align =center >" & VBCRLf
  indexf.Write "<td><font size =5 >Documentation for Process: <i>" & ProcessName &"</i>" & VBCRLf
  indexf.Write "</font></td></tr>" & VBCRLf
  indexf.Write "</table></center>" & VBCRLf
  indexf.Write "<i><p>Generation Date : " & Date & " at " & Time & "</i></p>"' & VBCRLf


'---------------------------------------------------------------
' Variables declaration 
'---------------------------------------------------------------
  Dim SetupList As MfgActivities
  Dim ProgramList As MfgActivities 
  Dim ActivityList As MfgActivities 
  Dim NumberOfSetup As Integer
  Dim NumberOfProgram As Integer 
  Dim NumberOfActivity As Integer 	
  Dim I As Integer
  Dim J As Integer
  Dim K As Integer
  Dim SetupName	
  Dim ProgramName 	
  Dim ActivityName 	
  Dim CurrentSetup As ManufacturingActivity
  Dim CurrentProgram As ManufacturingActivity
  Dim CurrentActivity As ManufacturingActivity
  Dim CurrentTool As ManufacturingTool
  Dim CurrentMachine As ManufacturingMachine
  Dim CurrentCatalogName
  Dim Comment
  Dim ToolNumber As Integer
  Dim ActivityType
  Dim ToolComments
  Dim MoSpindleParam As Parameter
  Dim MoFeedrateParam As Parameter
  Dim MoSpindle	
  Dim MoAppFeedrate

  Comment  = "comments "
  MoSpindle = 0.0
  MoAppFeedrate = 0.0
  
  Dim childs As Activities
  Dim child As Activity
  Dim quantity As Integer
  Dim ActivityRef As AnyObject

'---------------------------------------------------------------
' Retrieve the active document
'---------------------------------------------------------------
  Dim MfgDoc1 As Document	
  Set MfgDoc1 = CATIA.ActiveDocument
  
'---------------------------------------------------------------
' Retrieve the current Process 
'---------------------------------------------------------------
  Set ActivityRef = MfgDoc1.GetItem("Process")

'---------------------------------------------------------------
' Scan of the Process 
'---------------------------------------------------------------
  quantity = 0	

  If (ActivityRef.IsSubTypeOf("PhysicalActivity")) Then
    Set childs = ActivityRef.ChildrenActivities
    quantity = childs.Count

    if quantity <= 0 then
      Exit Sub
    End if

	NumberOfSetup = 0

    For I=1 To quantity
	  Set child = childs.Item(I)

	  If (child.IsSubTypeOf("ManufacturingSetup")) Then

		Set CurrentSetup = child
		NumberOfSetup = NumberOfSetup +1

		SetupName = CurrentSetup.Name

		'=> If no machine is defined, ignore error
		On Error Resume Next							
		Set CurrentMachine = CurrentSetup.Machine

		CurrentCatalogName = ""

		'=> If no machine is defined, ignore error
		On Error Resume Next		
		CurrentCatalogName= CurrentMachine.PreferedToolCatalogName

		Comment = CurrentSetup.comment

		indexf.Write "<p><b><font size =3 >Setup Number: "& NumberOfSetup & " , Name :"& SetupName &"</font></b><br>"	 & VBCRLf
		indexf.Write "<i>Setup Comment: "&Comment&" </i><br>"	 & VBCRLf
		indexf.Write "<n>Setup Tool Catalog Name: "&CurrentCatalogName&" </n></p>"	 & VBCRLf

	'---------------------------------------------------------------
	'       Read the Programs  of the Current Setup
	'---------------------------------------------------------------	
		Set ProgramList = CurrentSetup.Programs
		NumberOfProgram = ProgramList.Count	

			For J=1 To NumberOfProgram

				Set CurrentProgram = ProgramList.GetElement(J)	
				ProgramName = CurrentProgram.Name
				Comment = CurrentProgram.comment
				indexf.Write "<p><b><font size =3>Program Number: "& J & " , Name: " & ProgramName &"</font></b><br>"	 & VBCRLf
				indexf.Write "<i>Program Comment: "& Comment & "</i><br>"	 & VBCRLf
		'---------------------------------------------------------------
		'               Read the Activities Current Program
		'---------------------------------------------------------------
				Set ActivityList = CurrentProgram.Activities	
				NumberOfActivity = ActivityList.Count	

				indexf.Write "Program description</p>" & VBCRLf
				indexf.Write "<table border = 5 bgcolor=#EEEEFF width = 1000>" & VBCRLf
				indexf.Write "<tr>" & VBCRLf
				indexf.Write "<b>"	 & VBCRLf
				indexf.Write "<th align=center width = ""10%"">Activity number</th>" & VBCRLf
				indexf.Write "<th align=center width = ""20%"">Activity name</th>" & VBCRLf
     			indexf.Write "<th align=center width = ""10%"">Tool number</th>" & VBCRLf
				indexf.Write "<th align=center width = ""20%"">Activity type</th>" & VBCRLf
				indexf.Write "<th align=center width = ""10%"">Spindle speed</th>"	 & VBCRLf
    			indexf.Write "<th align=center width = ""10%"">Feedrate</th>"		 & VBCRLf
				indexf.Write "</b>"		 & VBCRLf
				indexf.Write "</tr>"	 & VBCRLf

    			For K=1 To NumberOfActivity 
					Set CurrentActivity = ActivityList.GetElement(K)
					ActivityName = CurrentActivity.Name	
					ActivityType= CurrentActivity.Type
					MoSpindle	= 500
					MoAppFeedrate = 200
					MoCompLengthTool = 4		
					MoCompRadiusTool =  3
					Comment = ActivityType 
		'---------------------------------------------------------------
		'           Read the Activity Type
		'           If the Activity is a tool Change
		'	        Read the Tool,Tool Number
		'			If The Activity is a Machining operation
		'			Read the Mo Name, Mo Spindle, Mo feedrate...	
		'---------------------------------------------------------------
					If ( ActivityType = "ToolChange" ) Then
						Set CurrentTool = CurrentActivity.Tool
						ToolNumber = CurrentTool.Number
						MoSpindle	= "x"
						MoAppFeedrate = "x"
					ElseIf (ActivityType = "PPInstruction") or (ActivityType = "TableHeadRotation") or (ActivityType = "CoordinateSystem")Then
						MoSpindle	= "x"
						MoAppFeedrate = "x"
					Else
						Set MoSpindleParam = CurrentActivity.GetAttribute("SpindleSpeed.Value")
						MoSpindle =MoSpindleParam.Value
						Set MoFeedrateParam = CurrentActivity.GetAttribute("MachiningFeedrate.Value")
						MoAppFeedrate =MoFeedrateParam.Value
							
					End If

					indexf.Write "<tr>" & VBCRLf
					indexf.Write "<td align=right  width = ""10%"">" & K &"</td>" & VBCRLf
    				indexf.Write "<td align=right  width = ""20%"">" &ActivityName &"</td>" & VBCRLf
	 				indexf.Write "<td align=right  width = ""10%"">" & ToolNumber & "</td>" & VBCRLf
					indexf.Write "<td align=right  width = ""20%"">" & Comment & "</td>" & VBCRLf
					indexf.Write "<td align=right  width = ""10%"">" & MoSpindle &"</td>" & VBCRLf
					indexf.Write "<td align=right  width = ""10%"">" & MoAppFeedrate & "</td>" & VBCRLf
					indexf.Write "</tr>"	 & VBCRLf

    			Next	

				indexf.Write "</table>" & VBCRLf
			Next
	  End If
	Next
     
  End If

'---------------------------------------------------------------
' End of the Scan
'---------------------------------------------------------------
indexf.Write "</html>" & VBCRLf
End Sub







