1 /*
2     This file is a part of the RepSnapper project.
3     Copyright (C) 2010  Kulitorum
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #ifndef MODEL_H
20 #define MODEL_H
21 
22 #include <math.h>
23 
24 #include <giomm/file.h>
25 
26 #include "stdafx.h"
27 
28 #include "files.h"
29 
30 
31 #include "objtree.h"
32 #include "gcode/gcode.h"
33 /* #include "gcodestate.h" */
34 #include "settings.h"
35 /* #include "progress.h" */
36 /* #include "slicer/poly.h" */
37 
38 #ifdef _MSC_VER // Visual C++ compiler
39 #  pragma warning( disable : 4244 4267)
40 #endif
41 
42 
43 class Model
44 {
45 	sigc::signal< void > m_signal_zoom;
46 	ViewProgress *m_progress;
47 
48 public:
49 	Gtk::Statusbar *statusbar;
50 	// Something in the rfo changed
signal_zoom()51 	sigc::signal< void > signal_zoom() { return m_signal_zoom; }
52 	sigc::signal< void > m_signal_gcode_changed;
53 
54 	Model();
55 	~Model();
56 
57 	void SimpleAdvancedToggle();
58 	void SaveConfig(Glib::RefPtr<Gio::File> file);
LoadConfig()59 	void LoadConfig() { LoadConfig(Gio::File::create_for_path("repsnapper.conf")); }
60 	void LoadConfig(Glib::RefPtr<Gio::File> file);
61 
62 	// STL Functions
63 	void ReadStl(Glib::RefPtr<Gio::File> file);
64 	vector<Shape*> ReadShapes(Glib::RefPtr<Gio::File> file,
65 				  uint max_triangles = 0);
66 	void SaveStl(Glib::RefPtr<Gio::File> file);
67 	void SaveAMF(Glib::RefPtr<Gio::File> file);
68 
69 	int AddShape(TreeObject *parent, Shape * shape, string filename,
70 		     bool autoplace = true);
71 	int SplitShape(TreeObject *parent, Shape *shape, string filename);
72 	int MergeShapes(TreeObject *parent, const vector<Shape*> shapes);
73 	int DivideShape(TreeObject *parent, Shape *shape, string filename);
74 	Shape GetCombinedShape() const;
75 
76 	sigc::signal< void, Gtk::TreePath & > m_signal_stl_added;
77 
78 	void Read(Glib::RefPtr<Gio::File> file);
79 	void SetViewProgress (ViewProgress *progress);
80 
81 	void DeleteObjTree(vector<Gtk::TreeModel::Path> &iter);
82 	vector<Gtk::TreeModel::Path> m_current_selectionpath;
83 
84 	void OptimizeRotation(Shape *shape, TreeObject *object);
85 	void ScaleObject(Shape *shape, TreeObject *object, double scale);
86 	void ScaleObjectX(Shape *shape, TreeObject *object, double scale);
87 	void ScaleObjectY(Shape *shape, TreeObject *object, double scale);
88 	void ScaleObjectZ(Shape *shape, TreeObject *object, double scale);
89 	void RotateObject(Shape *shape, TreeObject *object, Vector4d rotate);
90 	void TwistObject(Shape *shape, TreeObject *object, double angle);
91 	void PlaceOnPlatform(Shape *shape, TreeObject *object);
92 	bool updateStatusBar(GdkEventCrossing *event, Glib::ustring = "");
93 	void InvertNormals(Shape *shape, TreeObject *object);
94 	void Mirror(Shape *shape, TreeObject *object);
95 
96 	vector<Layer*> layers;
97 
98 	Layer * m_previewLayer;
99 	double get_preview_Z();
100 	//Layer * m_previewGCodeLayer;
101 	GCode m_previewGCode;
102 	double m_previewGCode_z;
103 
104 	vector<Shape*> preview_shapes;
105 
106 	// Slicing
107 	void SliceToSVG(Glib::RefPtr<Gio::File> file, bool single_layer=false);
108 
109 	// GCode Functions
110 	void init();
111 	void ReadGCode(Glib::RefPtr<Gio::File> file);
112 	void translateGCode(Vector3d trans);
113 
114 	void ConvertToGCode();
115 
116 	void MakeRaft(GCodeState &state, double &z);
117 	void WriteGCode(Glib::RefPtr<Gio::File> file);
118 	void ClearGCode();
119 	void ClearLayers();
120 	void ClearPreview();
121 	Glib::RefPtr<Gtk::TextBuffer> GetGCodeBuffer();
122 	void GlDrawGCode(int layer=-1); // should be in the view
123 	void GlDrawGCode(double Z);
setCurrentPrintingLine(long line)124 	void setCurrentPrintingLine(long line){ currentprintingline = line; }
125 	unsigned long currentprintingline;
126 
127 	Matrix4f &SelectedNodeMatrix(guint objectNr = 1);
128 	void SelectedNodeMatrices(vector<Matrix4d *> &result );
129 	void newObject();
130 
131 	Settings settings;
132 
133 	// Model derived: Bounding box info
134 	Vector3d Center;
135 	Vector3d Min;
136 	Vector3d Max;
137 
138 	void CalcBoundingBoxAndCenter(bool selected_only = false);
139 	Vector3d GetViewCenter();
140         bool AutoArrange(vector<Gtk::TreeModel::Path> &iter);
141 	Vector3d FindEmptyLocation(const vector<Shape*> &shapes,
142 				   const vector<Matrix4d> &transforms,
143 				   const Shape *shape);
144         bool FindEmptyLocation(Vector3d &result, const Shape *stl);
145 
146 	sigc::signal< void > m_model_changed;
147 	void ModelChanged();
148 	bool m_inhibit_modelchange;
149 	// Truly the model
150 	ObjectsTree objtree;
151 	Glib::RefPtr<Gtk::TextBuffer> errlog, echolog;
152 
153 	int draw(vector<Gtk::TreeModel::Path> &selected);
154 	int drawLayers(double height, const Vector3d &offset, bool calconly = false);
155 	void setMeasuresPoint(const Vector3d &point);
156 	Vector2d measuresPoint;
157 
158 	Layer * calcSingleLayer(double z, uint LayerNr, double thickness,
159 				bool calcinfill, bool for_gcode=false) const ;
160 
161 	sigc::signal< void, Gtk::MessageType, const char *, const char * > signal_alert;
162 	void alert (const char *message);
163 	void error (const char *message, const char *secondary);
164 
165 	void ClearLogs();
166 
167 	GCode gcode;
168 
SetIsPrinting(bool printing)169 	void SetIsPrinting(bool printing) { is_printing = printing; };
170 
171 	string getSVG(int single_layer_no = -1) const;
172 	void ReadSVG(Glib::RefPtr<Gio::File> file);
173 
174  private:
175 	bool is_calculating;
176 	bool is_printing;
177 	//GCodeIter *m_iter;
178 	Layer * lastlayer;
179 
180         // Slicing/GCode conversion functions
181 	void Slice();
182 
183 	void CleanupLayers();
184 	void CalcInfill();
185 	void MakeShells();
186 	void MakeUncoveredPolygons(bool make_decor, bool make_bridges=true);
187 	vector<Poly> GetUncoveredPolygons(const Layer *subjlayer,
188 					  const Layer *cliplayer);
189 	void MakeFullSkins();
190 	void MultiplyUncoveredPolygons();
191 	void MakeSupportPolygons(Layer * subjlayer, const Layer * cliplayer,
192 				 double widen=0);
193 	void MakeSupportPolygons(double widen=0);
194 	void MakeSkirt();
195 
196 };
197 
198 #endif // MODEL_H
199