
! script name: datatable.d/opholechart
! topic: operations hole charts

!A
!A NAME:
!A
!A 	OPERATION HOLE CHARTS
!A
!A DESCRIPTION:
!A
!A	This script generates a chart of the XYZ locations
!A	associated with the current operations.
!A
!B NOTES:
!B
!B 	This script is a template only. A user may copy and modify
!B 	this script to produce custom charts. Weber systems is not
!B 	responsible for customization of charts for particular needs.
!B 

stat=ChangeLabelStr("ScriptNameStatus","Function: operation hole charts ")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[1024]
DIM B$[1024]
DIM Descr$[256]
DIM Oper_name$[256]
DIM Tool_name$[256]
DIM Table_name$[256]
DIM Format$[32]

! StdWorkplane

ActivateUndo
A= GetDefault(WsPlacesAfter)
Format$= "decimal "&VAL$(A)

Add_z_column=1
Units= CurrentUnits

Grid_color= 14

Entry_color= 8
Entry_text_height= GetDefault(WsTextHeight)
Entry_text_width= GetDefault(WsTextWidth)

Header_color= 14
Header_ltype= 9
Head_text_height= 2.0*Entry_text_height
Head_text_width= 2.0*Entry_text_width
Header_slant= TAN(30)

Label_color= 8
Label_ltype= 0
Label_text_height= 1.5*Entry_text_height
Label_text_width= 1.5*Entry_text_width
Label_slant= 0

Oper_group= UserDataGroup(WsOperation)
IF NOT ValidGeometry(Oper_group) THEN Leave
Number= GroupCount(Oper_group)

Holes_group= GrpByEntType(WsHoleEnt)
IF NOT ValidGeometry(Holes_group) THEN Leave

Hole_count=0
FOR I=1 TO Number
Oper= GroupElement(Oper_group,I)
Operation_holes= GrpByProp(Holes_group,Oper,WsOperation)
IF ValidGeometry(Operation_holes) THEN
	H1= GroupElement(Operation_holes,1)
	IF ValidGeometry(H1) THEN
		Tool1= GetAssocTool(H1)
		IF ValidGeometry(Tool1) THEN
			Hole_locations= GrpByTool(Operation_holes,Tool1)
			IF ValidGeometry(Hole_locations) THEN
			   Hole_count= Hole_count+GroupCount(Hole_locations)
			   Stat=DeleteEntity(Hole_locations)
			END IF
		END IF
	END IF
	Stat=DeleteEntity(Operation_holes)
END IF
NEXT I
IF NOT Hole_count THEN Leave


Nr= Hole_count+2
Nc= 4 + Add_z_column
Table_name$= "OPERATION HOLE CHART"
Table1= FindNamedEntity(Table_name$)
IF ValidGeometry(Table1) THEN
   Stat= EraseEntity(Table1)
   Stat= ReDimTableRows(Table1,Nr)
   Stat= ReDimTableColumns(Table1,Nc)
   Stat= BlankDataTable(Table1)
ELSE
Table_pos:Stat=SetInputFormat("n4","")
        Pc=XyzInput("TABLE LOCATION ",WsXyzInput)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Pc) THEN Table_pos
   	Table1= CreateBlankTable(Pc,Table_name$,Nr,Nc,WsUpperLeft,"  ")
   	Stat= DeleteEntity(Pc)
END IF

Table_pos=1
Stat= InsertIntoTable(Table1,"Synergy Hole Chart",Table_pos,2)

Table_pos=2
Stat= InsertIntoTable(Table1,"Hole",Table_pos,1)
Stat= InsertIntoTable(Table1,"Operation",Table_pos,2)
Stat= InsertIntoTable(Table1,"X",Table_pos,3)
Stat= InsertIntoTable(Table1,"Y",Table_pos,4)
IF Add_z_column THEN
	Stat= InsertIntoTable(Table1,"Z",Table_pos,5)
END IF

Table_pos=3
Number= GroupCount(Oper_group)
FOR I= 1 TO Number
Oper= GroupElement(Oper_group,I)
Stat=GetUserDataName(Oper,Oper_name$)
IF NOT Stat THEN
   I1= (I MOD 27)
   I2= (I DIV 27) +1
   IF I1< 1 THEN I1=1
   h_letter= 64+I1
   A$= RPT$(CHR$(h_letter),I2)

   Num_tools= GetUserValue(Oper,"Number tools:")

      Operation_holes= GrpByProp(Holes_group,Oper,WsOperation)
      IF NOT ValidGeometry(Operation_holes) THEN Next_operation

      H1= GroupElement(Operation_holes,1)
      Tool1= GetAssocTool(H1)

      IF ValidGeometry(Tool1) THEN
	Hole_locations= GrpByTool(Operation_holes,Tool1)
	Hole_count= GroupCount(Hole_locations)
      ELSE
	GOTO Next_operation
      END IF

      Stat= InsertIntoTable(Table1,A$,Table_pos,1)
      Stat= InsertIntoTable(Table1,Oper_name$,Table_pos,2)
      IF Hole_count THEN
	A$= VAL$(Hole_count)&" locations"
       	Stat= InsertIntoTable(Table1,A$,Table_pos+1,2)
      END IF

      FOR J=1 TO Hole_count
	H1= GroupElement(Hole_locations,J)
	Pt= ParameterPt(H1,0.0)
        Stat= InsertIntoTable(Table1,XPointValue(Pt),Format$,Table_pos,3)
        Stat= InsertIntoTable(Table1,YPointValue(Pt),Format$,Table_pos,4)
	IF Add_z_column THEN
        Stat= InsertIntoTable(Table1,ZPointValue(Pt),Format$,Table_pos,5)
	END IF
        Table_pos= Table_pos+1
	Stat= DeleteEntity(Pt)
      NEXT J
END IF
Next_operation:!
Stat= DeleteEntity(Hole_locations)
Stat= DeleteEntity(Operation_holes)
NEXT I

Stat= NameEntity(table1,Table_name$)
Stat= TableTextOrigin(Table1,WsRightCenter)
Stat= TableTextOrigin(Table1,0,2,WsLeftCenter)
Stat= TableTextOrigin(Table1,0,1,WsCenterText)
Stat= SetTableColors(Table1,Entry_color)

Stat= TableTextSizes(Table1,1,0,Head_text_width,Head_text_height,Header_slant)
Stat= TableTextOrigin(Table1,1,0,WsCenterText)
Stat= SetTableColors(Table1,1,0,Header_color)
Stat= SetTableLType(Table1,1,0,Header_ltype)

Stat= TableTextSizes(Table1,2,0,Label_text_width,Label_text_height,Label_slant)
Stat= TableTextOrigin(Table1,2,0,WsCenterText)
Stat= SetTableColors(Table1,2,0,Label_color)
Stat= SetTableLType(Table1,2,0,Label_ltype)

Stat= SetColor(Table1,Grid_color)
Stat= PlotEntity(Table1)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Oper_group)
Stat= DeleteEntity(Holes_group)

ActivateUndo
Stat= AdvanceUndoNumber("hole chart")
Stat= AddItemToUndo(WsUBCreateMode,Table1)


Leave:!
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Oper_group)
Stat= DeleteEntity(Holes_group)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
