
! script name: wscturn.d/dynvmanturn
! var_topic: variational manual turn

!A
!A NAME:
!A
!A 	Manual Turn  
!A
!A DESCRIPTION:
!A
!A	This script generates a lathe contour by moving a tool from 
!A	point to point.
!A
!B INSTRUCTIONS:
!B
!B	TOOLS:  Select the tool for the cycle. The Tool must have an image.
!B
!B	Id or Od cycle: Specify if the cycle is Id or Od.
!B
!C NOTES:
!C
!C	This cycle positions the Tool Tip Tangent Location of the turning
!C	tool. Since contours in turbopost need tool nose center locations
!C	the final contour is shifted based on the tool quadrant and
!C	the tool nose radius.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: manual turn")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Point$[256]
DIM Name$[256]
INTEGER Ref_array(200,2)
REAL Param_array(200)

Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
        Var_object=OpenVarObject("Program")
END IF
! Var_object= CheckVarObject
! IF (ExitScript(Destination$)) THEN Leave

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)
Max_elements= 200

Current_pen=CurrentColor
Stat= SetCurrentColor(4)

IF ARGC>1 THEN
        Answer$= ARGV(1)
        GOTO Have_tool
END IF

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN
  LOAD "wscturn.d/readlathetool:wscturn.d/dynvmanturn"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL, New_tool ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
        LowerSelectBox
        GOTO Leave
END IF
IF Answer$="New_tool" THEN
        LowerSelectBox
        LOAD "wscturn.d/readlathetool:wscturn.d/dynvmanturn"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)
IF NOT ValidGeometry(Tool) THEN Start
Tool_plot=0
Stat=GetUserString(Tool,"Tool image:",Tool_image$)
Tool_group=FindNamedGrp(Tool_image$)
IF ValidGeometry(Tool_group) THEN Tool_plot=1

Contour= CreateGroup("MANUAL TURN CYCLE",WsContourGroup)
Pt_refs= CreateGroup("Point references",WsTempGroup)
Stat= AssociateTool(Contour,Tool)

PlotOff
Trans1= CurrentWorkplane
IF NOT ValidGeometry(Trans1) THEN
	Work_trans= IdentityTrans
ELSE
	Work_trans= OrientationTrans(Trans1)
END IF
Identity_trans= IdentityTrans
PlotOn

Start_point:!
ActivateUndo
Element_num=GroupCount(Pt_refs)+1
IF Element_num>= Max_elements THEN Leave
Stat=SetInputFormat("n4","")
Stat=SetXyzInput(WsLatheInput)
Pstart=XyzInput("START POINT",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Pstart) THEN Start_point
Stat=SetLastPoint(Pstart,WsXyzInput)

Stat= SetSelectableFlag(Pstart,WsFalse)
Element= LastPickEnt
Para= LastPickPara
Pick_return= CurrPickReturn
IF NOT ValidDependency(Element) THEN
        Element= CopyEntity(Pstart,WsFalse)
	Stat= SetSelectableFlag(Element,WsTrue)
        Stat= EntityNameCheck(Element,Point$)
	Ref_array(Element_num,1)= 0
	Ref_array(Element_num,2)= 0
	Stat= AddToGroup(Pt_refs,Element)
ELSE
        Stat= EntityNameCheck(Element,Point$)
	Ref_array(Element_num,1)= 1
	Ref_array(Element_num,2)= Pick_return
	Param_array(Element_num)= Para
	Stat= AddToGroup(Pt_refs,Element)
END IF
Stat=SetLastPoint(Pstart)



IF Tool_plot THEN
	CALL PlotToolImage(Tool_group,Pstart,Work_trans)
END IF



Pick_next_pt:!
PlotOff
FadeUndoButtons
IF Tool_plot THEN 
	Graph_tool= CopyGroup(Tool_group,WsFalse)
ELSE
	Graph_tool= CreateGroup("DUMMY TOOL")
	P1= PointXYZ(0,0,0)
	P2= PointXYZ(.1,0,0)
	P3= PointXYZ(0,.1,0)
	L1= LineTwoPoints(P1,P2)
	L2= LineTwoPoints(P1,P3)
	Stat= AddToGroup(Graph_tool,L1)
	Stat= AddToGroup(Graph_tool,L2)
	Stat= DeleteEntityList(P1,P2,P3)
END IF
Shift_trans=AngleTransform(Pstart,0,0,0,WsRealSpaceVals)
IF ValidGeometry(Trans1) THEN
	Trans1=MultTransforms(Shift_trans,Work_trans)
	Stat=TransGroupTrans(Graph_tool,Trans1)
ELSE
        Stat=TransGroupTrans(Graph_tool,Shift_trans)
END IF
Stat= MakeInvisible(Graph_tool)
Pnext= CopyEntity(Pstart,WsFalse)
Stat= SetSelectableFlag(Pnext,WsFalse)
Vnum= LastPickView
Erase_original= WsFalse
stat= SetInputFormat("n4b","")
Stat=SetPickMask(WsPickNoSurf)
Stat= MakeInvisible(Graph_tool)
Stat= MakeInvisible(Pnext)
Stat= MakeInvisible(Pstart)
PlotOn
Rotate_angle= DynamicMoveGroup(Vnum,Graph_tool,Pstart,Pnext,0.0,"PICK NEXT POINT",Erase_original,WsFalse,WsTrue,0.0)
PlotOff
IF Tool_plot THEN Stat= DeleteGroup(Graph_tool,WsTrue)
Stat= DeleteEntity(Shift_trans)
PlotOn
IF ExitScript(Destination$) THEN Leave

Element= LastPickEnt
Para= LastPickPara
Pick_return= CurrPickReturn

Stat= SetSelectableFlag(Pnext,WsFalse)
Stat=SetLastPoint(Pnext,WsXyzInput)
Dist= DistTwoPts(Pstart,Pnext)
IF Dist< 0.0001 THEN 
	Stat= SetSelectableFlag(Pnext,WsTrue)
	Stat=DeleteEntity(Pnext)
	GOTO Pick_next_pt
END IF

Element_num= GroupCount(Pt_refs)+1
IF NOT ValidDependency(Element) THEN
        Element= CopyEntity(Pnext,WsFalse)
	Stat= SetSelectableFlag(Element,WsTrue)
	Stat= MakeVisible(Element)
        Stat= EntityNameCheck(Element,Point$)
        Ref_array(Element_num,1)= 0
        Ref_array(Element_num,2)= 0
        Stat= AddToGroup(Pt_refs,Element)
ELSE
        Stat= EntityNameCheck(Element,Point$)
        Ref_array(Element_num,1)= 1
        Ref_array(Element_num,2)= Pick_return
        Param_array(Element_num)= Para
        Stat= AddToGroup(Pt_refs,Element)
END IF

Stat= SetSelectableFlag(Contour,WsFalse)
Stat= SetSelectedFlag(Contour,WsTrue)
Stat= PlotEntity(Contour)
CALL MoveToNextPoint(Tool,Tool_group,Contour,Pstart,Pnext,Tool,Work_trans,Identity_trans,Tool_plot)
Stat= SetSelectableFlag(Pstart,WsTrue)
Stat=DeleteEntity(Pstart)
Pstart= CopyEntity(Pnext)
Stat=DeleteEntity(Pnext)


Control_ques:!
Stat= SetSelectableFlag(Contour,WsFalse)
Stat= SetSelectedFlag(Contour,WsTrue)
Stat= PlotEntity(Contour)
Stat=SetInputFormat("a1=",",A,N,P,B,E,R,L,")
Stat= SetEqualsValue(0,"P")
Stat=GetValue("Point, Location, Backup, New tool, Exit ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "A"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
		Stat= AnimateImage(Tool_group,Contour,Identity_trans,1,1,1)
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
	END IF
	GOTO Control_ques
CASE "N"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
		DetachGroupImage
	END IF
	PlotOff
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat= DeleteEntity(Pstart)
	Stat= DeleteGroup(Graph_tool,WsTrue)
	PlotOn
	GOSUB Make_record
	Stat= SetSelectableFlag(Contour,WsTrue)
	Stat= DeleteGroup(Contour,WsTrue)
        Stat= DeleteEntity(Pt_refs)
        Contour= CreateGroup("MANUAL PATH",WsContourGroup)
        Pt_refs= CreateGroup("Point references",WsTempGroup)
        Stat= AssociateTool(Contour,Tool)
	GOTO Start
CASE "E"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
	END IF
	DetachGroupImage
	PlotOff
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat= DeleteEntity(Pstart)
	Stat= DeleteGroup(Graph_tool,WsTrue)
	PlotOn
	GOSUB Make_record
	GOTO Start
CASE "P"
	GOTO Pick_next_pt
CASE "B"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
	END IF
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat= DeleteEntity(Pstart)
	N1= GroupCount(Contour)
	Last_element= GroupElement(Contour,N1)
	Stat= RemoveFromGrp(Contour,N1)
	Stat= SetSelectableFlag(Last_element,WsTrue)
	Stat= DeleteEntity(Last_element)
	N1= GroupCount(Contour)

	N2= GroupCount(Pt_refs)
        IF N2 THEN
        	Element= GroupElement(Pt_refs,N2)
                Stat= RemoveFromGrp(Pt_refs,N2)
                IF Ref_array(N2,1)=0 THEN
			Stat= SetSelectableFlag(Element,WsTrue)
                        Stat= DeleteEntity(Element)
                END IF
                Ref_array(N2,1)=0
                Ref_array(N2,2)=0
                Param_array(N2)=0
                Element_num= GroupCount(Pt_refs)+1
        END IF
	IF N1= 0 THEN 
		Stat= SetSelectableFlag(Contour,WsTrue)
		Stat= DeleteGroup(Contour,WsTrue)
		Stat= DeleteEntity(Pt_refs)
		Contour= CreateGroup("MANUAL PATH",WsContourGroup)
		Pt_refs= CreateGroup("Point references",WsTempGroup)
		Stat= AssociateTool(Contour,Tool)
		GOTO Start_point
	END IF
	Last_element= GroupElement(Contour,N1)
	Pstart= ParameterPt(Last_element,1.0)
	!Stat= SetSelectableFlag(Pstart,WsFalse)
	Stat=SetLastPoint(Pstart,WsXyzInput)
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
	END IF
	GOTO Control_ques
CASE "R"
	Stat=RulerCenter(Pstart)
	Stat= RulerToggle(1)
CASE "L"
	Stat=SetInputFormat("n4","")
	Pnext=XyzInput("NEXT POINT",WsXyOnly)
	IF (ExitScript(Destination$)) THEN Leave

	!Stat= SetSelectableFlag(Pstart,WsFalse)

	Element= LastPickEnt
	Para= LastPickPara
	Pick_return= CurrPickReturn

	Dist= DistTwoPts(Pstart,Pnext)
	IF Dist< 0.0001 THEN
		Stat= SetSelectableFlag(Pnext,WsTrue)
		Stat=DeleteEntity(Pnext)
		GOTO Control_ques
	END IF
	Element_num= GroupCount(Pt_refs)+1
	IF NOT ValidDependency(Element) THEN
       		 Element= CopyEntity(Pnext,WsFalse)
		Stat= SetSelectableFlag(Element,WsTrue)
       		 Stat= MakeVisible(Element)
       		 Stat= EntityNameCheck(Element,Point$)
       		 Ref_array(Element_num,1)= 0
       		 Ref_array(Element_num,2)= 0
       		 Stat= AddToGroup(Pt_refs,Element)
	ELSE
       		 Stat= EntityNameCheck(Element,Point$)
       		 Ref_array(Element_num,1)= 1
       		 Ref_array(Element_num,2)= Pick_return
       		 Param_array(Element_num)= Para
       		 Stat= AddToGroup(Pt_refs,Element)
	END IF

	CALL MoveToNextPoint(Tool,Tool_group,Contour,Pstart,Pnext,Tool,Work_trans,Identity_trans,Tool_plot)
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat=DeleteEntity(Pstart)
	Pstart= CopyEntity(Pnext)
	Stat=DeleteEntity(Pnext)
END SELECT
GOTO Control_ques


Make_record:!
Stat= SetSelectableFlag(Contour,WsTrue)
Stat= DeleteGroup(Contour,WsTrue)
IF NOT ValidGeometry(Pt_refs) THEN RETURN
IF NOT GroupCount(Pt_refs) THEN RETURN
Nelements= GroupCount(Pt_refs)

New_pts_grp= CreateGroup("New points created",WsPointGroup)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CONTOUR: Manual turn")
Stat= SetRecDelLastExec
G1= CreateGroup(" ")
RecordFunctionOff
FOR I= 1 TO Nelements
	Element= GroupElement(Pt_refs,I)
	IF Ref_array(I,1)= 0 THEN     ! FREE POINT
        	Stat= EntityNameCheck(Element,Point$)
		Stat= AddToGroup(New_pts_grp,Element)
		Stat= SetSelectableFlag(Element,WsTrue)
        	RecordFunctionOn
        	Pt= CopyEntity(Point$,WsFalse)
        	RecordFunctionOff
	ELSE
       		 Pick_return= Ref_array(I,2)
       		 Para= Param_array(I)
       		 Stat= EntityNameCheck(Element,Point$)
       		 RecordFunctionOn
       		 SELECT Pick_return
       		 CASE WsGeometryEnd
       		          Pt= ParameterReturnPt(Point$,Para,WsGeometryEnd)
       		 CASE WsGeometryMid
       		          Pt= ParameterReturnPt(Point$,Para,WsGeometryMid)
       		 CASE WsGeomCenter
       		          Pt= ParameterReturnPt(Point$,Para,WsGeomCenter)
       		 CASE WsNearest
       		          Pt= ParameterReturnPt(Point$,Para,WsNearest)
       		 END SELECT
       		 RecordFunctionOff
	END IF
        RecordFunctionOn
	Stat= AddToGroup(G1,Pt)
	RecordFunctionOff
NEXT I
RecordFunctionOn
Tool=FindUserData(Tool_name$,WsToolData)
G2= ContourFromPoints(G1)
V1= FreeParameter("Diameter stock",Dia_stock)
V2= FreeParameter("Length stock",Len_stock)
V3= FreeParameter("Tool tip tangent",1)
V4= FreeParameter("Tool nose radius",ToolNoseRadius(Tool))
V5= FreeParameter("Tool quadrant",ToolQuadrant(Tool))
Path= LatheTipShift(G2,V4,V1,V2,V5,V3)
Stat= AssociateTool(Path,Tool)
Stat= AutoNameGroup(Path,Name$)
PlotOff
Stat= DeleteGroup(G1,WsTrue)
Stat= DeleteGroup(G2,WsTrue)
PlotOn
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(Pt_refs)
Stat= DeleteEntity(Pt_refs)
IF NOT ValidGeometry(Path) THEN
        DelLastVarRecord
ELSE
        IF Animate_tool THEN Stat= AnimateTool(Tool,Path)
	Stat= PlotEntity(Path)
        ActivateUndo
        Stat= AdvanceUndoNumber("manual turn")
        Stat= AddGroupToUndo(WsUBCreateMode,New_pts_grp,0,WsFalse)
        Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsFalse)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat= DeleteEntity(New_pts_grp)
RETURN




Leave:!
DetachGroupImage
Stat= SetSelectableFlag(Contour,WsTrue)
Stat= DeleteGroup(Contour,WsTrue)
Stat= DeleteGroup(Graph_tool,WsTrue)
Stat= SetSelectableFlag(Pstart,WsTrue)
Stat= SetSelectableFlag(Pnext,WsTrue)
Stat= DeleteEntity(Pstart)
Stat= DeleteEntity(Pnext)
GOSUB Make_record
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	LOAD "typecontrol"
ELSE
	IF Destination$= "SCRIPTSTART" THEN Start
	IF Destination$= "BACKUP" THEN Start
	LOAD Destination$
END IF
END

SUB PlotToolImage(ENTITY Tool_group,Pt,Work_trans)
	Trans1= AngleTransform(Pt,0.0,0.0,0.0)
	Trans2= MultTransforms(Trans1,Work_trans)
	Stat= PlotGroupImage(Tool_group,Trans2,1,1,1,1)
	Stat= AttachGroupImage(Tool_group,Trans2,1,1,1,1)
	Stat= DeleteEntity(Trans1)
	Stat= DeleteEntity(T2)
SUBEND

SUB MoveToNextPoint(ENTITY Tool,Tool_group,Contour,Pstart,Pnext,Tool,Work_trans,Identity_trans,INTEGER Tool_plot)

	Stat=SetLastPoint(Pnext,WsXyzInput)
	L1=LineTwoPoints(Pstart,Pnext,WsTRIMMED)
	Stat= AddToGroup(Contour,L1)
	Stat= AssociateTool(L1,Tool)
	Stat= AssociateTool(Contour,Tool)
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
!    		Stat= AnimateImage(Tool_group,L1,Identity_trans,1,1,1)
		CALL PlotToolImage(Tool_group,Pnext,Work_trans)
	END IF

SUBEND

