
! script name: 3dcpaths.d/radbdygouge
! 3dtopic: cuts a part using radial boundary gouge

!A
!A NAME:
!A
!A 	Gouge: Radial Boundary
!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
!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 Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[256]
DIM Name$[256]
DIM Name1$[256]

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zigzag_cuts=NamedVarVal("3D Zigzag Cuts",Ws3DSystemVars,1.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)
Units=CurrentUnits

IF Units=WsMetric THEN 
	Startx=-500
	Endx=500
	Starty=-500
	Endy=500
ELSE
	Startx=-50
	Endx=50
	Starty=-50
	Endy=50
END IF
Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0
Angle= 360

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)

Surface_dir$="F" 

Tool_assoc=0
Tool_val=FindNamedVar("tool association",WsToolVars)
IF ValidGeometry(Tool_val) THEN Tool_assoc=GetVariableVal(Tool_val)

IF NOT Tool_assoc THEN
	GOSUB Get_mill_type
	GOSUB Get_cutter_dia
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
	IF Stat THEN
		GOSUB Get_mill_type
		GOSUB Get_cutter_dia
		Tool_assoc=0
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("CUTTER PATH TOOL",Tool_name$)
		IF ExitScript(Destination$) THEN Leave
		IF Tool_name$="CANCEL" THEN
			LowerSelectBox
			GOSUB Get_mill_type
			GOSUB Get_cutter_dia
			Tool_assoc=0
			GOTO Past_dia
		END IF
		Tool=FindUserData(Tool_name$,WsToolData)

		Stat=GetUserString(Tool,"Tool type:",Tool_type$)

		SELECT Tool_type$
		    CASE "ENDMILL","BULLMILL"
			Cutter_type=WsEndMill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
		    CASE "TAPERMILL"
			Cutter_type=WsEndMill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Taper=GetUserValue(Tool,"Taper angle:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
		    CASE "BALLMILL"
			Cutter_type=WsBallCutter
			Cutter_dia=GetUserValue(Tool,"Diameter:")
		    CASE ELSE
			GOTO Tool_ques
		END SELECT

		LowerSelectBox
	END IF
END IF
Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

Past_dia:!
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

Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finishr)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Finishr=Finish_allow

VCutter_type=CreateVariable("cutter type",WsSystemVars,Cutter_type)
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VCorner=CreateVariable("corner radius",WsSystemVars,Corner_rad)
VWidth=CreateVariable("width of cut",WsSystemVars,Width_cut)

Stat=Setup3dCutter(Cutter_type,Cutter_rad,Corner_rad,Offset,Finish_allow,Taper)


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 ValidGeometry(Part_group) THEN Pick_loop


Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)

Cut_part:!
Stat=SetPickMask(WsPickGeometry)

Boundary_loop:!
Stat=SetPickMask(WsPickNoSurf)
Contour=PickEntity("PICK EXTERIOR CONTOUR",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(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)
	Isle=PickEntity("PICK ISLAND #"&VAL$(I))
	IF ExitScript(Destination$) THEN Leave
	Stat=SetPickMask(WsPickGeometry)

	Stat=AddToGroup(Islands,Isle)

NEXT I

Stat=SetInputFormat("n4=","")
Pc=XyzInput("CENTER POINT",WsXyOnly)
IF ExitScript(Destination$) THEN Leave

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

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

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

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

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

Xcenter=XPointValue(Pc)
Ycenter=YPointValue(Pc)
Zcenter=ZPointValue(Pc)

Pz=PointXyz(-Xcenter,-Ycenter,-Zcenter)

Trans1=AngleTransform(Pz,0,0,0,WsWorkplaneVals)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pz)

Cutter_path=RadialBdyGouge(Trans1,Part_group,Contour,Islands,Startr,Endr,Astart,Angle,Width_cut,Tol,Zigzag_cuts,Ext_dist,Join_cuts,Join_dist)

Stat=DeleteEntity(Trans1)

IF ValidGeometry(Cutter_path) THEN
	Datastr$= " 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	stat= CPathFileName(Cutter_path,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  RADIAL BOUNDARY GOUGE"&CHR$(10)
	Stat=GroupName(Part_group,Answer$)
	Datastr$=Datastr$&"SURFACE GROUP: "&Answer$&CHR$(10)

	group=ParentGroup(Contour,0)
	Stat=GroupName(group,Name1$)
	Datastr$=Datastr$&"EXTERIOR: "&Name1$&CHR$(10)
	Nisles=GroupCount(Islands)
	Datastr$=Datastr$&"NUMBER ISLANDS: "&VAL$(Nisles)&CHR$(10)
	FOR I=1 TO Nisles
		Isle=GroupElement(Islands,I)
		Group=ParentGroup(Isle,0)
		Stat=GroupName(group,Name1$)
		Datastr$=Datastr$&"ISLAND"&VAL$(I)&": "&Name1$&CHR$(10)
	NEXT I

	Datastr$=Datastr$&"CENTER PT X: "&VAL$(Xcenter)&CHR$(10)
	Datastr$=Datastr$&"CENTER PT Y: "&VAL$(Ycenter)&CHR$(10)
	Datastr$=Datastr$&"CENTER PT Z: "&VAL$(Zcenter)&CHR$(10)
	Datastr$=Datastr$&"ANGULAR EXTENT: "&VAL$(Angle)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"START RADIUS: "&VAL$(Startr)&CHR$(10)
	Datastr$=Datastr$&"END RADIUS: "&VAL$(Endr)&CHR$(10)
	Datastr$=Datastr$&"START ANGLE: "&VAL$(Astart)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&CHR$(10)
	Datastr$=Datastr$&"OFFSET: "&VAL$(Offset)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CORNER RAD: "&VAL$(Corner_rad)&CHR$(10)
	Datastr$=Datastr$&"TAPER: "&VAL$(Taper)&CHR$(10)
	Datastr$=Datastr$&"CUTTER TYPE: "&VAL$(Cutter_type)&CHR$(10)
	Datastr$=Datastr$&"CIRCLES: "&VAL$(Circle_output)&CHR$(10)
	Datastr$=Datastr$&"ZIGZAGS: "&VAL$(Zigzag_cuts)&CHR$(10)
	Datastr$=Datastr$&"JOIN CUTS: "&VAL$(Join_cuts)&CHR$(10)
	Datastr$=Datastr$&"JOIN DIST: "&VAL$(Join_dist)&CHR$(10)

	Cpath3d=FindUserData(Name$,Ws3dCpath)
	IF ValidGeometry(Cpath3d) THEN
		 Stat= DeleteEntity(Cpath3d)
	END IF
	Cpath3d=ReadUserData(Ws3dCpath,Name$,Datastr$)

	Stat= AssocUserData(Cutter_path,Cpath3d,Ws3dCpath)
	IF Tool_assoc THEN Stat= AssociateTool(Cutter_path,Tool)

	ActivateUndo
        Stat= AdvanceUndoNumber("radial bdy gouge check")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutter_path,1,WsTrue)
END IF
Stat= DeleteEntity(Islands)

GOTO Start

Leave:!
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat= SetPickMask(WsPickGeometry)
Stat= DeleteEntity(Nline)
Stat= DeleteEntity(Islands)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF

Get_mill_type:!
	SELECT Cutter_type
	CASE WsBallCutter
		Answer$="B"
	CASE WsEndMill
		Answer$="E"
	END SELECT
	Stat= SetEqualsValue(0,Answer$)
	Stat=SetInputFormat("a1=",",B,E,")
	Stat=GetValue("CUTTER TYPE Ball, Endmill",Answer$)
	IF ExitScript(Destination$) THEN Leave

	SELECT Answer$
	CASE "B"
		Cutter_type= WsBallCutter
	CASE "E"
		Cutter_type= WsEndMill
	END SELECT
RETURN

Get_cutter_dia:!
	Stat=SetInputFormat("n4=","")
	Cutter_dia=NamedVarVal("cutter diameter",WsSystemVars,0.0)
	Stat=SetRadiusEquals(Cutter_dia/2)
	Cutter_dia=InputRadius("CUTTER DIAMETER",WsDiaInput)
	IF ExitScript(Destination$) THEN Leave

	IF Cutter_type= WsEndMill THEN

		Corner_rad=NamedVarVal("corner radius",WsSystemVars,corner_rad)
		Stat=SetEqualsValue(Corner_rad)
		Corner_rad=GetValue("CORNER RADIUS",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Corner_rad=Corner_rad

	END IF
RETURN

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
