
! script name: items.d/dynmoveitem
! topic: Dynamic move an item

!A
!A NAME:
!A
!A 	Dynamic Move An Item
!A
!A DESCRIPTION:
!A
!A 	This script moves an item flag graphically.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ENTITY: Pick an entity in the item to be moved. If the entity
!B 	is contained in more than one item, a select box will appear with
!B 	the choice of items to be moved.
!B
!B 	REFERENCE POINT ABS X,Y,Z: Enter the X,Y,Z coordinates of the point
!B 	to be used as the reference for the move.  NOTE: See XYZ INPUT OPTIONS.
!B
!B 	PICK DESTINATION POINT: The elements to be moved are displayed
!B 	graphically with the cursor.  Clicking the Select Button on the mouse
!B 	will select the location.  The numeric keypad controls the orientation.
!B
!B 			(6) 0   degree orientation.
!B 			(9) 45  degree orientation.
!B 			(8) 90  degree orientation.
!B 			(7) 135 degree orientation.
!B 			(4) 180 degree orientation.
!B 			(1) 225 degree orientation.
!B 			(2) 270 degree orientation.
!B 			(3) 305 degree orientation.
!B
!B 		(+) and (-) turn the entities +/- the angular increment.
!B 		The default angular increment is 5 degrees. 
!B 		Hitting (A) will change the increment to 10 degrees.		
!B 		Hitting (a) will change the increment to 1 degree.		
!B 		Hitting (F) or (f) will change the increment to 0.1 degree.
!B 		Hitting (D) or (d) will return the increment to 5 degrees.
!B
!C NOTES:
!C
!C	Item flags should not be attached to non-geometry.
!C
!# see also - htdocs/help.d/items.htm
!# see also - htdocs/help.d/property.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: dynamic move item ")

DIM Answer$[256]
DIM Destination$[256]
DIM Name$[256]
DIM Class$[256]
DIM A$[256]
DIM B$[256]
DEG

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

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

Start: !
Active_workplane=0
Work=CurrentWorkplane
IF ValidGeometry(Work) THEN Active_workplane=1

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)
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 ValidGeometry(Item_ent) THEN Start
	Stat= DeleteEntity(Group)
	LowerSelectBox
	Id_number= UserDataType(Item_ent)
END IF
stat= SetInputFormat("n4","")

Group= GrpByProp(Item_ent,Id_number)
IF NOT ValidGeometry(Group) THEN
        GOTO Start
ELSE
        IF NOT GroupCount(Group) THEN 
		Stat= DeleteEntity(Group)
		GOTO Start
	END IF
END IF

Ref_point:!
Stat=SetPickMask(WsPickNoSurf)
Pref=XyzInput("REFERENCE POINT ",WsXyzInput)
IF ExitScript(Destination$) THEN Leave

Copy_loop:!
Angle= 0
Pdest= CopyEntity(Pref)
P1= PointXYZ(0,0,1)
Paxis= AddVectors(Pref,P1)
Stat= DeleteEntity(P1)
Axis_line= LineTwoPoints(Pref,Paxis)

Vnum= LastPickView
stat= SetInputFormat("n4b","")
Group1= CopyGroup(Group,WsFalse)
Group2= MakeSparseGroup(Group1,Place_min,Place_max,Place_density)
Rotate_angle= DynamicMoveGroup(Vnum,Group2,Pref,Pdest,Angle,"PICK DESTINATION POINT",WsTrue)
PlotOff
Stat= DeleteEntity(Group2)
Stat= DeleteGroup(Group1,WsTrue)
PlotOn
IF ExitScript(Destination$) THEN 
	Stat= PlotEntity(Group)
	Stat= DeleteEntity(Group)
	IF Destination$= "BACKUP" THEN Pick_entity
	GOTO Leave
END IF

StdWorkplane
Pm= SubtractVectors(Pdest,Pref)
Shift_trans=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Axis_trans= RotAboutAxisTrans(Axis_line,Rotate_angle)
Trans1=MultTransforms(Shift_trans,Axis_trans)

ActivateUndo
Stat= AdvanceUndoNumber("move item")
Stat= AddGroupToUndo(WsUBModifyMode,Group,0,WsTrue)

Stat=TransGroupTrans(Group,Trans1)
Stat= DeleteEntityList(Pm,Pdest,Paxis,Axis_line,Group)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetPickMask(WsPickAll)
GOTO Start

Leave: !
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetPickMask(WsPickAll)
stat= SetInputFormat("n4","")
Stat= DeleteEntityList(Group,Pm,Pref,Pdest,Paxis,Axis_line)

IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	IF Destination$= "SCRIPTSTART" THEN Start
        IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END
