
! script name: vdraft.d/diameterdim
! var_topic: construct diameter dimension

!A
!A NAME:
!A
!A 	Diameter Dimension		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script constructs a diameter dimension
!A
!A~ image location - images/diameterdim.gif
!A
!B INSTRUCTIONS:
!B
!B 	PICK CIRCLE/HOLE: Pick the circle/hole that
!B 		will have the diameter dimension label added to it.
!B
!B 	DIM LOCATION: PICK FREE POINT: Pick either inside
!B 		or outside the circle for the label location.
!B
!# see also - htdocs/help.d/dynamicdim.htm

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

DIM Answer$[256]
DIM Destination$[256]
DIM Dim_type$[256]
DIM Pre_text$[256]
DIM Post_text$[256]
DIM Circle$[256]
DIM Point3$[256]
DIM Hole$[256]
DIM Name$[256]
DIM Textflags$[20]
DEG

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

Dim_type= WsDiameterDim
Dim_style= GetDefault(WsCirDimStyle)
Dim_type$= "DIAMETER "
Pre_text$= CHR$(129)&" "

ActivateUndo
Units= CurrentUnits
Large_radius= 10.0
IF Units= WsMetric THEN Large_radius= 250

stat= SetInputFormat("n4","")
Dim_loop:! 
Dim_style= GetDefault(WsCirDimStyle)
Pcir= PIckEntity("PICK CIRCLE/HOLE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Pcir) THEN Dim_loop
T= LastPickPara
Stat= EntityNameCheck(Pcir,Circle$)
Hole$=Circle$

Etype= EntityType(Pcir)

SELECT Etype
CASE WsCircleEnt
	T= LastPickPara
	Pc= CircleCenter(Pcir)
	Radius= CircleRadius(Pcir)
	Radius= ABS(Radius)
	SELECT Dim_style
	CASE 1
		Pt= ParameterPt(Pcir,T)
	CASE 2
		Pt= CopyEntity(Pc,WsFalse)
	CASE ELSE
		Pt= ParameterPt(Pcir,T)
	END SELECT
	Dynamic_position=1
CASE WsHoleEnt
	Pc= ParameterPt(Pcir,0.0)
	Pt= CopyEntity(Pc,WsFalse)
 	Diameter= EntityValue(Pcir,WsHoleDia)
        Depth= EntityValue(Pcir,WsHoleDepth)
	Radius= Diameter/2
	Dynamic_position=1
	Dim_style= 2
CASE ELSE
	GOTO Dim_loop
END SELECT


IF Dynamic_position THEN
	Dist= DistTwoPts(Pc,Pt)
	IF Dist> 0.0001 THEN
        	Ploc= CopyEntity(Pt,WsFalse)
	ELSE
		Ploc= PtIncrXYZ(Pc,1.5*ABS(Radius),0,0)
		Dim_style=2
	END IF
ELSE
        Input_method= CurrInputMethod
        stat= SetXyzInput(WsFreePoint)
        Stat=SetLastPoint(Pc)
        Ploc=XyzInput("DIM LOCATION",WsXyOnly)
        stat= SetXyzInput(Input_method)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Ploc) THEN Dim_loop
	Last_pick_ent= LastPickEnt
        Pick_para3= LastPickPara
        Pick_return3= CurrPickReturn
        IF NOT ValidDependency(Last_pick_ent) THEN
                 Point_reference=0
        ELSE
                 Stat= EntityNameCheck(Last_pick_ent,Point3$)
                 Point_reference=1
        END IF
END IF

IF Dynamic_position THEN
	IF (Radius>= Large_radius) THEN
       		Locate_center=0
        	stat= SetDefaults(WsLocatedCenter,WsFalse)
	ELSE
       		Locate_center=1
                stat= SetDefaults(WsLocatedCenter,WsTrue)
	END IF
	PlotOff
	E1= RadiusDim(Dim_type,Radius,Pc,Pt,Ploc,Pre_text$,"")
	PlotOn
        Stat=SetLastPoint(Pt)
        Stat=MakeInvisible(Pc)
        Stat=MakeInvisible(Pt)
        Stat=DeleteEntity(Ploc)
        IF NOT ValidGeometry(E1) THEN Dim_loop
        Which_end=2
        Vnum= LastPickView
        Stat= DynamicDimEdit(Vnum,E1,Which_end,"PICK DIM LOCATION",WsFalse)
        IF (ExitScript(Destination$)) THEN
                Stat= DeleteEntity(E1)
		Stat=MakeVisible(Pc)
        	Stat=MakeVisible(Pt)
                Stat=DeleteEntity(Pc)
                Stat=DeleteEntity(Pt)
                GOTO Leave
        END IF
	Last_pick_ent= LastPickEnt
        Pick_para3= LastPickPara
        Pick_return3= CurrPickReturn
        IF NOT ValidDependency(Last_pick_ent) THEN
                 Point_reference=0
        ELSE
                 Stat= EntityNameCheck(Last_pick_ent,Point3$)
                 Point_reference=1
        END IF
        PlotOff
        Ploc= ParameterPt(E1,0.5)
        Stat= DimTextFlagStr(E1,Textflags$)
        Stat= DeleteEntity(E1)
        PlotOn
ELSE
        Textflags$=""
END IF

P1= SubtractVectors(Ploc,Pc)
Stat= XYZPointValues(P1,X1,Y1,Z1)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Ploc)
Stat=DeleteEntity(Pt)


SELECT Etype
CASE WsCircleEnt
IF (Radius>= Large_radius) THEN
       	Locate_center=0
ELSE
       	Locate_center=1
END IF

Stat= GenerateVarName("D",Name$)
RecordFunctionOn
Stat= RecordDescr("DIM: Diameter dimension")
Stat= SetRecDelLastExec
Pc= CircleCenter(Circle$)
IF Point_reference THEN
        SELECT Pick_return3
        CASE WsGeometryEnd
                 Ploc= ParameterReturnPt(Point3$,Pick_para3,WsGeometryEnd)
        CASE WsGeometryMid
                 Ploc= ParameterReturnPt(Point3$,Pick_para3,WsGeometryMid)
        CASE WsGeomCenter
                 Ploc= ParameterReturnPt(Point3$,Pick_para3,WsGeomCenter)
        END SELECT
ELSE
        V1= FreeParameter("Xloc",X1)
        V2= FreeParameter("Yloc",Y1)
        Ploc= PtIncrXyz(Pc,V1,V2,Z1)
END IF
SELECT Dim_style
CASE 1
	Pt= ParameterPt(Circle$,T)
CASE 2
	Pt= CopyEntity(Pc,WsFalse)
CASE ELSE
	Pt= ParameterPt(Circle$,T)
END SELECT
IF Locate_center THEN
stat= SetDefaults(WsLocatedCenter,WsTrue)
ELSE
stat= SetDefaults(WsLocatedCenter,WsFalse)
END IF
V3= StringConstant(Pre_text$)
V4= StringConstant(Textflags$)
E1= RadiusDim(WsDiameterDim,Circle$,Pc,Pt,Ploc,V3,"",V4)
Stat= NameEntity(E1,Name$)
Stat=DeleteEntityList(Pt,Pc,Ploc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(Circle$)
Stat= PlotEntity(Point3$)
IF NOT ValidGeometry(E1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("diameter dim")
	Stat= AddDimToUndo(WsUBCreateMode,E1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

CASE WsHoleEnt

Stat= GenerateVarName("D",Name$)
RecordFunctionOn
Stat= RecordDescr("DIM: Hole Dia dimension")
Pc= ParameterPt(Hole$,0.0)
C1= RadiusCenter(EntityValue(Hole$,WsHoleDia)/2,Pc)
IF Point_reference THEN
        SELECT Pick_return3
        CASE WsGeometryEnd
                 Ploc= ParameterReturnPt(Point3$,Pick_para3,WsGeometryEnd)
        CASE WsGeometryMid
                 Ploc= ParameterReturnPt(Point3$,Pick_para3,WsGeometryMid)
        CASE WsGeomCenter
                 Ploc= ParameterReturnPt(Point3$,Pick_para3,WsGeomCenter)
        END SELECT
ELSE
        V1= FreeParameter("Xloc",X1)
        V2= FreeParameter("Yloc",Y1)
        Ploc= PtIncrXyz(Pc,V1,V2,Z1)
END IF
Pt= Intersect(C1,Ploc)
stat= SetDefaults(WsLocatedCenter,0)
V3= StringConstant(Pre_text$)
E1= RadiusDim(WsDiameterDim,C1,Pc,Pt,Ploc,V3,"")
Stat= NameEntity(E1,Name$)
Stat=DeleteEntityList(Pt,Pc,Ploc,C1)
RecordFunctionOff
CloseVarRecord

Stat= PlotEntity(E1)
Stat= PlotEntity(Hole$)
Stat= PlotEntity(Point3$)
IF NOT ValidGeometry(E1) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("hole diameter dim")
        Stat= AddDimToUndo(WsUBCreateMode,E1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

END SELECT

GOTO Dim_loop

Leave:!
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Ploc)
Stat=DeleteEntity(C1)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
        IF Destination$= "SCRIPTSTART" THEN Start
        IF Destination$= "BACKUP" THEN Start
	LOAD Destination$
END IF
END



