      subroutine addeal(ientno1,ipatch1,ientno2,ipatch2,icnstuorv,uorv,
     & mxe,mxp,eauorv,iealnk,ieaptchlnk,ieacnstuorv)
      real eauorv(mxe,mxp,mxe,mxp)
      integer iealnk(mxe,mxp,mxe),ieacnstuorv(mxe,mxp,mxe,mxp),
     &        ieaptchlnk(mxe,mxp,mxe,4)
c
      call fndeaindx(iealnk,mxe,mxp,ientno1,ipatch1,ientno2,indx1)
      if(indx1 .gt. mxe) then
        call fndeaindx(iealnk,mxe,mxp,ientno1,ipatch1,0,indx1)
      endif
      indx2 = 0
      call fndeaptindx(ieaptchlnk,mxe,mxp,ientno1,ipatch1,ientno2,
     &                 0,indx2)
c
      iealnk(ientno1,ipatch1,indx1) = ientno2
      ieaptchlnk(ientno1,ipatch1,ientno2,indx2) = ipatch2
      ieacnstuorv(ientno1,ipatch1,ientno2,ipatch2) = icnstuorv
      eauorv(ientno1,ipatch1,ientno2,ipatch2) = uorv
c
      return
      end
c
      subroutine fndeaindx(iealnk,mxe,mxp,ientno1,iptch1,ientno2,indx)
      integer iealnk(mxe,mxp,mxe)
c
      indx = 1
      dowhile( (iealnk(ientno1,iptch1,indx).ne.ientno2).and.
     &    (iealnk(ientno1,iptch1,indx).ne.0).and.(indx.le.mxe) )
        indx = indx + 1
      enddo
c
      return
      end
c
      subroutine fndeaptindx(ieaptchlnk,mxe,mxp,ientno1,iptch1,
     & ientno2,iptch2,indx)
      integer ieaptchlnk(mxe,mxp,mxe,4)
c
      indx = indx + 1
      icont = 1
      dowhile(icont.eq.1)
       iptch = ieaptchlnk(ientno1,iptch1,ientno2,indx)
       if(iptch.ne.iptch2) then
        indx = indx + 1
        if(indx.gt.mxp) then
          icont = 0
          indx = 0
        endif
       else
        icont = 0
       endif
      enddo
c
      return
      end
c
      subroutine detentpsurf
      include 'ctparm.inc'
      include 'dspparam.inc'
      include 'eacnst.inc'
      include 'eadata.inc'
      include 'surfent.inc'
      integer naent(mxent),iaent(mxent)
      common /intyp/ igesordc
c
c determine iges entities that belong to each surface
c
      numigsurf = 1
      do ient1 = 1,lstent
       icont = 1
       ient = ient1
       do i = 1,numigsurf-1
         call igetclse(mxent,numentpsurf(i),ient,ientps(1,i),junk,dif)
         if(dif.eq.0.0) icont = 0
       enddo
       ichk = 0
       indx = 0
       dowhile(icont.ne.0)
        call getentasent(ient,ippe(ient),naent(ient),iaent)
        do nument = 1,naent(ient)
         if(indx.gt.0) then
          call igetclse(mxent,indx,iaent(nument),ientps(1,numigsurf),
     &    junk,dif)
         else 
          dif = 1.0
         endif
         if(dif.ne.0.0) then
          indx = indx + 1
          ientps(indx,numigsurf) = iaent(nument)
         endif
        enddo
        ichk = ichk + 1
        if(ichk.gt.indx) then 
         icont = 0
        else
         ient = ientps(ichk,numigsurf)
        endif
       enddo
       if(indx.gt.0) then
         numentpsurf(numigsurf) = indx
         numigsurf = numigsurf + 1
       endif
      enddo
      numigsurf = numigsurf - 1
c
c sort entity numbers
      do i = 1,numigsurf
       call isort1d(numentpsurf(i),ientps(1,i))
      enddo
c
c only if input is a dc file
      if(igesordc.eq.1) then
       lstsurf = 1
       dowhile(numorigsurf(lstsurf).ne.0)
         lstsurf = lstsurf + 1
       enddo
       lstsurf = lstsurf - 1
       call isort1d(lstsurf,numorigsurf)
c 
       do ix = 1,lstsurf
        do i = 1, numigsurf
         if(i.eq.numorigsurf(ix)) then
          numentpsurf(ix) = numentpsurf(i)
          do j = 1,numentpsurf(i)
           ientps(j,ix) = ientps(j,i)
          enddo
         endif
        enddo
       enddo
       numigsurf = lstsurf
      endif
c
      return
      end
c
       subroutine getentasent(ient1,itotbp,nasent,iaent)
       include 'ctparm.inc'
       include 'dspparam.inc'
       include 'eacnst.inc'
       include 'eadata.inc'
       integer nasent,iaent(mxent)
c
c get iges entities that associated with a given iges entity
c
       ient2 = 1
       indx = 0
       nasent = 0
       dowhile(ient2.ne.0)
        do ibp = 1,itotbp
         indx = indx + 1
         ient2 = iealnk(ient1,ibp,indx)
         if(ient2.ne.0) then
           icont = 1
           i = 0
           iok = 1
           dowhile(icont.ne.0 .and. nasent.gt.0)
            i = i + 1
            if(iaent(i).eq.ient2) then
             iok = 0
             icont = 0
            endif
            if(i.eq.nasent) icont = 0
           enddo
           if(iok.eq.1) then
            nasent = nasent + 1
            iaent(nasent) = ient2
           endif
         endif
         if(ient2.eq.0 .and. nasent.eq.0) then
            nasent = nasent + 1
            iaent(nasent) = ient1
         endif
        enddo
       enddo
c
      return
      end
c
cg      subroutine chgeauorv(ient1,iptch1,iuorvold,uorvold,
cg     & iuorvnew,uorvnew)
cg      include 'ctparm.inc'
cg      include 'eadata.inc'
c
cg      ixent = 1
cg      ient2 = iealnk(ient1,iptch1,ixent)
cg      dowhile(ient2.ne.0)
cg        ixptch = 1
cg        iptch2 = ieaptchlnk(ient1,iptch1,ient2,ixptch)
cg        dowhile(iptch2.ne.0)
cg         iuorv1 = ieacnstuorv(ient1,iptch1,ient2,iptch2)
cg         uorv1 = eauorv(ient1,iptch1,ient2,iptch2)
cg         if(iuorv1.eq.iuorvold .and. uorv1.eq.uorvold) then
cg          ieacnstuorv(ient1,iptch1,ient2,iptch2) = iuorvnew
cg          eauorv(ient1,iptch1,ient2,iptch2) = uorvnew
cg         endif
cg         ixptch = ixptch + 1
cg         iptch2 = ieaptchlnk(ient1,iptch1,ient2,ixptch)
cg       enddo
cg        ixent = ixent + 1
cg        ient2 = iealnk(ient1,iptch1,ixent)
cg      enddo
c
cg       return
cg      end        

