INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Parsing a dxf file to extract coordinate data from obscure entities

Parsing a dxf file to extract coordinate data from obscure entities

Parsing a dxf file to extract coordinate data from obscure entities

(OP)
Greetings,

I work with topographical cad files which normally contain well behaved polyline entities (2d, 2d with elevation and 3d linework). Most of the tools I have deal with these various entities. However, I often run across older cad files that contain polymesh entities. These rascals have been almost impossible to deal with. Upto this point, I have been redigitizing the linework using a 2d with elevation line. Very time consuming and not exact.

After messing around with these entities trying different approaches, I found that if I expost these entities to a DXF file, all the vertex data is found in a well behaved manner.

What I plan on doing is to export the offending entities to a DXF (nothing else included) then parse the file and creating a data line containing the data set for each vertex then placing each data line in a text file. I will ignore anything else in the dxf file.

In the dxf file, the data of interest consists of 22 lines and a sample is as follows
VERTEX
5
75
330
74
100
AcDbEntity
8
MINOR
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
1262334.232859563
20
687059.6123241198
30
0.0
70
192
0

I want this to be paced in a text file like so

VERTEX,5,75,330,74,100,AcDbEntity,8,MINOR,100,AcDbVertex,100,AcDbPolyFaceMeshVertex,10,1262334.23285956,20,687059.612324119,30,0,70,192,0

From here, I can manipulate this as I like either using a spreadsheet or other tools available.

I have been playing with code to do this and have been wildly unsuccessfull.

CODE --> TCL

set fi [open data_files\\strings_dtms\\topos\\1998\\polymesh.dxf "r"]
set fo  [open tempwrite.str "w"]

set a "VERTEX"
while {[gets $fi line] >= 0} {if {$a == $line} { {set var 0} {for {set i 1} {$i<=22} {incr $i} {{append $var "," $fi}{gets $fi line}}}{puts $fo $var}} else {gets $fi 

line}}


close $fi
close $fo 

Please be kind. My programming skills are rudimentary although I aspire to better....

Any thought, comments and or corrections are appreciated.

Brian

RE: Parsing a dxf file to extract coordinate data from obscure entities

Something like this

CODE

set fi [open "polymesh.dxf" "r"]
set fo [open "tempwrite.str" "w"]

set k 0
set out_line {}
while {! [eof $fi]} {
  # read line from file
  set line [gets $fi]
  # remove leading and trailing spaces
  set line [string trim $line]
  if {$line != {}} {
    incr k
    #puts "'$line'"
    if {$k == 1} {
      set out_line "$line"
    } else {
      set out_line "$out_line,$line"
    }
  }
}

#puts "$out_line"

# write to output file
puts $fo "$out_line"

close $fi
close $fo 

RE: Parsing a dxf file to extract coordinate data from obscure entities

(OP)
Thanks for the suggestion mikrom,

I plugged your code in and appear to be getting an endless loop (ran 5 minutes) After killing the run, the temp file had nothing in it.

Not sure what it occuring.

Brian

RE: Parsing a dxf file to extract coordinate data from obscure entities

I tried the script and it worked fine. I used this input file:
polymesh.dxf

CODE

VERTEX
5
75
330
74
100
AcDbEntity
8
MINOR
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
1262334.232859563
20
687059.6123241198
30
0.0
70
192
0 
and it created this output file:
tempwrite.str

CODE

VERTEX,5,75,330,74,100,AcDbEntity,8,MINOR,100,AcDbVertex,100,AcDbPolyFaceMeshVertex,10,1262334.232859563,20,687059.6123241198,30,0.0,70,192,0 

RE: Parsing a dxf file to extract coordinate data from obscure entities

(OP)
Ah,

I think I know the disconnect. In the file I am parsing, it contains sometimes several thousand lines. Each group of lines (as few as 4 and as many at 30+) are identified by a entity type. As mentioned, I am only interested in the entity type "VERTEX" which when found contains 21 additional lines of detail. The snippet I put in that you used for the data file contains one instance of "VERTEX". I need to ignore all occurances of other entities and only record the details when "VERTEX" is found. When I run the your code, it it attempting to place the entire file on a single line and is probably the reason for the hanging of the process.

The output you generate is exactly what I want for each instance of "VERTEX". How do i search through the file for VERTEX, then execute this code on the following 21 lines, saving the stransposed dataset to the tempwrite file then continue searching the file for the next VERTEX instance?

Thanks for your help so far.

Brian

RE: Parsing a dxf file to extract coordinate data from obscure entities

(OP)
Finally getting back to this and tried another stab using Mikrom's code.


CODE --> TCL

set fi [open data_files\\strings_dtms\\topos\\1998\\polymesh.dxf "r"]
set fo  [open tempwrite.str "w"]

set a "VERTEX"
#target entity to search for
set out_line {}

while {! [eof $fi]} {
  # read line from file
  set line [gets $fi]
  # remove leading and trailing spaces
  set line [string trim $line]

  if { $line = $a } {
      set k 0
      set out_line "$line"
      while { k < 22 } {
         set out_line "$out_line,[string trim [gets $fi]]"
         incr k
            }
      puts $line $fo
   }
} 

In the if statement, testing to see if the line = the target entity. TRUE - Process subsequent 21 lines into out_line FALSE - read next line and retest

here is the error message

Quote (TCL Error Message)


Warning: Tcl generated error at line 20:
syntax error in expression " line = $a "
while executing
"if { $line = $a } {
set k 0
set out_line "$line"
while { k < 22 } {
set out_line "$out_line,[string trim [gets $fi]]"
..."
("while" body line 6)
invoked from within
"while {! [eof $fi]} {
# read line from file
set line [gets $fi]
# remove leading and trailing spaces
set line [string trim $line]
if { line ..."
(file "macro_files\utility\polymesh_dxf_vertex_extract - copy.tcl" line 20)
invoked from within
"default_source "macro_files\\utility\\polymesh_dxf_vertex_extract - copy.tcl""

I somehow malformed the condition test. I also tried { line = a}, {= [ line a]}, {$line = a } and a few other variations before posting.


Thoughts, suggestions, dopeslaps?

Brian

RE: Parsing a dxf file to extract coordinate data from obscure entities

Now I hope I understand better what you need ...

Ginen is an input file
polymesh.dxf

CODE

VERTEX
5
75
330
74
100
AcDbEntity
8
MINOR
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
1262334.232859563
20
687059.6123241198
30
0.0
70
192
0
something else
skip
skip
VERTEX
6
75
330
74
100
AcDbEntity
8
MINOR
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
1262334.232859563
20
687059.6123241198
30
0.0
70
192
1

other data
other data


VERTEX
7
75
330
74
100
AcDbEntity
8
MINOR
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
1262334.232859563
20
687059.6123241198
30
0.0
70
192
2 

Now this script
Bminer.tcl

CODE

set fi [open "polymesh.dxf" "r"]
set fo [open "tempwrite.str" "w"]

set vertex_found 0
set out_line {}
while {! [eof $fi]} {
  # read line from file
  set line [gets $fi]
  # remove leading and trailing spaces
  set line [string trim $line]
  if {$line == "VERTEX"} {
    set vertex_found 1
    set k 0
    #puts "'$line'"
    set out_line "$line"
    # next iteartion
    continue
  }
  if {$vertex_found} {
    incr k
    if {$k <= 21} {
      #puts "'$line'"
      set out_line "$out_line,$line"
    } else {
      # write to output file
      #puts $out_line
      puts $fo "$out_line"
      set vertex_found 0
    }
  }  
}

close $fi
close $fo 

produces this output file
tempwrite.str

CODE

VERTEX,5,75,330,74,100,AcDbEntity,8,MINOR,100,AcDbVertex,100,AcDbPolyFaceMeshVertex,10,1262334.232859563,20,687059.6123241198,30,0.0,70,192,0
VERTEX,6,75,330,74,100,AcDbEntity,8,MINOR,100,AcDbVertex,100,AcDbPolyFaceMeshVertex,10,1262334.232859563,20,687059.6123241198,30,0.0,70,192,1
VERTEX,7,75,330,74,100,AcDbEntity,8,MINOR,100,AcDbVertex,100,AcDbPolyFaceMeshVertex,10,1262334.232859563,20,687059.6123241198,30,0.0,70,192,2 

RE: Parsing a dxf file to extract coordinate data from obscure entities

(OP)
Mikrom,

That is the ticket! Thanks for your help. It is doing exactly what I need. No I can get on with breaking up these pesky entities and eliminate all the extra work I was doing.

Brian

RE: Parsing a dxf file to extract coordinate data from obscure entities

I wish you lots of success

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close