
! script name: vdraft.d/autodim
! var_topic: label dimensions using reference point

!A
!A NAME:
!A
!A 	Auto Dimension Group (VARIATIONAL)
!A
!A DESCRIPTION:
!A
!A 	This script labels dimensions using a specified reference point
!A 	(datum point) for points and lines within a specified graphics box.
!A
!B INSTRUCTIONS:
!B
!B 	DIMENSION ANGLE: Enter the angle the dimension label should be
!B 		oriented at.
!B
!B 	DATUM ABS X,Y: Enter the X,Y coordinates of the datum point, or
!B 		reference point, to be used for calculating the dimensions.
!B
!B 	DIM LOCATION: PICK FREE POINT: Pick the location for the dimensions
!B 		to be printed at.  The dimensions will all be located parallel
!B 		to a line passing through this point, at the dimension angle.
!B
!C NOTE:
!C
!C	Style= 0   lines stretched to fit close dimensions. Skip too close.
!C	Style= 1   lines stretched to fit close dimensions. 
!C	Style= 2   lines staggered to fit close dimensions.
!C
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: auto dimension ")

DIM Destination$[256]
DIM Answer$[256]
DIM Textflags$[256]
DIM Dim_type$[256]
DIM Datum$[256]
DIM Location$[256]
DIM Name$[256]
DIM Group$[256]
DIM Textflags$[20]
DIM Angle_var$[20]
DEG

Start:!
Style=2
Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Group_loop:!
Dim_type= WsRunningDim
Textflags$= "FO" 
! Textflags$= "FOB"     text follows with text box on
! Textflags$= "FOU"     text follows with text underline on

Pick_group:G1=PickGroupEnt("GROUP TO BE DIMENSIONED")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(G1) THEN Pick_group
Stat= EntityNameCheck(G1,Group$)

Datum_point:E1=PickEntity("DATUM POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Datum_point
Pick_para= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Datum$)
Pdatum= ParameterReturnPt(E1,Pick_para,Pick_return)
Stat= SetLastPoint(Pdatum)

Input_method= CurrInputMethod
stat= SetXyzInput(WsFreePoint)
Ploc=XyzInput("DIM LOCATION",WsXyOnly)
stat= SetXyzInput(Input_method)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Ploc) THEN Dim_loop

Xc= XPointValue(Pdatum)
Yc= YPointValue(Pdatum)
X1= XPointValue(Ploc)-Xc
Y1= YPointValue(Ploc)-Yc
Stat=DeleteEntity(Ploc)
Stat=DeleteEntity(Pdatum)

Angle_ques:!
Angle= InputAngle("DIMENSION 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= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("DIM: Auto dimension group")
Stat= SetRecDelLastExec
SELECT Pick_return
CASE WsGeometryEnd
        Pdatum= ParameterReturnPt(Datum$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
        Pdatum= ParameterReturnPt(Datum$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
        Pdatum= ParameterReturnPt(Datum$,Pick_para,WsGeomCenter)
END SELECT
V1= FreeParameter("Xloc",X1)
V2= FreeParameter("Yloc",Y1)
Ploc= PtIncrXyz(Pdatum,V1,V2,Z1)
IF No_angle_var THEN
        V3= FreeParameter("Angle",Angle)
ELSE
        V3= FreeParameter("Angle",Angle_var$)
END IF
V4= FreeParameter("Layout style",Style)
V5= StringConstant(Textflags$)
G1= AutoDimGroup(WsRunningDim,Group$,Pdatum,Ploc,V3,V4,V5)
Stat=AutoNameGroup(G1,Name$)
RecordFunctionOff
    ActivateUndo
    Stat= AdvanceUndoNumber("auto dim")
    Stat= AddGroupToUndo(WsUBCreateMode,G1,0,WsTrue)
RecordFunctionOn
Stat=DeleteEntityList(Pdatum,Ploc,G1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

Stat= PlotEntity(G1)
Stat= PlotEntity(Group$)
Stat= PlotEntity(Datum$)
GOTO Start

Leave:!
Stat=DeleteEntityList(Pdatum,Ploc)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
