
! script name: 3dintersect.d/ztoolhole
! 3dtopic: associate tool with hole on 3D Part

!A
!A NAME:
!A
!A      Hole on 3D Part 	
!A
!A DESCRIPTION:
!A
!A      This script allows a hole to be constructed and associated with
!A      a specific tool on a 3D Part. The hole will be constructed
!A      in such a way that the depth of the hole will be the depth
!A      at which the tool comes in contact with the 3D part.
!A
!B INSTRUCTIONS:
!B
!B      INPUT TOOL ID NAME: The TOOLS selection box with the list of
!B              available tools will automatically appear for a selection
!B              to be made.
!B
!B      FINISH ALLOWANCE:  Give the finish allowance.
!B
!B      PICK GROUP, Single: Pick a group of points to determine
!B                              the tops of the holes or select S for
!B                              using single points for the top of the holes.
!B
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: part tool hole ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Toolid$[256]

ActivateUndo
Tool_assoc=0
Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)
Tool_assoc=NamedVarVal("tool association",WsToolVars,0.0)

Start:!

IF Tool_assoc THEN
	Stat=ToolSelectBox("TOOLS","selected tool")
END IF
IF (NOT Stat) AND Tool_assoc THEN 

	Stat=SetInputFormat("a99","")
	Stat=GetValue("INPUT TOOL ID NAME",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	IF Answer$="CANCEL" THEN Leave
	Name$=Answer$

	Tool=FindUserData(Name$,WsToolData)
	IF NOT ValidGeometry(Tool) THEN Start

	LowerSelectBox

	Stat=GetUserString(Tool,"Tool type:",Toolid$)
	Dia=GetUserValue(Tool,"Diameter:")
	Pt_angle=GetUserValue(Tool,"Point angle:")

	SELECT Toolid$
	CASE "DRILL","CENTER DRILL","TWIST DRILL"
		Cutter_type= WsEndMill
	CASE "COUNTER SINK"
		Cutter_type= WsEndMill
	CASE ELSE
		Pt_angle=180
	END SELECT
ELSE
	Tool_assoc=0

	Stat=SetInputFormat("n4=","")
	Diar=NamedVarVal("hole diameter",WsPointVars,0.0)
	Stat=SetRadiusEquals(Diar/2)

	Dia=InputRadius("HOLE DIAMETER",WsDiaInput)
	IF (ExitScript(Destination$)) THEN Leave
	Diar=Dia
	Hole_rad= Dia/2

END IF


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

Hole_rad= Dia/2
Cutter_type= WsEndMill
stat= Setup3dCutter(Cutter_type,Hole_rad,0.0,0.0,Finish_allow,0.0)

Pick_part:!
Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n4","")
Part=PickGroupEnt("PICK PART GROUP ")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Part) THEN Pick_part
Stat= SetPickMask(WsPickNoSurf)


Pick_loop:!
Stat= SetEqualsValue(0,"S")
Stat=SetInputFormat("a1=",",S,M,")
Pt_group=PickGroupEnt("PICK GROUP, Single, Multiple ",0,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "S", "s"
Pick_point:stat= SetInputFormat("n4","")
	Point= XyzInput("INPUT",WsXyzInput)
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT ValidGeometry(Point) THEN Pick_point
	Stat=SetLastPoint(Point,WsXyzInput)
	Group=CreateGroup("TEMPGROUP")
	stat= AddToGroup(Group,Point)
	Work=CurrentWorkplane
	Outgroup= ProjectPtGrpHoles(Work,Group,Part,Tol,Dia,Pt_angle)

	ActivateUndo
	Stat= AdvanceUndoNumber("points")
	Stat= AddGroupToUndo(WsUBCreateMode,Outgroup,0,WsTrue)

	stat= DeleteEntity(Outgroup)
	Stat= DeleteGroup(Group,WsTrue)
	GOTO Pick_point
CASE "M", "m"
Group=CreateGroup("TEMPGROUP")
Point_loop:Stat= SetInputFormat("n4","")
Pt_count= GroupCount(Group)
Pt=XyzInput("POINT #"&VAL$(Pt_count+1)&" ",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF Pt_count>0 THEN
	Plast= GroupElement(Group,Pt_count)
        Dist=DistTwoPts(Pt,Plast)
        IF (Dist>0.0001) THEN
                Stat=AddToGroup(Group,Pt)
                Stat=SetLastPoint(Pt,WsXyzInput)
                Stat= SetSelectedFlag(Pt,WsTrue)
                Stat= PlotEntity(Pt)
                GOTO Point_loop
        END IF
ELSE
        Stat=AddToGroup(Group,Pt)
        Stat=SetLastPoint(Pt,WsXyzInput)
        Stat= SetSelectedFlag(Pt,WsTrue)
        Stat= PlotEntity(Pt)
        GOTO Point_loop
END IF
Work=CurrentWorkplane
Outgroup= ProjectPtGrpHoles(Work,Group,Part,Tol,Dia,Pt_angle)
ActivateUndo
Stat= AdvanceUndoNumber("points")
Stat= AddGroupToUndo(WsUBCreateMode,Outgroup,0,WsTrue)
stat= DeleteEntity(Outgroup)
Stat= DeleteGroup(Group,WsTrue)
GOTO Pick_loop
END SELECT

IF NOT ValidGeometry(Pt_group) THEN Pick_loop
Work=CurrentWorkplane
Outgroup= ProjectPtGrpHoles(Work,Pt_group,Part,Tol,Dia,Pt_angle)
GOTO Pick_loop



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