fa8b5a17ff325300417ffaf64d657b6fbc606850
Stefan Schuermans initial version, DXFs can b...

Stefan Schuermans authored 11 years ago

1) /* drawing (DXF) to G-code (NGC) converter
2)  * Copyright 2013 Stefan Schuermans <stefan@schuermans.info>
3)  * Copyleft: CC-BY-SA http://creativecommons.org/licenses/by-sa/3.0/
4)  */
5) 
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

6) #include <list>
Stefan Schuermans initial version, DXFs can b...

Stefan Schuermans authored 11 years ago

7) 
8) #include "gcode.h"
9) #include "layer.h"
10) #include "path.h"
11) #include "settings.h"
12) 
13) /**
14)  * @brief add a new empty path to the layer
15)  * @return reference to new empty path
16)  */
17) Path & Layer::addPath()
18) {
19)   mPaths.push_back(Path());
20)   return mPaths.back();
21) }
22) 
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

23) /**
24)  * @brief improve paths
25)  * @param[in] eqDist maximum distance of two points to be considered equal
26)  */
27) void Layer::improvePaths(double eqDist)
28) {
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

29)   Paths::iterator path, other, best;
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

30)   bool change;
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

31)   double smallest, dist;
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

32) 
Stefan Schuermans bugfix: do not access begin...

Stefan Schuermans authored 11 years ago

33)   // remove empty paths
34)   path = mPaths.begin();
35)   while (path != mPaths.end())
36)     if (path->mPoints.empty())
37)       path = mPaths.erase(path);
38)     else
39)       ++path;
40) 
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

41)   // join paths with equal begin/end points
42)   do {
43)     change = false;
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

44)     // process all paths
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

45)     for (path = mPaths.begin(); path != mPaths.end(); ++path) {
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

46)       // check all paths following aftet the current one
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

47)       other = path;
48)       ++other;
49)       while (other != mPaths.end()) {
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

50)         // paths can be joined
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

51)         if (other->mPoints.front().equals(path->mPoints.back(),
52)                                           eqDist)) {
53)           path->appendPath(*other);
54)           other = mPaths.erase(other);
55)           change = true;
56)         }
57)         else if (other->mPoints.back().equals(path->mPoints.back(),
58)                                               eqDist)) {
59)           path->appendReversedPath(*other);
60)           other = mPaths.erase(other);
61)           change = true;
62)         }
63)         else if (other->mPoints.back().equals(path->mPoints.front(),
64)                                               eqDist)) {
65)           path->prependPath(*other);
66)           other = mPaths.erase(other);
67)           change = true;
68)         }
69)         else if (other->mPoints.front().equals(path->mPoints.front(),
70)                                                eqDist)) {
71)           path->prependReversedPath(*other);
72)           other = mPaths.erase(other);
73)           change = true;
74)         }
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

75)         // paths cannot be joined -> move to next one
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

76)         else
77)           ++other;
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

78)       } // while other
79)     } // for path
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

80)   } while (change);
81) 
82)   // remove nearby points in paths
83)   for (path = mPaths.begin(); path != mPaths.end(); ++path)
84)     path->removeEqPoints(eqDist);
85) 
86)   // remove empty paths
87)   path = mPaths.begin();
88)   while (path != mPaths.end())
89)     if (path->mPoints.empty())
90)       path = mPaths.erase(path);
91)     else
92)       ++path;
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

93) 
94)   // sort paths to minimize unproductive move distance
95)   for (path = mPaths.begin(); path != mPaths.end(); ++path) {
96)     // find path starting at closest distance from current paths end
97)     other = path;
98)     ++other;
99)     best = mPaths.end();
100)     while (other != mPaths.end()) {
101)       // calculate (squared) distance from path.end to other.begin
102)       //   path and other are never empty here
103)       //   empty paths have been removed before
104)       dist = (path->mPoints.back() - other->mPoints.front()).abs_sq();
105)       // keep path with smallest (squared) distance
106)       if (best == mPaths.end() || dist < smallest) {
107)         best = other;
108)         smallest = dist;
109)       }
110)       ++other;
111)     } // while other
112)     // use best path as next one, i.e. swap best path with next one
113)     other = path;
114)     other++;
115)     if (other != mPaths.end() && best != mPaths.end())
116)       mPaths.splice(other, mPaths, best);
117)   } // for path
Stefan Schuermans implement joining and impro...

Stefan Schuermans authored 11 years ago

118) }
119) 
Stefan Schuermans initial version, DXFs can b...

Stefan Schuermans authored 11 years ago

120) /**
121)  * @brief convert layer to G-code
122)  * @param[in] settings G-code creation settings
123)  * @param[in,out] gcode new G-code is appended to existing G-code
124)  */
125) void Layer::toGCode(const Settings &settings, GCode &gcode) const
126) {
Stefan Schuermans process each layer z step b...

Stefan Schuermans authored 11 years ago

127)   // cut step-wise
128)   double z = settings.base_z;
129)   do {
130)     z -= settings.cut_z_step;
131)     if (z < settings.cut_z)
132)       z = settings.cut_z;
133) 
134)     // cut path at current z
135)     Paths::const_iterator path;
136)     for (path = mPaths.begin(); path != mPaths.end(); ++path)
137)       path->toGCode(settings, z, gcode);
138) 
139)   } while (z > settings.cut_z);