
! script name: vlathe.d/profile
! var_topic: roughing - profile cutting

!A
!A NAME:
!A
!A 	Profile: roughing cycle
!A
!A DESCRIPTION:
!A
!A 	This script creates a group of paths spaced at equalized
!A 	distances to remove stock in length and diameter.
!A
!A~ image location - images/profilecycle.jpg
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the tool for profiling.
!B
!B 	NUMBER OF CUTS: Enter the number of passes.
!B
!B 	FINISH ALLOWANCE: Enter the diameter and length
!B 		amount of material to be left for a finish pass.
!B
!B 	PICK CONTOUR: Pick the contour to be roughed.
!B
!C NOTES:
!C
!C	lathe.d/setvals sets user definable constants.
!C
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: profile roughing ")

DIM Destination$[256], Answer$[256]
DIM Toolname$[256]
DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Spoint$[256]
DIM Num_cuts$[256]
DIM Dfinish$[256]
DIM Zfinish$[256]
DIM DStock$[256]
DIM ZStock$[256]
DIM Name$[256]
INTEGER Id_contour


ActivateUndo
Stat=SetXyzInput(WsLatheInput)
Restart:!

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Units= CurrentUnits
IF Units= WsMetric THEN
        Tool_clearance= .125
ELSE
        Tool_clearance= .05
END IF
Animate_tool=1
Current_pen= CurrentColor
Work= CurrentWorkplane
!   StdWorkplane
Id_contour= 0

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

Tool=FindUserData(Toolname$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
Tnose_rad=ToolNoseRadius(Tool)
Quad=ToolQuadrant(Tool)

Ent=CreateVariable("tool quadrant",WsLatheVars,Quad)
Ent=CreateVariable("tool nose radius",WsLatheVars,Tnose_rad)


Stat= SetInputFormat("n4=","")
Dia_allow=NamedVarVal("diameter fin allowance",WsLatheVars,.04)
Stat=SetEqualsValue(Dia_allow)
Dia_allow=GetValue("DIAMETER FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_dfinish_var= LastVariableInput(Dfinish$)
Ent=CreateVariable("diameter fin allowance",WsLatheVars,Dia_allow)

Stat= SetInputFormat("n4=","")
Len_allow=NamedVarVal("length fin allowance",WsLatheVars,.02)
Stat=SetEqualsValue(Len_allow)
Len_allow=GetValue("LENGTH FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zfinish_var= LastVariableInput(Zfinish$)
Ent=CreateVariable("length fin allowance",WsLatheVars,Len_allow)

Stat= SetInputFormat("n4=","")
Ncuts=NamedVarVal("number of cuts",WsLatheVars,3)
Stat=SetEqualsValue(Ncuts)
Ncuts=GetValue("NUMBER OF CUTS",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("number of cuts",WsLatheVars,Ncuts)
No_ncuts_var= LastVariableInput(Num_cuts$)

Stat= SetInputFormat("n4=","")
Dia_stock=NamedVarVal("diameter stock",WsLatheVars,1)
Stat=SetEqualsValue(Dia_stock)
Dia_stock=GetValue("TOTAL STOCK IN DIAMETER",Answer$)
IF ExitScript(Destination$) THEN Leave
No_dstock_var= LastVariableInput(DStock$)
Ent=CreateVariable("diameter stock",WsLatheVars,Dia_stock)

Stat= SetInputFormat("n4=","")
Len_stock=NamedVarVal("length stock",WsLatheVars,.5)
Stat=SetEqualsValue(Len_stock)
Len_stock=GetValue("TOTAL STOCK IN LENGTH",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zstock_var= LastVariableInput(ZStock$)
Ent=CreateVariable("length stock",WsLatheVars,Len_stock)

Pick_loop:!
Stat= SetInputFormat("n4a1",",N,")
Contour= PickGroupEnt("PICK CONTOUR,  New tool ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN Restart
IF (NOT ValidDependency(Contour)) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN pick_loop
Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop


Ploop:!
Stat=SetInputFormat("n4","")
Pstart=XyzInput("INPUT START POINT",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Pstart) THEN Ploop

E1= LastPickEnt
Pick_para= LastPickPara
Pick_return= CurrPickReturn
IF NOT ValidDependency(E1) THEN
         E1= CopyEntity(Pstart,WsFalse)
         Stat= EntityNameCheck(E1,Spoint$)
         Ps_created= 1
ELSE
         Stat= EntityNameCheck(E1,Spoint$)
         Ps_created= 0
END IF
Stat= DeleteEntity(Pstart,WsTrue)


Calc_start_end:!
Side= ContQuadrantSign(Contour,Quad)
IF Side= 1 THEN
        Pen_color=WsConvCutPen
ELSE
        Pen_color=WsClimbCutPen
END IF
Stat= SetCurrentColor(Pen_color)



Profile_cycle:!
PlotOff
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("LATHE ROUGH: Profile "&Toolname$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
SELECT Pick_return
CASE WsGeometryEnd
        Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
        Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
        Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeomCenter)
END SELECT
Tool=FindUserData(Toolname$,WsToolData)
V1= FreeParameter("Tool nose radius",ToolNoseRadius(Tool))
V2= FreeParameter("Tool quadrant",ToolQuadrant(Tool))
V3= FreeParameter("Side",ContQuadrantSign(Contour$,V2))
V4= FreeParameter("Cut direction",1)
IF No_dfinish_var THEN
        V5= FreeParameter("Diameter finish",Dia_allow)
ELSE
        V5= FreeParameter("Diameter finish",Dfinish$)
END IF
IF No_zfinish_var THEN
        V6= FreeParameter("Length finish",Len_allow)
ELSE
        V6= FreeParameter("Length finish",Zfinish$)
END IF
IF No_ncuts_var THEN
        V7= FreeParameter("Number cuts",Ncuts)
ELSE
        V7= FreeParameter("Number cuts",Num_cuts$)
END IF
IF No_dstock_var THEN
        V8= FreeParameter("Diameter stock",Dia_stock)
ELSE
        V8= FreeParameter("Diameter stock",Dstock$)
END IF
IF No_zstock_var THEN
        V9= FreeParameter("Length stock",Len_stock)
ELSE
        V9= FreeParameter("Length stock",Zstock$)
END IF
V10= FreeParameter("Uniform Offset",0.0)
V11= FreeParameter("Collision point color",7)
Path= LatheOffsetContour(Contour$,V1,V10,V5,V6,V3,V2,WsForward,WsMinLook)
Rough_path= LatheProfileCycle(Path,Ps,V8,V9,V7,V2,V4)
Stat= AutoNameGroup(Rough_path,Name$)
Stat= AssociateTool(Rough_path,Tool)
P1= LthToolInterfere(Tool,Rough_path,Contour$)
Stat= SetColor(P1,V10)
Stat= SetLineType(P1,11)
Stat= DeleteGroup(Path,WsTrue)
Stat= DeleteEntity(Ps)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Rough_path) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("cut profile")
	IF Ps_created THEN
                Stat= AddItemToUndo(WsUBCreateMode,Spoint$)
        END IF
        Stat= AddGroupToUndo(WsUBCreateMode,Rough_path,1,WsTrue)
        Stat= AddItemToUndo(WsUBCreateMode,P1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
        Stat= PlotEntity(Rough_path)
        Stat= PlotEntity(P1)
        IF Animate_tool AND NOT ValidGeometry(P1) THEN
                 Work_trans= CurrentWorkplane
                 Stat= AnimateTool(Tool,Rough_path)
        END IF
END IF

GOTO Pick_loop


Leave:!
PlotOn
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)
Stat=SetInputFormat("n4","")
LowerSelectBox
IF NOT LEN(Destination$) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

