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

!A
!A NAME:
!A
!A 	Graphical 2D	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows an 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: Graphical 2d")

DIM Answer$[256]
DIM Destination$[256]
DIM Group$[256]
DIM Name$[256]
DIM Gname$[256]
DIM Item_name$[256]
DIM Center$[256]
DIM Point$[256]
DIM Class$[256]
DIM A$[256]
DIM B$[256]
DEG

Place_min= NamedVarVal("Graph place min",WsSystemVars,50)
Place_max= NamedVarVal("Graph place max",WsSystemVars,500)
Place_density= NamedVarVal("Graph place density",WsSystemVars,3)

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

ActivateUndo
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$)
Group1= GrpByProp(Item_ent,UserDataType(Item_ent))
IF NOT ValidGeometry(Group1) THEN Start

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

Place_group:!
Angle= 0
Pdest= CopyEntity(Pref)
Axis_line= WorkplaneAxisLine(Work,Pref)

Vnum= LastPickView
stat= SetInputFormat("n4b","")
Group2= MakeSparseGroup(Group1,Place_min,Place_max,Place_density)
Stat= EraseEntity(Item_ent)
Stat= EraseEntity(Group1)
Stat= MakeInvisible(Group1)
Stat= MakeInvisible(Pref)
Stat= MakeInvisible(Pdest)
Rotate_angle= DynamicMoveGroup(Vnum,Group2,Pref,Pdest,Angle,"PICK DESTINATION POINT")
Stat= MakeVisible(Group1)
Stat= DeleteEntityList(Group1,Group2)
IF ExitScript(Destination$) THEN
        Stat= PlotEntity(Group1)
        GOTO Leave
END IF
E2= LastPickEnt
Para2= LastPickPara
Pick_return2= CurrPickReturn
IF NOT ValidDependency(E2) THEN
        E2= CopyEntity(Pdest)
        Stat= MakeVisible(E2)
        Stat= EntityNameCheck(E2,Point$)
ELSE
        Stat= EntityNameCheck(E2,Point$)
END IF
Stat= DeleteEntityList(Pref,Pdest,Axis_line)
Stat= SetSelectedFlag(Item_ent,WsFalse)

RecordFunctionOn
Stat= RecordDescr("MOVE: 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
V1= FreeParameter("Angle",Rotate_angle)
Work= CurrentWorkplane
L1= WorkplaneAxisLine(Work,Pc)
Stat=SetWorkplane
Pm= SubtractVectors(Pt,Pc)
Trans1=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Trans2= RotAboutAxisTrans(L1,V1)
Trans3=MultTransforms(Trans1,Trans2)
G1= GrpByProp(Item_name$,UserDataType(Item_name$))
ActivateUndo
Stat= AdvanceUndoNumber("move item")
Stat= AddGroupToUndo(WsUBModifyMode,G1,0,WsFalse)
Stat= TransGroupTrans(G1,Trans3)
Stat= DeleteEntityList(Trans1,Trans2)
Stat= DeleteEntityList(G1,L1,Pm,Pc,Pt)
Stat= SetWorkplane(Work)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat= SetSelectedFlag(Item_ent,WsFalse)
Stat= PlotEntity(Item_ent)

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Start

Leave:!
Stat= DeleteEntityList(Group1,Pref,Pdest,Axis_line)
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

