! script name: vpksolids.d/vpktorus
! var_solids: construct a torus in the current workplane.

!A
!A NAME:
!A
!A 	Solid Torus  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will construct a torus. 
!A
!B INSTRUCTIONS:
!B
!B 	MAJOR RADIUS: Give the major or axial radius of the torus.
!B
!B 	MINOR RADIUS: Give the minor or radial radius of the torus.
!B
!B 	LOCATION POINT: Enter the location of the center of the torus.
!B
!B 	AXIS DIRECTION POINT: Enter a point giving the direction of the
!B 		axis of the torus.
!B
!# see also - htdocs/help.d/parasolids.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: torus")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Radius1_var$[256]
DIM Radius2_var$[256]


ActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
        R1= 25
        R2= 25
ELSE
        R1= 1
        R2= 1
END IF

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

Stat= GLPlotOn
Cloop:!
Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(R1)
R1=InputRadius("MAJOR RADIUS")
IF ExitScript(Destination$) THEN Leave
No_radius1_var= LastVariableInput(Radius1_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Cloop
        No_radius1_var=0
        Stat= EntityNameCheck(C1,Radius1_var$)
END IF

End_radius:!
Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(R2)
R2=InputRadius("MINOR RADIUS")
IF ExitScript(Destination$) THEN Leave
No_radius2_var= LastVariableInput(Radius2_var$)
C2= LastPickEnt
IF ValidGeometry(C2) THEN
        IF NOT ValidDependency(C2) THEN End_radius
        No_radius2_var=0
        Stat= EntityNameCheck(C2,Radius2_var$)
END IF

Pick_point:Stat=SetInputFormat("n4=","")
Ps=XyzInput("CENTER POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Ps) THEN Pick_point
E1= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF NOT ValidDependency(E1) THEN
        E1= CopyEntity(Ps)
        Stat= EntityNameCheck(E1,Point1$)
        Ps_created= WsTrue
ELSE
        Stat= EntityNameCheck(E1,Point1$)
        Ps_created= WsFalse
END IF
Stat=SetLastPoint(Ps)
Stat= DeleteEntity(Ps)

Axis_point:Stat=SetInputFormat("n4=","")
Pe=XyzInput("AXIS DIRECTION POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Pe) THEN Axis_point
E2= LastPickEnt
Para2= LastPickPara
Pick_return2= CurrPickReturn
IF NOT ValidDependency(E2) THEN
        E2= CopyEntity(Pe)
        Stat= EntityNameCheck(E2,Point2$)
        Pe_created= WsTrue
ELSE
        Stat= EntityNameCheck(E2,Point2$)
        Pe_created= WsFalse
END IF
Stat= DeleteEntity(Pe)

WPKClearErrorFlag
Stat= GenerateVarName("Body",Name$)
RecordFunctionOn
Stat= RecordDescr("TORUS: Solid")
SELECT Pick_return1
CASE WsGeometryEnd
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryEnd)
CASE WsGeometryMid
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryMid)
CASE WsGeomCenter
        Ps= ParameterReturnPt(Point1$,Para1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryEnd)
CASE WsGeometryMid
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryMid)
CASE WsGeomCenter
        Pe= ParameterReturnPt(Point2$,Para2,WsGeomCenter)
END SELECT
IF No_radius1_var THEN
        V1= FreeParameter("Radius1",R1)
        IF No_radius2_var THEN
                V2= FreeParameter("Radius2",R2)
                C1=WPKTorus(Ps,Pe,V1,V2)
        ELSE
                C1=WPKTorus(Ps,Pe,V1,Radius2_var$)
        END IF
ELSE
        IF No_radius2_var THEN
                V2= FreeParameter("Radius2",R2)
                C1=WPKTorus(Ps,Pe,Radius1_var$,V2)
        ELSE
                C1=WPKTorus(Ps,Pe,Radius1_var$,Radius2_var$)
        END IF
END IF
Stat= WPKNameBody(C1,Name$)
Stat= WPKSetBodyParent(C1)
Stat= DeleteEntityList(Ps,Pe)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("solid torus")
IF Ps_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point1$)
IF Pe_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point2$)
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck
GOTO Pick_point

Leave:!
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
