
! script name: 3dmacros.d/sqrcircle
! EXAMPLE: create a ruled surface which is square blended into a circle

!A
!A NAME:
!A
!A 	SQUARE CIRCLE
!A
!A DESCRIPTION:
!A
!A 	This script creates a ruled surface which is a square blended
!A 	into a circle.
!A
!B INSTRUCTIONS:
!B
!B 	RECTANGLE CENTER ABS X,Y: Enter the X,Y coordinates of the rectangles'
!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 the
!B 		Y axis.
!B
!B 	CORNER RADIUS: Enter the radius to be used in all corners.
!B
!B 	HEIGHT: Enter the height.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: square circle ")

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

ActivateUndo
Rect_loop:!
Stat=SetInputFormat("n4","")

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)
Xlength=GetValue("X LENGTH OF RECTANGLE SIDE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Xlenr=Xlength

Stat=SetEqualsValue(Ywidr)
Ywidth=GetValue("Y LENGTH OF RECTANGLE SIDE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Ywidr=Ywidth

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

CRadius=InputRadius("CIRCLE RADIUS")
IF (ExitScript(Destination$)) THEN Leave

Stat=SetInputFormat("n4=","")
Pcir=XyzInput("CIRCLE CENTER ",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetRadiusEquals(Radius)

Stat=SetEqualsValue(Zheight)
ZHeight=GetValue("HEIGHT",Answer$)
IF (ExitScript(Destination$)) THEN Leave

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

Xlen= Xlength- 2*Radius
Ywid= Ywidth- 2*Radius

Delx= 0.00025
IF Xlen< 0.0005 THEN Rect_loop
IF Ywid< 0.0005 THEN Rect_loop

X0= Xcen- Xlength/2
Y0= Ycen

X1= X0
Y1= Y0 + Delx

X2=Xcen- Xlen/2
Y2=Ycen+ Ywid/2

X3= Xcen- Delx
Y3= Ycen+ Ywidth/2

X4= Xcen
Y4= Y3

R1=Radius

Tp0=Type1(1,X0,Y0,X1,Y1)
Tp1=Type3(3,X1,Y1,X2,Y2,0,R1)
Stat=JoinTypes(Tp0,Tp1)
Tp2=Type3(3,X2,Y2,X3,Y3,R1,0)
Stat=JoinTypes(Tp1,Tp2)
Tp3=Type1(1,X3,Y3,X4,Y4)
Stat=JoinTypes(Tp2,Tp3)
Tp4=InvertTypes(Tp0,Tp3,Tp3,90)
Tp5=InvertTypes(Tp0,Tp4,Tp4,0)
Stat=CloseType(Tp5)
Contour1=TypesContour(Tp5,1)


T1=ParallelTransform(Work,Zheight)

Xcen=XPointValue(Pcir)
Ycen=YPointValue(Pcir)

X0= Xcen- Cradius
Y0= Ycen

X1= X0
Y1= Y0 + Delx

X2=Xcen- 2*Delx
Y2=Ycen+ 2*Delx

X3= Xcen- Delx
Y3= Ycen+ Cradius

X4= Xcen
Y4= Y3

R1= Cradius- 2*Delx

Tp0=Type1(1,X0,Y0,X1,Y1)
Tp1=Type3(3,X1,Y1,X2,Y2,0,R1)
Stat=JoinTypes(Tp0,Tp1)
Tp2=Type3(3,X2,Y2,X3,Y3,R1,0)
Stat=JoinTypes(Tp1,Tp2)
Tp3=Type1(1,X3,Y3,X4,Y4)
Stat=JoinTypes(Tp2,Tp3)
Tp4=InvertTypes(Tp0,Tp3,Tp3,90)
Tp5=InvertTypes(Tp0,Tp4,Tp4,0)
Stat=CloseType(Tp5)
Contour2=TypesContour(Tp5,1)
Stat=TransGroupTrans(Contour2,T1,0,1)

Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pcir)

Blend: !
StdWorkplane
Ruled_type= 1
Surf1= RuledSurface(Ruled_type,contour1,contour2,"RULED SURFACE")

ActivateUndo
Stat= AdvanceUndoNumber("ruled surf")
Stat= AddItemToUndo(WsUBCreateMode,Surf1)

stat= UnLink(Contour1)
stat= DeleteGroup(Contour1,1)
stat= UnLink(Contour2)
stat= DeleteGroup(Contour2,1)
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
