
! script name: v3dfive_axis.d/fiveaxiszslice
! var_topic: cuts parts in a five axis mode using z slices

!A
!A NAME:
!A
!A 	Five Axis Part Slice   VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates the cutter path of Z slice through a part. The
!A 	cutter is locked in the horizontal Z plane and allowed to move in the
!A 	path which is generated by placing the cutter in contact with the part.
!A 	This method requires that the cutter fits into the part at all
!A 	locations.  This method does not check for gouges.
!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
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B
!B      Z CUTTER DISPLACEMENT: A positive value will move the cutter
!B                              that distance in the direction of the
!B                              cutter axis. A negative value in the
!B                              reverse direction.
!B
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!B 	Z START: Start of first pass in Z.
!B
!B 	Z END: Enter the last pass in Z.
!B
!B 	Z STEP: Enter the Z depth increment.
!B
!B 		The Z depth calculation is controlled by the user named
!B 		variable Z Step Method.  There are two methods available:
!B 			WsEqualZSteps - 0: Equalized steps in Z.
!B 			WsFullZDepth - 1: Each Z pass is at full depth
!B 			increment, only the last Z pass is smaller.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!B 	APPROXIMATE START POINT: Input or pick a point to determine the path
!B 		start points.  Each closed path will start on the point closest
!B 		to this point.  If the path is open the start point is ignored.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: five axis z slice part")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Angle_var$[256]
DIM Axis_line$[256]
DIM Name$[256]
DIM Part_group$[256]
DIM Finish_var$[256]
DIM Zstart_var$[256]
DIM Zend_var$[256]
DIM Zstep_var$[256]
DIM Point$[256]

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

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
Zaddon=NamedVarVal("Z Cutter Displacement",Ws3DSystemVars,0.0)

Make_cpath=1

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

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3dfive_axis.d/fiveaxiszslice"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CUTTER PATH TOOL, New_tool ",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
LowerSelectBox
IF Tool_name$="New_tool" THEN
	LOAD "vtools.d/readmilltool:v3dfive_axis.d/fiveaxiszslice"
END IF
Have_tool:!
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques

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

Past_dia:!
Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Finishr)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Finishr=Finish_allow
No_finish_var= LastVariableInput(Finish_var$)

Cmethod:Stat=SetInputFormat("a1=",",N,T,L,")
Stat=SetEqualsValue(0,"N")
Stat=GetValue("CUT METHOD: Normal, Tangent, Locked ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        Cmethod= WsFiveAxisNormal
        Osign= 1
CASE "T"
        Cmethod= WsFiveAxisTan2
        Osign= 1
	if(Mill_dir=WsConventional) THEN
                Osign=1
        ELSE
                Osign= -1
        END IF
CASE "L"
        Cmethod= WsLockedAxis
        Osign= 1
CASE ELSE
        GOTO Cmethod
END SELECT

IF Cmethod= WsLockedAxis THEN
Axis_line:Axis_line=PickSpecificEnt("PICK AXIS LINE",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidDependency(Axis_line) THEN Axis_line
        Stat= EntityNameCheck(Axis_line,Axis_line$)
        T= LastPickPara
        IF T > 0.5 THEN Osign= -1
        No_angle_var=1
        Tilt_angle=0
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Tilt_angle)
        Tilt_angle=GetValue("CUTTER TILT ANGLE",Answer$)
        IF ExitScript(Destination$) THEN Leave
        No_angle_var= LastVariableInput(Angle_var$)
END IF

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zaddon)
Zaddon=GetValue("Z CUTTER DISPLACEMENT",Answer$)
IF ExitScript(Destination$) THEN Leave
Vzaddon=CreateVariable("Z Cutter Displacement",Ws3DSystemVars,Zaddon)


Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave

Z_level: !
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zstart_var= LastVariableInput(Zstart_var$)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zend)
Zend=GetValue("Z END",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zend_var= LastVariableInput(Zend_var$)

IF Zend=Zstart THEN
        Zstep=0
        No_zstep_var=1
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Zstep)
        Zstep=GetValue("Z STEP",Answer$)
        IF ExitScript(Destination$) THEN Leave
        No_zstep_var= LastVariableInput(Zstep_var$)
END IF


Z_slice: !

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,")
Part_group=PickGroupEnt("PICK SURFACE GROUP, Name ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN
        GOSUB Get_named_group
END IF
IF NOT ValidDependency(Part_group) THEN Z_slice
Stat= EntityNameCheck(Part_group,Part_group$)


Stat=SetPickMask(WsPickGeometry)
Stat=SetPickMask(WsPickGeometry)
Start_point: Stat=SetInputFormat("n4","")
E1=PickEntity("PICK APPROXIMATE STARTING POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Start_point
Stat= EntityNameCheck(E1,Point$)
Pick_para= LastPickPara
Pick_return= CurrPickReturn


Stat=SetCurrentColor(Pen_color)


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Five axis z slice")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
SELECT Pick_return
CASE WsGeometryEnd
        Ps= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
        Ps= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
        Ps= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
END SELECT
Rotary_axis= PointXYZ(0,0,1)
IF Cmethod= WsLockedAxis THEN
        Locked_axis= LineVector(Axis_line$)
        V1= FreeParameter("Cutter Tilt angle",0)
ELSE
        Locked_axis= LineVector("")
        IF No_angle_var THEN
                V1= FreeParameter("Cutter Tilt angle",Tilt_angle)
        ELSE
                V1= FreeParameter("Cutter Tilt angle",Angle_var$)
        END IF
END IF
IF No_finish_var THEN
        V2= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V2= FreeParameter("Finish allowance",Finish_var$)
END IF
SELECT Cmethod
CASE WsLockedAxis
        V3= FreeParameter("Cut method",WsLockedAxis)
CASE WsFiveAxisNormal
        V3= FreeParameter("Cut method",WsFiveAxisNormal)
CASE WsFiveAxisTan1
        V3= FreeParameter("Cut method",WsFiveAxisTan1)
CASE WsFiveAxisTan2
        V3= FreeParameter("Cut method",WsFiveAxisTan2)
END SELECT
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Z step method",ZStepMethod)
IF No_zstart_var THEN
        V6= FreeParameter("Z start",Zstart)
ELSE
        V6= FreeParameter("Z start",Zstart_var$)
END IF
IF No_zend_var THEN
        V7= FreeParameter("Z end",Zend)
ELSE
        V7= FreeParameter("Z end",Zend_var$)
END IF
IF No_zstep_var THEN
        V8= FreeParameter("Z step",Zstep)
ELSE
        V8= FreeParameter("Z step",Zstep_var$)
END IF
V9= FreeParameter("Mill direction",Mill_dir)
V10= FreeParameter("Orient sign",Osign)
V11= FreeParameter("Z cutter displacement",Zaddon)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,V9)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0.0,V2)
Stat=Setup3dCutter(Mdef1)
Stat= Set3dZAddon(V11)
Z_Group=ZStepsPtGrp(V5,V6,V7,V8)
Stat= PrintToVarRecord("PlotOn")
Work= CurrentWorkplane
Cutter_path=RotarySlicePart(Part_group$,Rotary_axis,Locked_axis,V1,V3,Z_group,V4,V10,V9,Make_cpath,WsFalse,Ps,Work)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Ps,Rotary_axis,Locked_axis)
Stat= DeleteGroup(Z_group,WsTrue)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Cutter_path)
	ActivateUndo
	Stat= AdvanceUndoNumber("five axis cut")
	Stat= AddGroupToUndo(WsUBCreateMode,Cutter_path,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Z_level

Leave:!
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF


Get_named_group:!
Stat=ListGrpNames("SURFACE GROUPS","selected group","",WsSurfaceGroup)
IF Stat=1 THEN Leave_name

Stat=SetInputFormat("a99","")
Stat=GetValue("SURFACE GROUP NAME",Answer$)

IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave_name
Part_group=FindNamedGrp(Answer$)
IF NOT ValidGeometry(Part_group) THEN Get_named_group
Grp_type= GroupType(Part_group)
IF Grp_type <> WsSurfaceGroup THEN Get_named_group
LowerSelectBox

Leave_name:!
Stat=SetInputFormat("n4","")

RETURN


END
