
! script name: voffsets.d/voffsetentity
! topic: creates an entity offset from a given entity

!A
!A NAME:
!A
!A 	offset entity VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates an element offset to a given entity.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ELEMENT: Pick the element.
!B
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: offset entity ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Element$[256]
DIM Offset_var$[256]


Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave


Forward_color=3
Reverse_color=4

ActivateUndo
Pick_entity:!
Stat= SetInputFormat("n4","")
Stat=SetPickMask(WsPickNoSurf)
Element=PickEntity("PICK ELEMENT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Element) THEN Pick_entity
Stat= EntityNameCheck(Element,Element$)
Para_value= LastPickPara

Stat=SetInputFormat("n4=","")
Stat= SetEqualsValue(ABS(Offset))
Offset=GetValue("OFFSET FOR ELEMENT")
IF (ExitScript(Destination$)) THEN Leave
No_offset_var= LastVariableInput(Offset_var$)
Offset= ABS(Offset)

IF Offset > 0.0 THEN
	Normal_len= ABS(Offset)
	GOSUB Which_side
END IF

Stat= GenerateVarName(WsPtChainEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("OFFSET: Offset entity")
Stat= SetRecDelLastExec
IF No_offset_var THEN
        V1= FreeParameter("Offset",Offset)
ELSE
        V1= FreeParameter("Offset",Offset_var$)
END IF
V2= FreeParameter("Side",Norm_sign)
E1=OffsetEntity(Element$,VariableValue(V2)*VariableValue(V1))
Stat= NameEntity(E1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

IF NOT ValidGeometry(E1) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("offset entity")
	Stat= AddItemToUndo(WsUBCreateMode,E1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_entity



ShowNormalDir:!
        Forward_normal= ParameterLine(Element,Para_value,Normal_len,WsNormal)
        Pforward=ParameterPt(Forward_normal,1.0)
        Stat=SetColor(Forward_normal,Forward_color)
        Stat=SetColor(Pforward,Forward_color)

        Reverse_normal= ParameterLine(Element,Para_value,-Normal_len,WsNormal)
        Preverse=ParameterPt(Reverse_normal,1.0)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
RETURN

Which_side:!
        GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE OF ELEMENT",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
        T1= LastPickPara
        Norm_sign=0
        Pc=ParameterPt(Forward_normal,0.0)
        Pt=ParameterPt(L1,T1)
        D1=DistTwoPts(Pt,Pforward)
        D2=DistTwoPts(Pt,Preverse)
        D3=DistTwoPts(Pt,Pc)
        IF D1<= D2 THEN
                Norm_sign=-1
                Norm_dir= WsInside
                IF D1< D3 THEN Norm_dir= WsOutside
        ELSE
                Norm_sign=1
                Norm_dir= WsInside
                IF D2< D3 THEN Norm_dir= WsOutside
        END IF
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
RETURN


Leave:!
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat= SetInputFormat("n4","")
Stat=DeleteEntity(Selectpt)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

