
! script name: 3dcpaths.d/xsectioncuts
! var_topic:  generated cuts along specially oriented constant cross section

!A
!A NAME:
!A
!A 	Contour Cross Section Cuts 
!A
!A DESCRIPTION:
!A	
!A
!A	This script generates the cutterpath moves required to sweep a
!A	crosssection contour along a drive contour in a special orientation.
!A	The cross section is programmed in the xy-plane with the attachment
!A	point located a the origin. 
!A	The drive contour should be programmed in a workplane parallel to
!A	the xy-plane.
!A	The x axis on the cross section is located normal to the right
!A	of the drive contour.
!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 the 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 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	DRIVE CONTOUR: 
!B
!B 	CROSS CONTOUR: 
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: Cross section cuts ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Cross_cont$[256]
DIM Island$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Name$[256]

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


ActivateUndo

Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Units=CurrentUnits

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

Start:!
Offset=0
Taper=0
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3dcpaths.d/xsectioncuts"
  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:v3dcpaths.d/xsectioncuts"
END IF
Have_tool:!
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques

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)




Stat=SetPickMask(WsPickGeometry)
Pick_contour: !
Stat=SetInputFormat("a1",",N,")
Drive_contour=PickGroupEnt("DRIVE CONTOUR, None ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="N" THEN
        Has_exterior=0
        GOTO Ask_islands
END IF
IF (NOT ValidDependency(Drive_contour)) THEN Pick_contour
Stat= EntityNameCheck(Drive_contour,Contour$)
IF NOT LEN(Contour$) THEN Pick_contour
Stat=Highlight(Drive_contour,WsPickPen,WsDirArrowOff)
Has_exterior=1

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



Pick_contour2: !
Stat=SetInputFormat("n4","")
Cross_contour=PickGroupEnt("CROSS CONTOUR",0)
IF ExitScript(Destination$) THEN Leave
IF (NOT ValidDependency(Cross_contour)) THEN Pick_contour2
Stat= EntityNameCheck(Cross_contour,Cross_cont$)
IF NOT LEN(Cross_cont$) THEN Pick_contour2
Stat=Highlight(Cross_contour,WsPickPen,WsDirArrowOff)

Direction1= WsForward
Sign1= 1
Direction2= WsForward
Sign2= 1


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Cross Section Contour Cuts")
Stat= SetRecDelLastExec
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
Stat= NullReference(Cross_cont$)
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
V5= FreeParameter("Tolerance",Tol)
V6= FreeParameter("Direction1",Direction1)
V7= FreeParameter("Sign1",Sign1)
V8= FreeParameter("Direction2",Direction2)
V9= FreeParameter("Sign2",Sign2)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1,0.0,WsCircleMoves)
Stat=Setup3dCutter(Mdef1)
Stat= PrintToVarRecord("PlotOn")
IF Has_exterior THEN
Cutter_path=CrossSectionCuts(Contour$,Isles,V6,V7,Cross_cont$,V8,V9,V2,V5)
ELSE
Cutter_path=CrossSectionCuts(0,Isles,V6,V7,Cross_cont$,V8,V9,V2,V5)
END IF
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Isles)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("cross section cuts")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

Stat=DeleteEntity(Islands)
GOTO Start

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


