!*****************************************************************
! LGS_0517C   / 2015-03-17 / Vertex Systems                      *
!                                                                *
! Track-Stud corner joint.                                       *
!*****************************************************************

#UPDATE_JOINT#

!debug ./LGS_0517C_upd_dbg

! Get connecting objects

I= new_iterator("",0,"ROLED","THIS")
This = next_item(I)
if !This then exit

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","E1"
Piece = next_item(I)
if !Piece then exit

PieceEnd= 0
Type$= get_object_type(Piece)
if cmp(Type$,"PROFEND") then
  PieceEnd= Piece
  Piece= get_actual_piece(PieceEnd)
endif

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","E2"
End = next_item(I)
if !End then exit
Pce= get_actual_piece(End)
if !Pce then exit

Type$ = get_obj_param(This,"TYPE")
Type = Type$
End_Gap$ = get_obj_param(This,"ENDGAP")
End_Gap = End_Gap$
Full$ = get_obj_param(This,"FULLDIMPLE")
Full = Full$
Eshp$= get_obj_param(This,"ESHP")
Cd= get_obj_param(This,"RCD")
EshpLim= get_obj_param(This,"ESHP_LA")
Eshp2$= get_obj_param(This,"ESHP2")
!
! Truss tool and Notch diags are just to support customized libraries for a while
TrussTool$ = get_obj_param(This,"TRUSSTOOL")
TrussToolSize = get_obj_param(This,"TRUSSTOOLSIZE")
NotchDiags = get_obj_param(This,"NOTCHDIAGS")

DimpleSize = mm_to_dwg(16)
DimpleToEndDist = mm_to_dwg(3)

! Check base piece validity
Shape$= get_material_param(Piece,"SHAPE")
if (Type == 1 && (!cmp(Shape$,"C"))) then exit
if (Type == 2 && (!cmp(Shape$,"U"))) then exit
!
Hgt= get_section_param(Piece,"HEIGHT")
Hgt$ = Hgt
if findstr(TrussTool$,Hgt$) then
  TrussTool = 1
else
  TrussTool = 0
endif

Thick= get_section_param(Piece,"THICK")
!
SDP= 1; SDE= 2
P= 3; X= 4; Y= 5; Z= 6
PP= 7; PT= 8; PN= 9; PE= 10
PEP= 11; PEX= 12; PEY= 13; PEZ= 14
LPP= 15; LPE= 16; CPP= 17; CPE= 18
D= 19
PP= 20; PX= 21; PY= 22; PZ= 23
TP= 24; TP2= 25; TP3= 26
PP2= 29; PX2= 30; PY2= 31; PZ2= 32
Pnt1= 33; Pnt2= 34

!
GeomP= 1
GeomE= 2
!
! Check piece cross section directions
get_piece_dir(SDP) Piece
get_piece_dir(SDE) Pce
Dot= vec_dot_prod(SDP,SDE)
Dot= abs(Dot)
if Dot < 0.999999 then exit
!
! Check connecting side and angle
get_origin_pos(End) P,X,Y,Z
get_origin_pos(Pce) PP2,PX2,PY2,PZ2
get_origin_pos(PieceEnd) PEP,PEX,PEY,PEZ
get_vxgeom(GeomP) Piece
point_to_vxgeom(PP) GeomP,P
get_vxgeom_tan(PT) GeomP,PP
vec_cross_prod(PN) PT,SDP
Dot= vec_dot_prod(Z,PN)
if Dot < 0.10 then exit
PerpJnt= 0
if Dot > 0.999999 then PerpJnt= 1
Sharp= vec_dot_prod(Z,PEZ) > 0.000001
!
! Check if pieces meet each others
vec_sub(D) PP,P
Dot= vec_dot_prod(D,SDP)
if Dot < mm_to_dwg(-5) || Dot > mm_to_dwg(5) then
  exit
endif

get_origin_pos(Piece) PP,PX,PY,PZ
Dot= vec_dot_prod(PZ,Z)
Dot= abs(Dot)
Ang= acos(Dot)
!
! Check end shape based on limiting angle and end shape type
!
EndShape= 0
if !TrussTool then
  if cmp(Eshp$,"RND") then 
    if Cd > 0.01 then
      EndShape= 2
    else
      EndShape= 1
    endif  
  endif  
  if (EndShape && EshpLim > 0.01 && EshpLim < 89.99 && Ang>EshpLim) then EndShape= 0
  if EndShape then NotchDiags= 0
endif
!
! Check critical point on connecting piece
get_vxgeom(GeomE) Pce
get_vxgeom_tan(PE) GeomE,P
Ip= 8
Dot= vec_dot_prod(PX2,PN)
if Dot < 0 then Ip= 2

End2Ok= 0
if (EndShape == 1) then
  End2Ok= 1
  set_obj_param(This) "ESHP_F","1"

  ! Radius for rounding
  get_piece_localp(P) Pce,"IP",2
  Rad= -vec_get_x(P)

  ! Reference line
  get_piece_localp(P) Piece,"IP",2
  vec_to(PN) Thick+Rad,0,0
  vec_add(P) P,PN
  get_piece_vxgeom(GeomP) Piece,P

  ! Line to trim
  get_vxgeom(GeomE) Pce
  if vxgeom_cross_pnts(GeomP,P,GeomE,PE) == 0 then exit
  
  ! End point
  vec_times_scalar(D) Z,Rad
  vec_add(TP) PE,D
  
  ! End point for passing piece
  if cmp(Eshp2$,"RND") then  
    get_vxgeom(GeomE) Pce 
    get_vxgeom(GeomP) Piece 
    if vxgeom_cross_pnts(GeomP,P,GeomE,PE) == 0 then exit
    vec_times_scalar(D) PEZ,Rad
    vec_add(TP2) PE,D
  else
    if cmp(Eshp2$,"STRS") then 
      End2Ok= 0;
    else  
      vec_times_scalar(D) PEZ,Rad
      vec_add(TP2) PE,D
    endif  
  endif
endif
if (EndShape == 2) then
  End2Ok= 1
  set_obj_param(This) "ESHP_F","2"

    ! Reference line
  get_piece_localp(P) Piece,"IP",2
  vec_to(PN) Thick,0,0
  vec_add(P) P,PN
  get_piece_vxgeom(GeomP) Piece,P
  
  ! Line to trim
  get_piece_localp(P) Pce,"IP",2
  W2= abs(vec_get_x(P))
  if Cd > W2 then Cd= W2-mm_to_dwg(1)
  Ext= 0
  if abs(Ang > 45) then
    if Cd > W2 then Cd= W2
    Off= W2-Cd
  else
    Ext= Cd
    Off= W2
  endif
  if Ip == 2 then Off= -Off
  vec_to(P) Off,0,0

  get_piece_vxgeom(GeomE) Pce,P
  if vxgeom_cross_pnts(GeomP,P,GeomE,PE) == 0 then exit
  vec_times_scalar(D) Z,Ext
  vec_add(PE) P,D
  vec_copy(TP) PE
  get_vxgeom(GeomE) Pce
  point_to_vxgeom(TP) GeomE,TP

  if cmp(Eshp2$,"RND") then  
    ! Ipp for passing piece
    Ipp= 8
    Dot= vec_dot_prod(PEZ,PX2)
    if Dot < 0 then Ipp= 2
    
    ! Reference line
    get_piece_localp(P) Pce,"IP",Ipp
    get_piece_vxgeom(GeomP) Pce,P
  
    ! Line to trim
    get_piece_localp(P) Piece,"IP",2
    W2p= abs(vec_get_x(P))
    if Cd > W2p then Cd= W2p-mm_to_dwg(1)
    Ext= 0
    if abs(Ang > 45) then
      if Cd > W2p then Cd= W2p
      Off= W2p-Cd
    else
      Ext= Cd
      Off= W2p
    endif
    if vec_dot_prod(PEZ,PX2) < 0 then Off= -Off
    if Sharp then Off= -Off
    vec_to(P) Off,0,0

    get_piece_vxgeom(GeomE) Piece,P
    if vxgeom_cross_pnts(GeomP,P,GeomE,TP2) == 0 then exit
    vec_times_scalar(D) PEZ,Ext
    vec_add(TP2) P,D
    get_vxgeom(GeomE) Piece
    point_to_vxgeom(TP2) GeomE,TP2  
  else
    if cmp(Eshp2$,"STRS") then 
      End2Ok= 0
    else  
      if Sharp then
        Dot= vec_dot_prod(PX2,PEZ)
        vec_copy(D) PX2
        if Dot<0 then vec_rev(D) D
        if Ang < 45 then
          vec_times_scalar(D) D,W2-Cd
          vec_add(TP2) TP,D
        else
          vec_times_scalar(D) D,W2
          vec_add(TP2) TP,D
          vec_times_scalar(D) Z,-Cd
          vec_add(TP2) TP2,D
        endif
      else
        End2Ok= 0
      endif
    endif  
  endif
  
endif
if (EndShape == 0) then
  set_obj_param(This) "ESHP_F","0"

  ! Reference line
  get_piece_localp(P) Piece,"IP",2
  vec_to(PN) Thick,0,0
  vec_add(P) P,PN
  get_piece_vxgeom(GeomP) Piece,P

  ! Line to trim
  get_piece_localp(P) Pce,"IP",Ip
  if TrussTool && Ang < 80 then
    vec_to(PN) TrussToolSize,0,0
    if Ip == 2 then
      vec_add(P) P,PN
    else
      vec_sub(P) P,PN
    endif
  endif
  
  get_piece_vxgeom(GeomE) Pce,P
  if vxgeom_cross_pnts(GeomP,P,GeomE,PE) == 0 then exit
  get_vxgeom(GeomE) Pce
  point_to_vxgeom(TP) GeomE,PE
endif

if (!End2Ok) then
  ! End point for passing piece
  ! Eshp2$= STRL -> Extended corner
  !       = STRS -> Short corner  (default)
  if cmp(Eshp2$,"STRL") then
    if (Sharp) then
      if (Ip == 2) then
        Ip= 8
      else
        Ip= 2
      endif  
      get_piece_localp(P) Pce,"IP",Ip
      get_piece_vxgeom(GeomE) Pce,P
      point_to_vxgeom(TP2) GeomE,PE
    else
      if !proj_to_piece(TP2,D,PEP,PEZ,Pce,0.0,1) then exit
      get_vxgeom(GeomE) Pce
     endif 
  else
    Ip= 2
    if vec_dot_prod(PX2,PEZ) > 0 then Ip= 8
    get_piece_localp(P) Pce,"IP",Ip
    get_piece_vxgeom(GeomE) Pce,P
    if (Sharp) then  
      get_piece_localp(P) Piece,"IP",8
    else
      get_piece_localp(P) Piece,"IP",2
    endif    
     get_piece_vxgeom(GeomP) Piece,P
    if vxgeom_cross_pnts(GeomP,P,GeomE,PE) == 0 then exit
    vec_copy(PE) PE
    get_vxgeom(GeomP) Piece
    point_to_vxgeom(TP2) GeomE,PE     
  endif
endif

!
if PieceEnd then
  get_vxgeom(GeomP) Piece
  point_to_vxgeom(PP) GeomP,TP2
  if (piece_move_end(PieceEnd,PP,1) != 1) then exit
endif

vec_times_scalar(D) Z, End_Gap
vec_sub(P) TP,D

if (piece_move_end(End,P,1) != 1) then exit

GeomPiece = 1
GeomPce = 2

get_vxgeom(GeomPiece) Piece
get_vxgeom(GeomPce) Pce

if vxgeom_cross_pnts(GeomPiece,Pnt1,GeomPce,Pnt2) == 0 then exit

set_joint_pos(This) Pnt1,X,Y,Z
vec_to(D) 0,0,0
set_piece_rotpnt(This) End,D

set RETURN "1"

#UPDATE_JOINT#

#JOINT_TEST#

set RETURN "1"

#JOINT_TEST#

#JOINT_DETAILING#

!debug ./joint_LGS_0517C_det.dbg

I= new_iterator("",0,"ROLED","THIS")
This = next_item(I)
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","E1"
PceEnd = next_item(I)
Pce= get_actual_piece(PceEnd)
if !Pce then exit
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","E2"
End= next_item(I)
if !End then exit
Pce2= get_actual_piece(End)
if !Pce2 then exit

COMMONNOTCH$=    "120"
COMMONLIPNOTCH$= "121"

Cd= get_obj_param(This,"RCD")
LipCutLen$ = get_obj_param(This,"LIPCUTLEN")
LipCutLen = LipCutLen$
NotchLen$ = get_obj_param(This,"NOTCHLEN")
NotchLen = NotchLen$
SwageLen$ = get_obj_param(This,"SWAGELEN")
SwageLen = SwageLen$
NoSwage = get_obj_param(This,"NOSWAGE")
TrussTool$ = get_obj_param(This,"TRUSSTOOL")
TrussToolSize = get_obj_param(This,"TRUSSTOOLSIZE")
NotchDiags = get_obj_param(This,"NOTCHDIAGS")
DOffset = get_obj_param(This,"DOFFSET")

! Get parameters from piece material
if (LipCutLen < mm_to_dwg(0.001)) then
  LipCutLen$= get_material_param(Pce,"NC.LIPCUTLEN")
  LipCutLen = LipCutLen$
endif
if (NotchLen < mm_to_dwg(0.001)) then
  NotchLen$= get_material_param(Pce,"NC.NOTCHLEN")
  NotchLen = NotchLen$
endif
if (SwageLen < mm_to_dwg(0.001)) then
  SwageLen$= get_material_param(Pce,"NC.SWAGELEN")
  SwageLen = SwageLen$
endif
if (DOffset < mm_to_dwg(0.001)) then
  DOffset$= get_material_param(Pce,"NC.DIMPLE_OFFSET")
  DOffset = DOffset$
endif

StudLipcutGap = mm_to_dwg(3.0)
TrackSwageGap = mm_to_dwg(3.0)

JP= 1
JX= 2
JY= 3
JZ= 4
PP= 5
PX= 6
PY= 7
PZ= 8
EP= 9
EX= 10
EY= 11
EZ= 12
P= 13 
X= 14
Y= 15
Z= 16
D= 17
PP2= 18
PX2= 19
PY2= 20
PZ2= 21
Dir= 20
TAN= 21
OP= 22
OP2 = 23
D2= 24
PNT= 25
SP= 26
DP = 32
DP2 = 33
PN = 34

GeomE = 1
GeomP = 2
!
get_origin_pos(This) JP,JX,JY,JZ
get_origin_pos(PceEnd) PP,PX,PY,PZ
get_origin_pos(Pce) P,PX,Y,Z
get_origin_pos(End) EP,EX,EY,EZ
get_origin_pos(Pce2) PP2,PX2,PY2,PZ2
!
get_piece_tan(TAN) Pce,PP
get_piece_dir(Dir) Pce
vec_cross_prod(OP) Dir,TAN

get_piece_localp(P) Pce,"IP",2
vec_to(PN) DOffset,0,0
vec_add(P) P,PN
get_piece_vxgeom(GeomP) Pce,P

get_piece_localp(P) Pce2,"IP",2
vec_to(PN) DOffset,0,0
vec_add(P) P,PN
get_piece_vxgeom(GeomE) Pce2,P
if vxgeom_cross_pnts(GeomP,DP,GeomE,DP2) == 0 then exit

DimP= get_section_param(Pce,"HEIGHT")
DimP$ = DimP
Shape$= get_material_param(Pce,"SHAPE")
!
if findstr(TrussTool$,DimP$) then
  TrussTool = 1
else
  TrussTool = 0
endif

EshpFeat = 0;
if (!TrussTool) then
  EshpFeat= get_obj_param(This,"ESHP_F")
endif

Width= get_section_param(Pce,"WIDTH")
Width2= get_section_param(Pce2,"WIDTH") * 2.0
Thck = get_section_param(Pce,"THICK")
SwDim= Thck/2
if (cmp(Shape$,"U")) then SwDim= Thck
Ndepth= Thck*2.0
!
! Check connecting angle
Dot= vec_dot_prod(PZ,EZ)
if Dot < 0 then Dot= -Dot
Ang= acos(Dot)
if Ang < 20 && vec_dot_prod(EZ,PZ) > 0 then exit

! Set rounded end
Rnd= 0
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","RND"
Rnd= next_item(I)
if !EshpFeat then
  if (Rnd) then
   disconnect(Rnd)
   del_object(Rnd)
  endif
  Rnd= 0
else
  if (!Rnd) then
    if EshpFeat == 2 then
      Rnd= new_feature("PCEND_RND",0,0,"RndEndT2","2","2")
      set_obj_param(Rnd) "CD",Cd,1+2
    else
      Rnd= new_feature("PCEND_RND",0,0,"RndEndT1_V1","2","1")
    endif  
    if (!Rnd) then exit
    connect(This) Rnd,"RND"
    add_sobj(Pce2) Rnd
  endif
  set_origin_pos(Rnd) EP,EX,EY,EZ
endif

! Set rounded end
Rnd2= 0
Eshp2$= get_obj_param(This,"ESHP2")
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","RND2"
Rnd2= next_item(I)
if cmp(Eshp2$,"RND") && Rnd then
  if (!Rnd2) then
    if EshpFeat == 2 then
      Rnd2= new_feature("PCEND_RND",0,0,"RndEndT2","2","2")
      set_obj_param(Rnd2) "CD",Cd,1+2
    else
      Rnd2= new_feature("PCEND_RND",0,0,"RndEndT1_V1","2","1")
    endif  
    if (!Rnd2) then exit
    connect(This) Rnd2,"RND2"
    add_sobj(Pce) Rnd2
  endif
  set_origin_pos(Rnd2) PP,PX,PY,PZ
else
  if (Rnd2) then
    disconnect(Rnd2)
    del_object(Rnd2)
  endif
endif

! Set male counterforms
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","M"
Count1= 0
Count2= 0
vec_copy(Z) PY
loop
  Hole= next_item(I)
  exitif (!Hole)
  if DOffset then
    vec_copy(P) DP
  else
    vec_copy(P) JP
  endif
  if !proj_to_piece(P,X,P,Z,Pce,0.0,0) then exit
  vec_copy(X) PZ
  vec_cross_prod(Y) Z,X
  set_origin_pos(Hole) P,X,Y,Z
  vec_rev(Z) Z
  Count1= Count1+1
endloop

! Set female counterforms
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","F"
vec_copy(Z) PY
loop
  Hole= next_item(I)
  exitif (!Hole)
  if DOffset then
    vec_copy(P) DP
  else
    vec_copy(P) JP
  endif
  vec_copy(X) PZ
  vec_cross_prod(Y) Z,X
  ! Dimple slot
  Type$ = get_obj_param(Hole,"TYPE")
  if (cmp(Type$,"4")) then
    vec_copy(P) EP
    vec_copy(Y) EZ
    vec_cross_prod(X) Z,Y
    DimY = get_obj_param(Hole,"DIMY")
    vec_times_scalar(D) EZ,-DimY/2.0
    vec_add(P) P,D
  endif
  if !proj_to_piece(P,D,P,Z,Pce2,0.0,0) then exit
  set_origin_pos(Hole) P,X,Y,Z
  vec_rev(Z) Z
  Count2= Count2+1
endloop
if Count1 != Count2 then exit

! Set female counterforms
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","F2"
vec_copy(Z) PY
loop
  Hole= next_item(I)
  exitif !Hole
  if DOffset then
    vec_copy(P) DP
  else
  vec_copy(P) JP
  endif
  vec_copy(X) PZ
  vec_cross_prod(Y) Z,X
  ! Dimple slot
  Type$ = get_obj_param(Hole,"TYPE")
  if (cmp(Type$,"4")) then
    vec_copy(P) EP
    vec_copy(Y) EZ
    vec_cross_prod(X) Z,Y
    DimY = get_obj_param(Hole,"DIMY")
    vec_times_scalar(D) EZ,-DimY/2.0
    vec_add(P) P,D
  endif
  if !proj_to_piece(P,D,P,Z,Pce2,0.0,0) then exit
  set_origin_pos(Hole) P,X,Y,Z
  vec_rev(Z) Z
  Count3= Count3+1
endloop
if Count2 != Count3 then exit

! Set Lifting holes
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","W"
vec_rev(Z) EZ
Hole= next_item(I)
if (Hole) then
  vec_copy(P) JP
  if !proj_to_piece(P,X,PP,Z,Pce,0.0,0) then exit

  Dist = mm_to_dwg(50)
  vec_times_scalar(D) PZ,Dist
  vec_rev(D) D
  vec_add(P) P,D

  vec_rev(Z) EX
  vec_cross_prod(Y) Z,X
  set_origin_pos(Hole) P,PZ,PY,PX
endif


! Lipcut length
get_piece_tan(TAN) Pce2,EP
get_piece_dir(Dir) Pce2
vec_cross_prod(OP2) Dir,TAN
Dot= vec_dot_prod(TAN,OP)
if (Dot < 0.0) then vec_rev(TAN) TAN

Offset= 0
Dot= vec_dot_prod(OP,OP2)
if (Dot > 0.0) then Offset= Thck
Ok= proj_to_piece(PNT,D2,PP2,OP2,Pce2,0,1)
Ok= proj_to_piece(PNT,D2,PNT,TAN,Pce,-Offset,1)
if (Dot > 0.0) then
  vec_times_scalar(D) OP,Thck
  vec_add(PNT) PNT,D
  ! Swage point
  vec_copy(SP) PNT
  get_piece_tan(TAN) Pce,PP
  Ok= pnt_to_plane(SP,SP,PP,PZ,TAN)
  vec_copy(SP) SP
else
  ! Swage point
  vec_copy(SP) PNT
  point_to_piece(SP) Pce2,SP
endif
 
point_to_piece(PNT) Pce,PNT

! Lipcut len
vec_sub(D) PNT,PP
Len= vec_len(D)
if (Len > LipCutLen) then LipCutLen= Len + StudLipcutGap
LipCutLen$= LipCutLen:N?.2

! Swage len
point_to_piece(SP) Pce2,SP
vec_sub(D) SP,EP
Len= vec_len(D)
if (Len > SwageLen) then SwageLen= Len + TrackSwageGap

! Set notch place
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","N"
Notch= next_item(I)
if (cmp(Shape$,"U") && Notch) then
  disconnect(Notch)
  del_object(Notch)
  Notch= 0
endif
if (cmp(Shape$,"C") && !Notch) then
  Notch= new_feature("NOTCH",COMMONLIPNOTCH$,"0")
  if (!Notch) then exit
  add_sobj(Pce) Notch
  connect(This) Notch,"N",1
endif
if (Notch) then
  vec_copy(D) PX
  if !proj_to_piece(P,X,PP,D,Pce,0.0,1) then exit
  vec_copy(D) PX
  if vec_dot_prod(D,JZ) < 0 then vec_rev(D) D
  vec_times_scalar(D) D,Ndepth
  vec_add(P) P,D
  vec_times_scalar(D) PZ,-LipCutLen
  vec_add(P) P,D
  vec_copy(Z) PZ
  vec_copy(Y) PX
  vec_cross_prod(X) Y,Z
  set_origin_pos(Notch) P,X,Y,Z
  set_obj_param(Notch) "W",LipCutLen$
endif
!
Hgt= get_section_param(Pce2,"HEIGHT")
if (DimP - 2*Thck + mm_to_dwg(0.001) > Hgt) then NoSwage= 1

! Set swage place
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","S"
Swage= next_item(I)
if (!NoSwage && !Swage) then
  Swage= new_notch_feature("SWAGE","0","0")
  if (!Swage) then exit
  Dim$ = mm_to_dwg(10)
  set_obj_param(Swage) "DIM",Dim$
  add_sobj(Pce2) Swage
  connect(This) Swage,"S",1
endif
if (NoSwage && Swage) then
  disconnect(Swage)
  del_object(Swage)
  Swage= 0
endif
if (Swage) then
  vec_times_scalar(D) EZ,-SwageLen/2.0
  vec_add(P) EP,D
  set_origin_pos(Swage) P,EX,EY,EZ
  Lz= SwageLen
  Lz$= Lz
  set_obj_param(Swage) "LZ",Lz$
  Ly= DimP-SwDim*2
  Ly$= Ly
  Lx$= Width2
  set_obj_param(Swage) "LY",Ly$
  set_obj_param(Swage) "LX",Lx$
endif

! Set 45 degree notch
if TrussTool then
  init_iterator(I) "",This,"CONNECTION_ROLEPARTS","C"
  Camf= next_item(I)
  if Ang > 80.01 then
    if Camf then 
      disconnect(Camf)
      del_object(Camf)
    endif
  else
    if !Camf then 
      Camf= new_feature("CUT","0")
      if Camf then 
        connect(This) Camf,"C"
        add_sobj(Pce2) Camf
      endif
    endif
    if !Camf then exit
    if vec_dot_prod(EZ,PZ) > 0 then
      vec_rev(D) PZ
    else
      vec_copy(D) PZ
    endif
    if vec_dot_prod(EX,D) > 0 then
      vec_copy(D) EX
    else
      vec_rev(D) EX
    endif
    if !proj_to_piece(P,X,EP,D,Pce2,0,1) then exit
    vec_times_scalar(X) D,TrussToolSize
    vec_sub(P) P,X
    vec_add(Z) D,EZ
    vec_unit(Z) Z
    vec_copy(Y) EY
    vec_cross_prod(X) Y,Z
    set_origin_pos(Camf) P,X,Y,Z
  endif
  
  Camf= next_item(I)
  if Ang > 80.01 then
    if Camf then 
      disconnect(Camf)
      del_object(Camf)
    endif
  else
    if !Camf then 
      Camf= new_feature("CUT","0")
      if Camf then 
        connect(This) Camf,"C"
        add_sobj(Pce2) Camf
      endif
    endif
    if !Camf then exit
    if vec_dot_prod(EZ,PZ) > 0 then
      vec_copy(D) PZ
    else
      vec_rev(D) PZ
    endif
    if vec_dot_prod(EX,D) > 0 then
      vec_rev(D) EX
    else
      vec_copy(D) EX
    endif
    vec_rev(D) D
    if !proj_to_piece(P,X,EP,D,Pce2,0,1) then exit
    vec_times_scalar(X) D,TrussToolSize
    vec_sub(P) P,X
    vec_add(Z) D,EZ
    vec_unit(Z) Z
    vec_copy(Y) EY
    vec_cross_prod(X) Y,Z
    vec_rev(X) X
    set_origin_pos(Camf) P,X,Y,Z
  endif
endif

if (NotchDiags) then
  init_iterator(I) "",This,"CONNECTION_ROLEPARTS","ND"
  Notch= next_item(I)
  if Ang > 80.01 then
    if Notch then 
      disconnect(Notch)
      del_object(Notch)
      Notch= 0
    endif
  else        
    if vec_dot_prod(EZ,PZ) > 0 then
      vec_rev(D) PZ
    else
      vec_copy(D) PZ
    endif
    if vec_dot_prod(EX,D) > 0 then
      if vec_same(EX,PX2,mm_to_dwg(0.1)) then
        reversed = 1
      else
        reversed = 0
      endif      
      vec_copy(D) EX
    else
      if vec_same(EX,PX2,mm_to_dwg(0.1)) then
        reversed = 0
      else
        reversed = 1
      endif
      vec_rev(D) EX      
    endif
    
    if reversed then

      if (Notch) then
        Type$= get_feat_ctype(Notch)
        if (!cmp(Type$,COMMONLIPNOTCH$)) then
          disconnect(Notch)
          del_object(Notch)
          Notch= 0
        endif
      endif
      
      if (!Notch) then
        Notch= new_feature("NOTCH",COMMONLIPNOTCH$,"0")
        if (!Notch) then exit
        add_sobj(Pce2) Notch
        connect(This) Notch,"ND"
      endif

      vec_times_scalar(D) EZ,-LipCutLen
      vec_add(P) EP,D
      vec_copy(Y) EZ
      vec_copy(Z) PX2

      if !proj_to_piece(P,X,P,Z,Pce2,0.0,1) then exit
      vec_times_scalar(D) Z,mm_to_dwg(-Thck)
      vec_add(P) P,D
      vec_cross_prod(X) Y,Z
      set_origin_pos(Notch) P,X,Y,Z
      set_obj_param(Notch) "W",LipCutLen$
    else
      if (Notch) then
        Type$= get_feat_ctype(Notch)
        if (!cmp(Type$,COMMONNOTCH$)) then
          disconnect(Notch)
          del_object(Notch)
          Notch= 0
        endif
      endif

      if (!Notch) then
        Notch= new_feature("NOTCH",COMMONNOTCH$,"0")
        if (!Notch) then exit
        add_sobj(Pce2) Notch
        connect(This) Notch,"ND"
      endif

      vec_times_scalar(D) EZ,-LipCutLen
      vec_add(P) EP,D
      vec_copy(Y) EZ
      vec_copy(Z) PX2

      vec_rev(Z) Z
      if !proj_to_piece(P,X,P,Z,Pce2,0.0,1) then exit
      vec_times_scalar(D) Z,mm_to_dwg(-Thck*2.0)
      vec_add(P) P,D
      vec_cross_prod(X) Y,Z
      set_origin_pos(Notch) P,X,Y,Z
      set_obj_param(Notch) "W",LipCutLen$
    endif
  endif
endif

! Set screw pattern
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","SP"
Screws= next_item(I)
if Screws then 
  Wdt= get_obj_param(Screws,"WDT")
  if abs(DimP-Wdt) > mm_to_dwg(0.1) then
    Wdt$= DimP
    set_obj_param(Screws) "WDT",Wdt$,1
  endif
  if DOffset then
    vec_copy(P) DP
  else
    vec_copy(P) JP
  endif
  set_origin_pos(Screws) P,PZ,PX,PY
endif

set RETURN "1"

#JOINT_DETAILING#

#JOINT_CAPACITY#
!debug ./joint_capacity.dbg

I= new_iterator("",0,"ROLED","THIS")
This = next_item(I)
if !This then exit

set ROLE_PCE1 "E1"
set ROLE_PCE2 "E2"
set ROLE_SCREWS "SP"

File$ = get_obj_param(This,"CAP")
Lib$ = get_obj_param(This,"CAPLIB")&"/"

DirSystem$ = get($DIR_SYSTEM)&"complibs/"
Path$ = DirSystem$&Lib$&File$
CustomFilePath$ = filepath_custom(Path$) 
if (len(CustomFilePath$) && file_exists(CustomFilePath$)) then
  call CustomFilePath$ JOINT_CAPACITY
  exit
endif
if (len(File$) && file_exists(Path$)) then
  call Path$ JOINT_CAPACITY
endif

#JOINT_CAPACITY#
#UPDATE_JOINT_DATA#
!debug ./update_joint_data.dbg

I = new_iterator("",0,"ROLED","THIS")
This = next_item(I)
if !This then exit

set ROLE_SCREWS "SP"

File$ = get_obj_param(This,"CAP")
Lib$ = get_obj_param(This,"CAPLIB")&"/"

DirSystem$ = get($DIR_SYSTEM)&"complibs/"
Path$ = DirSystem$&Lib$&File$
CustomFilePath$ = filepath_custom(Path$) 
if (len(CustomFilePath$) && file_exists(CustomFilePath$)) then
  call CustomFilePath$ UPDATE_JOINT_DATA
  exit
endif
if (len(File$) && file_exists(Path$)) then
  call Path$ UPDATE_JOINT_DATA
endif

#UPDATE_JOINT_DATA#
