!if $user=Tracer le polygone
!! il faut ordonner les segments
segsplan=!listcomplement $segsaux in $segsplan
segpoly= $(segsplan[1])
segpolylst= !replace internal - by , in $segpoly
lstsegs= !listcomplement $segpoly in $segsplan 
sortpts=
cepoint= $(segpolylst[1])
sortpts= !append item $cepoint to $sortpts
cepoint= $(segpolylst[2])
sortpts= !append item $cepoint to $sortpts

nbpts= !itemcnt $segsplan
!if $nbpts<3
  error=pasassezpts
!else
!!advance $nbpts
!for kk=3 to $nbpts
 n= !itemcnt $lstsegs
 trouveseg=0
 !for ii = 1 to $n
   ceseg=$(lstsegs[$ii])
   ceseglst= !replace internal - by , in $ceseg
   ptdeb= $(ceseglst[1])
   ptfin= $(ceseglst[2])

   !if $ptdeb=$cepoint
      sortpts=!append item $ptfin to $sortpts
      cepoint=$ptfin
      segpoly= !append item $(lstsegs[$ii]) to $segpoly
      lstsegs= !listcomplement $segpoly in $segsplan 
!!debug  ptdeb $ptdeb $ptfin ceseg:$ceseg, $sortpts - $segpoly - $segsplan
      trouveseg=1
      !break
   !else
    !if $ptfin=$cepoint
      cepoint=$ptdeb
      !!text reverse $ceseg
      segpoly = !append item $ceseg to $segpoly
      lstsegs= !listcomplement $segpoly in $segsplan 
      sortpts = !append item $ptdeb to $sortpts
!!debug  ptfin $ptdeb $ptfin ceseg:$ceseg, $sortpts - $segpoly - $segsplan

      trouveseg=1
      !break
    !endif

   !endif
 !next
 !if $trouveseg=0
     error=$error openpoly
     !advance errcnt
     !advance errcnt
     !break
  !else
     error=
 !endif
!next 
 !if $error=
 n= !itemcnt $ptsplan
 bary_x=$bary_x / $n
 bary_y=$bary_y / $n
 polyg=fpoly orange
 n=!itemcnt $sortpts
 !for ii=1 to $n
    polyg= $polyg ,$(pt$(sortpts[$ii]))
 !next
 flpoly=ok
!!debug polyg: $polyg $sortpts plan:$segsplan - $ptsplan
           cadre = $cadre \
           $polyg\
           $cube

 !endif
 !endif
!endif
!endif

!if $user=Choix des segments
!!on vrifie que les 2 segments choisis sont coplanaires
!advance step
!if $rseg1= or $rseg2=
 error=notenoughinfo
!exit
!endif
rseg1lst= !replace internal - by , in $rseg1
pt1= $(rseg1lst[1])
fpt1= !text copy $pt1 mask 10
pt2= $(rseg1lst[2])
fpt2= !text copy $pt2 mask 10
rseg2lst= !replace internal - by , in $rseg2
pt3= $(rseg2lst[1])
fpt3= !text copy $pt3 mask 10
pt4= $(rseg2lst[2])
fpt4= !text copy $pt4 mask 10

!! si segment auxiliiaire
!if ($rseg1 isitemof $segsaux) or ($rseg2 isitemof $segsaux)
     trouveprol=0
     !for ii = 1 to 8
      !for ll = 0 to 5
          l1=!eval $ll*5+1
          l2=!eval $l1+4
       ceprol=  !item $l1..$l2 of $(aux$ii)
!!if $ii=1
 !!debug $ceprol - $rseg1 :$rseg2
!!endif 
       !if ($(ceprol[1])=$rseg1 and $(ceprol[2])=$rseg2) or ($(ceprol[1])=$rseg2 and $(ceprol[2])=$rseg1)
!!debug seg1 $rseg1 seg2 $rseg2 ceprol $ceprol
           pt1=$(ceprol[3])
           pt1=$(pt$pt1)
           lpt2=$(ceprol[4])
           pt2=$(pt$lpt2)
           pt3=$(ceprol[5])
           pt3=$(pt$pt3)
           ligne =  linewidth 1\
           dlines green,$pt1,$pt2,$pt3\
           text black,$pt2,medium,$(ceprol[4])\
           linewidth 3
           cadre = $cadre \
           $ligne
           ptsplan = !append item $lpt2 to $ptsplan
           color$lpt2=red
           ptsplan = !listuniq $ptsplan
           ptsplan= !sort item $ptsplan
           trouveprol=1
           !break
       !endif
     !next
     !next

!else
segcoplan=!listintersect $(face$(fpt1)) and $(face$(fpt2))
segcoplan=!listintersect $(face$(fpt3)) and $segcoplan
segcoplan=!listintersect $(face$(fpt4)) and $segcoplan
flsegcoplan=!itemcnt $segcoplan
  !if $flsegcoplan=0
     error=$error segnoncoplan
     !advance errcnt
     !advance errcnt
     !goto finprol
  !endif
:debutprol
     error=
     seg1 = $pt1,$pt2
     seg1 = !sort item $seg1
     seg1 = $(seg1[1])-$(seg1[2])
     seg2 = $pt3,$pt4
     seg2 = !sort item $seg2
     seg2 = $(seg2[1])-$(seg2[2])
     trouveprol=0
     !for ii = 1 to 24
       !for ll=0 to 3
          l1=!eval $ll*6+1
          l2=!eval $l1+5
       ceprol=  !item $l1..$l2 of $(prol$ii)
!!debug seg1 $seg1 seg2 $seg2 ceprol $ceprol
       !if ($(ceprol[1])=$seg1 and $(ceprol[2])=$seg2) or ($(ceprol[1])=$seg2 and $(ceprol[2])=$seg1)
           pt1=$(ceprol[3])
           pt1=$(pt$pt1)
           lpt2=$(ceprol[4])
           pt2=$(pt$lpt2)
           !if $(pt2[1]) > $xmax
              xmax=$xmaxmax
           !endif
           !if $(pt2[1]) < $xmin
              xmin=$xminmin
           !endif
           !if $(pt2[2]) > $ymax
              ymax=$ymaxmax
           !endif
           !if $(pt2[2]) < $ymin
              ymin=$yminmin
           !endif
           pt3=$(ceprol[5])
           pt3=$(pt$pt3)
           ligne =  linewidth 1\
           dlines green,$pt1,$pt2,$pt3\
           text black,$pt2,medium,$(ceprol[4])\
           linewidth 3
           cadre = $cadre \
           $ligne
           ptsplan = !append item $lpt2 to $ptsplan
           ptsplan = !listuniq $ptsplan
           ptsplan= !sort item $ptsplan
           color$lpt2=red
           trouveprol=1
           !break
       !endif
     !next
     !next
     !if $trouveprol=0
       error = prolpasutil
     !endif
:finprol
 !endif
!endif

!if $user=Choix du plan
!advance step
!if $raux=
 error=notenoughinfo
!exit
!endif
pt1=!text copy $raux mask 01000
Pt2=!text copy $raux mask 00100
Pt3=!text copy $raux mask 00010
facept1=!listintersect $(face$pt1) and $(face$Pt2)
!if $facept1=
  pt2=$Pt3
  pt3=$Pt2
!else
  pt2=$Pt2
  pt3=$Pt3
!endif
!! dtermination du milieu oppos
  pt4x=!eval $(pt$pt1[1])+$(pt$pt3[1])-$(pt$pt2[1])
  pt4y=!eval $(pt$pt1[2])+$(pt$pt3[2])-$(pt$pt2[2])
  !for ii=9 to 20
     pt=$(listpoint[$ii])
     !if $(pt$pt[1])=$pt4x and $(pt$pt[2])=$pt4y
       pt4=$(listpoint[$ii])
       !break
     !endif
  !next
!! ajout des segments aux segments du cube
   seg=!sort item $pt3,$pt4
   segscube=!append item $(seg[1])-$(seg[2]) to $segscube
!! ajout du segment au segment du plan
   nd=!listintersect $(dte$(pt2)) and $(dte$(pt3))
   !for ii=9 to 20
     !if $(dte$(listpoint[$ii]))=$nd
       ptmil=$(listpoint[$ii])
       !break
     !endif
   !next
   seg=!sort item $ptmil,$pt1
   segsplan=!append item $(seg[1])-$(seg[2]) to $segsplan
!! ajout du segment au segment auxiliaire
   segsaux=!append item $(seg[1])-$(seg[2]) to $segsaux
  drawaux=linewidth 1 \
  dlines blue,$(pt$pt1),$(pt$pt2),$(pt$pt3),$(pt$pt4),$(pt$pt1) \
  dline $(pt$ptmil),$(pt$pt1),orange
  linewidth 2
  cadre = $cadre \
   $drawaux
   $cube
!endif
