
! script name: turbopost.d/cmm_cir_cent

!A
!A NAME:
!A
!A 	CMM FIND CIRCLE CENTER
!A
!A DESCRIPTION:
!A
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: find circle center ")

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

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

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"
ELSE
	Probe_comp$= "INT"
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

Pc= CircleCenter(E_cir)
Xc= XPointValue(Pc)
Yc= YPointValue(Pc)
Zc= ZPointValue(Pc)
Stat= DeleteEntity(Pc)
Starting_angle= EntityValue(E_cir,WsStartAngle)
Angle_extent= EntityValue(E_cir,WsAngleExtent)
! Clearance_level= Zc+ 0.100


Command$="ASSIGN: Probe_comp="&Quote$&Probe_comp$&Quote$&";"
Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Diameter="&VAL$(2*Radius)&";"
Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Starting_angle="&VAL$(Starting_angle)&";"
Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Angle_extent="&VAL$(Angle_extent)&";"
Stat=OutputCmdBuff(Command$,0)
! Command$="ASSIGN: Clearance="&VAL$(Clearance_level)&";"
! Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Num_points="&VAL$(Num_points)&";"
Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Inside="&VAL$(-Nsign)&";"
Stat=OutputCmdBuff(Command$,0)

Xnormal= 0.0
Ynormal= 0.0
Znormal= 1.0
Command$="ASSIGN: Xstart= "&VAL$(Xc)&"; Ystart= "&VAL$(Yc)&"; Zstart= "&VAL$(Zc)&";"
Stat=OutputCmdBuff(Command$,0)
Command$="ASSIGN: Xnormal= "&VAL$(Xnormal)&"; Ynormal= "&VAL$(Ynormal)&"; Znormal= "&VAL$(Znormal)&";"
Stat=OutputCmdBuff(Command$,0)
Command$="MACRO: ccenterfind" 
Stat=OutputCmdBuff(Command$,0)


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 (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
	
END
