!*****************************************************************
! LGS_0515C_PRE              / 2015-05-13 / Vertex Systems       *
!                                                                *
! Preprocessing for Lgs track stud corner joint.                 *
!*****************************************************************

charset= latin1

#INPUT#

L = get($LANGUAGE)
Hdr$ = getprompt("ADD_SWAGE_CNR")
Prompt1$ = getprompt("SEL_TRACK_OK")
Prompt2$ = getprompt("SEL_PCE_END_OK")
if L==0 then
  Hdr$ = "Nurkkaliitoksen lisys"
  Prompt1$ = "Valitse track-osa valmis"
  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_PCENDS"
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","PICK_PCENDS"
set_macro_param(M) "HEADER",Hdr$
set_macro_param(M) "PROMPT",Prompt2$
set_macro_param(M) "ROLE","END"

#INPUT#


#BUILD#

debug ./LGS_0515C_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

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

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

Geom= 1
Geom2= 2

Done= 0

vec_to(CL) 0,0,0
vec_to(CL2) 0,0,0

! 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$

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")
if !Silent then
  if edit_obj_data(This,"LGS_0515C_form",1) < 1 then exit
endif
Sq= get_obj_param(This,"PRE~SQ")
Sq$= Sq
DOffset= get_obj_param(This,"PRE~DOFFSET")
DOffset$ = DOffset
WebHole$= get_obj_param(This,"PRE~WEBHOLE")
if cmp(WebHole$,"YES") then
  WebHole = 1
else
  WebHole = 0
endif
!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")
! Connector code
FCode2$ = get_obj_param(This,"PRE~FCODE2")
if (empty_string(FCode2$)) then FCode2$ = ""
DoneE0= 0

Eg= get_obj_param(This,"PRE~ENDGAP")
Eg$ = Eg
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")
WebDim = get_obj_param(This,"PRE~WEBDIM")
WebDim = mm_to_dwg(WebDim)
WebDim$ = WebDim
Dtype$= get_obj_param(This,"PRE~DTYPE")

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

EndShape$= get_obj_param(This,"PRE~ESHP")
EndShapeLim$= get_obj_param(This,"PRE~ESHP_LA")
EndShape2$= get_obj_param(This,"PRE~ESHP2")
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") 

Ip= new_iterator("",0,"ROLED","PCE")
loop
  PceEnd= next_item(Ip)
  exitif !PceEnd
  Pce= get_actual_piece(PceEnd)

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

  !Check material
  Shape$= get_material_param(Pce,"SHAPE")
  if (Type == 1 && (!cmp(Shape$,"C"))) then exit
  if (Type == 2 && (!cmp(Shape$,"U"))) then exit
  Hgt= get_section_param(Pce,"HEIGHT")
  
  Found= 0
  FoundDist= mm_to_dwg(100000)
  init_iterator(It) "",0,"ROLED","END")
  loop
    End= next_item(It)
    exitif !End
    I=1
    Ok= 1
    loop DoneE0
      if DoneE(I) == End then Ok= 0
      I= I+1
    endloop
    
    if Ok then
      get_origin_pos(End) EP,EX,EY,EZ
      Pce2= get_actual_piece(End)

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

      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.995 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

      ! Check connecting direction 
      Dot= vec_dot_prod(PX,EZ)
      if Dot < -0.1 then Ok5= 1

      if Ok1 && Ok2 && Ok3 && Ok4 && Ok5 then
        vec_sub(D) PP,EP
        Dist= vec_len(D)
        if Dist < FoundDist then
          Found= End
          FoundDist= Dist
        endif
      endif
    endif
  endloop

  if Found then
    End= Found
    Pce2= get_actual_piece(End)
    DoneE0= DoneE0+1
    DoneE(DoneE0)= End
    DoneE0= DoneE0+1
    DoneE(DoneE0)= PceEnd

    get_origin_pos(End) EP,EX,EY,EZ

    Wdt2= get_section_param(Pce2,"WIDTH")
    get_piece_vxgeom(Geom2) Pce2,CL2
    Ok1= 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 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 && Ok3 && Ok4 then
      vec_copy(JP) CP2
      vec_rev(JZ) PY
      vec_copy(JX) PZ
      vec_cross_prod(JY) JZ,JX

      Jnt = new_djoint("","",JP,JX,JY,JZ,0)
      exitif !Jnt
      set_djmember(Jnt) PceEnd,"E1"
      set_djmember(Jnt) End,"E2"
      set_djparam(Jnt) "ENDGAP",Eg$
      set_djparam(Jnt) "DOFFSET",DOffset$
      set_djparam(Jnt) "CAP",Cap$
      set_djparam(Jnt) "CAPLIB",CapLib$
      set_djparam(Jnt) "LIPCUTLEN",LipCutLen$
      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) "ESHP2",EndShape2$
      set_djparam(Jnt) "RCD",RndCdim$
      TrussTool= TrussTool$
      if (TrussTool == 1) then
        set_djparam(Jnt) "TRUSSTOOL",TrussTool$
        set_djparam(Jnt) "TRUSSTOOLSIZE",TrussToolSize$
      endif
      set_djparam(Jnt) "NOTCHDIAGS",NotchDiags$
      set_djparam(Jnt) "TYPE",Type$

      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,PceEnd
        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

      if WebHole then
        Hole = new_feature("HOLE","0","")
        if !Hole then exit
        set_origin_pos(Hole) JP,JX,JY,JZ
        set_obj_param(Hole) "TYPE","C"
        set_obj_param(Hole) "W",WebDim$
        set_obj_param(Hole) "H",WebDim$
        set_obj_param(Hole) "DIA",WebDim$
        set_obj_param(Hole) "D",Depth$
        add_sobj(Pce) Hole
        set_djmember(Jnt) Hole,"W",1
      endif

      Done= 1
    endif
  endif
endloop

if Done then set RETURN "1"

#BUILD#
