[GRASS5] Script for exporting 3D vectors

Crudeli crudeli at mail.nauta.it
Mon Mar 25 15:18:00 EST 2002


Dear Grass Developers,

since I needed to export a 3D dxf from the contour lines generated by 
r.contours and it seems that the dxf  format created by v.out.dxf 
isn't readable as 3Dpoly by many CAD (I tried Autocad, Intellicad and 
VectorWorks), I wrote this shell script to generate a compatible 3D 
dxf file.
its use is pretty straightforward:

v.out.isodxf VectFileName

You can read through it to hava an idea of how it works. I tried to 
comment it extensively.
This version isn't still as clean as it could be.

Of course the name "v.out.isodxf" is what I use on my box, and it 
isn't intended as a mainstream command name. Feel free to modify the 
code and to email your suggestions to me.

By the way, thanks to all serious coders for their efforts.

Raffaele Douglas candidi Tommasi Crudeli


_______________________________________________________________

#/!bin/bash

# v.out.isodxf v 0.4
# 18/03/2002

# This script writes a 3D DXF file of the contours
# obtained from r.contours to the $OUTDIR directory
# (in this version = current directory).
# wrote this because v.out.dxf seems to write only 2D polylines
# with a text object, placed somewhere on the same poly,
# to denote the z-value.
#
# This script assumes you obtained the vector file of the
# contours by r.contours and that you used v.support and v.out.ascii
# on the vector file.
# Furthermore the idea came observing that grass writes the ascii
# contour starting from the lowest one and going up in a strictly 
growing manner
# _and_
# that every lines in $LOCATION/dig_att/filename
# has its correspondent in
# $LOCATION/dig_ascii/filename
# i.e. in the same order
#
# As far as I can tell, this assumption is true
# but could be verifyed by
# seeing if the xy of the dig_att file
# falls between two following points of the corresponding
# dig_ascii file.
# Of course if you can dig in the v.out.dxf source code
# it would be a better confirmation.
#
# You can freely modify this code.
#
# RafDouglas Candidi Tommasi Crudeli
# ct at ehleng.com
# 18/03/2002
#
#

# The script itself works, but isn't clean:
# one still has to change the script to have it work
# with his files. (fixed)
# Next version will be usable simply from the grass shell (fixed)
# And won't need temp files

# All the resulting polies in the DXF file
# will be placed on a layer named "10"

# Initial control to verify if the user provided the name of the file 
to be converted

if [ -z "$1" ]; then
	echo
	echo "Usage: $0 BinaryVectorFile"
	echo
	exit
fi


# LAYERNAME="10"
f=$1
VECTDIR=$LOCATION"/dig/"
ASCIIDIR=$LOCATION"/dig_ascii/"
ATTDIR=$LOCATION"/dig_att/"
# OUTDIR=$LOCATION"/outputs/isotxt/"
OUTDIR="./"
SUFFIX=".iso.a"


if [ ! -f $VECTDIR$1 ]; then
	echo
	echo "Specified Vector File doesn't exist."
	echo "Please use g.list vect"
	echo
	exit
fi

# This is for loopings only
# for f in $LOCATION/dig_ascii/*$FILENAME; do
# h=$(echo $f|cut  -d'/' -f 8)
# g=$(echo $h|sed 's/\.iso\.a//g')
# name of the ascii file

k=$f".a"

# deletes the first 14 lines (header) from the dig_ascii file
# squeezes multiple spaces,
# converts the survived spaces to the letter R (just a placeholder),
# converts the linefeeds to the letter Y (just another placeholder),
# converts the leading L of every poly in the file to a linefeed
# cleans away the first line, which is blank
# numbers the lines with grep and
# converts the colons to spaces.
# All this is stored in the file named "temp"

echo
echo "Asciifying $k..."

v.out.ascii input=$f output=$k

cat $ASCIIDIR$k|tail +15|tr -s " "|tr " " "R"|tr "\n" "Y"|tr "L" 
"\n"|tail +2|grep -n ""|tr ":" " ">temp

# At this point we have a file (temp) with a _single_, possibly very 
long, line for every
# contour poly.
# Now we number also the lines in the dig_att file:

grep -n "" $ATTDIR$k|tr ":" " ">temp2

# and we join the two files
# into the file "joined"
# in which we can find the z-value of the poly and the (x,y) of all 
its vertexes

echo "Joining $k..."
join -o 1.5 2.1 2.2  temp2 temp>joined

#
# we develope the lines into something usable:
# Y's become linefeed again
# and the leading R's, which previously indicated the beginning of a 
vertex line
# are converted to "A A " (later why)
# All the remaining R's once separeted the x from the y
# so we convert them to spaces.
#

cat joined|tr "Y" "\n"|sed 's/^[R]/A A /g'>temp3
cat temp3|tr "R" " "|sed '/^$/d'>joined

#
# And evetually the tricky part
# We produce another temporary file, containing
# the x,y,z of all the vertexes
# at the beginning of every poly we write
# "stop" and some more numbers,
# just in case you need them for exporting into some other format
#

echo "Awking $k..."
cat joined |awk '{
if ($1 ~ /A/) {
printf("%f %f %s \n",$4,$3,z);
}else{
z=$1
printf("stop %s %s %s\n",$2,$3,$1);
}
}'>tempout

#
# Now we want to write the actual DXF file
# I first write the body (ENTITIES SECTION)
# and then the boring headers...
#
#
# the first lines (i.e. the if($1=="stop") block) are
# written for every new poly and terminate the previous one
# except (i.e. the if (NR>1) block) the all-first line
# which doesn't need to terminate any polygon
# The body is terminated with a
#
#
#

echo "Embodying $k..."

cat tempout |awk '{
if ($1=="stop") {
     if (NR>1)
	{
	printf("  0\n")
	printf("SEQEND\n")
	printf("  8\n")
	printf("10\n")
	}else {}
       printf("  0\n")
       printf("POLYLINE\n")
       printf("  8\n")
       printf("10\n")
       printf("  6\n")
       printf("CONTINUOUS\n")
#      printf(" 62\n")
#      printf("        5\n")
       printf(" 66\n")
       printf("        1\n")
       printf(" 70\n")
       printf("        8\n")
#      printf("  0\n")
#      printf("VERTEX\n")
#      printf("  8\n")
#      printf("10\n")
#      printf("  6\n")
#      printf("CONTINUOUS\n")
#      printf(" 62\n")
#      printf("        5\n")
#      printf("  10\n")
#      printf("0.00000\n")
#      printf("  20\n")
#      printf("0.00000\n")
#      printf("  30\n")
#      printf("207.132456\n")
#      printf("  70\n")
#      printf("      32\n")
}else{printf("  0\n")
       printf("VERTEX\n")
       printf("  8\n")
       printf("10\n")
       printf("  6\n")
       printf("CONTINUOUS\n")
#      printf(" 62\n")
#      printf("          5\n")
       printf(" 10\n")
       printf("%6.5f\n", $1)
       printf(" 20\n")
       printf("%6.5f\n", $2)
       printf(" 30\n")
       printf("%6.5f\n", $3)
       printf(" 70\n")
       printf("         32\n")
}
}'>body

#
# We now terminate the body
#

echo "  0
SEQEND
   8
10
   6
CONTINUOUS
  62
                5
   0
ENDSEC
   0
EOF" >>body


#
# and create the header
#

# Header - Part 1
# we grep the max extents of the DTM
# (I assume that the contours don't exceed them)
# The dtm is needed to provide the z-extent,
# which v.info doesn't print out
#r.info map=$g".z"|grep Res|sed 's/ //g'|sed 's/Res:0.5//g'|tr ":" 
"\t"|tr -d NEWS"|"|tr -d "\n"|awk '{


#
# I tried also the following, without z EXTMIN/EXTMAX
# which seems to be OK for Acad2000, Intellicad2000 and VectorWorks9
# and doesn't require the original DTM raster
#

v.info input=$f|grep "N:\|E:"|sed 's/ //g'|tr ":" "\t"|tr -d 
NEWS"|"|tr -d "\n"|awk '{
       printf("  0\n")
       printf("SECTION\n")
       printf("  2\n")
       printf("HEADER\n")
       printf("  9\n")
       printf("\$ACADVER\n")
       printf("  1\n")
       printf("\AC1004\n")
       printf("  9\n")
       printf("\$INSBASE\n")
       printf("  10\n")
       printf(" 0.0\n")
       printf("  20\n")
       printf(" 0.0\n")
       printf("  9\n")
       printf("\$EXTMIN\n")
       printf("  10\n")
       printf("%6.5f\n", $2)
       printf("  20\n")
       printf("%6.5f\n", $4)
       printf("  9\n")
       printf("\$EXTMAX\n")
       printf("  10\n")
       printf("%6.5f\n", $1)
       printf("  20\n")
       printf("%6.5f\n", $3)
}'>header

# Header - Part 2
#
# This is the boring part I promised to you :)
# there are some pseudo-constatns; most part probably unneeded...
#

echo "  9
\$LIMMIN
  10
0.0
  20
0.0
   9
\$LIMMAX
  10
0.0
  20
0.0
   9
\$ORTHOMODE
  70
                0
   9
\$REGENMODE
  70
                1
   9
\$FILLMODE
  70
                1
   9
\$QTEXTMODE
  70
                0
   9
\$DRAGMODE
  70
                2
   9
\$LTSCALE
  40
25.3999999999999986
   9
\$OSMODE
  70
                0
   9
\$ATTMODE
  70
                1
   9
\$TEXTSIZE
  40
0.2
   9
\$TRACEWID
  40
1.0
   9
\$TEXTSTYLE
   7
STANDARD
   9
\$CLAYER
   8
0
   9
\$CELTYPE
   6
CONTINUOUS
   9
\$CECOLOR
  62
              256
   9
\$DIMSCALE
  40
1.0
   9
\$DIMASZ
  40
0.18
   9
\$DIMEXO
  40
0.0625
   9
\$DIMDLI
  40
0.38
   9
\$DIMRND
  40
0.0
   9
\$DIMDLE
  40
0.0
   9
\$DIMEXE
  40
0.18
   9
\$DIMTP
  40
0.0
   9
\$DIMTM
  40
0.0
   9
\$DIMTXT
  40
0.18
   9
\$DIMCEN
  40
0.09
   9
\$DIMTSZ
  40
0.0
   9
\$DIMTOL
  70
                0
   9
\$DIMLIM
  70
                0
   9
\$DIMTIH
  70
                1
   9
\$DIMTOH
  70
                1
   9
\$DIMSE1
  70
                0
   9
\$DIMSE2
  70
                0
   9
\$DIMTAD
  70
                0
   9
\$DIMZIN
  70
                0
   9
\$DIMBLK
   1

   9
\$DIMASO
  70
                1
   9
\$DIMSHO
  70
                1
   9
\$DIMPOST
   1

   9
\$DIMAPOST
   1

   9
\$DIMALT
  70
                0
   9
\$DIMALTD
  70
                2
   9
\$DIMALTF
  40
25.3999999999999986
   9
\$DIMLFAC
  40
1.0
   9
\$LUNITS
  70
                2
   9
\$LUPREC
  70
                4
   9
\$AXISMODE
  70
                0
   9
\$AXISUNIT
  10
0.0
  20
0.0
   9
\$SKETCHINC
  40
0.1
   9
\$FILLETRAD
  40
0.0
   9
\$AUNITS
  70
                0
   9
\$AUPREC
  70
                0
   9
\$MENU
   1
acad
   9
\$ELEVATION
  40
0.0
   9
\$THICKNESS
  40
0.0
   9
\$VIEWDIR
  10
0.0
  20
0.0
  30
1.0
   9
\$LIMCHECK
  70
                0
   9
\$BLIPMODE
  70
                0
   9
\$CHAMFERA
  40
0.0
   9
\$CHAMFERB
  40
0.0
   9
\$FASTZOOM
  70
                0
   9
\$SKPOLY
  70
                0
   9
\$TDCREATE
  40
2452348.384930556
   9
\$TDUPDATE
  40
2452348.384930556
   9
\$TDINDWG
  40
0.0000000000
   9
\$TDUSRTIMER
  40
0.0000000000
   9
\$USRTIMER
  70
                1
   9
\$ANGBASE
  50
0.0
   9
\$ANGDIR
  70
                0
   9
\$PDMODE
  70
                1
   9
\$PDSIZE
  40
-2.0
   9
\$PLINEWID
  40
0.0
   9
\$COORDS
  70
                0
   9
\$SPLFRAME
  70
                0
   9
\$SPLINESEGS
  70
                8
   9
\$ATTDIA
  70
                0
   9
\$ATTREQ
  70
                1
   9
\$USERI1
  70
                0
   9
\$USERI2
  70
                0
   9
\$USERI3
  70
                0
   9
\$USERI4
  70
                0
   9
\$USERI5
  70
                0
   9
\$USERR1
  40
0.0
   9
\$USERR2
  40
0.0
   9
\$USERR3
  40
0.0
   9
\$USERR4
  40
0.0
   9
\$USERR5
  40
0.0
   0
ENDSEC
   0
SECTION
   2
TABLES
   0
TABLE
   2
LTYPE
  70
               11
   0
LTYPE
   2
CONTINUOUS
  70
               64
   3
Solid line
  72
               65
  73
                0
  40
0.0
   0
LTYPE
   2
STYLE_01
  70
                0
   3

  72
               65
  73
                2
  40
0.41664
  49
0.20828
  49
-0.20836
   0
LTYPE
   2
STYLE_02
  70
                0
   3

  72
               65
  73
                2
  40
0.6944250000000001
  49
0.48607
  49
-0.2083550000000001
   0
LTYPE
   2
STYLE_03
  70
                0
   3

  72
               65
  73
                2
  40
1.25
  49
1.0416400000000001
  49
-0.2083599999999999
   0
LTYPE
   2
STYLE_04
  70
                0
   3

  72
               65
  73
                2
  40
1.5277099999999999
  49
1.3194249999999998
  49
-0.208285
   0
LTYPE
   2
STYLE_05
  70
                0
   3

  72
               65
  73
                2
  40
0.20828
  49
0.069425
  49
-0.138855
   0
LTYPE
   2
STYLE_06
  70
                0
   3

  72
               65
  73
                4
  40
1.0416400000000001
  49
0.625
  49
-0.20828
  49
0.06943
  49
-0.13893
   0
LTYPE
   2
STYLE_07
  70
                0
   3

  72
               65
  73
                6
  40
1.875
  49
0.625
  49
-0.20828
  49
0.6250000000000001
  49
-0.2083599999999999
  49
0.06943
  49
-0.1389299999999999
   0
LTYPE
   2
STYLE_08
  70
                0
   3

  72
               65
  73
                6
  40
1.25
  49
0.625
  49
-0.20828
  49
0.06943
  49
-0.13893
  49
0.0694299999999999
  49
-0.13893
   0
LTYPE
   2
STYLE_09
  70
                0
   3

  72
               65
  73
                4
  40
5.0
  49
3.75
  49
-0.2777849999999998
  49
0.6944300000000003
  49
-0.2777849999999998
   0
LTYPE
   2
STYLE_10
  70
                0
   3

  72
               65
  73
                6
  40
5.9375
  49
3.75
  49
-0.2777849999999998
  49
0.6944300000000003
  49
-0.2777849999999998
  49
0.625
  49
-0.3125
   0
ENDTAB
   0
TABLE
   2
LAYER
  70
                2
   0
LAYER
   2
0
  70
                0
  62
                7
   6
CONTINUOUS
   0
LAYER
   2
10
  70
                0
  62
                7
   6
CONTINUOUS
   0
ENDTAB
   0
TABLE
   2
STYLE
  70
                1
   0
STYLE
   2
STANDARD
  70
                0
  40
0.0
  41
1.0
  50
0.0
  71
                0
  42
0.0
   3
txt
   4

   0
ENDTAB
   0
TABLE
   2
VIEW
  70
                0
   0
ENDTAB
   0
ENDSEC
   0
SECTION
   2
BLOCKS
   0
ENDSEC
   0
SECTION
   2
ENTITIES">>header

echo "DXFing $f..."
cat header body > $OUTDIR$f".dxf"

echo "Gzipping $f..."
gzip -9 $OUTDIR$f".dxf"

#
# we remove all the tempfileswe created
#

rm -f joined
rm -f temp
rm -f temp2
rm -f temp3
rm -f tempout
rm -f header
rm -f body


#done

echo "Exporting complete. Goodbye"






-------------- next part --------------
A non-text attachment was scrubbed...
Name: v.out.isodxf
Type: application/mac-binhex40
Size: 16758 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-dev/attachments/20020325/a4ffdc20/v.out.hqx


More information about the grass-dev mailing list