
! script name: items.d/dyncopyitem
! topic: Dynamic copy an item

!A
!A NAME:
!A
!A 	Dynamic Copy An Item
!A
!A DESCRIPTION:
!A
!A 	This script graphically copies an item.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ENTITY: Pick any entity in the item that you want copied.  If the
!B 	entity you select is contained in more than one item, a selection box
!B 	will appear, giving you the choice of items to copy.
!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 cursor displays the elements being moved.
!B 	Clicking the Select button on the mouse will select the location.
!B 	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 the default.
!B
!C NOTES:
!C
!C 	Item flags should not be attached to non-geometry.  Non-geometry
!C 	entities are not copied by this function.  For example: groups,
!C 	composite dimensions, and tools would not be copied.
!C
!# see also - htdocs/help.d/items.htm
!# see also - htdocs/help.d/property.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: dynamic copy 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",WsFalse)
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)
New_item= TransformItem(Item_ent,Trans1)

G1= GrpByProp(New_item,UserDataType(New_item))
G2= EntityItemGroup(G1)
ActivateUndo
Stat= AdvanceUndoNumber("copy item")
Stat= AddGroupToUndo(WsUBCreateMode,G1,0,WsFalse)
Stat= AddGroupToUndo(WsUBCreateMode,G2,0,WsFalse)

Stat= DeleteEntityList(G1,G2,Pm,Pdest,Paxis,Axis_line)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetPickMask(WsPickAll)
GOTO Copy_loop

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
