Пример #1
0
def drawRectangle(npts):
    CTK.t = C.addBase2PyTree(CTK.t, 'CONTOURS', 1)
    nodes = Internal.getNodesFromName1(CTK.t, 'CONTOURS')
    nob = C.getNobOfBase(nodes[0], CTK.t)
    CTK.TXT.insert('START', 'Click left/lower corner...\n')
    w = WIDGETS['draw']
    prev = []; second = []
    if (CTK.__BUSY__ == False):
        CTK.__BUSY__ = True
        TTK.sunkButton(w)
        CPlot.setState(cursor=1)
        while (CTK.__BUSY__ == True):
            CPlot.unselectAllZones()
            CTK.saveTree()
            surfaces = getSurfaces()
            l = []
            while (l == []):
                l = CPlot.getActivePoint()
                time.sleep(CPlot.__timeStep__)
                w.update()
                if (CTK.__BUSY__ == False): break
            if (CTK.__BUSY__ == True):
                if (prev == []):
                    prev = l
                    CTK.TXT.insert('START', 'Click right/up corner...\n')
                elif (prev != l):
                    e1,e2 = getVectorsFromCanvas()
                    e1n = Vector.norm(e1)
                    e2n = Vector.norm(e2)
                    if (e2n > e1n): e1 = e2
                    P1 = l; P2 = prev
                    P1P2 = Vector.sub(P2, P1)
                    P1P2n = Vector.norm(P1P2)
                    Q = Vector.norm(Vector.cross(e1, P1P2))
                    L = math.sqrt( P1P2n*P1P2n - Q*Q )
                    sign = Vector.dot(e1, P1P2)
                    if (sign > 0): e1 = Vector.mul(L, e1)
                    else: e1 = Vector.mul(-L, e1)
                    P3 = Vector.add(P1, e1)
                    P4 = Vector.sub(P2, e1)
                    l1 = D.line(P1, P3, npts)
                    l2 = D.line(P3, P2, npts)
                    l3 = D.line(P2, P4, npts)
                    l4 = D.line(P4, P1, npts)
                    rect = T.join([l1,l2,l3,l4])
                    if (surfaces != []):
                        rect = T.projectOrthoSmooth(rect, surfaces)
                    CTK.add(CTK.t, nob, -1, rect)
                    CTK.TXT.insert('START', 'Rectangle created.\n')
                    CTK.__BUSY__ = False
                    TTK.raiseButton(w)
                    CPlot.setState(cursor=0)
                    #C._fillMissingVariables(CTK.t)
                    (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t)
                    CTK.TKTREE.updateApp()
                    CPlot.render()
                    CPlot.setState(cursor=0)
                    prev = []
                    return
        CTK.__BUSY__ = False
        TTK.raiseButton(w)
        CPlot.setState(cursor=0)
    else:
       CTK.__BUSY__ = False
       TTK.raiseButton(w)
       CPlot.setState(cursor=0)
Пример #2
0
def stretch1D(h):
    fail = False
    nzs = CPlot.getSelectedZones()
    nz = nzs[0]
    nob = CTK.Nb[nz]+1
    noz = CTK.Nz[nz]
    z = CTK.t[2][nob][2][noz]
    dims = Internal.getZoneDim(z)
    try:
        if dims[0] == 'Unstructured': a = C.convertBAR2Struct(z)
        else: a = z
    except Exception as e:
        #print 'Error: stretch1D: %s.'%str(e)
        Panels.displayErrors([0,str(e)], header='Error: stretch1D')
        return True # Fail

    ind = CPlot.getActivePointIndex()
    if ind == []: return True # Fail
    ind = ind[0]
    
    l = D.getLength(a)
    a = D.getCurvilinearAbscissa(a)
    zp = D.getCurvilinearAbscissa(z)
    distrib = C.cpVars(a, 's', a, 'CoordinateX')
    C._initVars(distrib, 'CoordinateY', 0.)
    C._initVars(distrib, 'CoordinateZ', 0.)
    distrib = C.rmVars(distrib, 's')
    
    N = dims[1]
    val = C.getValue(zp, 's', ind)
    
    Xc = CPlot.getActivePoint()
    valf = val
    Pind = C.getValue(z, 'GridCoordinates', ind)
    if ind < N-1: # cherche avec indp1
        Pindp1 = C.getValue(z, 'GridCoordinates', ind+1)
        v1 = Vector.sub(Pindp1, Pind)
        v2 = Vector.sub(Xc, Pind)
        if Vector.dot(v1,v2) >= 0:
            val2 = C.getValue(zp, 's', ind+1)
            alpha = Vector.norm(v2)/Vector.norm(v1)
            valf = val+alpha*(val2-val)
    if ind > 0 and val == valf: # cherche avec indm1
        Pindm1 = C.getValue(z, 'GridCoordinates', ind-1)
        v1 = Vector.sub(Pindm1, Pind)
        v2 = Vector.sub(Xc, Pind)
        if Vector.dot(v1,v2) >= 0:
            val2 = C.getValue(zp, 's', ind-1)
            alpha = Vector.norm(v2)/Vector.norm(v1)
            valf = val+alpha*(val2-val)
    
    if h < 0: # enforce point
        distrib = G.enforcePoint(distrib, valf)
    else: # enforce h
        if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1)
        elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1)
        else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1)
    try:
        a1 = G.map(a, distrib)
        CTK.replace(CTK.t, nob, noz, a1)
    except Exception as e:
        fail = True
        Panels.displayErrors([0,str(e)], header='Error: stretch1D')
    return fail
Пример #3
0
def apply2D(density, npts, factor, ntype=0):
    nzs = CPlot.getSelectedZones()
    nz = nzs[0]
    nob = CTK.Nb[nz]+1
    noz = CTK.Nz[nz]
    zone = CTK.t[2][nob][2][noz]
    ret = getEdges2D(zone, 0.)
    if ret is None: return True
    (m, r, u, ind) = ret
    out = []
    # Applique la fonction sur m[0] (edge a modifier)
    i = m[0]
    dims = Internal.getZoneDim(i)
    np = dims[1]*dims[2]*dims[3]
    if ntype == 0: # uniformize
        if density > 0: npts = D.getLength(i)*density
        if factor > 0: npts = np*factor[0]
        npts = int(max(npts, 2))
        distrib = G.cart((0,0,0), (1./(npts-1.),1,1), (npts,1,1))
        b = G.map(i, distrib)
    elif ntype == 1: # refine
        if factor < 0: factor = (npts-1.)/(np-1)
        else: npts = factor*(np-1)+1
        b = G.refine(i, factor, 1)
    elif ntype == 2: # stretch (factor=h)
        h = factor
        l = D.getLength(i)
        a = D.getCurvilinearAbscissa(i)
        distrib = C.cpVars(a, 's', a, 'CoordinateX')
        C._initVars(distrib, 'CoordinateY', 0.)
        C._initVars(distrib, 'CoordinateZ', 0.)
        distrib = C.rmVars(distrib, 's')
        N = dims[1]
        val = C.getValue(a, 's', ind)
        Xc = CPlot.getActivePoint()
        valf = val
        Pind = C.getValue(i, 'GridCoordinates', ind)
        if ind < N-1: # cherche avec indp1
            Pindp1 = C.getValue(i, 'GridCoordinates', ind+1)
            v1 = Vector.sub(Pindp1, Pind)
            v2 = Vector.sub(Xc, Pind)
            if Vector.dot(v1,v2) >= 0:
                val2 = C.getValue(a, 's', ind+1)
                alpha = Vector.norm(v2)/Vector.norm(v1)
                valf = val+alpha*(val2-val)
        if ind > 0 and val == valf: # cherche avec indm1
            Pindm1 = C.getValue(i, 'GridCoordinates', ind-1)
            v1 = Vector.sub(Pindm1, Pind)
            v2 = Vector.sub(Xc, Pind)
            if Vector.dot(v1,v2) >= 0:
                val2 = C.getValue(a, 's', ind-1)
                alpha = Vector.norm(v2)/Vector.norm(v1)
                valf = val+alpha*(val2-val)
        if h < 0: distrib = G.enforcePoint(distrib, valf)
        else:
            if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1)
            elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1)
            else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1)
        b = G.map(i, distrib)
    elif ntype == 3: # copyDistrib (factor=source=edge pour l'instant)
        source = factor
        b = G.map(i, source, 1)
    elif ntype == 4: # smooth (factor=eps, npts=niter)
        niter = npts
        eps = factor
        a = D.getCurvilinearAbscissa(i)
        distrib = C.cpVars(a, 's', a, 'CoordinateX')
        C._initVars(distrib, 'CoordinateY', 0.)
        C._initVars(distrib, 'CoordinateZ', 0.)
        distrib = C.rmVars(distrib, 's')
        bornes = P.exteriorFaces(distrib)
        distrib = T.smooth(distrib, eps=eps, niter=niter, 
                           fixedConstraints=[bornes])
        b = G.map(i, distrib, 1)
    dimb = Internal.getZoneDim(b)
    npts = dimb[1]
    out.append(b)
    # Raffine les edges si necessaires
    if npts != np:
        ret = getEdges2D(zone, 2.)
        if ret is None: return True
        (m, r, u, ind) = ret
    for i in r:
        dims = Internal.getZoneDim(i)
        np = dims[1]*dims[2]*dims[3]
        factor = (npts-1.)/(np-1)  # npts de m
        b = G.refine(i, factor, 1)
        out.append(b)
    # Garde les autres
    out += u
    #tp = C.newPyTree(['Base'])
    #tp[2][1][2] += out
    #C.convertPyTree2File(tp, 'edges.cgns')

    # Rebuild
    try:
        b = G.TFI(out)
        # Projection du patch interieur
        #dimsb = Internal.getZoneDim(b)
        #bs = T.subzone(b, (2,2,1), (dimsb[1]-1,dimsb[2]-1,1))
        #bs = T.projectOrtho(bs, [zone])
        #b = T.patch(b, bs, position=(2,2,1))

        #tp = C.newPyTree(['Base'])
        #tp[2][1][2] += [b, zone]
        #C.convertPyTree2File(tp, 'face.cgns')
        b = T.projectOrtho(b, [zone])
        CTK.replace(CTK.t, nob, noz, b)
        return False
    except Exception as e:
        Panels.displayErrors([0,str(e)], header='Error: apply2D')
        return True
Пример #4
0
def apply3D(density, npts, factor, ntype):
    nzs = CPlot.getSelectedZones()
    nz = nzs[0]
    nob = CTK.Nb[nz]+1
    noz = CTK.Nz[nz]
    zone = CTK.t[2][nob][2][noz]
    ret = getEdges3D(zone, 0.)
    if ret is None: return True
    (m, r, f, ue, uf, ind) = ret
    out = []
    # Applique la fonction sur m
    i = m[0]
    dims = Internal.getZoneDim(i)
    np = dims[1]*dims[2]*dims[3]
    if ntype == 0: # uniformize
        if density > 0: npts = D.getLength(i)*density
        if factor > 0: npts = np*factor[0]
        npts = int(max(npts, 2))
        distrib = G.cart((0,0,0), (1./(npts-1.),1,1), (npts,1,1))
        b = G.map(i, distrib)
    elif ntype == 1: # refine
        if factor < 0: factor = (npts-1.)/(np-1)
        else: npts = factor*(np-1)+1
        b = G.refine(i, factor, 1)
    elif ntype == 2: # stretch (factor=h)
        h = factor
        l = D.getLength(i)
        a = D.getCurvilinearAbscissa(i)
        distrib = C.cpVars(a, 's', a, 'CoordinateX')
        C._initVars(distrib, 'CoordinateY', 0.)
        C._initVars(distrib, 'CoordinateZ', 0.)
        distrib = C.rmVars(distrib, 's')
        N = dims[1]
        val = C.getValue(a, 's', ind)
        Xc = CPlot.getActivePoint()
        valf = val
        Pind = C.getValue(i, 'GridCoordinates', ind)
        if ind < N-1: # cherche avec indp1
            Pindp1 = C.getValue(i, 'GridCoordinates', ind+1)
            v1 = Vector.sub(Pindp1, Pind)
            v2 = Vector.sub(Xc, Pind)
            if Vector.dot(v1,v2) >= 0:
                val2 = C.getValue(a, 's', ind+1)
                alpha = Vector.norm(v2)/Vector.norm(v1)
                valf = val+alpha*(val2-val)
        if ind > 0 and val == valf: # cherche avec indm1
            Pindm1 = C.getValue(i, 'GridCoordinates', ind-1)
            v1 = Vector.sub(Pindm1, Pind)
            v2 = Vector.sub(Xc, Pind)
            if Vector.dot(v1,v2) >= 0:
                val2 = C.getValue(a, 's', ind-1)
                alpha = Vector.norm(v2)/Vector.norm(v1)
                valf = val+alpha*(val2-val)
        if h < 0: distrib = G.enforcePoint(distrib, valf)
        else:
            if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1)
            elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1)
            else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1)
        b = G.map(i, distrib)
    elif ntype == 3:
        source = factor
        b = G.map(i, source, 1)
    elif ntype == 4: # smooth (factor=eps, npts=niter)
        niter = npts
        eps = factor
        a = D.getCurvilinearAbscissa(i)
        distrib = C.cpVars(a, 's', a, 'CoordinateX')
        C._initVars(distrib, 'CoordinateY', 0.)
        C._initVars(distrib, 'CoordinateZ', 0.)
        distrib = C.rmVars(distrib, 's')
        bornes = P.exteriorFaces(distrib)
        distrib = T.smooth(distrib, eps=eps, niter=niter, 
                           fixedConstraints=[bornes])
        b = G.map(i, distrib, 1)
    dimb = Internal.getZoneDim(b)
    npts = dimb[1]
    out.append(b)
    # Raffine les edges si necessaires
    if npts != np:
        ret = getEdges3D(zone, 2.)
        if ret is None: return True
        (m, r, f, ue, uf, ind) = ret
    for i in r:
        dims = Internal.getZoneDim(i)
        np = dims[1]*dims[2]*dims[3]
        factor = (npts-1.)/(np-1)  # npts de m
        b = G.refine(i, factor, 1)
        out.append(b)
    # Garde les autres
    out += ue
    outf = []
    # Rebuild les faces
    for i in f:
        # trouve les edges de la face
        edges = P.exteriorFacesStructured(i)
        match = []
        for e in edges:
            dime = Internal.getZoneDim(e)
            np = dime[1]-1
            P0 = C.getValue(e, Internal.__GridCoordinates__, 0)
            P1 = C.getValue(e, Internal.__GridCoordinates__, np)
            for ei in out: # retrouve les edges par leurs extremites
                dimei = Internal.getZoneDim(ei)
                npi = dimei[1]-1
                Q0 = C.getValue(ei, Internal.__GridCoordinates__, 0)
                Q1 = C.getValue(ei, Internal.__GridCoordinates__, npi)
                t1 = Vector.norm2(Vector.sub(P0,Q0))
                t2 = Vector.norm2(Vector.sub(P1,Q1))
                if (t1 < 1.e-12 and t2 < 1.e-12): match.append(ei)
        if len(match) == 4: # OK
            fn = G.TFI(match)
            # Projection du patch interieur
            #dimsf = Internal.getZoneDim(fn)
            #fns = T.subzone(fn, (2,2,1), (dimsf[1]-1,dimsf[2]-1,1))
            #fns = T.projectOrtho(fns, [i])
            #fn = T.patch(fn, fns, position=(2,2,1))

            #fn = T.projectOrtho(fn, [i])
            outf.append(fn)
        else: return True
    outf += uf
    try:
        b = G.TFI(outf)
        CTK.replace(CTK.t, nob, noz, b)
        return False
    except Exception as e:
        Panels.displayErrors([0,str(e)], header='Error: apply3D')
        return True
Пример #5
0
def replaceText(event=None):
    if CTK.t == []: return
    if CTK.__MAINTREE__ <= 0:
        CTK.TXT.insert('START', 'Fail on a temporary tree.\n')
        CTK.TXT.insert('START', 'Error: ', 'Error')
        return
    nzs = CPlot.getSelectedZones()
    if nzs == []:
        CTK.TXT.insert('START', 'Selection is empty.\n')
        CTK.TXT.insert('START', 'Error: ', 'Error')
        return
    CTK.saveTree()
    # Recupere l'OBB de la selection
    Z = []
    dels = []
    for nz in nzs:
        nob = CTK.Nb[nz] + 1
        noz = CTK.Nz[nz]
        Z.append(CTK.t[2][nob][2][noz])
        dels.append(CTK.t[2][nob][0] + Internal.SEP1 +
                    CTK.t[2][nob][2][noz][0])
    nob0 = CTK.Nb[nzs[0]] + 1
    try:
        a = T.join(Z)
    except:
        a = Z[0]
    OBB = G.BB(a, method='OBB')

    P0 = C.getValue(OBB, 'GridCoordinates', 0)
    P1 = C.getValue(OBB, 'GridCoordinates', 1)
    P2 = C.getValue(OBB, 'GridCoordinates', 2)
    P3 = C.getValue(OBB, 'GridCoordinates', 4)
    v1 = Vector.sub(P1, P0)
    n1 = Vector.norm(v1)
    v2 = Vector.sub(P2, P0)
    n2 = Vector.norm(v2)
    v3 = Vector.sub(P3, P0)
    n3 = Vector.norm(v3)
    v1p = v1
    v2p = v2
    v3p = v3
    if abs(n1) < 1.e-12:
        v1 = Vector.cross(v2, v3)
        v1p = (0., 0., 0.)
    elif abs(n2) < 1.e-12:
        v2 = Vector.cross(v1, v3)
        v2p = (0., 0., 0.)
    elif abs(n3) < 1.e-12:
        v3 = Vector.cross(v2, v3)
        v3p = (0., 0., 0.)

    # Essaie de matcher les vecteur sur la vue p1,p2,p3
    # On suppose que dirCam doit etre e2, ...
    posCam = CPlot.getState('posCam')
    posEye = CPlot.getState('posEye')
    dirCam = CPlot.getState('dirCam')
    e2 = dirCam
    e3 = Vector.sub(posCam, posEye)
    e1 = Vector.cross(e2, e3)

    f1 = None
    f2 = None
    f3 = None
    Pt = P0
    s1 = Vector.dot(e1, v1)
    s2 = Vector.dot(e1, v2)
    s3 = Vector.dot(e1, v3)
    if abs(s1) > abs(s2) and abs(s1) > abs(s3):
        if s1 > 0: f1 = v1
        else:
            f1 = Vector.mul(-1., v1)
            Pt = Vector.add(Pt, v1p)
    elif abs(s2) > abs(s1) and abs(s2) > abs(s3):
        if s2 > 0: f1 = v2
        else:
            f1 = Vector.mul(-1., v2)
            Pt = Vector.add(Pt, v2p)
    elif abs(s3) > abs(s1) and abs(s3) > abs(s2):
        if s3 > 0: f1 = v3
        else:
            f1 = Vector.mul(-1., v3)
            Pt = Vector.add(Pt, v3p)
    s1 = Vector.dot(e2, v1)
    s2 = Vector.dot(e2, v2)
    s3 = Vector.dot(e2, v3)
    if abs(s1) > abs(s2) and abs(s1) > abs(s3):
        if s1 > 0: f2 = v1
        else:
            f2 = Vector.mul(-1., v1)
            Pt = Vector.add(Pt, v1p)
    elif abs(s2) > abs(s1) and abs(s2) > abs(s3):
        if s2 > 0: f2 = v2
        else:
            f2 = Vector.mul(-1., v2)
            Pt = Vector.add(Pt, v2p)
    elif abs(s3) > abs(s1) and abs(s3) > abs(s2):
        if s3 > 0: f2 = v3
        else:
            f2 = Vector.mul(-1., v3)
            Pt = Vector.add(Pt, v3p)
    s1 = Vector.dot(e3, v1)
    s2 = Vector.dot(e3, v2)
    s3 = Vector.dot(e3, v3)
    if abs(s1) > abs(s2) and abs(s1) > abs(s3):
        if s1 > 0: f3 = v1
        else:
            f3 = Vector.mul(-1., v1)
            Pt = Vector.add(Pt, v1p)
    elif abs(s2) > abs(s1) and abs(s2) > abs(s3):
        if s2 > 0: f3 = v2
        else:
            f3 = Vector.mul(-1., v2)
            Pt = Vector.add(Pt, v2p)
    elif abs(s3) > abs(s1) and abs(s3) > abs(s2):
        if s3 > 0: f3 = v3
        else:
            f3 = Vector.mul(-1., v3)
            Pt = Vector.add(Pt, v3p)
    (x0, y0, z0) = Pt
    n2 = Vector.norm(f2)

    # Cree le texte
    text = VARS[0].get()
    type = VARS[1].get()
    font = VARS[3].get()

    smoothness = VARS[2].get()
    smooth = 0
    if smoothness == 'Regular': smooth = 0
    elif smoothness == 'Smooth': smooth = 2
    elif smoothness == 'Very smooth': smooth = 4
    if type == '3D': a = D.text3D(text, smooth=smooth, font=font)
    elif type == '2D': a = D.text2D(text, smooth=smooth, font=font)
    elif type == '1D':
        a = D.text1D(text, smooth=smooth, font=font)
        a = C.convertArray2Tetra(a)
        a = T.join(a)
    BB = G.bbox(a)
    h2 = BB[4] - BB[1]

    # Scale, positionne le texte
    factor = n2 / h2
    a = T.homothety(a, (BB[0], BB[1], BB[2]), factor)
    a = T.translate(a, (x0 - BB[0], y0 - BB[1], z0 - BB[2]))
    a = T.rotate(a, (x0, y0, z0), ((1, 0, 0), (0, 1, 0), (0, 0, 1)),
                 ((f1, f2, f3)))

    CTK.t = CPlot.deleteSelection(CTK.t, CTK.Nb, CTK.Nz, nzs)
    CPlot.delete(dels)
    CTK.add(CTK.t, nob0, -1, a)
    #C._fillMissingVariables(CTK.t)
    CTK.TXT.insert('START', 'Text replaced.\n')
    (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t)
    CTK.TKTREE.updateApp()
    CPlot.render()