Notifications
Clear all

[Closed] Loading a csv file with .net

Hello guys,

    I must be missing something here, i can't clear my head and get this script work properly.( i'm a .net noob , bear with me please :) )
    i'm having a problem with this particular function:
fn LoadCsv_fn lv =
        	(
        		theRange = #()
        		for h = 1 to lineVal.count do
        		(
        			li = dotNetObject "System.Windows.Forms.ListViewItem"
        			ThisVal = lineVal[h] as string
        			ThatVal = filterString ThisVal ","
        			for n in 1 to TheColumn.count do
        			(
        				sub_li = li.SubItems.add (ThatVal[n] as string)
        			)
        			append theRange li
        		)
        		lv.Items.AddRange theRange	
        	)
    the csv file looks like this:(object name, X coord, Y coord, Z coord)
box001,0,0,0
  Teapot,10,0,0
  Torus,3,2,-3
  Cylinder,5,66,-9
   Sphere,9,3,33
   box002,150,125,36
    The file loads just fine however with a one column shift in the .net listView
   Here's the whole code so far:
if (ObjLoader != undefined) and (ObjLoader.isdisplayed ) do	(destroyDialog ObjLoader)
       
       rollout  ObjLoader "Object Loader"  
       (	
       	
       	local align = dotNetClass "System.Windows.Forms.HorizontalAlignment"
       	local ColItems = dotNetClass "ListViewItem"
       	local TheColumn = #(#("Objects", 76, align.Center),#("X coord", 60, align.Center),#("Y coord", 60, align.Center),#("Z coord", 60, align.Center))
       	local ObjArr = #()
       
       	label LBLoadF "Load CSV file :" pos:[5,10]
       	button btn_Load "Load file" pos:[75,5] width:80 height:25
       	edittext ObjName "Name: " pos:[5,35] width:150
       	label LB_x "X :" pos:[23,60] 	spinner SpnrX "" pos:[42,60] range:[-9999,9999,0] type:#integer width:48 
       	label LB_y "Y : " pos:[23,80] 	spinner SpnrY "" pos:[42,80] range:[-9999,9999,0] type:#integer width:48 
       	label LB_z "Z : " pos:[23,100] 	spinner SpnrZ "" pos:[42,100] range:[-9999,9999,0] type:#integer width:48 
       	button btn_add "Add to List" pos:[95,60] width:60 height:25
       	button btn_clr "Clear List" pos:[95,90] width:60 height:25
       	button btn_mrg "Merge max file" pos:[40,140] width:100 height:30
       	dotNetControl lv "system.windows.forms.listView" pos:[160,15] width:260 height:157 
       		
       /*--------------Functions Start-------------*/
       		
       	fn init_fn lv  =
       	(
       		lv.view=(dotNetClass "system.windows.forms.view").details
       		lv.FullRowSelect = true		
       		lv.GridLines = true			
       		lv.IsAccessible = false
       		lv.LabelEdit = true
       		lv.checkboxes = false
       	)	
       	
       	fn AddCol_fn MyLv Array =
       	(
       		for col in Array do
       		(
       			MyLv.columns.add col[1] col[2] col[3] 
       		)
       	)		
       	
       	fn Populate_fn MyLv txt_Input ObjCoordX ObjCoordY ObjCoordZ =
       	(
       		TheName = txt_Input
       		X =  ObjCoordX 
       		Y = ObjCoordY
       		Z = ObjCoordZ
       		dt = #(TheName)
       		local item = for i in dt collect
       		(
       			dotNetObject ColItems #(i ,X, Y, Z)
       		)
       		MyLv.items.AddRange item	
       		MyLV.Update()
       	)			
       	
       	[color=Orange]fn LoadCsv_fn lv =
       	(
       		theRange = #()
       		for h = 1 to lineVal.count do
       		(
       			li = dotNetObject "System.Windows.Forms.ListViewItem"
       			ThisVal = lineVal[h] as string
       			ThatVal = filterString ThisVal ","
       			for n in 1 to TheColumn.count do
       			(
       				sub_li = li.SubItems.add (ThatVal[n] as string)
       			)
       			append theRange li
       		)
       		lv.Items.AddRange theRange	
       	)
       [/color]
       /*--------------Operations--------------*/	
       	on ObjLoader open do
       		(
       			init_fn lv 
       			AddCol_fn lv TheColumn
       		)
       		
       	on btn_Load pressed do
       		(
       			theOpenFile = getOpenFileName \
       			caption:"CSV File" \
       			filename:(getDir #Import + @"\") \
       			types:"Comma Seperated Value(*.csv)|*.csv" \
 			if theOpenFile != undefined then
       			(
       				theFile = openFile theOpenFile
       				lineCount = ((dotnetClass "System.IO.File").ReadAllLines theOpenFile).count
       				lineVal = (dotnetClass "System.IO.File").ReadAllLines theOpenFile
       				close theFile
       				LoadCsv_fn lv
       			)
       	)		
       
       	on btn_add pressed do
       		(
       			ObjtName = ObjName.text
       			Xval = SpnrX.value as string
       			Yval = SpnrY.value as string
       			Zval = SpnrZ.value as string
       				
       				(
       					Populate_fn lv	ObjtName Xval Yval Zval
       				)
       		)	
       		
       	on btn_clr pressed do 
       		(
       			SpnrX.value = 0
       			SpnrY.value = 0
       			SpnrZ.value = 0
       			ObjName.text = ""  
       			lv.items.clear() 		
       		) 
      	on btn_mrg pressed do 
      	(
      		
      	)   
       )	
       createDialog ObjLoader 430 180 style:#(#style_titlebar, #style_sysmenu,#style_minimizebox) 

Any ideas?
Appreciate your help and thank you !

2 Replies
1 Reply
(@denist)
Joined: 10 months ago

Posts: 0

try this filter [b]filterString ThisVal “,

[/b]edited. it was another problem
this will fix you code


  		   local lineVal
  		   fn LoadCsv_fn lv =
  		   (
  			   theRange = #()
  			   for h = 1 to lineVal.count do
  			   (
  				   ThisVal = lineVal[h] as string
  				   ThatVal = filterString ThisVal ","
  				   li = dotNetObject "System.Windows.Forms.ListViewItem" ThatVal[1]
  				   for n=2 to lv.columns.count do
  				   (
  					   sub_li = li.SubItems.add (ThatVal[n] as string)
  				   )
  				   append theRange li
  			   )
  			   lv.Items.AddRange theRange	
  		   )
  

fist item’s subitem automatically adds with the item creation.

Thank you Denis, much appreciated