
! script name: pkvmach.d/pkroughedgegrp
! var_topic: rough mill an edge group with leadin

!A
!A NAME:
!A
!A 	Rough Edge Group	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path which rough mills an edge
!A	group as it it were a contour. The script allows leadin and
!A	leadout moves to be added.
!A
!B INSTRUCTIONS:
!B
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path which is
!B 		    clockwise for an interior cut and counterclockwise
!B 		    for an exterior cut.
!B 		CL - Climb mill will generate a path which is clockwise for
!B 		    an exterior cut and counterclockwise for an interior cut.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE OFFSET: Pick an element on
!B 		the contour to be offset.
!B
!B 	PICK WHICH SIDE: Pick which side of the contour the path should
!B 		be generated.
!B
!B	FINISH DEPTH: Enter the finish depth.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter and
!C 	the finish allowance. (offset=.5*cutter_diameter + finish_allowance)
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: rough edge group mill")

DIM Destination$[256]
DIM Answer$[256],A$[32]
DIM Datastr$[1256]
DIM Boundary_name$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Edgegroup$[256]
DIM Finish_var$[256]
DIM Depth_var$[256]
DIM Depthcut_var$[256]
DIM Rough_var$[256]
DIM Width_var$[256]
DIM Name$[256]
DIM Namelist$[500]
REAL Leadintype
REAL Leadouttype

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

ActivateUndo
Units= CurrentUnits

IF Units= WsMetric THEN
        Dist= 12.5
        Radius= 6.25
ELSE
        Dist= 0.5
        Radius= 0.25
END IF
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4

LeadinVarsType= 20011
Leadintype=NamedVarVal("leadin type",LeadinVarsType,1.0)
Leadouttype=NamedVarVal("leadout type",LeadinVarsType,1.0)
Leadindist=NamedVarVal("leadin dist",LeadinVarsType,Dist)
Leadoutdist=NamedVarVal("leadout dist",LeadinVarsType,leadindist)
Rin=NamedVarVal("leadin radius",LeadinVarsType,Radius)
Rout=NamedVarVal("leadout radius",LeadinVarsType,Radius)

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

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:pkvmach.d/pkroughedgegrp"
  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 "vtools.d/readmilltool:pkvmach.d/pkroughedgegrp"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox
Leadindist=.6*Cutter_dia
Leadoutdist=.6*Cutter_dia

Namelist$=""
Leadin: !
A$=VAL$(Leadintype)
Stat=SetEqualsValue(0,A$)
Stat=SetInputFormat("a99=","")
Namelist$="0: NO LEAD IN,"
Namelist$=Namelist$&"1: LINE--CIRCLE TANGENT TO CONTOUR,"
Namelist$=Namelist$&"2: TANGENT CIRCLE,"
Namelist$=Namelist$&"3: LINE TANGENT TO CIRCLE TANGENT TO CONTOUR,"
Namelist$=Namelist$&"4: LINE NORMAL TO CONTOUR,"
Namelist$=Namelist$&"5: LINE TANGENT TO CONTOUR,"
Namelist$=Namelist$&"6: Z LEADIN,"
Stat=GeneralListBox("LEAD IN TYPES","TYPE SELECTED",Namelist$)
Stat=GetValue("SELECT LEAD IN TYPE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
IF Answer$[1]<CHR$(48) OR Answer$[1]>CHR$(56) THEN Leadin
Leadintype=VAL(Answer$[1])
IF Leadintype<0 OR Leadintype>6 THEN Leadin
LowerSelectBox

Leadout: !
A$=VAL$(Leadouttype)
Stat=SetEqualsValue(0,A$)
Stat=SetInputFormat("a99=","")
Namelist$="0: NO LEAD OUT,"
Namelist$=Namelist$&"1: LINE--CIRCLE TANGENT TO CONTOUR,"
Namelist$=Namelist$&"2: TANGENT CIRCLE,"
Namelist$=Namelist$&"3: LINE TANGENT TO CIRCLE TANGENT TO CONTOUR,"
Namelist$=Namelist$&"4: LINE NORMAL TO CONTOUR,"
Namelist$=Namelist$&"5: LINE TANGENT TO CONTOUR,"
Namelist$=Namelist$&"6: Z LEADOUT,"
Stat=GeneralListBox("LEAD OUT TYPES","TYPE SELECTED",Namelist$)
Stat=GetValue("SELECT LEAD OUT TYPE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
IF Answer$[1]<CHR$(48) OR Answer$[1]>CHR$(56) THEN Leadout
Leadouttype=VAL(Answer$[1])
IF Leadouttype<0 OR Leadouttype>6 THEN Leadout
LowerSelectBox

Mill_direction:!
Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Mill_dir=GetValue("MILL CLimb OR Conventional",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (Answer$="C") THEN
	Mill_dir=WsConventional
	Pen_color=WsConvCutPen
	Dcomp_dir= WsRight
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
	Dcomp_dir= WsLeft
END IF
Stat=SetCurrentColor(Pen_color)

Rough_stock:!
Stat=SetInputFormat("n4=","")
Rough_stock=NamedVarVal("rough stock",WsSystemVars,0.0)
Stat=SetEqualsValue(Rough_stock)
Rough_stock=GetValue("ROUGH STOCK",Answer$)
IF (ExitScript(Destination$)) THEN Leave
VStock=CreateVariable("rough stock",WsSystemVars,Rough_stock)
No_rough_var= LastVariableInput(Rough_var$)

Width_cut:!
Stat=SetInputFormat("n4=","")
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
        Width_factor=GetVariableVal(Width_val)
        Width_cut=Cutter_dia*Width_factor
ELSE
        Width_cut=NamedVarVal("width of cut",WsSystemVars,0.0)
END IF
Stat=SetEqualsValue(Width_cut)
Stat=SetInputFormat("n4=","")
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Width_cut=ABS(Width_cut)
Vwidth=CreateVariable("width of cut",WsSystemVars,Width_cut)
No_width_var= LastVariableInput(Width_var$)

Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Stat=SetInputFormat("n4=","")
Finish_allow=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_allow)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finish_allow)
No_finish_var= LastVariableInput(Finish_var$)

Pen_color= CurrentColor
Stat=SetCurrentColor(Pen_color)

Pick_loop:!
E1=WPKPickEntity("PICK EDGE GROUP",WsPKPickEdges)
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Start

Pkgroup= WPKContainingGroup(E1,1)
IF NOT WPKIsEntity(Pkgroup) THEN Pick_loop
Stat= EntityNameCheck(Pkgroup,Edgegroup$)

Edge_groups= WPKConvertGroup(Pkgroup,WsPKConvertEdges,WsTrue)
C1= GroupElement(Edge_groups,1)
Contour=OffsetContour(C1,0.0,WsForward,WsNoLook)
Stat= DeleteGroup(Edge_groups,WsTrue,WsTrue)
Element= GroupElement(Contour,1)
Para_value= 0.5
GOSUB Which_side

Side=OffsetLeftRight(Element,Pt)
IF Side=0 THEN Side=1
Direction_sign=MillDirection(E1,Pt)
Dist=-Side*Offset

Leadinside= Side
Direction=WsForward
IF (Direction_sign=1)AND(Mill_dir=WsConventional) THEN
	Direction=WsReverse
	Leadinside= -Side
END IF
IF (Direction_sign=-1)AND(Mill_dir=WsClimb) THEN
	Direction=WsReverse
	Leadinside= -Side
END IF
Stat=DeleteEntity(Pt)
Stat= DeleteGroup(Contour,WsTrue,WsTrue)

Finish_depth:!
Finish_depth=NamedVarVal("finish depth",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_depth)
Stat=SetInputFormat("n4=","")
Finish_depth=GetValue("FINISH DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Vdepth=CreateVariable("finish depth",WsSystemVars,Finish_depth)

Depth_cut:!
Depth_cut=NamedVarVal("depth of cut",WsSystemVars,0.0)
Stat=SetEqualsValue(Depth_cut)
Stat=SetInputFormat("n4=","")
Depth_cut=GetValue("DEPTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Depth_cut=ABS(Depth_cut)
Vdepth=CreateVariable("depth of cut",WsSystemVars,Depth_cut)
No_depthcut_var= LastVariableInput(Depthcut_var$)
Stat=SetCurrentColor(Pen_color)

IF Leadintype=6 THEN Leadindist=Depth_cut
IF Leadouttype=6 THEN Leadoutdist=Depth_cut

Side1= -Side
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("PATH: Rough mill with leadin ")
V0= FreeParameter("Bottom allowance",0)
IF No_finish_var THEN
	V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
	V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_rough_var THEN
	V2= FreeParameter("Rough stock",Rough_stock)
ELSE
	V2= FreeParameter("Rough stock",Rough_var$)
END IF
IF No_width_var THEN
	V3= FreeParameter("Width cut",Width_cut)
ELSE
	V3= FreeParameter("Width cut",Width_var$)
END IF
V4= FreeParameter("Side",Side1)
IF No_depth_var THEN
	V5= FreeParameter("Finish depth",Finish_depth)
ELSE
	V5= FreeParameter("Finish depth",Depth_var$)
END IF
IF No_depthcut_var THEN
	V6= FreeParameter("Depth cut",Depth_cut)
ELSE
	V6= FreeParameter("Depth cut",Depthcut_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
	Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
	Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V4,V3,V1,V2,1)
Stat= MillDefZInfo(Mdef1,1,V5,V6,V0)
G1= WPKConvertGroup(Edgegroup$,WsPKConvertEdges,WsTrue)
Contour= GroupElement(G1,1)
Paths=MillContour(Contour,Mdef1,WsFullLook,WsCircleMoves)
Stat= DeleteGroup(G1)
Stat= UnAssocMachDef(Paths,Mdef1)
Stat=DeleteEntity(Mdef1)
V1= FreeParameter("Leadin radius",Rin)
V2= FreeParameter("Leadin distance",Leadindist)
V3= FreeParameter("Leadin type",Leadintype)
V4= FreeParameter("Leadout radius",Rout)
V5= FreeParameter("Leadout distance",Leadoutdist)
V6= FreeParameter("Leadout type",Leadouttype)
V7= FreeParameter("Leadin side",Leadinside)
Stat= PathGroupLeadIn(Paths,V1,V4,V2,V5,V3,V6,V7)
Stat= AutoNameGroup(Paths,Name$)
V8= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
Stat= SetDCompDirection(Paths,V8)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("rough mill")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF


GOTO Pick_loop

Leave:!
LowerSelectBox
Stat=SetPickMask(WsPickGeometry)
Vleadintype=CreateVariable("leadin type",LeadinVarsType,leadintype)
Vleadouttype=CreateVariable("leadout type",LeadinVarsType,leadouttype)
Vleadin=CreateVariable("leadin dist",LeadinVarsType,leadindist)
Vleadout=CreateVariable("leadout dist",LeadinVarsType,leadoutdist)
VRout=CreateVariable("leadin radius",LeadinVarsType,Rin)
VRin=CreateVariable("leadout radius",LeadinVarsType,Rout)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat= DeleteGroup(Contour,WsTrue,WsTrue)
Stat=DeleteEntity(Pt)
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF

ShowNormalDir:!
        Forward_normal= ParameterLine(Element,Para_value,Normal_len,WsNormal)
        Pforward=ParameterPt(Forward_normal,1.0)

        Reverse_normal= ParameterLine(Element,Para_value,-Normal_len,WsNormal)
        Preverse=ParameterPt(Reverse_normal,1.0)

        Stat= SetForceVisible(Forward_normal,WsTrue)
        Stat= SetForceVisible(Pforward,WsTrue)
        Stat= SetForceVisible(Reverse_normal,WsTrue)
        Stat= SetForceVisible(Preverse,WsTrue)

        Stat=SetColor(Forward_normal,Forward_color)
        Stat=SetColor(Pforward,Forward_color)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
RETURN

Which_side:!
        GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE OF CONTOUR",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
        T1= LastPickPara
        Pt=ParameterPt(L1,T1)
        Stat=DeleteEntity(Forward_normal)
        Stat=DeleteEntity(Reverse_normal)
        Stat=DeleteEntity(Pforward)
        Stat=DeleteEntity(Preverse)
RETURN

END

