
! script name: offsets.d/offsetentity
! topic: creates an entity offset from a given entity

!A
!A NAME:
!A
!A 	offset entity
!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]

Forward_color=3
Reverse_color=4

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

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

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

Off_element=OffsetEntity(Element,Offset)

ActivateUndo
Stat= AdvanceUndoNumber("offset entity")
Stat= AddItemToUndo(WsUBCreateMode,Off_element)

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

