1 #include "stdafx.h"
2 #include "msPlugInImpl.h"
3 #include "msLib.h"
4 
5 
6 
DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)7 BOOL APIENTRY DllMain( HANDLE hModule,
8                        DWORD  ul_reason_for_call,
9                        LPVOID lpReserved
10 					 )
11 {
12     switch (ul_reason_for_call)
13 	{
14 		case DLL_PROCESS_ATTACH:
15 		case DLL_THREAD_ATTACH:
16 		case DLL_THREAD_DETACH:
17 		case DLL_PROCESS_DETACH:
18 			break;
19     }
20     return TRUE;
21 }
22 
23 
24 
25 cMsPlugIn*
CreatePlugIn()26 CreatePlugIn ()
27 {
28     return new cPlugIn ();
29 }
30 
31 void
DestroyPlugIn(cMsPlugIn * pPlugin)32 DestroyPlugIn (cMsPlugIn* pPlugin)
33 {
34     delete pPlugin;
35 }
36 
37 
cPlugIn()38 cPlugIn::cPlugIn ()
39 {
40     strcpy (szTitle, "Text...");
41 }
42 
43 
44 
~cPlugIn()45 cPlugIn::~cPlugIn ()
46 {
47 }
48 
49 
50 
51 int
GetType()52 cPlugIn::GetType ()
53 {
54     return cMsPlugIn::eTypeExport|cMsPlugIn::eNormalsAndTexCoordsPerTriangleVertex;
55 }
56 
57 
58 
59 const char*
GetTitle()60 cPlugIn::GetTitle ()
61 {
62     return szTitle;
63 }
64 
65 
66 int
Execute(msModel * pModel)67 cPlugIn::Execute (msModel *pModel)
68 {
69     if (!pModel)
70         return -1;
71 
72     //
73     // check, if we have something to export
74     //
75     if (msModel_GetMeshCount (pModel) == 0)
76     {
77         ::MessageBox (NULL, "The model is empty!  Nothing exported!", "Text Export", MB_OK | MB_ICONWARNING);
78         return 0;
79     }
80 
81     //
82     // choose filename
83     //
84     OPENFILENAME ofn;
85     memset (&ofn, 0, sizeof (OPENFILENAME));
86 
87     char szFile[MS_MAX_PATH];
88     char szFileTitle[MS_MAX_PATH];
89     char szDefExt[32] = "txt";
90     char szFilter[128] = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
91     szFile[0] = '\0';
92     szFileTitle[0] = '\0';
93 
94     ofn.lStructSize = sizeof (OPENFILENAME);
95     ofn.lpstrDefExt = szDefExt;
96     ofn.lpstrFilter = szFilter;
97     ofn.lpstrFile = szFile;
98     ofn.nMaxFile = MS_MAX_PATH;
99     ofn.lpstrFileTitle = szFileTitle;
100     ofn.nMaxFileTitle = MS_MAX_PATH;
101     ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
102     ofn.lpstrTitle = "Export Text";
103 
104     if (!::GetSaveFileName (&ofn))
105         return 0;
106 
107     //
108     // export
109     //
110     FILE *file = fopen (szFile, "wt");
111     if (!file)
112         return -1;
113 
114 	int nVersion = 1;
115 	fprintf(file, "SimpleMeshFormat %d\n", nVersion);
116 
117     int i, j, v;
118 	int nNumMeshes = msModel_GetMeshCount(pModel);
119 	fprintf(file, "%d\n", nNumMeshes);
120     for (i = 0; i < nNumMeshes; i++)
121     {
122         msMesh *pMesh = msModel_GetMeshAt(pModel, i);
123         char szName[64];
124         msMesh_GetName(pMesh, szName, 64);
125 
126         fprintf(file, "%s\n", szName);
127 
128 		// vertices
129 		int nNumVertices = msMesh_GetVertexCount(pMesh);
130 		fprintf(file, "%d\n", nNumVertices);
131 		for (j = 0; j < nNumVertices; j++)
132 		{
133 			msVertex *pVertex = msMesh_GetVertexAt(pMesh, j);
134 			msVec3 Vertex;
135 			msVertex_GetVertex(pVertex, Vertex);
136 			fprintf(file, "%g %g %g\n", Vertex[0], Vertex[1], Vertex[2]);
137 		}
138 
139 		// triangles
140 		int nNumTriangles = msMesh_GetTriangleCount(pMesh);
141 		fprintf(file, "%d\n", nNumTriangles);
142         for (j = 0; j < nNumTriangles; j++)
143         {
144             msTriangle *pTriangle = msMesh_GetTriangleAt(pMesh, j);
145 			msTriangleEx *pTriangleEx = msMesh_GetTriangleExAt(pMesh, j);
146 
147             word nIndices[3];
148             msTriangle_GetVertexIndices (pTriangle, nIndices);
149 
150 			msVec3 Normals[3];
151 			msVec2 TexCoords[3];
152 			for (v = 0; v < 3; v++)
153 			{
154 				msTriangleEx_GetNormal(pTriangleEx, v, Normals[v]);
155 				msTriangleEx_GetTexCoord(pTriangleEx, v, TexCoords[v]);
156 			}
157 
158 			int nSmoothingGroup = msTriangle_GetSmoothingGroup(pTriangle);
159 
160 			fprintf(file, "%d %d %d %d %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
161 				nSmoothingGroup,
162 				nIndices[0], nIndices[1], nIndices[2],
163 				Normals[0][0], Normals[0][1], Normals[0][2],
164 				Normals[1][0], Normals[1][1], Normals[1][2],
165 				Normals[2][0], Normals[2][1], Normals[2][2],
166 				TexCoords[0][0], TexCoords[0][1],
167 				TexCoords[1][0], TexCoords[1][1],
168 				TexCoords[2][0], TexCoords[2][1]
169 				);
170         }
171     }
172 
173     fclose (file);
174 
175     // dont' forget to destroy the model
176     msModel_Destroy (pModel);
177 
178     return 0;
179 }
180