!*****************************************************************
! LGS_0515A_PRE              / 2015-05-12 / Vertex Systems       *
!                                                                *
! Preprocessing for Lgs track stud joint.                        *
!*****************************************************************

charset= latin1

#INPUT#

L = get($LANGUAGE)
Hdr$ = getprompt("ADD_SWAGEJ")
Prompt1$ = getprompt("SEL_TRACK") 
Prompt2$ = getprompt("SEL_PCE_END_OK")
if L==0 then
  Hdr$ = "Upotusliitoksen lisys"
  Prompt1$ = "Valitse track-osa"
  Prompt2$ = "Valitse liitettvt profiilit valmis"
endif

vec_to(1) 0,0,0
vec_to(2) 1,0,0
vec_to(3) 0,1,0
vec_to(4) 0,0,1

M = new_macro("","",1,2,3,4,1,1,0)
set_macro_param(M) "TASK","PICK_PIECE"
set_macro_param(M) "HEADER",Hdr$
set_macro_param(M) "PROMPT",Prompt1$
set_macro_param(M) "ROLE","PCE"

M = new_macro("","",1,2,3,4,1,1,0)
set_macro_param(M) "TASK","MARKINGS"
set_macro_param(M) "INPUT","0"

M = new_macro("","",1,2,3,4,1,1,0)
set_macro_param(M) "TASK","PICK_PCENDS"
set_macro_param(M) "HEADER",Hdr$
set_macro_param(M) "PROMPT",Prompt2$
set_macro_param(M) "ROLE","END"

#INPUT#


#BUILD#

!debug ./LGS_0515A_PRE_build_dbg

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

init_iterator(It) "",0,"ROLED","PCE")
Pce= next_item(It)
if !Pce then exit

! Type 0: allow both C-C and U-C
! Type 1: allow only C-C
! Type 2: allow only U-C
Type$= get_obj_param(This,"PRE~TYPE")
Type= Type$

! Check material
Shape$= get_material_param(Pce,"SHAPE")
if (!Type && !cmp(Shape$,"C") && !cmp(Shape$,"U")) then exit
if (Type == 1 && (!cmp(Shape$,"C"))) then exit
if (Type == 2 && (!cmp(Shape$,"U"))) then exit
Hgt= get_section_param(Pce,"HEIGHT")

FitTole= mm_to_dwg(2.0)
MinLen= mm_to_dwg(40.0)
EndGap= mm_to_dwg(20.0)
CheckTrack= 0

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

Depth= mm_to_dwg(5)
Depth$= Depth

PP= 1
PX= 2
PY= 3
PZ= 4
EP= 5
EX= 6
EY= 7
EZ= 8
D=  9
CL=  10
CL2= 11
CP=  12
CP2= 13
P= 14
X= 15
Y= 16
Z= 17
D2= 18
JP= 19
JX= 20
JY= 21
JZ= 22
NP(1)= 23
NP(2)= 24

Geom= 1
Geom2= 2

Done= 0

vec_to(CL) 0,0,0
vec_to(CL2) 0,0,0
NP(1)= 0
NP(2)= 0

get_origin_pos(Pce) PP,PX,PY,PZ
get_piece_vxgeom(Geom) Pce,CL

DimpleSlotOn$ = get_material_param(Pce,"DIMPLESLOT.ON")
if (len(DimpleSlotOn$)) then set_obj_param(This) "PRE~DTYPE",DimpleSlotOn$

Silent= get_obj_param(This,"PRE~SILENT")
Reinforced = get_obj_param(This,"PRE~REIN")
if (!Silent) then
  if Reinforced then
    if edit_obj_data(This,"LGS_0515A2_rein_form",1) < 1 then
      exit
    endif
  else
    if (edit_obj_data(This,"LGS_0515A2_form",1) < 1) then
      exit
    endif
  endif
endif
Sq= get_obj_param(This,"PRE~SQ")
Sq$= Sq
Eg= get_obj_param(This,"PRE~ENDGAP")
Eg$ = Eg
Full$ = get_obj_param(This,"PRE~FULLDIMPLE")
DOffset= get_obj_param(This,"PRE~DOFFSET")
DOffset$ = DOffset
!capacity program
Cap$ = get_obj_param(This,"PRE~CAP")
CapLib$ = get_obj_param(This,"PRE~LIB")
! fastener code
FCode$ = get_obj_param(This,"PRE~FCODE")
FCode2$ = get_obj_param(This,"PRE~FCODE2")
if (empty_string(FCode2$)) then FCode2$ = ""
PlateWidth$ = get_obj_param(This,"PRE~PLATE_WDT")
PlateHeight$ = get_obj_param(This,"PRE~PLATE_HGT")
PlateThick$ = get_obj_param(This,"PRE~PLATE_THCK")

LipCutLen = get_obj_param(This,"PRE~LIPLEN")
LipCutLen = mm_to_dwg(LipCutLen)
LipCutLen$ = LipCutLen
NotchLen = get_obj_param(This,"PRE~NOTCHLEN")
NotchLen = mm_to_dwg(NotchLen)
NotchLen$ = NotchLen
SwageLen = get_obj_param(This,"PRE~SWLEN")
SwageLen = mm_to_dwg(SwageLen)
SwageLen$ = SwageLen
NoSwage = get_obj_param(This,"PRE~NOSWAGE")
EndMode= get_obj_param(This,"PRE~ENDMODE")
MinEndDist= get_obj_param(This,"PRE~ENDST")
ExtraLipLen= get_obj_param(This,"PRE~EXTRALIPLEN")
Dtype$= get_obj_param(This,"PRE~DTYPE")

ScrewLib$ = get_obj_param(This,"PRE~SLIB")
ScrewMacro$ = get_obj_param(This,"PRE~SMAC")
ScrewDist$ = get_obj_param(This,"PRE~SCREW_DIST")

EndShape$= get_obj_param(This,"PRE~ESHP")
EndShapeLim$= get_obj_param(This,"PRE~ESHP_LA")
RndCdim$= get_obj_param(This,"PRE~RCD") 
!
! Truss tool is to support possible customizations for a while
!
TrussTool$ = get_obj_param(This,"PRE~TRSTOOL")
TrussToolSize = get_obj_param(This,"PRE~TRSTOOLSZ")
TrussToolSize = mm_to_dwg(TrussToolSize)
TrussToolSize$ = TrussToolSize

NotchDiags$ = get_obj_param(This,"PRE~ND")

! Get virtual parameters
set PCE Pce
set LIPCUTLEN LipCutLen$
set NOTCHLEN NotchLen$
set SWAGELEN SwageLen$
call_virtual GetParams.prg
LipCutLen$= get("LIPCUTLEN")
NotchLen$= get("NOTCHLEN")
SwageLen$= get("SWAGELEN") 

init_iterator(It) "",0,"ROLED","END")
loop
  End= next_item(It)
  exitif !End
  get_origin_pos(End) EP,EX,EY,EZ
  Pce2= get_actual_piece(End)

  Ok1= 0
  Ok2= 0
  Ok3= 0
  Ok4= 0
  Ok5= 0

  !Check material
  Shape2$= get_material_param(Pce2,"SHAPE")
  Hgt2= get_section_param(Pce2,"HEIGHT")
  if (abs(Hgt-Hgt2) < mm_to_dwg(10)) then Ok1= 1

  ! Check if cross sections are in same direction
  Dot= vec_dot_prod(PY,EY)
  if abs(Dot) > 0.9999 then Ok2= 1

  ! Check if pieces are not parallel
  Dot= vec_dot_prod(PZ,EZ)
  if abs(Dot) < 0.999 then Ok3= 1

  ! Check if pieces are on same plane
  vec_sub(D) PP,EP
  Dot= vec_dot_prod(D,PY)
  if abs(Dot) < FitTole then Ok4= 1

  if Ok1 && Ok2 && Ok3 && Ok4 then
    Wdt2= get_section_param(Pce2,"WIDTH")
    get_piece_vxgeom(Geom2) Pce2,CL2
    Ok1= 0
    Ok2= 0
    Ok3= 0
    Ok4= 0

    ! Cross point have to be found
    if vxgeom_cross_pnts(Geom,CP,Geom2,CP2) then Ok1= 1

    ! Check point in track
    if is_in_vxgeom(Geom,CP,1,Wdt2*0.5-FitTole) then Ok2= 1

    ! Check point in connecting piece
    E2= get_actual_pcend(Pce2,0)
    if E2 == End then E2= get_actual_pcend(Pce2,1)
    get_origin_pos(E2) P,X,Y,Z
    vec_sub(D) EP,P
    vec_sub(D2) CP2,P
    if vec_dot_prod(D,D2) > FitTole then Ok3= 1
    if vec_len(D2) > MinLen then Ok4= 1

    if Ok1 && Ok2 && Ok3 && Ok4 then
      if CheckTrack then
        E= 0
        loop 2
          E2= get_actual_pcend(Pce,E)
          get_origin_pos(E2) P,X,Y,Z
          vec_sub(D) P,CP
          Gap= vec_dot_prod(D,Z)
          if abs(Gap-EndGap) > mm_to_dwg(0.001) then
            if Gap < Wdt2*0.5+FitTole then
              NP(E+1)= 23+End
              vec_times_scalar(D) Z,EndGap
              vec_add(NP(E+1)) CP,D
            endif
          endif
          E= E+1
        endloop
      endif

      vec_sub(JP) EP,EZ
      vec_copy(JX) X
      vec_copy(JY) Y
      vec_copy(JZ) Z

      Jnt = new_djoint("","",JP,JX,JY,JZ,0,0)
      exitif !Jnt
      set_djmember(Jnt) Pce,"PCE"
      set_djmember(Jnt) End,"END"
      set_djparam(Jnt) "ENDGAP",Eg$
      set_djparam(Jnt) "FULLDIMPLE",Full$
      set_djparam(Jnt) "DOFFSET",DOffset$
      set_djparam(Jnt) "CAP",Cap$
      set_djparam(Jnt) "CAPLIB",CapLib$
      set_djparam(Jnt) "LIPCUTLEN",LipCutLen$
      set_djparam(Jnt) "EXTRALIPLEN",ExtraLipLen
      set_djparam(Jnt) "NOTCHLEN",NotchLen$
      set_djparam(Jnt) "SWAGELEN",SwageLen$
      set_djparam(Jnt) "NOSWAGE",NoSwage
      set_djparam(Jnt) "ESHP",EndShape$
      set_djparam(Jnt) "ESHP_LA",EndShapeLim$
      set_djparam(Jnt) "RCD",RndCdim$
      set_djparam(Jnt) "TYPE",Type$
      set_djparam(Jnt) "ENDMODE",EndMode
      set_djparam(Jnt) "ENDDIST",MinEndDist
      TrussTool= TrussTool$
      if (TrussTool > mm_to_dwg(0.1)) then
        set_djparam(Jnt) "TRUSSTOOL",TrussTool$
        set_djparam(Jnt) "TRUSSTOOLSIZE",TrussToolSize$
      endif
      set_djparam(Jnt) "NOTCHDIAGS",NotchDiags$
      set_djparam(Jnt) "SQ",Sq$
      set_djparam(Jnt) "REIN",Reinforced
      set_djparam(Jnt) "PLATE_WIDTH",PlateWidth$
      set_djparam(Jnt) "PLATE_HEIGHT",PlateHeight$
      set_djparam(Jnt) "PLATE_THICK",PlateThick$
      set_djparam(Jnt) "SCREW_LIB",ScrewLib$
      set_djparam(Jnt) "SCREW_MAC",ScrewMacro$
      set_djparam(Jnt) "SCREW_DIST",ScrewDist$

      loop 2
        set PCE Pce
        set FCODE FCode$
        set FCODE2 FCode2$
        call_virtual MaleDimple.prg
        Hole= get("FEAT")
        if (Hole) then
          set_origin_pos(Hole) JP,JX,JY,JZ
          add_sobj(Pce) Hole
          set_djmember(Jnt) Hole,"M",1
        endif
      endloop

      loop 2
        set PCE Pce2
        set DTYPE Dtype$
        set FCODE FCode$
        set FCODE2 FCode2$
        call_virtual FemaleDimple.prg
        Hole= get("FEAT")
        if (Hole) then
          set_origin_pos(Hole) JP,JX,JY,JZ
          add_sobj(Pce2) Hole
          set_djmember(Jnt) Hole,"F",1
        endif

        set PCE Pce2
        set DTYPE "0"
        set FCODE FCode$
        set FCODE2 FCode2$
        call_virtual FemaleDimple.prg
        Hole= get("FEAT")
        if (Hole) then
          set_origin_pos(Hole) JP,JX,JY,JZ
          add_sobj(Pce2) Hole
          set_djmember(Jnt) Hole,"F2",1
        endif
      endloop  
      
      if (Sq > 0 || len(FCode2$) > 1) then
        Screws= new_macro(ScrewLib$,ScrewMacro$,JP,JX,JY,JZ,1,1,0,0)
        set_djmember(Jnt) Screws,"SP",1,Pce
        set_obj_param(Screws) "QTY",Sq$
        set_obj_param(Screws) "FCODE",FCode$
        if (len(FCode2$) > 1) then
          set_obj_param(Screws) "FCODE2",FCode2$
          set_obj_param(Screws) "CONN","1"
        endif
        set_same_tlink(Screws) Pce
        set_same_visib(Screws) Pce
        add_sobj(Pce2) Screws
      endif
       
      Done= 1
    endif
  endif

  // Move track ends if needed
  E= 0
  loop 2
    E2= get_actual_pcend(Pce,E)
    if NP(E+1) then
      if !piece_move_end(E2,NP(E+1),1) then exit
    endif
    E= E+1
  endloop

endloop

if Done then set RETURN "1"

#BUILD#
