ScriptSpot

Forum topic · Scripts Wanted

Create a grid mesh from text file

By titane357 · 2021-01-07

Description

Hi,
In France IGN give for free some .asc files.
https://geoservices.ign.fr/documentation/diffusion/telechargement-donnee…
This is simple .asc files like :
ncols 3
nrows 5
xllcorner 849997.500000000000
yllcorner 6360002.500000000000
cellsize 5.000000000000
NODATA_value -99999.00
133.00 134.00 135.00
133.00 134.00 135.00
133.00 134.00 135.00
133.00 134.00 135.00
133.00 134.00 135.00
Values are separated by spaces.
In reality, ncols =1000 and nrows=1000
(xcorner, ycorner) is the position of the first point (origine).
So I would like to create a mesh grid :
read first line, first value, create a vertex ( or place first vertex of the grid ) at origine with z=first value
read first line, second value, create a vertex ( or place second vertex of the grid ) at origine + cell size with z=second value
(...)
Same for each line.
As there is a lot of data, I would like to be able to skip some values : ncol divided by a value. Same for nrows.
As 3dsmax don't like very big model, I would like to be able to "move" origine value.
I made an ugly UI to give an idea.
Thanks. I think it could help a few people.

Next step is a batch importer, as there can be a lot of files... ;-)

Attachments
Comments (14)

what about existing converters?

jahman · 2021-01-07

titane357 · 2021-01-07

In fact I saw this page already, but I didn't understand really what it is...
The file I want to use is more "easy" as it is a ortho 2d grid with a z value for each point.
My first problem is that I saw tutorials to read value of a text file line, but I don't know how to read one value at a time of each lines.
My second problem is that I'm not very comfortable with arrays :-)
My main problem is that I'm a ass...le in scripting as soon as there is maths...

.

jahman · 2021-01-07

you can do it the ugly way
1. read all lines
2. split each line by space character into three parts
3. convert each part into float and ensure that result is actually a float value
4. do whatever you need with the values


(

lines_array = (dotNetClass "System.IO.File").ReadAllLines "path_to_file"

for line_of_file in lines_array do
(
values = filterString line_of_file " "

if values.count == 3 do
(
values[1] = values[1] as float
values[2] = values[2] as float
values[3] = values[3] as float

if iskindof values[1] number and iskindof values[2] number and iskindof values[3] number do
(
...
)
)
)

)

titane357 · 2021-01-08

Thanks for answer jahman but dotnet... can't even open my file.

titane357 · 2021-01-08

I succeed to make my grid, move my grid vertex, but I can't enter my value in VTX:
polyop.moveVert PL VERT [(COL*CELLSIZE),(ROW*CELLSIZE),TX]
because it is a STRING !!!

It is ok for few rows and columns but with NCOLLO=NROWWO= 1000, 3dsmax eats all memory and freeze...


in_name = "E:/test_only3x3.txt"
in_file = openFile in_name
NCOLLO = 3
NROWWO = 3
NCOLL = (NCOLLO - 1)
NROWW = (NROWWO - 1)
COUNTER = NCOLLO * NROWWO
CELLSIZE = 5
PL = plane()
PL.length = NROWW * CELLSIZE
PL.width= NCOLL * CELLSIZE
PL.lengthsegs = NROWW
PL.widthsegs = NCOLL
ConvertTo PL Editable_Poly
NBVERT = polyop.getNumVerts PL

COL = 1
ROW = 1
VERT = 1
for R=1 to NROWWO do
(
	for C=1 to NCOLLO do
	(
	TX= readDelimitedString in_file " "
	polyop.moveVert PL VERT [(COL*CELLSIZE),(ROW*CELLSIZE),TX]
	format "COLONNE :%  LIGNE :% VERTEX :%\n" COL ROW VERT
	COL +=1
	VERT +=1
	)
	ROW +=1
	COL = 1
)

.

jahman · 2021-01-08

but I can't enter my value in VTX because it is a STRING !!!
convert it like in the example I posted above

titane357 · 2021-01-08

Hi,
This is my last try as I don't know how to optimize it more...
Virtually it works. But as it is too slow, I never see the 1000x1000 mesh.
:-(

Attachments

.

jahman · 2021-01-08


(
	f = openFile @"...\test_ori_0.txt"

	LF = #()
	l = readline f ; LF = filterString l " " ; NCOLLO = LF[2] as float
	l = readline f ; LF = filterString l " " ; NROWWO = LF[2] as float
	l = readline f ; LF = filterString l " " ; XCORNER = LF[2] as float
	l = readline f ; LF = filterString l " " ; YCORNER = LF[2] as float
	l = readline f ; LF = filterString l " " ; CELLSIZE = LF[2] as float
	l = readline f
	--format "COLONNE :%  LIGNE :% XCORNER :% YCORNER :% CELLSIZE :%\n" NCOLLO NROWWO XCORNER YCORNER CELLSIZE

	---- test values :
	NCOLLO =1000
	NROWWO =1000
	----------------------

	NCOLL = (NCOLLO - 1)
	NROWW = (NROWWO - 1)
		
	PL = plane()
	PL.length = NROWW * CELLSIZE
	PL.width= NCOLL * CELLSIZE
	PL.lengthsegs = NROWW
	PL.widthsegs = NCOLL
	convertToMesh PL

	COL = 1
	ROW = 1
	VERT = 1
	COUNT = 1	

	try ( destroydialog ::progressTest ) catch()
	global progressTest = rollout progressTest "IGN asc"
	(
		spinner sNCOLLO "COL:" range:[0,1000,NCOLLO] align:#right type:#integer fieldwidth:100
		spinner sNROWW "ROW:" range:[0,1000,NROWWO] align:#right type:#integer fieldwidth:100
		spinner sXCORNER "XC:" range:[-1000000,1000000,XCORNER] align:#right fieldwidth:100
		spinner sYCORNER "YC:" range:[-1000000,1000000,YCORNER] align:#right fieldwidth:100
		spinner sCELLSIZE "CELL:" range:[.01,100,CELLSIZE] align:#right fieldwidth:100
		progressbar doit_prog color:green 
		button doit "APPLY" 
		on doit pressed do
		with undo off 
		with redraw off
		(
			t1=timestamp();hf = heapfree
						
			for R=1 to NROWWO do
			(
				doit_prog.value = 100.*R/NROWWO -- this is slow and if you have 10000 rows progress will update 10000 times which is nonsence
				l = readline f -- reads a line of 1000 values
				LF = filterString l " " -- converts it to array of 1000 string values

				for C=1 to NCOLLO do
				(
					-- this is wrong
-- 					VAL = LF[COUNT]as float 
					
					VAL = LF[ C ]as float					
					setVert PL VERT [((COL-1)*CELLSIZE),((ROW-1)*CELLSIZE),VAL] 
					COL  += 1
					VERT += 1
				)
				ROW +=1
				COL = 1
				COUNT = 1
			)
			doit_prog.value = 0 -- when ready, reset the progress bar to 0%
			redrawViews()
			update PL
			format "Time: %sec. Mem: %\n" ((timestamp()-t1)/1000 as float) (hf-heapfree)
		)
		
		on progressTest close do 
		(
			close f
		)
	)--rollout
	createDialog progressTest 200 200
)--undo off

titane357 · 2021-01-09

WAW !!! Thank you soooo much Jahman !
Works at lightspeed, and yours is working ( mine was repeatiting same row of values )
I can open all files now, and now I'm going to try ( ah ah ) to make the script more usable for my work.

Attachments

titane357 · 2021-01-10

Thanks again ! Just to say, mesh was inverted in y axis, I corrected it !

Attachments

.

jahman · 2021-01-10

You made it. Congratulations! ;)

titane357 · 2021-01-08

I make a script. It don't work with the full IGN file as it is 1000x1000.
But if I overwrite the value to 200x200 it works and create a grid mesh with z values (but it don't make the good mesh as the values are shifted)
Here is the .ms and the original IGN file as text.
I'll continue to invertigate :-)

Attachments

titane357 · 2021-01-08

Hi,
I succeed with undo off to keep memory very low ! :-)
And I added a progress bar ( my first one !)
But the script is too slow, and beyond 500x500, it took forever...

Attachments