
! script name: v3dcpaths.d/hsradialbdy
! var_topic: cuts a part using radial boundary gouge

!A
!A NAME:
!A
!A 	Gouge: High Speed Radial Boundary	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script cuts a group of surfaces using radial cuts, just like
!A 	Radial Gouge, but with a Contour Boundary.  The cutter center is locked
!A 	in the vertical plane containing the cut, and allowed to move in that
!A 	plane, in contact with the part.  The cutter path is generated using
!A 	gouge avoidance.
!A
!A	When finished the cutterpath is smoothed to minimize sudden 
!A	changes of direction. The smoothing process may cause the cutterpath
!A	to violate the original surfaces. The smoothing parameters
!A	can be adjusted to minimize these problems.
!A
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER TYPE Ball, Endmill:
!B
!B 		B - Cutter is assumed to be a ball endmill.  Offset is
!B 		    measured from the center of the ball radius.  The bottom
!B 		    of the ball is the programmed point.
!B 		E - Cutter is assumed to be a standard, flat-bottom endmill,
!B 		    with possibly rounded corners.  The programmed point is the
!B 		    center of the bottom of the cutter.  It is this point which
!B 		    is kept away from part boundary.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	CORNER RADIUS: Enter the corner radius of the endmill.
!B 		NOTE: This question is only asked for endmills.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!B 	PICK EXTERIOR CONTOUR: Pick the contour that will be the boundary.
!B
!B 	NUMBER OF ISLANDS: Enter the number of islands to be avoided by
!B 		the path generated.
!B
!B 	PICK ISLAND #: Pick the islands to be avoided by the path.
!B
!B 	CENTER POINT ABS X,Y: Enter the X,Y coordinates of the center of the
!B 		annular region bounding cuts.
!B
!B 	START RADIUS: Enter the starting radius, from center of radial cut.
!B
!B 	END RADIUS: Enter the ending radius of the radial cut.
!B
!B 	START ANGLE: Enter the starting angle for the radial cuts.
!B
!B 	ANGULAR EXTENT: Enter the angular extent for the radial cuts.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!C NOTES:
!C
!C 	An additional feature of radial boundary is auto edge extension.  If
!C 	the auto edge extend switch is set on an edge the edge will extend a
!C 	distance equal to the extension dist (a named variable) along the
!C 	tangent to the surface on the edge.  Then the normal vector controlling
!C 	the cutter will roll through 90 deg of arc.  This feature helps insure
!C 	the the gouge check will work in tight corners where the cutter paths
!C 	extend beyond the edge of the part.
!C
!C OPTIONS:
!C
!C 	Circle output is controlled by the named variable "3D Circle Output".
!C
!C 	There are two methods available:
!C
!C 		WsLinearMoves - 0: Outputs straight line moves.
!C 		WsCircleMoves - 1: Outputs circular arc moves.
!C
!C      Joining adjacent cuts is controlled by the named variables
!C      "3D Join Cuts" and "3D Join Dist"
!C
!C              3D Join Cuts: - 0: do not join cuts-- turbopost controls
!C                              1: join cuts within join dist.
!C
!C              3D Join Dist:  - longest dist allowed for a join. The
!C                               default of 0.0 yields an internal default
!C                               based on the width of cut.
!C
!C      It is assumed that for joining cut the join line can project
!C      onto the part. Otherwise the cuts will not be joined and
!C      turbo post will control positioning.
!C
!C
!C      The join line may deviate from the boundary curve. For large
!C      widths of cut the join connections may generate unusual moves.
!C


Stat=ChangeLabelStr("ScriptNameStatus","Function: radial boundary gouge ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Contour$[256]
DIM Island$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Angle_var$[256]
DIM Astart_var$[256]
DIM Rstart_var$[256]
DIM Rend_var$[256]
DIM Point$[256]
DIM Name$[256]

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



ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zigzag_cuts=NamedVarVal("3D Zigzag Cuts",Ws3DSystemVars,0.0)
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Ext_dist=NamedVarVal("3D Auto Extend Dist",Ws3DSystemVars,0.010)
Join_cuts=NamedVarVal("3D Join Cuts",Ws3DSystemVars,0.0)
Join_dist=NamedVarVal("3D Join Dist",Ws3DSystemVars,0.0)
Smooth_dist=NamedVarVal("smoothing distance",WsSystemVars,50*Tol)
Smooth= 1

Units=CurrentUnits
Astart=0
Angle=360
IF Units=WsMetric THEN
        Startr=0
        Endr=250
ELSE
        Startr=0
        Endr=10
END IF

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:vhscpaths.d/hsradialbdy"
  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
IF Tool_name$="New_tool" THEN
	LowerSelectBox
	LOAD "vtools.d/readmilltool:vhscpaths.d/hsradialbdy"
END IF
Have_tool:!


Tool=FindUserData(Tool_name$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
LowerSelectBox

Past_dia:!
Cutter_dia=GetUserValue(Tool,"Diameter:")
Widthr=NamedVarVal("width of cut",WsSystemVars,Width_cut)
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Cut_width=Cutter_dia*GetVariableVal(Width_val)
ELSE
	Cut_width= Cutter_dia
END IF
IF Widthr>Cut_width THEN Widthr= Cut_width

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Widthr)
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
Width_cut=ABS(Width_cut)
Widthr=Width_cut
No_width_var= LastVariableInput(Width_var$)
Vwidth=CreateVariable("width of cut",WsSystemVars,Widthr)

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$)

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


Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VWidth=CreateVariable("width of cut",WsSystemVars,Width_cut)



Pick_loop:!
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 Pick_loop
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)



Cut_part:!
Stat=SetPickMask(WsPickGeometry)

Boundary_loop:!
Stat=SetPickMask(WsPickNoSurf)
Contour=PickGroupEnt("PICK EXTERIOR CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidDependency(Contour)) THEN Boundary_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN Boundary_loop
Stat=SetPickMask(WsPickGeometry)

Closed=IsContClosed(Contour)
IF NOT Closed THEN Boundary_loop

Stat=SetInputFormat("n0=","")
Stat=SetEqualsValue(0.0)
Nisles=GetValue("NUMBER OF ISLANDS",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4","")
Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles
        Stat=SetPickMask(WsPickNoSurf)
Pick_isle:Isle=PickGroupEnt("PICK ISLAND #"&VAL$(I))
        IF ExitScript(Destination$) THEN Leave
        Stat=SetPickMask(WsPickGeometry)
        IF (NOT ValidDependency(Isle)) THEN Pick_isle
        Stat= EntityNameCheck(Isle,Island$)
        IF NOT LEN(Island$) THEN Pick_isle
        Stat=AppendToGroup(Islands,Isle)
NEXT I


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


Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(Startr)
Startr=InputRadius("START RADIUS")
IF ExitScript(Destination$) THEN Leave
No_rstart_var= LastVariableInput(Rstart_var$)

Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(Endr)
Endr=InputRadius("END RADIUS")
IF ExitScript(Destination$) THEN Leave
No_rend_var= LastVariableInput(Rend_var$)

Stat=SetInputFormat("n4=","")
Stat=SetAngleEquals(Astart)
Astart=InputAngle("START ANGLE")
IF ExitScript(Destination$) THEN Leave
No_astart_var= LastVariableInput(Astart_var$)

Stat=SetInputFormat("n4=","")
Stat=SetAngleEquals(Angle)
Angle=InputAngle("ANGULAR EXTENT")
IF ExitScript(Destination$) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)



Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Radial Boundary Gouge Check")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
Stat= NullReference(Contour$)
Isles=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Nisles
        Isle= GroupElement(Islands,I)
        Stat= EntityNameCheck(Isle,Island$)
        RecordFunctionOn
        Stat= AppendToGroup(Isles,Island$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
SELECT Pick_return
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
END SELECT
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_width_var THEN
        V2= FreeParameter("Width cut",Width_cut)
ELSE
        V2= FreeParameter("Width cut",Width_var$)
END IF
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Zigzag cuts",Zigzag_cuts)
IF No_rstart_var THEN
        V6= FreeParameter("Start radius",Startr)
ELSE
        V6= FreeParameter("Start radius",Rstart_var$)
END IF
IF No_rend_var THEN
        V7= FreeParameter("End radius",Endr)
ELSE
        V7= FreeParameter("End radius",Rend_var$)
END IF
IF No_astart_var THEN
        V8= FreeParameter("Start angle",Astart)
ELSE
        V8= FreeParameter("Start angle",Astart_var$)
END IF
IF No_angle_var THEN
        V9= FreeParameter("Angle extent",Angle)
ELSE
        V9= FreeParameter("Angle extent",Angle_var$)
END IF
V11= FreeParameter("Extension dist",Ext_dist)
V12= FreeParameter("Join cuts",Join_cuts)
V13= FreeParameter("Join_dist",Join_dist)
Pz=PointXyz(-XPointValue(Pc),-YPointValue(Pc),-ZPointValue(Pc))
Trans1=AngleTransform(Pz,0,0,0,WsWorkplaneVals)
Stat=DeleteEntity(Pz)
Stat=DeleteEntity(Pc)

Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
Stat=Setup3dCutter(Mdef1)
Stat= PrintToVarRecord("PlotOn")
Cutter_path=RadialBdyGouge(Trans1,Part_group$,Contour$,Isles,V6,V7,V8,V9,V2,V4,V5,V11,V12,V13)

Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Trans1,Isles)
V15=FreeParameter("Smooth distance",Smooth_dist)
V16=FreeParameter("Smooth",Smooth)
Stat=SmoothCpath(Cutter_path,V15,V4,V16,0)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("radial bdy gouge check")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat= DeleteEntity(Islands)

GOTO Pick_loop

Leave:!
V1=CreateVariable("smoothing distance",WsSystemVars,Smooth_dist)
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat= SetPickMask(WsPickGeometry)
Stat= DeleteEntity(Islands)
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
