Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with rad.ObjDrwVTK #17

Open
mf-qian opened this issue May 2, 2021 · 4 comments
Open

issue with rad.ObjDrwVTK #17

mf-qian opened this issue May 2, 2021 · 4 comments

Comments

@mf-qian
Copy link

mf-qian commented May 2, 2021

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
Copy link

quorx commented May 12, 2021

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 mf-qian closed this as completed May 15, 2021
@mf-qian
Copy link
Author

mf-qian commented May 15, 2021

Thank you quorx

@mf-qian
Copy link
Author

mf-qian commented May 20, 2021

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

@mf-qian mf-qian reopened this May 20, 2021
@quorx
Copy link

quorx commented May 21, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants