
! script name: item.d/dyn3dcopyitem
! 3dtopic: Dynamic 3D copy an item

!A
!A NAME:
!A
!A 	Dynamic 3D Copy 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.
!B 	If the entity you select is contained in more than one item, a
!B 	selection box 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 3d copy item ")

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

ActivateUndo
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: !
Stat=SetPickMask(WsPickAll)
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

Stat=SetInputFormat("n4="," ")
Stat=SetEqualsValue(Xangle)
Xangle=GetValue("X AXIS ROTATION  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Stat=SetEqualsValue(Yangle)
Yangle=GetValue("Y AXIS ROTATION  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Stat=SetEqualsValue(Zangle)
Zangle=GetValue("Z AXIS ROTATION  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Copy_loop:!
Angle= 0
PlotOff
Pdest= CopyEntity(Pref)
P1= PointXYZ(0,0,1)
Paxis= AddVectors(Pref,P1)
Stat= DeleteEntity(P1)
Axis_line= LineTwoPoints(Pref,Paxis)
Stat= MakeInvisible(Pref)
Stat= MakeInvisible(Pdest)
Stat= MakeInvisible(Paxis)
Stat= MakeInvisible(Axis_line)
Orient= Rot3dAnglesTrans(Pref,Xangle,Yangle,Zangle)
Group2= CopyGroup(Group)
Stat=TransGroupTrans(Group2,Orient)
PlotOn

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


StdWorkplane
PlotOff
Stat= DeleteGroup(Group2,WsTrue)
Orient= Rot3dAnglesTrans(Pref,Xangle,Yangle,Zangle)
Pm= SubtractVectors(Pdest,Pref)
Shift_trans=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Axis_trans= RotAboutAxisTrans(Axis_line,Rotate_angle)
Trans1=MultTransforms(Axis_trans,Orient)
Trans2=MultTransforms(Shift_trans,Trans1)
PlotOn
New_item= TransformItem(Item_ent,Trans2)
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)

PlotOff
Stat= DeleteEntityList(G1,G2,Pm,Pdest,Paxis,Axis_line)
Stat= DeleteEntityList(Orient,Shift_trans,Axis_trans,Trans1)
PlotOn

IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF

Stat=SetPickMask(WsPickAll)
GOTO Copy_loop

Leave: !
Stat=SetPickMask(WsPickAll)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF

stat= SetInputFormat("n4","")
Stat= DeleteEntityList(Group,Pm,Pref,Pdest,Paxis,Axis_line)
Stat= DeleteEntity(Orient)


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