0

I'm writing a path-following application for my robot for 2D navigation and the path is contained in a DWG file. Let's say, for example, that the DWG file contains a 2x2 meters square.

I would like to open the DWG file, find out the lines and get some coordinates to use as waypoints for my robot.

At the moment, it would be perfect for me to find out a way to just read the coordinates from the DWG file. I saw that there are some open-source libraries that allow to handle DWG files, like libredwg or libDWG, but i can't understand if they can be used for my purpose.

Can you help me, please?

EDIT: I found that libopencad can be used for my application, I tested the sample application and it outputs something like this:

Layers count: 1
1. Layer 0 contains 8 geometries
|---------Line---------|
Start Position:     62.5852 -36.5942    0
End Position:   -62.5852    -36.5942    0

Entity color: #ffffffff
|---------Line---------|
Start Position:     62.5852 -36.5942    0
End Position:   62.5852 36.5942 0

Entity color: #ffffffff
|---------Line---------|
Start Position:     62.5852 36.5942 0
End Position:   -62.5852    36.5942 0

Entity color: #ffffffff
|---------Line---------|
Start Position:     -62.5852    -36.5942    0
End Position:   -62.5852    36.5942 0

Entity color: #ffffffff
|---------Line---------|
Start Position:     62.5852 -76.4212    0
End Position:   -62.5852    -76.4212    0

Entity color: #ffffffff
|---------Line---------|
Start Position:     62.5852 -76.4212    0
End Position:   62.5852 -61.4214    0

Entity color: #ffffffff
|---------Line---------|
Start Position:     62.5852 -61.4214    0
End Position:   -62.5852    -61.4214    0

Entity color: #ffffffff
|---------Line---------|
Start Position:     -62.5852    -76.4212    0
End Position:   -62.5852    -61.4214    0

Entity color: #ffffffff

for a sample DWG file that contains two squares:

enter image description here

The problem is: how can I separate the coordinates of the first rectangle from the second one?

Marcus Barnet
  • 2,083
  • 6
  • 28
  • 36

1 Answers1

1

In the data extracted from the DWG, you don't really have 2 rectangles. You have 8 lines, with other grouping provided. Based on this one sample you might assume that that the first 4 lines would represent one rectangle, and the next 4 lines would represent the second. That's true in this case, but you can't depend on it. It would all depend on who created the DWG, what tools they used, and whether they drew the rectangles in sequence or if they alternated drawing, deleting, drawing, moving lines.

After importing the lines, you can group them yourself by doing some lookup. Start with the first line. Any other line that has either endpoint that matches either endpoint of the first line is part of the same shape. Repeat the process using the endpoints of the first next line, looking for another line that has an endpoint in common with it.

This algorithm certainly looks like a place to use recursion, a standard container like vector or map, maybe both. Also, the coordinates are floating point so you have to be careful with comparison operators and decide how close is close enough to be the same point.

Finally, you need to define the requirements of acceptable / unacceptable input files and decide what to do with data that doesn't match. A good general algorithm could find triangles, rectangles, or any connected line shapes. There's another layer of complexity around determining whether the connected lines form a simple closed polygon, if lines intersect, etc.

It's much easier if you can be sure that the input files always only contain lines that form rectangles. Unless you can be sure of that, your program needs to at a minimum detect when it can't find rectangles and fail safely.

Mark Taylor
  • 1,843
  • 14
  • 17