
! script name: pkvmach.d/contourgouge
! var_topic: cuts projected contour on solid using gouge avoidance

!A
!A NAME:
!A
!A 	Gouge Check: Contour	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script projects a path onto a solid body.  The center of
!A 	the cutter is constrained to move along the contour while staying in
!A 	contact with the part.  The corresponding cutter path is generated with
!A 	gouge avoidance.
!A
!B~ image location - images/contourgouge.gif
!B
!B INSTRUCTIONS:
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	PICK BODY TO BE MACHINED: Pick the body.  It may be a sheet or solid.
!B 		A group of faces may be selected instead of a body. This
!B 		group will be treated as if it were a free standing sheet body.
!B
!B 	PICK ADDITIONAL SURFACE GROUP, Name, Proceed   Additional synergy
!B 		surfaces may be added here to the body to be machined.
!B 		There surfaces may be used to keep the cutter out of areas
!B 		of the body or to to extend parts of the body for machining.
!B
!B 	PROJECT CONTOUR # : Pick the contour to be projected onto the part.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!C  NOTES:
!C
!C 	An additional feature of contour gouge is auto edge extension.  If the
!C 	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 	that the gouge check will work in tight corners where the cutter paths
!C 	extend beyond the edge of the part.
!C
!C
!C	If this function is applied to a group of faces, the group of
!C	faces is treated like a stand alone sheet body. It does not check
!C	for cutter interference in the body beyond the faces in the group.
!C	Special care should be taken with the behavior at the edge of the
!C	group where the edge rolling may cause interference. It is 
!C	recommended that face groups only be used when a boundary contour
!C	is present to contain the cuts.
!C


Stat=ChangeLabelStr("ScriptNameStatus","Function: solid contour gouge ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Body_name$[256]
DIM Contour$[256]
DIM Finish_var$[256]
DIM Name$[256]

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


Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Ext_dist=NamedVarVal("3D Auto Extend Dist",Ws3DSystemVars,0.010)
Units=CurrentUnits

ActivateUndo

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:pkvmach.d/vpkcontourgouge"
  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:pkvmach.d/vpkcontourgouge"
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:!
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)

Pick_loop:!
Stat=SetInputFormat("a1",",F,A,")
E1=WPKPickEntity("PICK BODY  Face group, Assembly ",WsPKPickAll,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "A"
	Pkassembly= WPKCurrentAssembly
	IF NOT WPKIsEntity(Pkassembly) THEN
       		 Stat=SetInputFormat("n4","")
       		 E1=WPKPickEntity("PICK ASSEMBLY")
       		 IF (ExitScript(Destination$)) THEN Leave
       		 IF NOT WPKIsEntity(E1) THEN pick_loop

       		 Pkbody= WPKParentBody(E1)
       		 Pkassembly= WPKParentAssembly(Pkbody)
	END IF
	IF NOT WPKIsEntity(Pkassembly) THEN Pick_loop
        Stat= EntityNameCheck(Pkassembly,Body_name$)
CASE "F"
	Stat=SetInputFormat("n4","")
	E1=WPKPickEntity("PICK GROUP TO BE MACHINED")
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT WPKIsEntity(E1) THEN Pick_loop
	G1= WPKContainingGroup(E1,1,WsTrue)
	IF NOT WPKIsEntity(G1) THEN Pick_loop
	Stat= EntityNameCheck(G1,Body_name$)
CASE ELSE
	IF NOT WPKIsEntity(E1) THEN Pick_loop
	Body_ent= WPKParentBody(E1)
	Stat= EntityNameCheck(Body_ent,Body_name$)
END SELECT


Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,P,")
Part_group=PickGroupEnt("PICK ADDITIONAL SURFACE GROUP, Name, Proceed ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        GOSUB Get_named_group
CASE "P"
        Extra_surfs=0
        GOTO Cut_part
END SELECT
IF NOT ValidDependency(Part_group) THEN Pick_loop
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Extra_surfs=1

Cut_part:!
Group=CreateGroup("TEMP")
Stat=SetPickMask(WsPickGeometry)
Cont_count=0
Pick_contour: !
Stat=SetInputFormat("a1",",P,")
Contour=PickGroupEnt("PROJECT CONTOUR #"&VAL$(Cont_count+1)&", Proceed ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="P" THEN Start_variation
IF (NOT ValidDependency(Contour)) THEN Pick_contour
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN Pick_contour
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)
Stat=AppendToGroup(Group,Contour)
Cont_count=Cont_count+1
GOTO Pick_contour

Start_variation:!

Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Contour Gouge Check")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
Contours=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Cont_count
        Contour= GroupElement(Group,I)
        Stat= EntityNameCheck(Contour,Contour$)
        RecordFunctionOn
        Stat= AppendToGroup(Contours,Contour$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V4= FreeParameter("Tolerance",Tol)
V6= FreeParameter("Extension dist",Ext_dist)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0.0,V1)
Stat=Setup3dCutter(Mdef1)
PlotOff
Stat= PrintToVarRecord("PlotOff")
Body_group= WPKConvertBody(Body_name$,WsPKConvertFaces)
Stat= SetEdgeExt(Body_group)
Stat= HideEntity(Body_group)
RecordFunctionOff
IF Extra_surfs THEN
RecordFunctionOn
        Part= CopyEntity(Part_group$)
        Stat= CombineGroups(Part,Body_group)
RecordFunctionOff
ELSE
RecordFunctionOn
        Part= CopyEntity(Body_group)
RecordFunctionOff
END IF
RecordFunctionOn
Stat= PrintToVarRecord("PlotOn")
PlotOn
Cutter_path=ContGrpGouge(0,Part,Contours,0.0,V4,0,V6)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteEntity(Part)
Stat= DeleteEntityList(Mdef1,Contours)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("contour gouge check")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

Stat= DeleteEntity(Group)
GOTO Pick_loop


Leave:!
PlotOn
Stat=SetInputFormat("n4","")
LowerSelectBox
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
