
! script name: amo.d/cmmexecute

!A
!A NAME:
!A
!A 	EXECUTE CMM
!A
!A DESCRIPTION:
!A
!A 	This is a CUSTOM script that will create Automated Manufacturing
!A	Output (CMM) for a specific post processor.  The script is specific
!A	to one post processor.  A command_file is automatically created for
!A	this post and then executed.  The command_file can be
!A	edited and, rerun using TurboPost. NOTE: DO NOT rerun the command_file
!A	using AMO.  The changes that were made will be overwritten.
!A
!B INSTRUCTIONS:
!B
!B	The areas in here are broken into SUBroutines.  There are two
!B	main areas that are the most customizable.
!B	  1) SUB Toolch - This is where the output for a probe change is 
!B	                  created. The startup is also output in this section.
!B 	  2) SUB Probing - This is where the different probe options are created.
!B
!B	There are comments throughout the script to better explain
!B	specific areas.
!B
!# see also - amo.d/execute.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: execute cmm")

! Standard Variables are in /Amo_vars1/ and /Amo_vars2/
! Custom Variables are in /Amo_defaults/ /Amo_defaults2/

COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$[128],Last_tool$[256]

DIM Destination$[256],Dir$[1024]
DIM Data_name$[128],Dname$[128],Dstr$[128],Event_data$[128]
DIM Base_name$[128],Next_probe$[256]

ENTITY Last_point	! Holds the last known position for Optimization
ENTITY NextTool		! Entity To hold the next TOOL in the tasks.
Start_work= CurrentWorkplane
WsEventData= 12345
WsAmoData= 23051
DeActivateUndo

! SWITCHES: These are convenience switches. This will aide in the customization

Use_tdata=0	! 0= Start at 1 and increment tool numbers
		! 1= Use CNC tool-pocket number: from the tool
Do_nxt=0	! 0= No Next_probe and 1= Use Next_probe (Pre-Staging)
One_pkt=0	! Used to determine if a Path Group that has one contour
		! should be cut using mcut_path_grp or contour_cut.
		! 0= Use mcut_path_grp 1= Use contour_cut
Do_safe=0	! For canned cycles where a position to the first location
		! is done before the length offset is applied.
		! 0= No safe start and 1= Pre-position for length offset

! DEFAULT SECTION: Defaults can be added and placed in the COM /Amo_defaults/
! line. The COM should be duplicated in the SUB's where it is used.

Units= CurrentUnits
Last_tool$=""

! Setup area after script re-entry.

Edata= FindUserData("EVENT INFO",WsGeneralString)
IF ValidGeometry(Edata) THEN
	Start_num= GetUserValue(Edata,"Start number:")
	S_up= GetUserValue(Edata,"Startup:")
	In_post= GetUserValue(Edata,"In post:")
	Stat= DeleteEntity(Edata)
ELSE
	In_post= 0
	Start_num= 1
	S_up= 1
END IF
No_tool=0

Oper_grp= FindNamedGrp("CMM TASKS")	! Task list
IF NOT ValidGeometry(Oper_grp) THEN Leave

All_face=NamedVarVal("amo direction",WsToolVars,0.0) ! Many->One or One->Many

! WORK ZONES: This section will find all the active work zones.
! Note: Components is the internal name for work zones.

Num_comps= 1
CompList= FindUserData("CMM WORK ZONES",WsAmoData)
IF ValidGeometry(Complist) THEN
	Num_comps= GetUserValue(Complist,"Number components:")
	Do_components= 0
	IF Num_comps THEN
		ALLOCATE Comp(Num_comps)
		Do_components= 1
		FOR K= 1 TO Num_comps
			Comp(K)=GetUserValue(Complist,"Component "&VAL$(K)&":")
		NEXT K
	END IF
END IF
StdWorkplane

Number= GroupCount(Oper_grp)

! This is the main section. This area does not really need to be customized.
! it calls the SUBs to do its tasks.

FOR Big_pass= 1 TO Num_comps^(All_face AND Do_components) ! Once or Many 
FOR K= Start_num TO Number  ! For each task

	EntData= GroupElement(Oper_grp,K)		! Get task
	Stat= GetUserDataName(EntData,Data_name$)	! Find name
	Is_tool= (UserDataType(EntData)= WsCMMProbeData)

! RECORD POST NAME: This is where the name of the post_processor is 
! recorded. DO NOT change /weber to be /usr/weber. Change only the name
! mill to be <post processor>. Nothing else in this section is to be changed.
	IF NOT In_post THEN
		Dir$= NamedVariableStr("post dir",WsSystemVars,"/usr/weber/post_formats/")
		Found= RecordPostName(FileNameString(Dir$,"cmm_cordax"),Units)
		IF NOT Found THEN Leave
		Stat= RemoveDirFiles("./","*","command_file")
		Stat= RemoveDirFiles("./","*","notepad")
		Stat= RemoveFile("command_buffer")
		First= InitCmdMenu(1,0)
		In_post= 1
	END IF

	IF Is_tool THEN 	! If current task is a tool
! Find a delete old group entity for this tool and get tool type
		Oldgroup=FindNamedGrp(Data_name$&" group")
		IF ValidGeometry(Oldgroup) THEN Stat= DeleteEntity(Oldgroup)
!		Group= GrpByTool(EntData)
		Group= GrpByProp(EntData,WsCMMProbeData)
		IF NOT ValidGeometry(Group) THEN End_tool
		Stat=GetUserString(EntData,"Probe type:",Data_type$)

! Checks to see if base name of tool is the same as the last tools base name.
! This will insure a tool_change is not performed unnecessarily.
		Inst_spot= POS(Data_name$,"--")
		IF NOT Inst_spot THEN Inst_spot= LEN(Data_name$)+1
		Base_name$= Data_name$[1;Inst_spot-1]
		Tl_ch= 0
		IF Last_tool$<>Base_name$ THEN Tl_ch=1

		F_tl_num= K*(K<Number)+1
		IF Do_nxt THEN
		   Curr_tl= K*(K<Number)+1
		   LOOP
		      NextTool= GroupElement(Oper_grp,Curr_tl)
		      IF UserDataType(NextTool)<>WsCMMProbeData THEN End_loop
		      Stat= GetUserDataName(NextTool,Next_probe$)
                      Nxt_spot= POS(Next_probe$,"--")
                      IF NOT Nxt_spot THEN Nxt_spot= LEN(Next_probe$)+1
		      EXIT IF Base_name$<>Next_probe$[1;Nxt_spot-1]
End_loop:	      Curr_tl= Curr_tl*(Curr_tl<Number)+1
		      EXIT IF Curr_tl= F_tl_num 
		   END LOOP
		END IF

		IF Do_components AND NOT All_face THEN   ! ONE TASK->MANY ZONES
			FOR Cpass= 1 TO Num_comps	
				Do_workzones(EntData,NextTool,Group,Data_name$,Data_name$[1;Inst_spot-1],Comp(Cpass),Last_point)
				IF No_tool THEN End_tool
			NEXT Cpass
		ELSE
		   IF Do_components THEN 	! MANY TASKS->ONE ZONE
			Do_workzones(EntData,NextTool,Group,Data_name$,Data_name$[1;Inst_spot-1],Comp(Big_pass),Last_point)
		   ELSE				! DO TASKS ONLY
			Tool_cycle(EntData,NextTool,Group,Data_name$,Data_name$&" group",Last_point,0,0)
		   END IF
		END IF
	ELSE	! If current task is an event
		Stat= GetUserString(EntData,"Event type:",Data_type$)
		Stat= GetUserString(EntData,"Event data:",Event_data$)
		IF Data_type$= "SCRIPT" THEN
			Dstr$= " EVENT INFO"&CHR$(10)
			Dstr$= Dstr$&" "&CHR$(10)
			Dstr$= Dstr$&"Start number: "&VAL$(K+1)&CHR$(10)
			Dstr$= Dstr$&"Startup: "&VAL$(Startup)&CHR$(10)
			Dstr$= Dstr$&"In post: "&VAL$(In_post)&CHR$(10)
			Edata= ReadUserData(WsGeneralString,"EVENT INFO",Dstr$)	
			IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
			IF ValidGeometry(Start_work) THEN Stat=SetWorkplane(Start_work)
			LOAD Event_data$
		ELSE
			Stat= OutputCmdBuff(Data_type$&": "&Event_data$,1)
			UpdateCmdFile
		END IF
	END IF

End_tool:No_tool=0
IF Do_components THEN Stat= DeleteEntity(Group)
NEXT K
NEXT Big_pass

End_codes:!
IF In_post THEN 	! Add the end of the program if it was opened.
	Stat=OutputCmdBuff("OUTPUT: SUBEND",1)
	UpdateCmdFile

	StdWorkplane
	Command_type=InterpretCmd("EXEC: ")	   ! EXECUTE Command File
	Stat=ListTapeFile("tapefile","tapelist",1) ! Make tapelist file

	Command_type=InterpretCmd("EXIT: ")	   ! EXIT Post
END IF

Leave:!
IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
IF ValidGeometry(Start_work) THEN Stat=SetWorkplane(Start_work)
IF (NOT LEN(Destination$)) THEN
         LOAD "typecontrol"
ELSE
         LOAD Destination$
END IF

END

SUB Do_newzero(Component)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe

! This calculates the rotary movement and new workplane position
! The line rotary_zero can be change to reflect a differnet work zone
! change MACRO.

WsPplane= 7746279
DIM Pne$[256],Hnum$[256],Namelist$[4000]

	Pplane= FindUserData(VAL$(Component),WsPplane)
	IF NOT ValidGeometry(Pplane) THEN SUBEXIT
        Group1= GrpByProp(Pplane,WsPplane)
        IF NOT ValidGeometry(Group1) THEN SUBEXIT
	IF NOT GroupCount(Group1) THEN SUBEXIT

	Hle= GroupElement(Group1,1)
	Hnum$= VAL$(HoleNumber(Hle))
        Stat= DeleteEntity(Group1)

	Stat=OutputCmdBuff("ASSIGN: Start_hole= "&Hnum$&"; Holestartpt",New_cb)
	Stat=OutputCmdBuff("ASSIGN: Zone_num= "&VAL$(Component)&";",0)
	Stat=OutputCmdBuff("MACRO: rotary_zero",0)
	New_cb= 0
SUBEND

SUB Do_cycle(EntData,Group,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$,Last_tool$

	SELECT Data_type$    ! Determines tool cycle based upon the tool type.
	CASE "Ball","Tree"
		Probing(EntData,Group,Last_point)
	CASE ELSE
		No_tool= 1
		SUBEXIT
	END SELECT
SUBEND

SUB Do_workzones(EntData,NextTool,Group,Data_name$,Base_name$,Component,Last_point)

DIM Dname$[256]

Dname$=Data_name$&" group Component "&VAL$(Component)	
Oldgroup=FindNamedGrp(Dname$)
Stat= DeleteEntity(Oldgroup)
Grp2= GrpByComponent(Group,Component)
IF ValidGeometry(Grp2) THEN
	Tool_cycle(EntData,NextTool,Grp2,Base_name$,Dname$,Last_point,Component,1)
END IF
SUBEND

SUB Tool_cycle(EntData,NextTool,Group,Data_name$,Dname$,Last_point,Component,Newzero)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$,Last_tool$

	IF Tl_ch OR S_up THEN
		Toolch(EntData,NextTool,Last_point)
		Tl_ch= 0
		New_cb=0
		Last_tool$= Data_name$
	ELSE
		New_cb=1
	END IF
	Stat= ChangeGrpName(Group,Dname$)
	IF Newzero THEN CALL Do_newzero(Component)
	Do_cycle(EntData,Group,Last_point)
	IF NOT No_tool THEN UpdateCmdFile
SUBEND

SUB Toolch(EntData,NextTool,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$,Last_tool$

DIM Data_id$[128],Tool_image$[128]
DIM Datestr$[256]

Stat=DateString(Datestr$)

Stat=GetUserString(EntData,"Probe id name:",Data_id$)  ! Get Probe Name
Stat=GetUserString(EntData,"Probe image:",Tool_image$) ! Get Animation Image Name
Probe_dia=GetUserValue(EntData,"Diameter:")	! Get Probe Diameter (Optional)
Probe_tip=GetUserValue(EntData,"Probe tip #:")	! Get Probe tip

Stat=OutputCmdBuff("ASSIGN: Probe_desc= """&Data_id$&""";",1)
Stat=OutputCmdBuff("ASSIGN: Tool_symbol= """&Tool_image$&""";",0)
Stat=OutputCmdBuff("ASSIGN: Probe_dia= "&VAL$(Probe_dia)&";",0)
Stat=OutputCmdBuff("ASSIGN: Probe_tip= "&VAL$(Probe_tip)&";",0)
IF S_up THEN		! For startup macro only
	Stat= OutputCmdBuff("ASSIGN: Part_number= ""1-1"";",0)
	Stat= OutputCmdBuff("ASSIGN: Revision= ""1"";",0)
	Stat= OutputCmdBuff("ASSIGN: Part_name= ""cmm example"";",0)
	Stat= OutputCmdBuff("ASSIGN: Hp_number= ""1"";",0)
	Stat= OutputCmdBuff("ASSIGN: Programmer= ""Weber Systems"";",0)
	Stat= OutputCmdBuff("ASSIGN: Date_str= """&Datestr$&""";",0)
	Stat= OutputCmdBuff("ASSIGN: Xstart1= 0",0)
	Stat= OutputCmdBuff("ASSIGN: Ystart1= 0",0)
	Stat= OutputCmdBuff("ASSIGN: Zstart1= 0",0)
	Stat= OutputCmdBuff("ASSIGN: Prog_desc= ""program description"";",0)
	Stat= OutputCmdBuff("ASSIGN: Prog_number= ""CMM"";",0)
	Last_point= PointXYZ(0,0,0)	! Set to zero. Can be any X,Y,Z Values.
END IF
	Stat= OutputCmdBuff("TOOLSYMBOL:",0)

IF Do_safe THEN
	Stat=OutputCmdBuff("ASSIGN: Tool_ch= 1;",0)
END IF

IF S_up THEN
	Stat= OutputCmdBuff("MACRO: startup",0)	
ELSE
	Stat= OutputCmdBuff("MACRO: probech",0)	
END IF
S_up= 0
SUBEND

SUB Probing(EntData,Group,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$[128],Last_tool$[256]

DIM Data_name$[128],Dtype$[128]
DIM Command$[256]
DIM Quote$[2]
DIM Plane_name$[256]

WsCmmNavigateGrp=21396
WsCmmCircleInfo=21496
Quote$= CHR$(34)

! PathsGrp= FindGrpsByEle(Group,WsContourGroup)
! IF ValidGeometry(PathsGrp) THEN
! 	Pcount=GroupCount(PathsGrp)
! END IF

OptimizeHoles(Group,Last_point)
Stat= GroupName(Group,Data_name$)

IF Do_safe THEN
	E1=GroupElement(Group,1)
	Hnum=HoleNumber(E1)
	Stat=OutputCmdBuff("ASSIGN: Start_hole= "&VAL$(Hnum)&";  Holestartpt",New_cb)
	Stat=OutputCmdBuff("MACRO: safe_start  Repeats= (Tool_ch<>0);",0)
	New_cb= 0
END IF
Stat= OutputCmdBuff("ASSIGN: Group_name= """&Data_name$&""";",New_cb)
Stat= OutputCmdBuff("ASSIGN: Groupon",0)

Xendr=999
Yendr=999
Zendr=999
Each_holer=0
Grp_count=GroupCount(Group)
FOR Each_hole=1 TO Grp_count
	Ent1=GroupElement(Group,Each_hole)
	Etype=EntityType(Ent1)
	Cont_grp=0
	C_grp=ParentGroup(Ent1,WsContourGroup)
	IF ValidGeometry(C_grp) THEN Cont_grp=1
	Navigate_only=0
	P_grp=ParentGroup(Ent1,WsCmmNavigateGrp)
	IF ValidGeometry(P_grp) THEN Navigate_only=1
	SELECT Etype
	CASE WsPointEnt
PRINT "POINT ENTITY"
	CASE WsHoleEnt
        	Hdepth= EntityValue(Ent1,WsHoleDepth)
		Stat= OutputCmdBuff("ASSIGN: Num_points=5;",0)
		Stat= OutputCmdBuff("ASSIGN: Z_probe_dist="&VAL$(Hdepth/4)&";",0)
		Stat= OutputCmdBuff("ASSIGN: Clearance=.078;",0)
		Stat= OutputCmdBuff("ASSIGN: Rep_format_no=1;",0)
		Stat= OutputCmdBuff("ASSIGN: Plot_pen=6;",0)
		Stat=OutputCmdBuff("ASSIGN: Start_hole= "&VAL$(Each_hole)&";  Holestartpt",New_cb)
		Stat= OutputCmdBuff("MACRO: hcenterfind",0)

	CASE WsLineEnt
	IF Navigate_only THEN
		PlotOff
		Pstart=EndPoint(Ent1,WsStart)
		Xstart= XPointValue(Pstart)
		Ystart= YPointValue(Pstart)
		Zstart= ZPointValue(Pstart)
		IF Each_holer+1<>Each_hole OR (Each_holer+1=Each_hole AND ABS(Xtart-Xendr)>.00001 AND ABS(Ystart-Yendr)>.00001 AND ABS(Zstart-Zendr)>.00001) THEN
			Stat= OutputCmdBuff("ASSIGN: Xstart="&VAL$(Xstart)&";",0)
			Stat= OutputCmdBuff("ASSIGN: Ystart="&VAL$(Ystart)&";",0)
			Stat= OutputCmdBuff("ASSIGN: Zstart="&VAL$(Zstart)&";",0)
			Stat= OutputCmdBuff("MACRO: moveprobe",0)
		END IF
		Pend=EndPoint(Ent1,WsEnd)
		Xend= XPointValue(Pend)
		Yend= YPointValue(Pend)
		Zend= ZPointValue(Pend)
		Stat= OutputCmdBuff("ASSIGN: Xstart="&VAL$(Xend)&";",0)
		Stat= OutputCmdBuff("ASSIGN: Ystart="&VAL$(Yend)&";",0)
		Stat= OutputCmdBuff("ASSIGN: Zstart="&VAL$(Zend)&";",0)
		Stat= OutputCmdBuff("MACRO: moveprobe",0)
		Stat=DeleteEntity(Pstart)
		Stat=DeleteEntity(Pend)
		Xendr=Xend
		Yendr=Yend
		Zendr=Zend
		Each_holer=Each_hole
		PlotOn
	ELSE
		Stat= OutputCmdBuff("ASSIGN: Feature_name=""2d line"";",0)
		Stat= OutputCmdBuff("ASSIGN: Approach_dist=.25;",0)
		Stat= OutputCmdBuff("ASSIGN: Z_probe_dist=.1;",0)
		Stat= OutputCmdBuff("ASSIGN: Plot_pen=3;",0)
		PlotOff
		Ps=ContStartPt(Ent1)
		Perp_line=Perpendicular(Ent1,Ps,WsUnTrimmed)
		V1= LineVector(Perp_line)
		Normal_vector= UnitVector(V1)
		Stat= DeleteEntity(V1)
		Ps_perp=ContStartPt(Perp_line)
		Stat= DeleteEntity(Perp_line)
	        Nsign= -1
		Xnormal= Nsign*XPointValue(Normal_vector)
		Ynormal= Nsign*YPointValue(Normal_vector)
		Znormal= Nsign*ZPointValue(Normal_vector)
		Stat=DeleteEntity(Ps)

		IF ABS(Xnormal)>= ABS(Ynormal) THEN
			IF Xnormal> 0 THEN
				Probe_comp$= "+X" 
			ELSE
				Probe_comp$= "-X" 
			END IF
		ELSE
			IF Ynormal> 0 THEN
				Probe_comp$= "+Y" 
			ELSE
				Probe_comp$= "-Y" 
			END IF
		END IF

		Stat= DeleteEntity(Ps_perp)
		Stat= DeleteEntity(Normal_vector)

		Num_points=2
		Tmp_group=CreateGroup("TEMP")
		P1=EndPoint(Ent1,WsStart)
		P2=EndPoint(Ent1,WsEnd)
		Stat= AddToGroup(Tmp_group,P1)
		Stat= AddToGroup(Tmp_group,P2)

		Stat=OutputCmdBuff("MACRO: cb_clear",0)
		Command$="ASSIGN: Probe_comp="&Quote$&Probe_comp$&Quote$&";"
		Stat=OutputCmdBuff(Command$,0)

		FOR I=1 TO Num_points
			P1= GroupElement(Tmp_group,I)
			X1= XPointValue(P1)
			Y1= YPointValue(P1)
			Z1= ZPointValue(P1)
			IF I=1 THEN
				Command$="ASSIGN: Xstart= "&VAL$(X1)&"; Ystart= "&VAL$(Y1)&"; Zstart= Z;"
				Stat=OutputCmdBuff(Command$,0)
				Command$="ASSIGN: Xnormal= "&VAL$(Xnormal)&"; Ynormal= "&VAL$(Ynormal)&"; Znormal= "&VAL$(Znormal)&";"
				Stat=OutputCmdBuff(Command$,0)
				Stat=OutputCmdBuff("MACRO: xyapproach",0)
			END IF
			Command$="ASSIGN: Xstart= "&VAL$(X1)&"; Ystart= "&VAL$(Y1)&"; Zstart= "&VAL$(Z1)&";"
			Stat=OutputCmdBuff(Command$,0)
			Command$="ASSIGN: Xnormal= "&VAL$(Xnormal)&"; Ynormal= "&VAL$(Ynormal)&"; Znormal= "&VAL$(Znormal)&";"
			Stat=OutputCmdBuff(Command$,0)
			IF I=1 THEN
				Command$="MACRO: sum_touch" 
			ELSE
				Command$="MACRO: sum_touch_next" 
			END IF
			Stat=OutputCmdBuff(Command$,0)
		NEXT I
		Stat=OutputCmdBuff("MACRO: bf_line2",0)

		IF  ValidGeometry(Tmp_group) THEN 
			Stat= DeleteGroup(Tmp_group,1)
		END IF
		PlotOn
	END IF

	CASE WsCircleEnt
		Stat= OutputCmdBuff("ASSIGN: Feature_name=""2d circle"";",0)
		Stat= OutputCmdBuff("ASSIGN: Approach_dist=.25;",0)
		Stat= OutputCmdBuff("ASSIGN: Z_probe_dist= .1;",0)
		Stat= OutputCmdBuff("ASSIGN: Plot_pen=3;",0)
		Stat= OutputCmdBuff("ASSIGN: Clearance= .078;",0)
		Stat= OutputCmdBuff("ASSIGN: Rep_format_no= 2;",0)
		PlotOff

		CircleDir=GetAssocEnt(Ent1,WsCmmCircleInfo)
		IF ValidGeometry(CircleDir) THEN
			Direction=GetUserValue(CircleDir,"DIRECTION:")
		ELSE
			Direction=1
		END IF
		Radius=CircleRadius(Ent1)
		Nsign= -1*SGN(Radius)
		Radius= ABS(Radius)

		IF Radius<.00001 THEN Leave_circle
		IF Nsign> 0 THEN
			Probe_comp$= "EXT"
			Retract_on=1
		ELSE
			Probe_comp$= "INT"
			Retract_on=0
		END IF

		Num_points=3

		IF Retract_on THEN Z_retract=.1

		Tmp_group=CreateGroup("TEMP")
		P1=ParameterPt(Ent1,0.0)
		P2=ParameterPt(Ent1,0.33)
		P3=ParameterPt(Ent1,0.67)
		Stat= AddToGroup(Tmp_group,P1)
		Stat= AddToGroup(Tmp_group,P2)
		Stat= AddToGroup(Tmp_group,P3)
		

		Stat=OutputCmdBuff("MACRO: cb_clear",0)
		Command$="ASSIGN: Probe_comp="&Quote$&Probe_comp$&Quote$&";"
		Stat=OutputCmdBuff(Command$,0)
		Pc= CircleCenter(Ent1)
		X_center= XPointValue(Pc)
		Y_center= YPointValue(Pc)
		Z_center= ZPointValue(Pc)
		Stat=OutputCmdBuff("ASSIGN: X_center="&VAL$(X_center)&";",0)
		Stat=OutputCmdBuff("ASSIGN: Y_center="&VAL$(Y_center)&";",0)
		Stat=OutputCmdBuff("ASSIGN: Z_center="&VAL$(Z_center)&";",0)
		Stat=OutputCmdBuff("ASSIGN: Diameter="&VAL$(2*Radius)&";",0)

		FOR I=1 TO Num_points
			P1= GroupElement(Tmp_group,I)
			X1= XPointValue(P1)
			Y1= YPointValue(P1)
			Z1= ZPointValue(P1)
			L1=LineTwoPoints(Pc,P1,WsTRIMMED)
			V1= LineVector(L1)
			Normal_vector= UnitVector(V1)

			Xnormal= Nsign*XPointValue(Normal_vector)
			Ynormal= Nsign*YPointValue(Normal_vector)
			Znormal= Nsign*ZPointValue(Normal_vector)

			Stat= DeleteEntity(L1)
			Stat= DeleteEntity(V1)
			Stat= DeleteEntity(Normal_vector)
			IF I=1 OR Retract_on THEN
				Command$="ASSIGN: Xstart= "&VAL$(X1)&"; Ystart= "&VAL$(Y1)&"; Zstart= Z;"
				Stat=OutputCmdBuff(Command$,0)
				Command$="ASSIGN: Xnormal= "&VAL$(Xnormal)&"; Ynormal= "&VAL$(Ynormal)&"; Znormal= "&VAL$(Znormal)&";"
				Stat=OutputCmdBuff(Command$,0)
				Command$="MACRO: xyapproach" 
				Stat=OutputCmdBuff(Command$,0)
			END IF

			Command$="ASSIGN: Xstart= "&VAL$(X1)&"; Ystart= "&VAL$(Y1)&"; Zstart= "&VAL$(Z1)&";"
			Stat=OutputCmdBuff(Command$,0)
			Command$="ASSIGN: Xnormal= "&VAL$(Xnormal)&"; Ynormal= "&VAL$(Ynormal)&"; Znormal= "&VAL$(Znormal)&";"
			Stat=OutputCmdBuff(Command$,0)
			IF I=1 THEN
				Command$="MACRO: sum_touch_cir1" 
			ELSE
				Command$="MACRO: sum_touch_cir2" 
			END IF
			Stat=OutputCmdBuff(Command$,0)
			IF Retract_on THEN
				Command$="ASSIGN: Retract_z= "&VAL$(Z_retract)&";"
				Stat=OutputCmdBuff(Command$,0)
				Command$="MACRO: zretract" 
				Stat=OutputCmdBuff(Command$,0)
			END IF

		NEXT I
		Stat=OutputCmdBuff("MACRO: bf_circle2",0)
		Stat= DeleteEntity(Pc)

		IF  ValidGeometry(Tmp_group) THEN 
			Stat= DeleteGroup(Tmp_group,1)
		END IF
Leave_circle: PlotOn

	CASE WsPlaneSolidEnt
		PlotOff

		Stat=ReadEntityName(Ent1,Plane_name$)
		Pt_grp=FindNamedGrp(Plane_name$)
		Pt_count=GroupCount(Pt_grp)

		IF Pt_count<>0 THEN
			P1=GroupElement(Pt_grp,1)
			P2=GroupElement(Pt_grp,2)
			P3=GroupElement(Pt_grp,3)
		ELSE
			P1=SurfParameterPt(Ent1,.25,.25)
			P2=SurfParameterPt(Ent1,.5,.75)
			P3=SurfParameterPt(Ent1,.67,.3)
		END IF

		T1=ThreePtTrans(P1,P2,P3)
		IF NOT ValidGeometry(T1) THEN
			PRINT "INVALID PLANE"
			Stat= DeleteEntity(P1)
			Stat= DeleteEntity(P2)
			Stat= DeleteEntity(P3)
		END IF

		X1= XPointValue(P1)
		Y1= YPointValue(P1)
		Z1= ZPointValue(P1)

		X2= XPointValue(P2)
		Y2= YPointValue(P2)
		Z2= ZPointValue(P2)

		X3= XPointValue(P3)
		Y3= YPointValue(P3)
		Z3= ZPointValue(P3)

		IF Z1<0 THEN
			Command$="ASSIGN: Xmove= "&VAL$(X1)&"; Ymove= "&VAL$(Y1)&";"
			Stat=OutputCmdBuff(Command$,0)
			Stat=OutputCmdBuff("MACRO: moveprobe_xy",0)
		END IF
		Stat= OutputCmdBuff("ASSIGN: Feature_name="""&Plane_name$&""";",0)
		Stat= OutputCmdBuff("ASSIGN: Approach_dist= .25;",0)
		Stat= OutputCmdBuff("ASSIGN: Plot_pen=5;",0)

		Vector= PointXyz(0,0,1)
		Normal_vector= TransformVector(Vector,T1)
		Xnormal= XPointValue(Normal_vector)
		Ynormal= YPointValue(Normal_vector)
		Znormal= ZPointValue(Normal_vector)
		Nsign= (Znormal>0)- (Znormal<0)

		Xnormal= Nsign*Xnormal
		Ynormal= Nsign*Ynormal
		Znormal= Nsign*Znormal
		IF Pt_count=0 THEN
			Stat= DeleteEntity(P1)
			Stat= DeleteEntity(P2)
			Stat= DeleteEntity(P3)
		END IF
		Stat= DeleteEntity(Normal_vector)
		Stat= DeleteEntity(Vector)
		Stat= DeleteEntity(Pt)
		Stat= DeleteEntity(T1)

		IF ((ABS(Znormal)< ABS(Xnormal)) OR (ABS(Znormal)< ABS(Ynormal))) THEN
			IF ABS(Xnormal)>= ABS(Ynormal) THEN
		       		 IF Xnormal> 0 THEN
		       		         Probe_comp$= "+X"
		       		 ELSE
		       		         Probe_comp$= "-X"
		       		 END IF
			ELSE
		       		 IF Ynormal> 0 THEN
		       		         Probe_comp$= "+Y"
		       		 ELSE
		       		         Probe_comp$= "-Y"
		       		END IF
			END IF
		ELSE
		      	IF Znormal> 0 THEN
		       	         Probe_comp$= "+Z"
		       	ELSE
		       	         Probe_comp$= "-Z"
		       	END IF
		END IF

		Command$="ASSIGN: Probe_comp="&Quote$&Probe_comp$&Quote$&";"
		Stat=OutputCmdBuff(Command$,0)
		Command$="ASSIGN: X1= "&VAL$(X1)&"; Y1= "&VAL$(Y1)&"; Z1= "&VAL$(Z1)&";"
		Stat=OutputCmdBuff(Command$,0)
		Command$="ASSIGN: X2= "&VAL$(X2)&"; Y2= "&VAL$(Y2)&"; Z2= "&VAL$(Z2)&";"
		Stat=OutputCmdBuff(Command$,0)
		Command$="ASSIGN: X3= "&VAL$(X3)&"; Y3= "&VAL$(Y3)&"; Z3= "&VAL$(Z3)&";"
		Stat=OutputCmdBuff(Command$,0)
		Command$="ASSIGN: Xnormal= "&VAL$(Xnormal)&"; Ynormal= "&VAL$(Ynormal)&"; Znormal= "&VAL$(Znormal)&";"
		Stat=OutputCmdBuff(Command$,0)

		Stat=OutputCmdBuff("MACRO: plane3pts",0)
		IF Z3<0 THEN
			Command$="ASSIGN: Zmove= Approach_dist;"
			Stat=OutputCmdBuff(Command$,0)
			Stat=OutputCmdBuff("MACRO: moveprobez",0)
		END IF

Leave_plane: PlotOn

	CASE WsContourGroup
		PRINT "THIS IS A CONTOUR GROUP"

	END SELECT
NEXT Each_hole

Stat= OutputCmdBuff("ASSIGN: Groupoff",0)

Number= GroupCount(Group)
E1= GroupElement(Group,Number)
Last_point= ParameterPt(E1,0)
SUBEND

SUB OptimizeHoles(Group,Last_point)
Optimize_holes= NamedVarVal("optimize amo",WsSystemVars,WsTrue)
Opt_factor= 10

IF Optimize_holes THEN
	Stat= DisplayMessage("OPTIMIZING HOLES  factor: "&VAL$(Opt_factor))
! Both optimize routines works well together
!	Stat= ReOrderPtGrp(Group,90,WsForward,WsReverse)
	Stat= OptimizePointPath(Group,Last_point,Opt_factor)
END IF
IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
SUBEND
