14
14
15
15
using UnityEngine ;
16
16
using System . IO ;
17
+ using System . Linq ;
17
18
using System . Text ;
18
19
// using System.Windows.Forms;
19
20
25
26
namespace com . google . apps . peltzer . client . desktop_app
26
27
{
27
28
/// <summary>
28
- /// Responsible for handling the button-click to import obj files from the desktop app, and
29
+ /// Responsible for handling the button-click to import obj files from the desktop app, and
29
30
/// loading them into the model.
30
31
/// </summary>
31
32
public class ObjImportController : MonoBehaviour
@@ -37,14 +38,15 @@ public class ObjImportController : MonoBehaviour
37
38
private const float MIN_IMPORTED_OBJ_DISTANCE_FROM_USER = 2.0f ;
38
39
39
40
/// <summary>
40
- /// Handles the button-click to import an obj. Opens up a dialog and in the background, waits for the
41
+ /// Either handles the button-click to import an obj. Opens up a dialog and in the background, waits for the
41
42
/// user to hit 'ok' with two files selected.
43
+ /// or imports the obj files passed in as arguments.
42
44
/// </summary>
43
- public void SelectObjToImport ( )
45
+ public void Import ( string [ ] filenames = null )
44
46
{
45
47
Model model = PeltzerMain . Instance . GetModel ( ) ;
46
- BackgroundWork openDialog = new OpenFileDialogAndLoadObj ( model ) ;
47
- PeltzerMain . Instance . DoPolyMenuBackgroundWork ( openDialog ) ;
48
+ BackgroundWork work = new LoadObj ( model , filenames ) ;
49
+ PeltzerMain . Instance . DoPolyMenuBackgroundWork ( work ) ;
48
50
}
49
51
50
52
/// <summary>
@@ -69,51 +71,91 @@ private static string FileToString(string filename)
69
71
}
70
72
}
71
73
72
- class OpenFileDialogAndLoadObj : BackgroundWork
74
+ public class LoadObj : BackgroundWork
73
75
{
74
76
// A reference to the model.
75
77
private readonly Model model ;
76
78
// File contents to be passed from a background thread to a foreground thread.
77
79
string mtlFileContents ;
78
80
string objFileContents ;
81
+ private string [ ] filenames ;
79
82
PeltzerFile peltzerFile ;
80
83
81
- public OpenFileDialogAndLoadObj ( Model model )
84
+ public LoadObj ( Model model , string [ ] filenames = null )
82
85
{
83
86
this . model = model ;
87
+ this . filenames = filenames ;
84
88
}
85
89
86
90
// In the background we perform all the File I/O to get file contents. There are no graceful failures here,
87
91
// and there is no feedback to the user in case of failure.
88
92
public void BackgroundWork ( )
89
93
{
90
- // OpenFileDialog dialog = new OpenFileDialog();
91
- // dialog.Multiselect = true;
92
- // Expect that the user selected two files, one .obj and one .mtl
93
- // if (dialog.ShowDialog() == DialogResult.OK) {
94
- // if (dialog.FileNames.Length == 1) {
95
- // if (dialog.FileNames[0].EndsWith(".peltzer") || dialog.FileNames[0].EndsWith(".poly")
96
- // || dialog.FileNames[0].EndsWith(".blocks")) {
97
- // byte[] peltzerFileBytes = File.ReadAllBytes(dialog.FileNames[0]);
98
- // PeltzerFileHandler.PeltzerFileFromBytes(peltzerFileBytes, out peltzerFile);
99
- // } else if (dialog.FileNames[0].EndsWith(".obj")) {
100
- // objFileContents = FileToString(dialog.FileNames[0]);
101
- // } else {
102
- // Debug.Log("When selecting only one file for OBJ import, it must have a .obj extension");
103
- // }
104
- // } else if (dialog.FileNames.Length == 2) {
105
- // string objFile = dialog.FileNames[0].EndsWith(".obj") ? dialog.FileNames[0] : dialog.FileNames[1];
106
- // string mtlFile = dialog.FileNames[0].EndsWith(".mtl") ? dialog.FileNames[0] : dialog.FileNames[1];
107
- // if (!objFile.EndsWith(".obj") || !mtlFile.EndsWith(".mtl")) {
108
- // Debug.Log("When selecting two files for OBJ import, one must be .obj and the other .mtl");
109
- // }
94
+ if ( filenames != null )
95
+ {
96
+ DoImport ( filenames ) ;
97
+ }
98
+ else
99
+ {
100
+ // OpenFileDialog dialog = new OpenFileDialog();
101
+ // dialog.Multiselect = true;
102
+ // // Expect that the user selected two files, one .obj and one .mtl
103
+ // if (dialog.ShowDialog() == DialogResult.OK)
104
+ // {
105
+ // DoImport(dialog.FileNames);
106
+ // }
107
+
108
+ }
109
+ }
110
+
111
+ public void DoImport ( string [ ] filenames )
112
+ {
113
+ string objFile = null ;
114
+ string mtlFile = null ;
115
+
116
+ // Should we retire some of these file extensions?
117
+ // Does anything other than blocks exist in the wild?
118
+ if ( filenames . Length == 1 &&
119
+ ( filenames [ 0 ] . EndsWith ( ".peltzer" )
120
+ || filenames [ 0 ] . EndsWith ( ".poly" )
121
+ || filenames [ 0 ] . EndsWith ( ".blocks" ) ) )
122
+ {
123
+ byte [ ] peltzerFileBytes = File . ReadAllBytes ( filenames [ 0 ] ) ;
124
+ PeltzerFileHandler . PeltzerFileFromBytes ( peltzerFileBytes , out peltzerFile ) ;
125
+ return ;
126
+ }
127
+
128
+ if ( filenames . Length == 1 && filenames [ 0 ] . EndsWith ( ".obj" ) )
129
+ {
130
+ objFile = filenames [ 0 ] ;
131
+ mtlFile = filenames [ 0 ] . Replace ( ".obj" , ".mtl" ) ;
132
+ if ( ! File . Exists ( mtlFile ) )
133
+ {
134
+ mtlFile = null ;
135
+ }
136
+ }
137
+ else if ( filenames . Length == 2 )
138
+ {
139
+ objFile = filenames . FirstOrDefault ( f => f . EndsWith ( ".obj" ) ) ;
140
+ mtlFile = filenames . FirstOrDefault ( f => f . EndsWith ( ".mtl" ) ) ;
141
+ }
110
142
111
- // objFileContents = FileToString(objFile);
112
- // mtlFileContents = FileToString(mtlFile);
113
- // } else {
114
- // Debug.Log("Exactly one .obj file or a pair of .obj and .mtl files must be selected for OBJ import");
115
- // }
116
- // }
143
+ if ( filenames . Length == 2 && mtlFile == null )
144
+ {
145
+ Debug . Log ( "When selecting two files for OBJ import, one must be .obj and the other .mtl" ) ;
146
+ }
147
+ else if ( objFile == null )
148
+ {
149
+ Debug . Log ( "Exactly one .obj file or a pair of .obj and .mtl files must be selected for OBJ import" ) ;
150
+ }
151
+ else
152
+ {
153
+ objFileContents = FileToString ( objFile ) ;
154
+ if ( mtlFile != null )
155
+ {
156
+ mtlFileContents = FileToString ( mtlFile ) ;
157
+ }
158
+ }
117
159
}
118
160
119
161
// In the foreground we add the mesh to the model.
0 commit comments