8125c92b1a6de1d7883bfd32663054354826b480
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>
Stefan Schuermans license CC-BY-SA --> GPL (m...

Stefan Schuermans authored 11 years ago

3)  * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
Stefan Schuermans initial version, DXFs can b...

Stefan Schuermans authored 11 years ago

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();
Stefan Schuermans adaptions for debian jessie

Stefan Schuermans authored 8 years ago

100)     smallest = 0;
Stefan Schuermans sort paths in order to shor...

Stefan Schuermans authored 11 years ago

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

Stefan Schuermans authored 11 years ago

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

Stefan Schuermans authored 11 years ago

121) /**
122)  * @brief convert layer to G-code
123)  * @param[in] settings G-code creation settings
124)  * @param[in,out] gcode new G-code is appended to existing G-code
125)  */
126) void Layer::toGCode(const Settings &settings, GCode &gcode) const
127) {
Stefan Schuermans add support for cutting a l...

Stefan Schuermans authored 11 years ago

128)   double z;
129)   Paths::const_iterator path;
130) 
131)   switch (settings.layer_mode) {
132) 
133)     case Settings::LevelByLevel:
134) 
135)       // cut step-wise
136)       z = settings.base_z;
137)       do {
138)         z -= settings.cut_z_step;
139)         if (z < settings.cut_z)
140)           z = settings.cut_z;
141) 
142)         // cut all paths at current z
143)         for (path = mPaths.begin(); path != mPaths.end(); ++path)
144)           path->toGCode(settings, z, gcode);
145) 
146)       } while (z > settings.cut_z);
147) 
148)       break;
149) 
150)     case Settings::PathByPath:
151) 
152)       // cut each path
153)       for (path = mPaths.begin(); path != mPaths.end(); ++path) {
154) 
155)         // cut current path step-wise
156)         z = settings.base_z;
157)         do {
158)           z -= settings.cut_z_step;
159)           if (z < settings.cut_z)
160)             z = settings.cut_z;
161)           path->toGCode(settings, z, gcode);
162)         } while (z > settings.cut_z);
163) 
164)       } // for path
Stefan Schuermans process each layer z step b...

Stefan Schuermans authored 11 years ago

165) 
Stefan Schuermans add support for cutting a l...

Stefan Schuermans authored 11 years ago

166)       break;
Stefan Schuermans process each layer z step b...

Stefan Schuermans authored 11 years ago

167) 
Stefan Schuermans add support for cutting a l...

Stefan Schuermans authored 11 years ago

168)   } // switch (settings.layer_mode)