
! script name: macros.d/cavity
! EXAMPLE: construct rectangular cavity with rounded corners and tapered sides

!A
!A NAME:
!A
!A 	Cavity
!A
!A DESCRIPTION:
!A
!A 	This script creates a rectangular cavity with rounded corners and
!A 	tapered sides.
!A
!B INSTRUCTIONS:
!B
!B 	RECTANGLE CENTER ABS X,Y: Enter the X,Y coordinates of the rectangle's
!B 		center.  NOTE: See XYZ INPUT OPTIONS.
!B
!B 	X LENGTH OF RECTANGLE: Enter the length of the rectangle, parallel to
!B 		the X axis.
!B
!B 	Y WIDTH OF RECTANGLE: Enter the width of the rectangle, parallel to
!B 		the Y axis.
!B
!B 	CORNER RADIUS: Enter the radius to be used in all corners.
!B
!B 	CAVITY DEPTH: Enter the depth of the cavity.
!B
!B 	TAPER ANGLE: Enter the angular taper of the sides of the cavity.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: cavity ")

DIM Answer$[256]
DIM Destination$[256]
DEG

DeActivateUndo
Rect_loop:!

Active_workplane=0
Work=CurrentWorkplane
IF (ValidGeometry(Work)) THEN Active_workplane=1

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

Stat=SetEqualsValue(Xlenr)
Xlen=GetValue("X LENGTH OF RECTANGLE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Xlenr=Xlen

Stat=SetEqualsValue(Ywidr)
Ywid=GetValue("Y WIDTH OF RECTANGLE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Ywidr=Ywid

Radius=InputRadius("CORNER RADIUS")
IF (ExitScript(Destination$)) THEN Leave

Stat=SetEqualsValue(Depthr)
Depth=GetValue("CAVITY DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Depthr=Depth

Stat=SetEqualsValue(Taperr)
Taper=InputAngle("TAPER ANGLE")
IF (ExitScript(Destination$)) THEN Leave
Taperr=Taper

Stat=SetRadiusEquals(Radius)

Xlen=Xlen-2*Radius
Ywid=Ywid-2*Radius
IF Xlen< 0.0001 THEN Rect_loop
IF Ywid< 0.0001 THEN Rect_loop

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

X1=Xcen-Xlen/2
Y1=Ycen+Ywid/2

X2=Xcen+Xlen/2
Y2=Y1

X3=X2
Y3=Ycen-Ywid/2

X4=Xcen-Xlen/2
Y4=Y3

R1=Radius

Tp1=Type3(3,X1,Y1,X2,Y2,Radius,Radius)
Tp2=Type3(3,X2,Y2,X3,Y3,Radius,Radius)
Stat=JoinTypes(Tp1,Tp2)
Tp3=Type3(3,X3,Y3,X4,Y4,Radius,Radius)
Stat=JoinTypes(Tp2,Tp3)
Tp4=Type3(3,X4,Y4,X1,Y1,Radius,Radius)
Stat=JoinTypes(Tp3,Tp4)
Stat=CloseType(Tp4)
Contour=TypesContour(Tp4,1)

Stat=DeleteEntity(Pc)

T1=ParallelTransform(Work,-Depth)
Stat=SetWorkplane(T1)

R2=Radius-Depth*TAN(Taper)

Tp1=Type3(3,X1,Y1,X2,Y2,R2,R2)
Tp2=Type3(3,X2,Y2,X3,Y3,R2,R2)
Stat=JoinTypes(Tp1,Tp2)
Tp3=Type3(3,X3,Y3,X4,Y4,R2,R2)
Stat=JoinTypes(Tp2,Tp3)
Tp4=Type3(3,X4,Y4,X1,Y1,R2,R2)
Stat=JoinTypes(Tp3,Tp4)
Stat=CloseType(Tp4)
Contour=TypesContour(Tp4,1)

Stat=DeleteEntity(Pc)

IF Active_workplane THEN
	Stat=SetWorkplane(Work)
ELSE
	StdWorkplane
END IF

P1=PointXYZ(X1-R1,Y1,0)
P2=PointXYZ(X1-R2,Y1,-Depth)
L1=LineTwoPoints(P1,P2,WsTRIMMED)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)

P1=PointXYZ(X2+R1,Y2,0)
P2=PointXYZ(X2+R2,Y2,-Depth)
L1=LineTwoPoints(P1,P2,WsTRIMMED)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)

P1=PointXYZ(X3+R1,Y3,0)
P2=PointXYZ(X3+R2,Y3,-Depth)
L1=LineTwoPoints(P1,P2,WsTRIMMED)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)

P1=PointXYZ(X4-R1,Y4,0)
P2=PointXYZ(X4-R2,Y4,-Depth)
L1=LineTwoPoints(P1,P2,WsTRIMMED)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)

IF R2 THEN
	P1=PointXYZ(X1,Y1+R1,0)
	P2=PointXYZ(X1,Y1+R2,-Depth)
	L2=LineTwoPoints(P1,P2,WsTRIMMED)
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)

	P1=PointXYZ(X2,Y2+R1,0)
	P2=PointXYZ(X2,Y2+R2,-Depth)
	L2=LineTwoPoints(P1,P2,WsTRIMMED)
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)

	P1=PointXYZ(X3,Y3-R1,0)
	P2=PointXYZ(X3,Y3-R2,-Depth)
	L2=LineTwoPoints(P1,P2,WsTRIMMED)
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)

	P1=PointXYZ(X4,Y4-R1,0)
	P2=PointXYZ(X4,Y4-R2,-Depth)
	L2=LineTwoPoints(P1,P2,WsTRIMMED)
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)
END IF

GOTO Rect_loop

Leave:!
IF Active_workplane THEN
	Stat=SetWorkplane(Work)
ELSE
	StdWorkplane
END IF
Stat=DeleteEntity(Pc)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
