How to get original coordinates from imported dxf file
-
Hi,
newbie for ruby...
I have imported Dxf(dwg) file in Sketchup Pro 6 and wanted use Ruby script to find out the corners original coordinates (not local model coordinates) of each face in the model, is that possible? Is there any example to do the similar?
thanks!
vinc. -
Hi Vinc,
Is the original dxf georeferenced? If so, the import should already put your model where it is in the dxf file. What I mean is;
- I have a dxf wireframe plan of my city that was exported from Arc GIS
- it is in orthogonal projection and georeferenced to the origin of the sytem that is standard in my country
- when I open it, it appears the exact hundreds (!) of kilometres away from the SU origin my city is away from the geological origin.
I think if this data is not coded in the dxf file, there's no way you can get the coordinates (either with ruby or else) because it is simply not there i.e. there is nothing to get.
You could, however, get at least one coordinate point in real life, you could reference it in your model and either use a grid (relative coordinate system) or now, some ruby gurus; could there be a script that would change the default coordinate numbering (which appears with the query tool or the text tool when clicked on an endpoint)?
-
Thank you Gaieus for the quick answer!
Yes the dxf file is georeferenced, we have here one vertex as example:
VERTEX
5
AB
330
A1
100
AcDbEntity
8
36456188
100
AcDbVertex
100
AcDb3dPolylineVertex
10
-68005.41899999999
20
214842.715
30
373.85
70
32
0
where I the two original coordinates should be -68005 214842 373. And I use following code I wrote:#---------------------------------------
list all faces vertices coordinates of POSITION (local model coordinates)
vincent 2008.09.09
model=Sketchup.active_model # point to the active model
entities = model.entities # get the selected entitiesputs entities.length # see how many entities in model
i=0
Ausgabe = File.new("C:/list.txt","w")entities.each {|entity|
if (entity.kind_of? Sketchup::Face) i=i+1 vertices = entity.vertices length = vertices.length puts length for j in 0...length vertex = vertices[j] point = vertex.position xc = point.to_a strx = xc.x.to_s stry = xc.y.to_s strz = xc.z.to_s strmess = sprintf("x: %s y: %s z: %s \n", strx, stry, strz) puts strmess Ausgabe.syswrite(strmess) end end
}
puts "Alltogether "+i.to_s+" faces"
#-------------------------------------------------------------------
and see lots of coordinates in the produced file like these:
x: 1674.70131904589 y: 5984.79975691969 z: 968.425196850396
x: 1737.61470487252 y: 5947.2407018018 z: 922.36220472441
x: 1840.37061038431 y: 5978.57928447771 z: 924.330708661419
......any idea?
vinc.
-
@unknownuser said:
any idea?
Not me...
But thanks for elaborating I'm pretty sure some of those more learned scripters will chime in soon
-
have imported the dxf file and exported it again in dxf, the coordinates of exported file are in local model system. Difficult imagine Sketchup lost the original coordinates!
Or have done something wrong?vinc.
-
Hi,
@unknownuser said:
Difficult imagine Sketchup lost the original coordinates!
No, it simply converts them in inches, the output coordinates are shown in inches in the code.
The code 30 in a DXF represents the Z coordinate of a point. In the example, you have 373.85 (in meters I suppose).
373.85 * 2.54 = 949.579,that's in the range you've got in strzstrx = xc.x.to_m.to_s stry = xc.y.to_m.to_s strz = xc.z.to_m.to_s
to get coordinates in meters, or
strx = Sketchup.format_length(xc.x) stry = Sketchup.format_length(xc.y) strz = Sketchup.format_length(xc.z)
to get strings formatted in the current SU unit.
Let's compact the code a bit:
def myfunction model=Sketchup.active_model # point to the active model entities = model.entities # get the selected entities puts entities.length.to_s # see how many entities in model i=0 ausgabe = File.new("C;/list.txt","w") # Warning; no variable with Upcase, constants instead entities.each do |entity| next if not entity.kind_of? Sketchup;;Face # speed up and skip non-faces ! puts entity.vertices.length entity.vertices.each do |vertex| strx = Sketchup.format_length(vertex.position.x) stry = Sketchup.format_length(vertex.position.y) strz = Sketchup.format_length(vertex.position.z) strmes = "x; " << strx << " y; " << stry << " z; " << strz << "\n" puts strmess ausgabe.syswrite(strmess) end i+=1 # count only faces end puts "Alltogether "+i.to_s+" faces" end
Tschuss,
-
Thank you Didier! Your codes work perfect!
One small question:
why there are many symbol ~ in the results:
...
x: ~ -68102,27m y: 214820,00m z: 349,82m
x: -68101,73m y: ~ 214828,80m z: 349,82m
x: ~ -68102,68m y: ~ 214813,19m z: 349,82m
...vinc.
-
Hi,
Because the current precision display is set to say, 2 decimals and real coordinates are stored 12 decimals or more.
Increase the precision display in the Model Info Window, and you'll get that:x: -11,509615m y: -7,670234m z: 0,000000m
x: 6,380385m y: -7,670234m z: 0,000000m
x: 6,380385m y: -7,670234m z: 12,860000m
x: -11,509615m y: -7,670234m z: 12,860000mor use that:
strx = xc.x.to_m.to_s
stry = xc.y.to_m.to_s
strz = xc.z.to_m.to_sthis will prevent the output rounding values through the use of the format_length method.
Regards, -
Thank you very much!!
Advertisement