/* CMD: ½Calculator                                                     */
/* By Brett Evan Hester      13032 Copenhill Rd. Dallas, Tx. 75240-5302 */
	MacrosName = "Calculator"
/* Macro Type:                                                          */
/* CALCULATES                                                           */
/* Description:                                                         */

Info1A = "!Calculator ©                  Information 1 of 5"
Info1B = ""
Info1C = "@This macro allows for performing calculations,  "
Info1D = "@even those that involve variables, constants,   "
Info1E = "@and commands.                                   "
Info1F = ""
Info1G = "þ Variables X, Y and Z default to the coords. of"
Info1H = "  selected pnt. or previous calculator settings."
Info1I = ""
Info1J = "þ The output from the last formula is sent to a "
Info1K = '  variable labeled "RESULT".                    '
Info1L = ""
Info1M = "@Information on constants, commands, and usage  "
Info1N = "@are on the following information windows.      "

Info2A = "!Calculator ©                  Information 2 of 5"
Info2B = ""
Info2C = "@þ Constants:                                    "
Info2D = '  Type "PI" to get 3.141592653589793238462643   '
Info2E = '  Type "Eunit" for 2.718281828459045235360287   '
Info2F = ""
Info2G = "@¤ Constants for conversion purposes:            "
Info2H = "þ 8 length constants are included, multiply by -"
Info2I = "  MtToFt, MtToIn, KmToMl, KmToFt, & the reverse "
Info2J = "þ 6 speed constants are included, multiply by - "
Info2K = "  KPHToMPH, KPHToFPS, MPHToFPS, and the reverse "
Info2L = "þ 2 angle constants are included, multiply by - "
Info2M = "  DegToRad or RadToDeg                          "
Info2N = "@Information on commands and usage to follow... "

Info3A = "!Calculator ©                  Information 3 of 5"
Info3B = ""
Info3C = "@þ Commands:                                     "
Info3D = "  SQRT(x) = Square Root  ABS(x)  = Absolute     "
Info3E = "  FACT(x) = Factorial    NINT(x) = Rounds off   "
Info3F = ""
Info3G = "@¤ Commands involving angle calculations:        "
Info3H = "  SIN(x°)    ASIN(x°)    SINH(x°)    ASINH(x°)  "
Info3I = "  COS(x°)    ACOS(x°)    COSH(x°)    ACOSH(x°)  "
Info3J = "  TAN(x°)    ATAN(x°)    TANH(x°)    ATANH(x°)  "
Info3K = ""
Info3L = "@NOTE: The computer calculates angles in radians."
Info3M = "@If you are suppling the angles in degrees, type "
Info3N = "@ALT-W (°) after the angle. This equals DegToRad."

Info4A = "!Calculator ©                  Information 4 of 5"
Info4B = ""
Info4C = "@þ Arithmetic operations and their priority:     "
Info4D = "@Operator           Priority  Example      Result"
Info4E = "+  (prefix conversion) 8     '3.12'       3.12  "
Info4F = '-  (prefix negation)   8     -"3.12"      -3.12 '
Info4G = "** (exponentiation)    7     0.5 ** 3     0.125 "
Info4H = "*  (multiplication)    6     1.5 * 1.50   2.250 "
Info4I = "/  (division)          6     6 / 3        2     "
Info4J = "%  (integer division)  6     -8 % 3       -2    "
Info4K = "// (remainder)         6     5.1 // 0.2   0.1   "
Info4L = "+  (addition)          5     3.1 + 4.05   7.15  "
Info4M = "-  (subtraction)       5     5.55 - 1     4.55  "
Info4N = "@((((( Use Parentheses When Ever Necessary. )))))"

Info5A = "!Calculator ©                  Information 5 of 5"
Info5B = "@               Plug-Ins and Go! ©               "
Info5C = "                           Hester and associates"
Info5D = "                           13032 Copenhill Road "
Info5E = "                           Dallas, Texas 75240  "
Info5F = "@Special Thanks to:                              "
Info5G = "Arnie Cachelin  Henry Ribron    Mark J. Holland "
Info5H = "J. Phil Kelso   Terry Wester    Steven K. Simms "
Info5I = "Kevin DeRita    Greg Glaser     William S. Hawes"
Info5J = "NewTek ©        Commodore ©     INOVAtronics ©  "
Info5K = ""
Info5L = "@This macro represents a lot of time & hard work."
Info5M = "@Encourage people to create new ones and not kill"
Info5N = "@that possibility by stealing those that are out."

/* -------------------------------------------------------------------- */
                                     /* Start Error Detection (See End) */
SIGNAL ON ERROR
SIGNAL ON SYNTAX
                                                   /* Address LightWave */
VT3DLib = ADDLIB("LWModelerARexx.port",0)
ADDRESS "LWModelerARexx.port"
                                               /* Add Support Functions */
SupportLib = "rexxsupport.library"
IF POS(SupportLib , SHOW('L')) = 0 THEN
	IF ~ADDLIB(SupportLib , 0 , -30 , 0) THEN DO
		CALL Notify(1,"!Can't find "SupportLib)
		CALL Exiting
	END
                                                  /* Add Math Functions */
MATHLIB= "rexxmathlib.library"
IF POS(MATHLIB , SHOW('L')) = 0 THEN
    IF ~ADDLIB(MATHLIB , 0 , -30 , 0) THEN DO
    	CALL Notify(1,"!Can't find "MATHLIB)
		IF VT3DLib THEN CALL REMLIB("LWModelerARexx.port")
		EXIT
	END

/* -------------------------------------------------------------------- */
                                    /* Reading Global Macro Preferences */
BEHDefaultFilePath = "Sys:"
BEHSettingsSavedTo = "T:"
BEHSpeechAndSound = "1"

IF (EXISTS("S:PlugInPrefs")) THEN DO
	IF (~OPEN(PlugInPrefs, "S:PlugInPrefs", 'R')) THEN BREAK
	IF (READLN(PlugInPrefs) ~= "PlugInPrefs") THEN BREAK
	BEHDefaultFilePath = READLN(PlugInPrefs)
	BEHSettingsSavedTo = READLN(PlugInPrefs)
	BEHSpeechAndSound = READLN(PlugInPrefs)
	CALL CLOSE PlugInPrefs
END

/* -------------------------------------------------------------------- */
                                            /* Recalling Macro Settings */
ReqFormula = "1 + 2"
ReqResults = "3"
ReqX = "0"
ReqY = "0"
ReqZ = "0"

PrefsFileName = BEHSettingsSavedTo||MacrosName||".PLUG"

IF (EXISTS(PrefsFileName)) THEN DO
	IF (~OPEN(PrefsFile, PrefsFileName, 'R')) THEN BREAK
	IF (READLN(PrefsFile) ~= MacrosName) THEN BREAK

	ReqFormula = READLN(PrefsFile)
	ReqResults = READLN(PrefsFile)
	ReqX = READLN(PrefsFile)
	ReqY = READLN(PrefsFile)
	ReqZ = READLN(PrefsFile)

	CALL CLOSE PrefsFile
END

/* -------------------------------------------------------------------- */
                                            /* If One Point is selected */
CALL Sel_Mode(USER)

NumberOfPnts = XFrm_Begin()

IF NumberOfPnts = 1 THEN DO
	PARSE VALUE Xfrm_GetPos(1) WITH ReqX ReqY ReqZ
END

CALL End_All()

/* -------------------------------------------------------------------- */
                                             /* Setting Fixed Variables */
PI = 3.141592653589793238462643
Eunit = 2.718281828459045235360287
                                                    /* Angle Conversion */
RadToDeg = 180 / PI  ; DegToRad = PI / 180
                                                 /* Distance Conversion */
MtToFt = 3.28084     ; FtToMt = 0.3048
MtToIn = 39.370079   ; InToMt = 0.0254
KmToMl = 0.621371    ; MlToKm = 1.609344
KmToFt = 3280.839895 ; FtToKm = 0.000305
                                                    /* Speed Conversion */
KPHToMPH = 0.621371  ; MPHToKPH = 1.609344
KPHToFPS = 0.911344  ; FPSToKPH = 1.09728
MPHToFPS = 1.466667  ; FPSToMPH = 0.681818

/* ******************************************************************** */
                                                   /* Main Body Of Code */
MainBodyOfCode:

	DO FOREVER
		CALL MenuRequester
		CALL Calculations
		CALL SaveSettings
	END

RETURN

/* ******************************************************************** */
                                                      /* User Interface */
MenuRequester:

	CALL Req_Begin("Calculator ©                                   by Brett Hester")

	ReqA = Req_AddControl("þ Formula:",'S',52)
	CALL Req_AddControl("¤ Result =",'T',ReqResults)
	CALL Req_AddControl("",'T',"")
	ReqC = Req_AddControl("Variable X",'S',20)
	ReqD = Req_AddControl("Variable Y",'S',20)
	ReqE = Req_AddControl("Variable Z",'S',20)
	CALL Req_AddControl("",'T',"")
	ReqF = Req_AddControl("",'CH', "Information")

	CALL Req_SetVal(ReqA, ReqFormula)
	CALL Req_SetVal(ReqC, ReqX)
	CALL Req_SetVal(ReqD, ReqY)
	CALL Req_SetVal(ReqE, ReqZ)
	CALL Req_SetVal(ReqF, 0)

	IF Req_Post() = 0 THEN CALL Exiting

	ReqFormula = Req_GetVal(ReqA)
	ReqX = Req_GetVal(ReqC)
	ReqY = Req_GetVal(ReqD)
	ReqZ = Req_GetVal(ReqE)
	BEHInfo = Req_GetVal(ReqF)

	CALL Req_End()

	IF BEHInfo = 1 THEN CALL InformationWindows

RETURN

/* -------------------------------------------------------------------- */
                                                        /* Calculations */
Calculations:
                                              /* User Defined Variables */
	INTERPRET "X = "||ReqX
	INTERPRET "Y = "||ReqY
	INTERPRET "Z = "||ReqZ
                                                /* Adding NEW Variables */
	Result = ReqResults
	Formula = ReqFormula
                           /* Structuring Formulas with Degree symbols  */
	QAAngle = 1
	DO UNTIL QAAngle = 0
		QAAngle = 0

		FormulaLength = LENGTH(Formula)

		QAAngle = POS("°", Formula)
		IF QAAngle ~= 0 THEN DO
			PartA = LEFT(Formula, QAAngle - 1)
			PartB = RIGHT(Formula, FormulaLength - QAAngle)

			Formula = PartA||" * "||DegToRad||PartB
		END
	END
                                                          /* The RESULT */
	INTERPRET "ReqResults = "||Formula
                                      /* If Formula is in Yes/No Format */
	IF POS("=", Formula) ~= 0 THEN DO
		IF ReqResults = 1 THEN ReqResults = "Correct"
		IF ReqResults = 0 THEN ReqResults = "Incorrect"
	END
                             /* If the Formula is actually an operation */
	IF ReqResults = "CALL 1" THEN ReqResults = "The above operation returned a positive response."
	IF ReqResults = "CALL 0" THEN ReqResults = "The above operation returned a negative response."

RETURN

/* -------------------------------------------------------------------- */
                                            /* Recording Macro Settings */
SaveSettings:

	IF (OPEN(PrefsFile, PrefsFileName, 'W')) THEN DO
		CALL WRITELN(PrefsFile, MacrosName)

		CALL WRITELN(PrefsFile, ReqFormula)
		CALL WRITELN(PrefsFile, ReqResults)
		CALL WRITELN(PrefsFile, ReqX)
		CALL WRITELN(PrefsFile, ReqY)
		CALL WRITELN(PrefsFile, ReqZ)

		CALL CLOSE PrefsFile
	END

RETURN

/* -------------------------------------------------------------------- */
                                                              /* Ending */
Exiting:

	IF (VT3DLib) THEN CALL REMLIB("LWModelerARexx.port")
	EXIT

RETURN

/* -------------------------------------------------------------------- */
                                                 /* Information Windows */
InformationWindows:

	OKorCancel = Notify(2, Info1A, Info1B, Info1C, Info1D, Info1E, Info1F, Info1G, Info1H, Info1I, Info1J, Info1K, Info1L, Info1M, Info1N)
	IF OKorCancel = 1 THEN OKorCancel = Notify(2, Info2A, Info2B, Info2C, Info2D, Info2E, Info2F, Info2G, Info2H, Info2I, Info2J, Info2K, Info2L, Info2M, Info2N)
	IF OKorCancel = 1 THEN OKorCancel = Notify(2, Info3A, Info3B, Info3C, Info3D, Info3E, Info3F, Info3G, Info3H, Info3I, Info3J, Info3K, Info3L, Info3M, Info3N)
	IF OKorCancel = 1 THEN OKorCancel = Notify(2, Info4A, Info4B, Info4C, Info4D, Info4E, Info4F, Info4G, Info4H, Info4I, Info4J, Info4K, Info4L, Info4M, Info4N)
	IF OKorCancel = 1 THEN OKorCancel = Notify(1, Info5A, Info5B, Info5C, Info5D, Info5E, Info5F, Info5G, Info5H, Info5I, Info5J, Info5K, Info5L, Info5M, Info5N)
	CALL MenuRequester

RETURN

/* -------------------------------------------------------------------- */
                                                      /* Error Handling */
SYNTAX:
ERROR:

	ErrCode = RC
	ErrLine = SIGL
	ErrInfo = ERRORTEXT(ErrCode)

	Err1 = "!Sorry!"
	Err2 = "An Error has been detected"
	Err3 = "@þ Macro -            "
	Err4 = "@þ Line Number -      "
	Err5 = "@þ Error Code -       "
	Err6 = "@þ Error Description -"
	Err7 = "@¤ Please Inform -    "
	Err8 = '  "Error Notice"     '
	Err9 = "  13032 Copenhill Rd."
	Err10 = "  Dallas, TX. 75240  "

	Call Notify(1,Err1,Err2,Err3,MacrosName,Err4,ErrLine,Err5,ErrCode,Err6,ErrInfo,Err7,Err8,Err9,Err10)

/* -------------------------------------------------------------------- */
                                             /* Advanced Error Handling */
	CALL SETCLIP("ErrorMacro",MacrosName)
	CALL SETCLIP("ErrorLine",ErrLine)
	CALL SETCLIP("ErrorCode",ErrCode)
	CALL SETCLIP("ErrorDesc",ErrInfo)

	PARSE SOURCE TempA TempB ErrFile TempC TempD TempE

	CALL SETCLIP("ErrorFile",ErrFile)

/* -------------------------------------------------------------------- */

	SIGNAL ON ERROR
	SIGNAL ON SYNTAX
	SIGNAL MainBodyOfCode
