
! script name: macros.d/slotmill
! EXAMPLE: construct slot for milling

!A
!A NAME:
!A 	Slot Mill
!A
!A DESCRIPTION:
!A
!A 	This script constructs a slot for milling.  A hole is constructed
!A 	at the end point of the slot.  The appropriate tools will be
!A 	associated with the different operations needed to mill the slot
!A 	constructed.
!A
!B INSTRUCTIONS:
!B
!B 	INPUT MATERIAL FILE NAME: Enter the type of material the slot is
!B 		to be milled on.
!B
!B 	LENGTH OF SLOT: Enter the length of the slot to be constructed.
!B
!B 	WIDTH OF SLOT: Enter the width of the slot to be constructed.
!B
!B 	SLOT ANGLE: Enter the angle the slot is to be oriented at.
!B
!B 	SLOT END ABS X,Y: Enter the X,Y coordinates of the end point of
!B 		the slot.  NOTE: See XYZ INPUT OPTIONS.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: slot mill ")

DIM Answer$[256]
DIM Destination$[256]
DIM Toolid$[256]
DIM Toolmat$[256]
DIM Toollib$[256]
DIM Matlib$[256]
DIM File_name$[256]
DIM Tool1$[256],File1$[256]
DIM Tool2$[256],File2$[256]
DIM Tool3$[256],File3$[256]
DIM Tool4$[256],File4$[256]

DEG

DeActivateUndo
Stat=SetInputFormat("n4=","")

Stat= NamedVarStr("tools dir",WsSystemVars,"/usr/weber/util/tooldata/tools_in/",Toollib$)
Stat= NamedVarStr("material dir",WsSystemVars,"/usr/weber/util/tooldata/materials/",Matlib$)

Material=FindUserData("MATERIAL",WsMaterial)
IF (NOT (ValidGeometry(Material))) THEN
	Stat=SetInputFormat("a99","")
	Stat=GetValue("INPUT MATERIAL FILE NAME",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	File_name$= FileNameString(Matlib$,Answer$)
	Material=ReadUserData(WsMaterial,"MATERIAL",File_name$)
	Stat=SetInputFormat("n4","")
END IF

Slot_length=GetValue("LENGTH OF SLOT",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Slot_width=GetValue("WIDTH OF SLOT",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Slot_angle=InputAngle("SLOT ANGLE")
IF (ExitScript(Destination$)) THEN Leave

Stat=SetAngleEquals(Slotangle)




Slot_loop:!

Pc=XyzInput("SLOT END ",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave

Xcen=XPointValue(Pc)
Ycen=YPointValue(Pc)

Radius=Slot_width/2
R1=Radius
R2=Slot_length-Radius
X1=Xcen+R1*COS(Slot_angle)
Y1=Ycen+R1*SIN(Slot_angle)

X2=Xcen+R2*COS(Slot_angle)
Y2=Ycen+R2*SIN(Slot_angle)

Pc1=PointXYZ(X1,Y1,0)
Pc2=PointXYZ(X2,Y2,0)

Tp1=Type3(3,X1,Y1,X2,Y2,Radius,Radius)
IF (NOT ValidGeometry(Tp1)) THEN Leave
Tp2=Type3(3,X2,Y2,X1,Y1,Radius,Radius)
IF (NOT ValidGeometry(Tp2)) THEN Leave
Stat=JoinTypes(Tp1,Tp2)
Stat=CloseType(Tp2)
Contour=TypesContour(Tp2,1)


Tool1$="#3 Center Drill"
File1$="centerdrills/N3cdrill"

IF (Slot_width>=0.190)AND(Slot_width<0.400) THEN
	Tool2$="5/16 Twist Drill"
	File2$="twistdrills/twistdr5_16"
	Tool3$="1/4 Endmill"
	File3$="endmills/endmill_0.25"
	Tool4$="1/4 Endmill"
	File4$="endmills/endmill_0.25"
	GOTO Tool3
END IF

IF (Slot_width>=0.400)AND(Slot_width<0.580) THEN
	Tool2$="7/16 Twist Drill"
	File2$="twistdrills/twistdr7_16"
	Tool3$="3/8 Endmill"
	File3$="endmills/endmill_0.375"
	Tool4$="3/8 Endmill"
	File4$="endmills/endmill_0.375"
	GOTO Operation1
END IF

IF (Slot_width>=0.580)AND(Slot_width<0.820) THEN
	Tool2$="9/16 Twist Drill"
	File2$="twistdrills/twistdr9_16"
	Tool3$="1/2 Endmill"
	File3$="endmills/endmill_0.50"
	Tool4$="1/2 Endmill"
	File4$="endmills/endmill_0.50"
	GOTO Operation1
END IF

IF (Slot_width>=0.820)AND(Slot_width<1.090) THEN
	Tool2$="13/16 Twist Drill"
	File2$="twistdrills/twistdr13_16"
	Tool3$="3/4 Endmill"
	File3$="endmills/endmill_0.75"
	Tool4$="3/4 Endmill"
	File4$="endmills/endmill_0.75"
	GOTO Operation1
END IF


IF (Slot_width>=1.090) THEN
	Tool2$="17/16 Twist Drill"
	File2$="twistdrills/twistdr17_16"
	Tool3$="1.0 Endmill"
	File3$="endmills/endmill_1.0"
	Tool4$="1.0 Endmill"
	File4$="endmills/endmill_1.0"
	GOTO Operation1
END IF

Operation1:!  Center drill
IF Skip_op1 THEN Operation2

Tool1=FindUserData(Tool1$,WsToolData)
IF (NOT (ValidGeometry(Tool1))) THEN
	File_name$= FileNameString(Toollib$,File1$)
	Tool1=ReadUserData(WsToolData,Tool1$,File_name$)
	Stat=GetUserString(Tool1,"Tool id name:",Answer$)
	Stat=ChangeDataName(Tool1,Answer$)
END IF
Dia1=GetUserValue(Tool1,"Diameter:")
Depth1=GetUserValue(Tool1,"Maximum depth of cut:")
F1=GetUserValue(Tool1,"Machinability factor:")
Angle1=GetUserValue(Tool1,"Point angle:")
Surface_speed=GetUserValue(Material,"Twist Drill(sfpm)(HSS):")
Ipr=GetFeedrate(Tool1,Material,2)
Speed=F1*(12*Surface_speed)/(PI*Dia1)
Feed=Ipr*Speed
Speed=PROUND(Speed,0)
Feed=PROUND(Feed,-2)
Stat=PutUserValue(Tool1,"Feed(ipm):",Feed)
Stat=PutUserValue(Tool1,"Speed(rpm):",Speed)

H1=Hole(Pc1,Dia1,Depth1,Angle1)
Stat=SetLayer(H1,200)
Stat=AssociateTool(H1,Tool1)
Stat=SetColor(H1,28)


Operation2:!  Drill startup hole
IF Skip_op2 THEN Operation3

Tool2=FindUserData(Tool2$,WsToolData)
IF (NOT (ValidGeometry(Tool2))) THEN
	File_name$= FileNameString(Toollib$,File2$)
	Tool2=ReadUserData(WsToolData,Tool2$,File_name$)
	Stat=GetUserString(Tool2,"Tool id name:",Answer$)
	Stat=ChangeDataName(Tool2,Answer$)
END IF
Dia2=GetUserValue(Tool2,"Diameter:")
Depth2=GetUserValue(Tool2,"Maximum depth of cut:")
F2=GetUserValue(Tool2,"Machinability factor:")
Surface_speed=GetUserValue(Material,"Twist Drill(sfpm)(HSS):")
Angle2=GetUserValue(Tool2,"Point angle:")
Ipr=GetFeedrate(Tool2,Material,2)
Speed=F2*(12*Surface_speed)/(PI*Dia2)
Feed=Ipr*Speed
Speed=PROUND(Speed,0)
Feed=PROUND(Feed,-2)
Stat=PutUserValue(Tool2,"Feed(ipm):",Feed)
Stat=PutUserValue(Tool2,"Speed(rpm):",Speed)

H2=Hole(Pc1,Dia2,Depth2,Angle2)
Stat=SetLayer(H2,200)
Stat=AssociateTool(H2,Tool2)
Stat=SetColor(H2,19)



Operation3:!  Rough mill between centers
IF Skip_op3 THEN Operation4

Tool3:Tool3=FindUserData(Tool3$,WsToolData)
IF (NOT (ValidGeometry(Tool3))) THEN
	File_name$= FileNameString(Toollib$,File3$)
	Tool3=ReadUserData(WsToolData,Tool3$,File_name$)
	Stat=GetUserString(Tool3,"Tool id name:",Answer$)
	Stat=ChangeDataName(Tool3,Answer$)
END IF
Dia3=GetUserValue(Tool3,"Diameter:")
Depth3=GetUserValue(Tool3,"Maximum depth of cut:")
F3=GetUserValue(Tool3,"Machinability factor:")
Nflutes=GetUserValue(Tool3,"Number flutes:")
Surface_speed=GetUserValue(Material,"Rough (sfpm)(HSS):")
Ipt=GetUserValue(Material,"Rough (ipt)(HSS):")
Speed=F3*(12*Surface_speed)/(PI*Dia3)
Feed=Ipt*Nflutes*Speed
Speed=PROUND(Speed,0)
Feed=PROUND(Feed,-2)
Stat=PutUserValue(Tool3,"Feed(ipm):",Feed)
Stat=PutUserValue(Tool3,"Speed(rpm):",Speed)

L1=LineTwoPoints(Pc1,Pc2)
Stat=SetLayer(L1,201)
Stat=AssociateTool(L1,Tool3)
Stat=SetColor(L1,13)


Operation4:!  Finish  mill 
IF Skip_op4 THEN Leave

Tool4:Tool4=FindUserData(Tool4$,WsToolData)
IF (NOT (ValidGeometry(Tool4))) THEN
	File_name$= FileNameString(Toollib$,File4$)
	Tool4=ReadUserData(WsToolData,Tool4$,File_name$)
	Stat=GetUserString(Tool4,"Tool id name:",Answer$)
	Stat=ChangeDataName(Tool4,Answer$)
END IF
Dia4=GetUserValue(Tool4,"Diameter:")
Depth4=GetUserValue(Tool4,"Maximum depth of cut:")
F4=GetUserValue(Tool4,"Machinability factor:")
Nflutes=GetUserValue(Tool4,"Number flutes:")
Surface_speed=GetUserValue(Material,"Finish (sfpm)(HSS):")
Ipt=GetUserValue(Material,"Finish (ipt)(HSS):")
Speed=F4*(12*Surface_speed)/(PI*Dia4)
Feed=Ipt*Nflutes*Speed
Speed=PROUND(Speed,0)
Feed=PROUND(Feed,-2)
Stat=PutUserValue(Tool4,"Feed(ipm):",Feed)
Stat=PutUserValue(Tool4,"Speed(rpm):",Speed)

Stat=SetCurrentColor(201)
Direction=1
Pen_color=150
C1=TypeElement(Tp1,1)
! extra roughing cuts

Finish_allowance=0.025

D1=(Slot_width-Dia3)/2
IF (D1>Finish_allowance) THEN

	Width_cut=.700*Dia3/2
	N1=INT(D1/Width_cut)
	Offset1=D1
	FOR I=1 TO N1
		Dist=Offset1-I*Width_cut
		E2=OffsetContour(C1,Dist,Direction,Pen_color,WsDirArrowOn)
		IF(ValidGeometry(E2)) THEN
			Stat=AssociateTool(E2,Tool3)
		END IF
	NEXT I
	A1=N1*Width_cut-Finish_allowance
	IF (A1>N1) THEN
		Dist=Dia3/2+Finish_allowance
		E2=OffsetContour(C1,Dist,Direction,Pen_color,WsDirArrowOn)
		IF(ValidGeometry(E2)) THEN
			Stat=AssociateTool(E2,Tool3)
		END IF
	END IF
END IF

! Finish cut
Stat=SetCurrentColor(202)
Dist=Dia4/2
E2=OffsetContour(C1,Dist,Direction,Pen_color,WsDirArrowOn)
IF(ValidGeometry(E2)) THEN
	Stat=AssociateTool(E2,Tool4)
END IF

Stat=SetCurrentColor(0)


Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pc1)
Stat=DeleteEntity(Pc2)
GOTO Slot_loop

Leave:!
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pc1)
Stat=DeleteEntity(Pc2)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
