ochubar / Radia

3D Magnetostatics Computer Code
Other
34 stars 19 forks source link

issue with rad.ObjDrwVTK #17

Open mf-qian opened 3 years ago

mf-qian commented 3 years ago

when using this function in modelling script, nothing happened when running. no window pop out nor error message shown for both Windows and Linux.

quorx commented 3 years ago

Hi, You may need the function ObjDrwOpenGL. ObjDrwVTK just returns a dictionary of polygon data suitable for creating a *.vtk file (see help below). Quorx

Help on built-in function ObjDrwVTK in module radia:

ObjDrwVTK(...) ObjDrwVTK(obj,'EdgeLines->True|False,Faces->True|False,Axes->True|False') exports data for viewing 3D geometry of the object obj. The data is in the format compatible with VTK graphics library. The option 'EdgeLines->True|False' (default 'EdgeLines->True') highlights the edge lines of objects; the option 'Faces->True|False' (default 'Faces->True') shows faces of the objects; the option 'Axes->True|False' (default 'Axes->True') shows the Cartesian frame axes.

mf-qian commented 3 years ago

Thank you quorx

mf-qian commented 3 years ago

Hi, You may need the function ObjDrwOpenGL. ObjDrwVTK just returns a dictionary of polygon data suitable for creating a *.vtk file (see help below). Quorx

Help on built-in function ObjDrwVTK in module radia: ObjDrwVTK(...) ObjDrwVTK(obj,'EdgeLines->True|False,Faces->True|False,Axes->True|False') exports data for viewing 3D geometry of the object obj. The data is in the format compatible with VTK graphics library. The option 'EdgeLines->True|False' (default 'EdgeLines->True') highlights the edge lines of objects; the option 'Faces->True|False' (default 'Faces->True') shows faces of the objects; the option 'Axes->True|False' (default 'Axes->True') shows the Cartesian frame axes.


Hi, Could you please share a sample code showing how to create *.vtk file from the dictionary returned by radObjDrwVTK? Thank you

quorx commented 3 years ago

Hi mf-qian,

Below is the example Python code.

Good luck!

quorx

import radia as rad
import csv
from itertools import accumulate

rad.UtiDelAll()

def chunks(lst, n):
    """Yield successive n-sized chunks from a list called 'lst'."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

def exportGeometryToVTK(obj, fileName='radia_Geometry'):
    '''
    Writes the geometry of RADIA object "obj" to file fileName.vtk for use in Paraview. The format is VTK legacy, because it's simple. The file consists of polygons only (no cells).    
    '''

    vtkData = rad.ObjDrwVTK(obj, 'Axes->False')

    lengths = vtkData['polygons']['lengths']
    nPoly = len(lengths)
    offsets = list(accumulate(lengths))
    offsets.insert(0, 0) # prepend list with a zero
    points = vtkData['polygons']['vertices']
    nPnts = int(len(points)/3)

    # format the points array to be floats rather than double
    points = [round(num, 8) for num in points]        

    # define the connectivity list
    conn = list(range(nPnts)) 

    # define colours array
    colors = vtkData['polygons']['colors']

    # pre-process the output lists to have chunkLength items per line
    chunkLength = 9 # this writes 9 numbers per line (9 is the number used in Paraview if data is saved as the VTK Legacy format)
    offsets = list(chunks(offsets, chunkLength))
    points = list(chunks(points, chunkLength))
    conn = list(chunks(conn, chunkLength))
    colors = list(chunks(colors, chunkLength))

    # write the data to file
    with open(fileName + ".vtk", "w", newline="") as f:
        f.write('# vtk DataFile Version 5.1\n')
        f.write('vtk output\nASCII\nDATASET POLYDATA\n')
        f.write('POINTS ' + str(nPnts) + ' float\n')

        writer = csv.writer(f, delimiter=" ")
        writer.writerows(points)
        f.write('\n')
        f.write('POLYGONS ' + str(nPoly+1) + ' ' + str(nPnts) + '\n')
        f.write('OFFSETS vtktypeint64\n')
        writer.writerows(offsets)
        f.write('CONNECTIVITY vtktypeint64\n')  
        writer.writerows(conn)
        f.write('\n')
        f.write('CELL_DATA ' + str(nPoly) + '\n')
        f.write('COLOR_SCALARS Radia_colours 3\n')
        writer.writerows(colors)

#----------------------------------------------------------
# The function below is from the RADIA example #2        
#----------------------------------------------------------
def BuildGeometry():

    #Current Densities in A/mm^2
    j1 = 128; j2 = 256

    #Coil Presentation Parameters
    n1 = 3; n2 = 6; c2 = [1,0,0]; c1 = [0,1,1]; thcn = 0.001

    #Create 5 Coils
    Rt1 = rad.ObjRaceTrk([0.,0.,38.], [9.5,24.5], [120.,0.], 36, n1, j1)
    rad.ObjDrwAtr(Rt1, c1, thcn)
    Rt3 = rad.ObjRaceTrk([0.,0.,76.], [10.,25.], [90.,0.], 24, n1, j1)
    rad.ObjDrwAtr(Rt3, c1, thcn)
    Rt2 = rad.ObjRaceTrk([0.,0.,38.], [24.5,55.5], [120.,0.], 36, n1, j2)
    rad.ObjDrwAtr(Rt2, c2, thcn)
    Rt4 = rad.ObjRaceTrk([0.,0.,76.], [25.,55.], [90.,0.], 24, n1, j2)
    rad.ObjDrwAtr(Rt4, c2, thcn)
    Rt5 = rad.ObjRaceTrk([0.,0.,60.], [150.,166.3], [0.,0.], 39, n2, -j2)
    rad.ObjDrwAtr(Rt5, c2, thcn)

    Grp = rad.ObjCnt([Rt1, Rt2, Rt3, Rt4, Rt5])

    #Define Mirror Coils
    rad.TrfZerPara(Grp, [0,0,0], [0,0,1])

    return Grp

# Build the Geometry
g = BuildGeometry()

# Display the Geometry in 3D Viewer
rad.ObjDrwOpenGL(g)

# export the geometry to *.vtk file 
exportGeometryToVTK(g, 'RADIA_Python_Example_#2')

Image from Radia viewer image

Image from Paraview image

In Paraview for Coloring select the 'Radia_colours' cell data and uncheck 'Map Scalars' image