!*****************************************************************
! LGS_0615AE   / 2015-06-12 / Vertex Systems                     *
!                                                                *
! Light Gauge Steel Track-Stud joint.                            *
!*****************************************************************

#UPDATE_JOINT#

!debug ./update_joint.dbg

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

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","PCE1"
Pce1 = next_item(I)
if !Pce1 then exit

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","PCE2"
Pce2 = next_item(I)
if !Pce2 then exit

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","B"
Bolt = next_item(I)


! Check base piece validity
Shape$= get_material_param(Pce1,"SHAPE")
if !cmp(Shape$,"C") && !cmp(Shape$,"U") then exit
Shape$= get_material_param(Pce2,"SHAPE")
if !cmp(Shape$,"C") && !cmp(Shape$,"U") then exit
!
Hgt1= get_section_param(Pce1,"HEIGHT")
Hgt2= get_section_param(Pce2,"HEIGHT")
Hgt$ = Hgt1

Wdt1= get_section_param(Pce1,"WIDTH")
Wdt2= get_section_param(Pce2,"WIDTH")

P1= 1
PX1= 2
PY1= 3
PZ1= 4
P2= 5
PX2= 6
PY2= 7
PZ2= 8
P = 9
X = 10
Y = 11
Z = 12
BP = 13
JP = 14
LP = 15
D = 16

Geom1= 1
Geom2= 2
Geom3= 3
Geom4= 4
Geom5= 5

get_origin_pos(Pce1) P1,PX1,PY1,PZ1
get_piece_localp(LP) Pce1,"IP",2
get_piece_vxgeom(Geom1) Pce1,LP

get_origin_pos(Pce2) P2,PX2,PY2,PZ2
get_piece_localp(LP) Pce2,"IP",2
get_piece_vxgeom(Geom2) Pce2,LP

if Bolt then
  get_origin_pos(Bolt) P,X,Y,Z
else
  get_origin_pos(This) P,Y,Z,X
endif


Tol = mm_to_dwg(10.01)
Ok= 0

point_to_vxgeom(BP) Geom1,P
if (is_in_vxgeom(Geom2,BP,1,Tol)) then
  vec_copy(JP) BP
  Ok= 1
endif
!
if Ok then
  set_joint_pos(This) JP,PY1,PZ1,PX1

  set RETURN "1"
endif

#UPDATE_JOINT#

#JOINT_TEST#

!debug ./joint_test.dbg

set RETURN "1"

#JOINT_TEST#

#JOINT_DETAILING#

!debug ./joint_detailing.dbg

I= new_iterator("",0,"ROLED","THIS")
This = next_item(I)

BoltLib$ = get_obj_param(This,"BOLT_LIB")
BoltMac$ = get_obj_param(This,"BOLT_MAC")

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","PCE1"
Pce1 = next_item(I)
if !Pce1 then exit

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","PCE2"
Pce2= next_item(I)
if !Pce2 then exit

JP= 1
JX= 2
JY= 3
JZ= 4
PP= 5
PX= 6
PY= 7
PZ= 8
D= 17
SP = 19
PP2= 20
PX2= 21
PY2= 22
PZ2= 23
LP= 24
BP= 25

Geom1 = 1

reversed = 0
!
get_origin_pos(This) JP,JX,JY,JZ
get_origin_pos(Pce1) PP,PX,PY,PZ
get_origin_pos(Pce2) PP2,PX2,PY2,PZ2
!
Hgt1= get_section_param(Pce1,"HEIGHT")
Wdt1= get_section_param(Pce1,"WIDTH")
Thick1= get_section_param(Pce1,"THICK")
Thick2= get_section_param(Pce2,"THICK")
Thc= Thick1+Thick2

! Delete old bolts
init_iterator(I) "",This,"CONNECTION_ROLEPARTS","B"
loop
  Bolt= next_item(I)
  if Bolt then
    disconnect(Bolt)
    del_object(Bolt)
  endif
  exitif !Bolt
endloop

! Set bolts
vec_rev(D) PX
if !proj_to_piece(SP,D,JP,D,Pce2,0.0,3) then exit

Bolt= new_macro(BoltLib$,BoltMac$,SP,JX,JY,JZ,1,1,0)
if Bolt then
  connect(This) Bolt,"B"
  set_same_tlink(Bolt) Pce1
  set_same_visib(Bolt) Pce1
  add_sobj(Pce2) Bolt
endif

vec_times_scalar(D) JX,BoltDist
vec_rev(D) D
vec_add(SP) SP,D

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","H1"
Hole= next_item(I)
exitif !Hole
set_origin_pos(Hole) JP,JX,JY,JZ

init_iterator(I) "",This,"CONNECTION_ROLEPARTS","H2"
Hole= next_item(I)
exitif !Hole
vec_rev(D) PX
if !proj_to_piece(JP,D,JP,D,Pce2,0.0,3) then exit
set_origin_pos(Hole) JP,JX,JY,JZ

set RETURN "1"

#JOINT_DETAILING#
