
! script name: vrotate.d/2drotposbox
! var_topic: 2D rotate and position boxed group of entities

!A
!A NAME:
!A
!A 	Box: 2D Rotate and Position	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows a group of entities or a single entity to be
!A 	rotated and and positioned.
!A
!B INSTRUCTIONS:
!B
!B 	SELECT VIEW FOR SELECTION BOX: Pick
!B 		the view that elements are to be rotated in.
!B
!B 	REFERENCE POINT : Pick the point to
!B 		be used as the reference for the rotation.
!B
!B 	DESTINATION POINT: Pick the destination point.
!B
!B 	ROTATION ANGLE: Enter the angular
!B 		amount the entities should be rotated.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: 2d rotate and position boxed group ")

DIM Answer$[256]
DIM Destination$[256]
DIM Element$[256]
DIM Name$[256]
DIM Center$[256]
DIM Point$[256]
DIM Angle_var$[256]
DEG

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave
Start: !
ActivateUndo

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

stat= SetInputFormat("n4","")

Group=CreateGroup("TEMP")


Vnum= LastPickView
Stat=PickBox(Group,Vnum,WsPickPen)
IF ExitScript(Destination$) THEN Leave

IF NOT ValidGeometry(Group) THEN 
	GOTO Start
ELSE
	IF NOT GroupCount(Group) THEN Start
END IF
Num_elements= GroupCount(Group)
IF Num_elements> 500 THEN
	Stat= SetInputFormat("n99","")
	Stat= GetValue("TOO MANY ELEMENTS, Proceed",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Stat= DeleteEntity(Group)
	GOTO Start
END IF
Stat= SetSelectedFlag(Group,WsTrue)
Stat= PlotEntity(Group)

Ref_point:E2=PickEntity("REFERENCE POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E2) THEN Ref_point
Para1= LastPickPara
Pick_return1= CurrPickReturn
Stat= EntityNameCheck(E2,Center$)

Dest_point:E2=PickEntity("DESTINATION POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E2) THEN Dest_point
Para2= LastPickPara
Pick_return2= CurrPickReturn
Stat= EntityNameCheck(E2,Point$)

Angle_ques:Angle=InputAngle("ROTATION ANGLE")
IF ExitScript(Destination$) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)
L1= LastPickEnt
IF ValidGeometry(L1) THEN
	IF NOT ValidDependency(L1) THEN Angle_ques
	No_angle_var=0
	Stat= EntityNameCheck(L1,Angle_var$)
END IF


Num_elements= GroupCount(Group)
Stat= GenerateVarName(WsVarRecordEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("ROTATE: 2d rotate and position mult ent")
Stat= SetRecDelLastExec
Egroup= CreateGroup("Picked Entities")

FOR I=1 TO Num_elements
RecordFunctionOff
	E1= GroupElement(Group,I)
	Stat= EntityNameCheck(E1,Element$)
	IF ValidDependency(E1,WsFalse) THEN 
		RecordFunctionOn
		Stat= AddToGroup(Egroup,Element$)
		RecordFunctionOff
	END IF
NEXT I
RecordFunctionOn
SELECT Pick_return1
CASE WsGeometryEnd
	Pc= ParameterReturnPt(Center$,Para1,WsGeometryEnd)
CASE WsGeometryMid
	Pc= ParameterReturnPt(Center$,Para1,WsGeometryMid)
CASE WsGeomCenter
	Pc= ParameterReturnPt(Center$,Para1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
	Pt= ParameterReturnPt(Point$,Para2,WsGeometryEnd)
CASE WsGeometryMid
	Pt= ParameterReturnPt(Point$,Para2,WsGeometryMid)
CASE WsGeomCenter
	Pt= ParameterReturnPt(Point$,Para2,WsGeomCenter)
END SELECT
IF No_angle_var THEN
	V1= FreeParameter("Angle",Angle)
        G1= PlaneRotateGroup(Egroup,Pc,V1)
ELSE
        G1= PlaneRotateGroup(Egroup,Pc,Angle_var$)
END IF
Move_vector= VectorTwoPts(Pc,Pt)
G2= VectorCopyGroup(G1,Move_vector)
Stat= AutoNameGroup(G2,Name$)
RecordFunctionOff
Stat= SetSelectedFlag(G2,WsFalse)
RecordFunctionOn

ActivateUndo
Stat= AdvanceUndoNumber("rotate contour")
Stat= AddGroupToUndo(WsUBCreateMode,G2,2,WsTrue)

Stat= DeleteSubGroups(G2)
Stat= DeleteGroup(G1,WsTrue)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Move_vector)
Stat= DeleteEntity(Egroup)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Dest_point

Leave: !
Stat= SetInputFormat("n4","")
Stat= SetSelectedFlag(Group,WsFalse)
Stat= PlotEntity(Group)
Stat= DeleteEntity(Group)

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

