!***************************************************************
! ANCHOR_HOLE                 / 2006-10-02 / Vertex Systems    *
!***************************************************************

#UPDATE_JOINT#

!debug ./update_joint.dbg

JP = 1
JX = 2
JY = 3
JZ = 4
LP = 5
SP = 6
EP = 7
D = 8

Geom = 1

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

Side$ = get_obj_param(This,"SIDE")

get_origin_pos(This) JP,JX,JY,JZ

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","STUD"
Stud = next_item(It)
if !Stud then exit

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","PLATE"
Plate = next_item(It)
if !Plate then exit

Shp$= get_material_param(Stud,"SHAPE")
if cmp(Shp$,"C") then
  Thick= get_section_param(Stud,"THICK")
  get_piece_localp(LP) Stud,"IP",2
  Xoffset = vec_get_x(LP)
  vec_to(LP) Xoffset+Thick,vec_get_y(LP),vec_get_z(LP)
  get_piece_vxgeom(Geom) Stud,LP
else
  if cmp(Side$,"EXT") then
    get_piece_localp(LP) Stud,"IP",8
  else
    get_piece_localp(LP) Stud,"IP",2
  endif
  get_piece_vxgeom(Geom) Stud,LP  
endif
point_to_vxgeom(JP) Geom,JP

Shp$= get_material_param(Plate,"SHAPE")
if cmp(Shp$,"C") || cmp(Shp$,"U") then
  Thick= get_section_param(Plate,"THICK")
  get_piece_localp(LP) Plate,"IP",2
  Xoffset = vec_get_x(LP)
  vec_to(LP) Xoffset+Thick,vec_get_y(LP),vec_get_z(LP)
  get_piece_vxgeom(Geom) Plate,LP
else
  get_piece_localp(LP) Plate,"IP",8
  get_piece_vxgeom(Geom) Plate,LP  
endif
point_to_vxgeom(JP) Geom,JP

set_origin_pos(This) JP,JX,JY,JZ

set RETURN "1"

#UPDATE_JOINT#


#JOINT_TEST#

set RETURN "1"

#JOINT_TEST#


#JOINT_DETAILING#

debug ./joint_detailing.dbg

JP = 1
JX = 2
JY = 3
JZ = 4
PP = 5
PX = 6
PY = 7
PZ = 8
MP = 9
D = 10
SP = 11
EP = 12
HP = 13
HN = 14
LP = 15

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

get_origin_pos(This) JP,JX,JY,JZ

AnchorCode$= get_obj_param(This,"ANCHOR_CODE")
AnchorLib$= get_obj_param(This,"ANCHOR_LIB")
AnchorDist = get_obj_param(This,"ANCHOR_DIST")

BoltCode$ = get_obj_param(This,"BOLT_CODE")
BoltLen$ = get_obj_param(This,"BOLT_LEN")
BoltDia$ = get_obj_param(This,"BOLT_DIA")
BoltDists$ = get_obj_param(This,"BOLT_DISTS")
SHoleDist = get_obj_param(This,"SHOLE_DIST")
SHoleSize$ = get_obj_param(This,"SHOLE_SIZE")

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","TOOL"
Tool = next_item(It)
if !Tool then exit

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","PLATE"
BottomPlate = next_item(It)
if !BottomPlate then exit

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","STUD"
Stud = next_item(It)
if !Stud then exit

get_origin_pos(Stud) PP,PX,PY,PZ

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","ANCHOR"
Anchor= next_item(It)
if !Anchor then
  Anchor= new_macro(AnchorLib$,AnchorCode$,JP,JX,JY,JZ,1,1,0)
  connect(This) Anchor,"ANCHOR"
  Parent = get_parent(Stud)
  add_sobj(Parent) Anchor
  set_same_tlink(Anchor) BottomPlate
endif
if Anchor then
  set_obj_param(Anchor) "MOUNT","L"
  vec_times_scalar(D) JX,AnchorDist
  vec_add(MP) JP,D

  set_origin_pos(Anchor) MP,JX,JY,JZ
endif

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","B"
loop
  Macro= next_item(It)
  exitif !Macro

  disconnect(Macro)
  del_object(Macro)
endloop

init_iterator(It) "",This,"CONNECTION_ROLEPARTS","H"
loop
  Hole= next_item(It)
  if Hole then
    disconnect(Hole)
    del_object(Hole)
  endif
  exitif !Hole
endloop

if len(BoltDists$) > 0 and !findstr(BoltDists$,"---") then
  Temp$ = BoltDists$

  I=1
  MaxLoop=100
  loop
    StrP= findstr(Temp$," ")
    if StrP=0 then StrP=findstr(Temp$,",")
    if StrP > 0 then
      Height$ = Temp$[1,StrP-1]
      Height = Height$
      Temp$ = Temp$[StrP+1,0]
    else 
        Height$ = Temp$
        Height = Height$
      endif

    vec_times_scalar(D) JZ,Height
    vec_add(MP) JP,D
    Hole= new_feature("HOLE","0","0")
    if Hole then
      connect(This) Hole,"H"
      add_sobj(Stud) Hole

      set_origin_pos(Hole) MP,PZ,PY,PX

      set_obj_param(Hole) "TYPE","C"
      set_obj_param(Hole) "DIA","10"
      DD = mm_to_dwg(5)
      set_obj_param(Hole) "D",DD
      set_obj_param(Hole) "T","1"
      set_obj_param(Hole) "IDENTS","1"
    endif

    vec_rev(D) JX
    Bolt= new_macro("PLATE_MACRO","BOLT",MP,PY,D,PZ,1,1,0)
    if Bolt then
      set_obj_param(Bolt) "CODE",BoltCode$
      set_obj_param(Bolt) "DESCRIPTION",BoltCode$
      set_obj_param(Bolt) "DIA",BoltDia$,1
      set_obj_param(Bolt) "LEN",BoltLen$,1
      connect(This) Bolt,"B"
      set_same_tlink(Bolt) Stud
      set_same_visib(Bolt) Stud
      add_sobj(Stud) Bolt
    endif

    init_iterator(It) "",This,"CONNECTION_ROLEPARTS","H2"
    loop
      Hole= next_item(It)
      exitif !Hole

      if Hole then
        Parent = get_parent(Hole)
        disconnect(Hole)
        del_object(Hole)
        upd_object_geom(Parent)
      endif
    endloop

    Parent = get_parent(Stud)

    Ip = new_iterator("PROFOBJ",Parent,"PARTS","")
    loop
      Pce= next_item(Ip)
      exitif !Pce

      Hit = proj_to_piece(HP,HN,MP,PX,Pce,0,3)

      if Pce != Stud && Hit then
        vec_sub(D) MP,HP
        Dist = vec_len(D)

        vec_sub(D) HP,JP
        Dot = vec_dot_prod(PX,D)
        if Dist < SHoleDist && Dot > 0 then
          Hole= new_feature("HOLE","147")
          if !Hole then exit
          connect(This) Hole,"H2"
          add_sobj(Pce) Hole
          set_obj_param(Hole) "T","1"
          Wdt$= get_section_param(Pce,"WIDTH")
          set_obj_param(Hole) "D",Wdt$
          set_origin_pos(Hole) HP,PY,PZ,PX

          Ptr= findstr(SrvHole$,"x")
          if Ptr then
            set_feat_param(Hole) "TYPE","RT"
            HH$= SrvHole$$[1,Ptr-1]
            HL$= SrvHole$$[Ptr+1,0]
            set_obj_param(Hole) "W",HL$
            set_obj_param(Hole) "H",HH$
          else
            set_feat_param(Hole) "TYPE","C"
            set_obj_param(Hole) "DIA",SHoleSize$
          endif
        endif
      endif
    endloop

    I=I+1 
    exitif (StrP=0) or (MaxLoop<I)
  endloop
endif

set RETURN "1"

#JOINT_DETAILING#
