
! script name: vitem.d/2dmoveitem
! var_topic: 2D rotate and position an item

!A
!A NAME:
!A
!A 	Move Item 2D	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows a item to be rotated and positioned.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ENTITY:  Pick an entity in the item
!B 		to be moved. If the entity is contained in more than
!B 		one item a select box will appear with the choice of
!B 		items to be moved.
!B
!B 	REFERENCE POINT: Pick the point to be
!B 		used as the reference for the rotate.
!B
!B 	DESTINATION POINT: Pick the destination point.
!B
!B 	ROTATION ANGLE: Enter the angular amount
!B 		the entities should be rotated.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: Move Item 2D")

DIM Answer$[256]
DIM Destination$[256]
DIM Contour$[256]
DIM Name$[256]
DIM Gname$[256]
DIM Item_name$[256]
DIM Center$[256]
DIM Point$[256]
DIM Angle_var$[256]
DIM Class$[256]
DIM A$[256]
DIM B$[256]
DEG
Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Start: !
Pick_entity:!
Stat= SetInputFormat("n4","")
E1=PickEntity("PICK ENTITY")
IF ExitScript(Destination$) THEN Leave

Group= EntityItemGroup(E1)
IF NOT ValidGeometry(Group) THEN Leave
Nitems= GroupCount(Group)
IF Nitems= 0 THEN Leave
IF Nitems=1 THEN
        Item_ent= GroupElement(Group,1)
        Id_number= UserDataType(Item_ent)
        Stat= DeleteEntity(Group)
        IF NOT ValidDependency(Item_ent) THEN Existing_items
ELSE
Existing_items:!
Stat=UserSelectBox("EXISTING ITEMS","Item selected",0,WsPropGroupSelect,Group)

        Pick_item:!
        Stat=SetInputFormat("a99","")
        Stat=GetValue("INPUT ITEM ID NAME",Name$)
        IF (ExitScript(Destination$)) THEN Leave
        SELECT Name$
        CASE "CANCEL"
                 Stat= DeleteEntity(Group)
                 GOTO Start
        END SELECT

	Num1= NumericPrefix(Name$)
        IF NOT Num1 THEN Pick_item
        Item_ent= GroupElement(Group,Num1)
        IF NOT ValidDependency(Item_ent) THEN Start
        Stat= DeleteEntity(Group)
        LowerSelectBox
        Id_number= UserDataType(Item_ent)
END IF
stat= SetInputFormat("n4","")
Stat= SetSelectedFlag(Item_ent,WsTrue)
Stat= PlotEntity(Item_ent)
Stat= EntityNameCheck(Item_ent,Item_name$)

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


Stat= SetSelectedFlag(Item_ent,WsFalse)
Stat= EraseEntity(Item_ent)

Stat= GenerateVarName(WsGroupEnt,Gname$)
Stat= GenerateVarName(WsDataStringEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("ROTATE: 2d position item")
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)
ELSE
	V1= FreeParameter("Angle",Angle_var$)
END IF
P1= PointXYZ(0,0,1)
Paxis= AddVectors(Pc,P1)
Axis_line= LineTwoPoints(Pc,Paxis)
Work= CurrentWorkplane
Stat= SetWorkplane
Pm= SubtractVectors(Pt,Pc)
Shift_trans=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Axis_trans= RotAboutAxisTrans(Axis_line,V1)
Trans1=MultTransforms(Shift_trans,Axis_trans)
G1= GrpByProp(Item_name$,UserDataType(Item_name$))
ActivateUndo
Stat= AdvanceUndoNumber("move item")
Stat= AddGroupToUndo(WsUBModifyMode,G1,0,WsFalse)
Stat= TransGroupTrans(G1,Trans1)
Stat= AutoNameGroup(G1,Gname$)
Stat= SetNoPathFlag(G1,WsTrue)
Stat= SetSelectedFlag(G1,WsFalse)
Stat= DeleteEntityList(Shift_trans,Axis_trans,Pm,Paxis,Axis_line)
Stat= DeleteEntityList(P1,Pc,Pt,G1)
Stat= SetWorkplane(Work)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat= PlotEntity(Item_ent)

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Dest_point

Leave:!
Stat= SetSelectedFlag(Item_ent,WsFalse)
Stat= PlotEntity(Item_ent)
Stat= SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END

