
! script name: turbopost.d/cmm_circle2

!A
!A NAME:
!A
!A 	CMM BEST FIT CIRCLE 2D
!A
!A DESCRIPTION:
!A
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: best fit 2d circle ")

DIM Destination$[256]
DIM Answer$[256]
DIM Command$[256]
DIM Command1$[256]
DIM Name$[256]
DIM Var_name$[128]
DIM Vname$[128]
DIM Quote$[2]

DeActivateUndo
Quote$= CHR$(34)
Units= CurrentUnits

Stat=MakeActiveCurr
Stat=CurrActionName(Command1$)

Normal_len=NamedVarVal("Normal Length",WsSystemVars,1.0)
Forward_color= 3
Reverse_color= 4
Start:!
E_cir=PickSpecificEnt("PICK CIRCLE TO BE MEASURED",WsPickCircles)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(E_cir) THEN Start

T= LastPickPara
Pc= CircleCenter(E_cir)
Pt= ParameterPt(E_cir,T)
L1= LineTwoPoints(Pc,Pt)
V1= LineVector(L1)
Normal_vector= UnitVector(V1)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(V1)
Stat= DeleteEntity(L1)

GOSUB Orient_normal

Radius= ABS(CircleRadius(E_cir))
IF Nsign> 0 THEN
	Probe_comp$= "EXT"
	Retract_on=1
ELSE
	Probe_comp$= "INT"
	Retract_on=0
END IF
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Normal_vector)

Stat=SetInputFormat("n0=","")
Stat= SetEqualsValue(3)
Num_points=GetValue("NUMBER OF POINTS")
IF (ExitScript(Destination$)) THEN Leave
IF Num_points< 3 THEN Start

IF Retract_on THEN
	Stat=SetInputFormat("n4=","")
	Stat= SetEqualsValue(Z_retract)
	Z_retract=GetValue("Z RETRACT LEVEL")
	IF (ExitScript(Destination$)) THEN Leave
END IF

Group=CreateGroup("TEMP")
Pick_points:!

FOR I=1 TO Num_points

Pick_point:!
	Stat=SetInputFormat("n4","")
	P1=XyzInput("INPUT POINT #"&VAL$(I)&" ",WsXyzInput)
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT ValidGeometry(P1) THEN Pick_point

	Pc= CircleCenter(E_cir)
	Dist= DistTwoPts(Pc,P1)
	Stat= DeleteEntity(Pc)
	IF ABS(Dist-Radius)> 0.001 THEN 
		Stat= DeleteEntity(P1)
		GOTO Pick_point
	END IF
	Stat= AddToGroup(Group,P1)
NEXT I


Command$="MACRO: cb_clear"
Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Probe_comp="&Quote$&Probe_comp$&Quote$&";"
Stat=OutputCmdBuff(Command$,0)
Pc= CircleCenter(E_cir)
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(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
Command$="MACRO: bf_circle2" 
Stat=OutputCmdBuff(Command$,0)
Stat= DeleteEntity(Pc)

IF  ValidGeometry(Group) THEN 
	Stat= DeleteGroup(Group,1)
END IF


New_command:UpdateCmdFile
	Stat=SetPickMask(WsPickAll)
	Stat=SetInputFormat("n4=","")
	LOAD "turbopost.d/turbocmd"


Orient_normal:! Pass down Normal_vector and Pt
        GOSUB ShowNormalDir
Pick_normal:Pline1=PickSpecificEnt("PICK WHICH SIDE OF CIRCLE",WsPickLines)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Pline1) THEN Line_loop
        Pt1= ParameterPt(Pline1,1.0)
        d1= DistTwoPts(Pt1,Pforward)
        d2= DistTwoPts(Pt1,Preverse)
        Nsign= 1
        IF D2<D1 THEN Nsign= -1
        IF (NOT Nsign) THEN Pick_normal
stat= DeleteEntity(Forward_normal)
stat= DeleteEntity(Reverse_normal)
stat= DeleteEntity(Pt1)
stat= DeleteEntity(Pforward)
stat= DeleteEntity(Preverse)
RETURN


ShowNormalDir:!  Pass down Normal_vector and Pt
        PlotOff
        N1= ScaleVector(Normal_vector,Normal_len)
        Pforward= AddVectors(Pt,N1)
        Forward_normal= LineTwoPoints(Pt,Pforward)
        Stat=SetColor(Forward_normal,Forward_color)
        Stat=SetColor(Pforward,Forward_color)
        Preverse= SubtractVectors(Pt,N1)
        Reverse_normal= LineTwoPoints(Pt,Preverse)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
        stat= DeleteEntity(V1)
        stat= DeleteEntity(N1)
        PlotOn
        stat= PlotEntity(Forward_normal)
        stat= PlotEntity(Pforward)
        stat= PlotEntity(Reverse_normal)
        stat= PlotEntity(Preverse)
RETURN




Leave:!
stat= DeleteEntity(Forward_normal)
stat= DeleteEntity(Reverse_normal)
stat= DeleteEntity(Pt1)
stat= DeleteEntity(Pforward)
stat= DeleteEntity(Preverse)
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(L1)
Stat= DeleteEntity(V1)
Stat= DeleteEntity(Normal_vector)
IF  ValidGeometry(Group) THEN
        Stat= DeleteGroup(Group,1)
END IF

IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
	
END
